diff2html/src/diff2html.ts

48 lines
1.8 KiB
TypeScript
Raw Normal View History

2019-12-29 22:31:32 +00:00
import * as DiffParser from './diff-parser';
2023-09-18 23:41:51 +00:00
import { FileListRenderer } from './file-list-renderer';
2019-12-29 22:31:32 +00:00
import LineByLineRenderer, { LineByLineRendererConfig, defaultLineByLineRendererConfig } from './line-by-line-renderer';
import SideBySideRenderer, { SideBySideRendererConfig, defaultSideBySideRendererConfig } from './side-by-side-renderer';
import { DiffFile, OutputFormatType } from './types';
import HoganJsUtils, { HoganJsUtilsConfig } from './hoganjs-utils';
2019-10-12 21:45:49 +00:00
export interface Diff2HtmlConfig
extends DiffParser.DiffParserConfig,
LineByLineRendererConfig,
SideBySideRendererConfig,
HoganJsUtilsConfig {
outputFormat?: OutputFormatType;
2019-10-13 18:21:19 +00:00
drawFileList?: boolean;
2019-10-12 21:45:49 +00:00
}
export const defaultDiff2HtmlConfig = {
...defaultLineByLineRendererConfig,
...defaultSideBySideRendererConfig,
2019-10-21 22:37:42 +00:00
outputFormat: OutputFormatType.LINE_BY_LINE,
2019-12-29 22:31:32 +00:00
drawFileList: true,
lineFolding: false
2019-10-12 21:45:49 +00:00
};
export function parse(diffInput: string, configuration: Diff2HtmlConfig = {}): DiffFile[] {
return DiffParser.parse(diffInput, { ...defaultDiff2HtmlConfig, ...configuration });
}
export function html(diffInput: string | DiffFile[], configuration: Diff2HtmlConfig = {}): string {
const config = { ...defaultDiff2HtmlConfig, ...configuration };
2019-12-29 22:31:32 +00:00
const diffJson = typeof diffInput === 'string' ? DiffParser.parse(diffInput, config) : diffInput;
2019-10-12 21:45:49 +00:00
const hoganUtils = new HoganJsUtils(config);
2023-09-19 01:10:30 +00:00
const { colorScheme } = config;
const fileListRendererConfig = { colorScheme };
const fileList = config.drawFileList ? new FileListRenderer(hoganUtils, fileListRendererConfig).render(diffJson) : '';
2019-10-12 21:45:49 +00:00
const diffOutput =
2019-12-29 22:31:32 +00:00
config.outputFormat === 'side-by-side'
2019-10-12 21:45:49 +00:00
? new SideBySideRenderer(hoganUtils, config).render(diffJson)
: new LineByLineRenderer(hoganUtils, config).render(diffJson);
return fileList + diffOutput;
}