From 5373ae180bfee55109bce8c46e86ca5e2546f7fe Mon Sep 17 00:00:00 2001 From: Rodrigo Fernandes Date: Sun, 23 Oct 2022 18:26:19 +0100 Subject: [PATCH] add support for language override --- README.md | 2 ++ src/ui/js/diff2html-ui-base.ts | 12 +++++++++++- website/templates/pages/demo/demo.ts | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4d3a113..949e756 100644 --- a/README.md +++ b/README.md @@ -385,6 +385,8 @@ The HTML output accepts a Javascript object with configuration. Possible options For example: `{ "tag-file-changed": "MODIFIED" }` > For more information regarding the possible templates look into > [src/templates](https://github.com/rtfpessoa/diff2html/tree/master/src/templates) +- `highlightLanguages`: Map of extension to language name, used for highlighting. This overrides the default language + detection based on file extensions. ### Diff2Html Browser diff --git a/src/ui/js/diff2html-ui-base.ts b/src/ui/js/diff2html-ui-base.ts index 7aaaa5f..9a84326 100644 --- a/src/ui/js/diff2html-ui-base.ts +++ b/src/ui/js/diff2html-ui-base.ts @@ -9,6 +9,7 @@ export interface Diff2HtmlUIConfig extends Diff2HtmlConfig { highlight?: boolean; fileListToggle?: boolean; fileListStartVisible?: boolean; + highlightLanguages?: Map; /** * @deprecated since version 3.1.0 * Smart selection is now enabled by default with vanilla CSS @@ -23,6 +24,7 @@ export const defaultDiff2HtmlUIConfig = { highlight: true, fileListToggle: true, fileListStartVisible: false, + highlightLanguages: new Map(), /** * @deprecated since version 3.1.0 * Smart selection is now enabled by default with vanilla CSS @@ -141,8 +143,16 @@ export class Diff2HtmlUI { files.forEach(file => { // HACK: help Typescript know that `this.hljs` is defined since we already checked it if (this.hljs === null) return; + const language = file.getAttribute('data-lang'); - const hljsLanguage = language ? getLanguage(language) : 'plaintext'; + + const hljsLanguage = + language && this.config.highlightLanguages.has(language) + ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.config.highlightLanguages.get(language)! + : language + ? getLanguage(language) + : 'plaintext'; // Collect all the code lines and execute the highlight on them const codeLines = file.querySelectorAll('.d2h-code-line-ctn'); diff --git a/website/templates/pages/demo/demo.ts b/website/templates/pages/demo/demo.ts index 0cca1f5..53c7019 100644 --- a/website/templates/pages/demo/demo.ts +++ b/website/templates/pages/demo/demo.ts @@ -22,7 +22,7 @@ import './demo.css'; type URLParams = { diff?: string; diffTooBigMessage?: string; - [key: string]: string | boolean | number | undefined; + [key: string]: string | boolean | number | Map | undefined; }; const searchParam = 'diff';