8000 fix: Do not highlight code when language is unknown · rtfpessoa/diff2html@f58992a · GitHub
[go: up one dir, main page]

Skip to content

Commit f58992a

Browse files
committed
fix: Do not highlight code when language is unknown
1 parent 00296d7 commit f58992a

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ export class Diff2HtmlUI {
144144
// Collect all the diff files and execute the highlight on their lines
145145
const files = this.targetElement.querySelectorAll('.d2h-file-wrapper');
146146
files.forEach(file => {
147+
// HACK: help Typescript know that `this.hljs` is defined since we already checked it
148+
if (this.hljs === null) return;
149+
const language = file.getAttribute('data-lang');
150+
const hljsLanguage = language ? this.hljs.getLanguage(language) : undefined;
151+
147152
// Collect all the code lines and execute the highlight on them
148153
const codeLines = file.querySelectorAll('.d2h-code-line-ctn');
149154
codeLines.forEach(line => {
@@ -155,15 +160,12 @@ export class Diff2HtmlUI {
155160

156161
if (text === null || lineParent === null || !this.isElement(lineParent)) return;
157162

158-
const language = file.getAttribute('data-lang') || 'plaintext';
159-
const result: HighlightResult = this.hljs.getLanguage(language)
160-
? closeTags(
161-
this.hljs.highlight(text, {
162-
language,
163-
ignoreIllegals: true,
164-
}),
165-
)
166-
: closeTags(this.hljs.highlightAuto(text));
163+
const result: HighlightResult = closeTags(
164+
this.hljs.highlight(text, {
165+
language: hljsLanguage?.name || 'plaintext',
166+
ignoreIllegals: true,
167+
}),
168+
);
167169

168170
const originalStream = nodeStream(line);
169171
if (originalStream.length) {

0 commit comments

Comments
 (0)
0