Skip to content

Commit f9d328e

Browse files
authored
Merge pull request #451 from rtfpessoa/support-language-override
add support for language override
2 parents 95e4c40 + 5373ae1 commit f9d328e

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ The HTML output accepts a Javascript object with configuration. Possible options
385385
For example: `{ "tag-file-changed": "<span class="d2h-tag d2h-changed d2h-changed-tag">MODIFIED</span>" }`
386386
> For more information regarding the possible templates look into
387387
> [src/templates](https://github.com/rtfpessoa/diff2html/tree/master/src/templates)
388+
- `highlightLanguages`: Map of extension to language name, used for highlighting. This overrides the default language
389+
detection based on file extensions.
388390

389391
### Diff2Html Browser
390392

src/ui/js/diff2html-ui-base.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface Diff2HtmlUIConfig extends Diff2HtmlConfig {
99
highlight?: boolean;
1010
fileListToggle?: boolean;
1111
fileListStartVisible?: boolean;
12+
highlightLanguages?: Map<string, string>;
1213
/**
1314
* @deprecated since version 3.1.0
1415
* Smart selection is now enabled by default with vanilla CSS
@@ -23,6 +24,7 @@ export const defaultDiff2HtmlUIConfig = {
2324
highlight: true,
2425
fileListToggle: true,
2526
fileListStartVisible: false,
27+
highlightLanguages: new Map<string, string>(),
2628
/**
2729
* @deprecated since version 3.1.0
2830
* Smart selection is now enabled by default with vanilla CSS
@@ -141,8 +143,16 @@ export class Diff2HtmlUI {
141143
files.forEach(file => {
142144
// HACK: help Typescript know that `this.hljs` is defined since we already checked it
143145
if (this.hljs === null) return;
146+
144147
const language = file.getAttribute('data-lang');
145-
const hljsLanguage = language ? getLanguage(language) : 'plaintext';
148+
149+
const hljsLanguage =
150+
language && this.config.highlightLanguages.has(language)
151+
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
152+
this.config.highlightLanguages.get(language)!
153+
: language
154+
? getLanguage(language)
155+
: 'plaintext';
146156

147157
// Collect all the code lines and execute the highlight on them
148158
const codeLines = file.querySelectorAll('.d2h-code-line-ctn');

website/templates/pages/demo/demo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import './demo.css';
2222
type URLParams = {
2323
diff?: string;
2424
diffTooBigMessage?: string;
25-
[key: string]: string | boolean | number | undefined;
25+
[key: string]: string | boolean | number | Map<string, string> | undefined;
2626
};
2727

2828
const searchParam = 'diff';

0 commit comments

Comments
 (0)