Skip to content

Commit 6b22381

Browse files
committed
create: lib/highlightjs-lines.js — hljs-line to every line
1 parent 404afbd commit 6b22381

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

lib/highlightjs-lines.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* This will convert every lines that is not
3+
* starts with - or + (diff-support) to be encase
4+
* inside a div with the class of hljs-line
5+
*
6+
* @param {*} hljs
7+
* @returns
8+
*/
9+
10+
export default function (hljs) {
11+
function highlight(code, options) {
12+
const result = hljs.highlight(code, { ...options, language: options.language })
13+
const lines = result.value.split('\n')
14+
15+
const processedLines = lines.map(line => {
16+
const hasAddition = line.startsWith(`<span class="hljs-addition">+`)
17+
const hasDeletion = line.startsWith(`<span class="hljs-deletion">-`)
18+
const isEmpty = line === ""
19+
let outputLine = line.replace("+ ", "<span class='hljs-indicator'>+</span>").replace("- ", "<span class='hljs-indicator'>-</span>");
20+
if (!isEmpty && !hasAddition && !hasDeletion) {
21+
outputLine = `<span class="hljs-line">${line}</span>`
22+
}
23+
24+
return outputLine
25+
}).join('\n')
26+
27+
return {
28+
...result,
29+
code,
30+
value: processedLines,
31+
language: options.language
32+
}
33+
}
34+
35+
return {
36+
...hljs,
37+
highlight(codeOrLanguageName, optionsOrCode, ignoreIllegals = false) {
38+
return typeof optionsOrCode === 'string'
39+
? highlight(optionsOrCode, { language: codeOrLanguageName, ignoreIllegals })
40+
: highlight(codeOrLanguageName, optionsOrCode)
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)