set highlight language with highlightLanguage option

This commit is contained in:
Quentin Sabah 2022-06-12 16:38:39 +02:00
parent e5c813949f
commit b7d2ed01ff
2 changed files with 13 additions and 2 deletions

View file

@ -385,6 +385,8 @@ The HTML output accepts a Javascript object with configuration. Possible options
For example: `{ "tag-file-changed": "<span class="d2h-tag d2h-changed d2h-changed-tag">MODIFIED</span>" }`
> For more information regarding the possible templates look into
> [src/templates](https://github.com/rtfpessoa/diff2html/tree/master/src/templates)
- `highlightLanguage`: language used for highlighting, default is `''` to select the language based on the diffed
filename extension.
### Diff2Html Browser

View file

@ -9,6 +9,7 @@ export interface Diff2HtmlUIConfig extends Diff2HtmlConfig {
highlight?: boolean;
fileListToggle?: boolean;
fileListStartVisible?: boolean;
highlightLanguage?: string;
/**
* @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,
highlightLanguage: '',
/**
* @deprecated since version 3.1.0
* Smart selection is now enabled by default with vanilla CSS
@ -141,8 +143,15 @@ 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 = (() => {
const highlightLanguage = this.config.highlightLanguage;
if (highlightLanguage === '') {
const language = file.getAttribute('data-lang');
return language ? getLanguage(language) : 'plaintext';
} else {
return highlightLanguage;
}
})();
// Collect all the code lines and execute the highlight on them
const codeLines = file.querySelectorAll('.d2h-code-line-ctn');