File tree Expand file tree Collapse file tree 1 file changed +11
-9
lines changed Expand file tree Collapse file tree 1 file changed +11
-9
lines changed Original file line number Diff line number Diff line change @@ -144,6 +144,11 @@ export class Diff2HtmlUI {
144
144
// Collect all the diff files and execute the highlight on their lines
145
145
const files = this . targetElement . querySelectorAll ( '.d2h-file-wrapper' ) ;
146
146
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
+
147
152
// Collect all the code lines and execute the highlight on them
148
153
const codeLines = file . querySelectorAll ( '.d2h-code-line-ctn' ) ;
149
154
codeLines . forEach ( line => {
@@ -155,15 +160,12 @@ export class Diff2HtmlUI {
155
160
156
161
if ( text === null || lineParent === null || ! this . isElement ( lineParent ) ) return ;
157
162
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
+ ) ;
167
169
168
170
const originalStream = nodeStream ( line ) ;
169
171
if ( originalStream . length ) {
You can’t perform that action at this time.
0 commit comments