forked from timlrx/rehype-prism-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
296 lines (271 loc) · 8.68 KB
/
generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* @typedef {import('hast').Element} Element
* @typedef {import('hast').Root} Root
* @typedef Options options
* Configuration.
* @property {boolean} [showLineNumbers]
* Set `showLineNumbers` to `true` to always display line number
* @property {boolean} [ignoreMissing]
* Set `ignoreMissing` to `true` to ignore unsupported languages and line highlighting when no language is specified
*/
import { visit } from 'unist-util-visit'
import { toString } from 'hast-util-to-string'
import { filter } from 'unist-util-filter'
import rangeParser from 'parse-numeric-range'
/**
* @param {Element} node
* @return {string|null}
*/
const getLanguage = (node) => {
const className = node.properties.className
//@ts-ignore
for (const classListItem of className) {
if (classListItem.slice(0, 9) === 'language-') {
return classListItem.slice(9).toLowerCase()
}
}
return null
}
/**
* Create a closure that determines if we have to highlight the given index
*
* @param {string} meta
* @return { (index:number) => boolean }
*/
const calculateLinesToHighlight = (meta) => {
const RE = /{([\d,-]+)}/
// Remove space between {} e.g. {1, 3}
const parsedMeta = meta
.split(',')
.map((str) => str.trim())
.join()
if (RE.test(parsedMeta)) {
const strlineNumbers = RE.exec(parsedMeta)[1]
const lineNumbers = rangeParser(strlineNumbers)
return (index) => lineNumbers.includes(index + 1)
} else {
return () => false
}
}
/**
* Check if we want to start the line numbering from a given number or 1
* showLineNumbers=5, will start the numbering from 5
* @param {string} meta
* @returns {number}
*/
const calculateStartingLine = (meta) => {
const RE = /showLineNumbers=(?<lines>\d+)/i
// pick the line number after = using a named capturing group
if (RE.test(meta)) {
const {
groups: { lines },
} = RE.exec(meta)
return Number(lines)
}
return 1
}
/**
* Create container AST for node lines
*
* @param {number} number
* @return {Element[]}
*/
const createLineNodes = (number) => {
const a = new Array(number)
for (let i = 0; i < number; i++) {
a[i] = {
type: 'element',
tagName: 'span',
properties: { className: [] },
children: [],
}
}
return a
}
/**
* Split multiline text nodes into individual nodes with positioning
* Add a node start and end line position information for each text node
*
* @return { (ast:Element['children']) => Element['children'] }
*
*/
const addNodePositionClosure = () => {
let startLineNum = 1
/**
* @param {Element['children']} ast
* @return {Element['children']}
*/
const addNodePosition = (ast) => {
return ast.reduce((result, node) => {
if (node.type === 'text') {
const value = /** @type {string} */ (node.value)
const numLines = (value.match(/\n/g) || '').length
if (numLines === 0) {
node.position = {
// column: 1 is needed to avoid error with @next/mdx
// https://github.com/timlrx/rehype-prism-plus/issues/44
start: { line: startLineNum, column: 1 },
end: { line: startLineNum, column: 1 },
}
result.push(node)
} else {
const lines = value.split('\n')
for (const [i, line] of lines.entries()) {
result.push({
type: 'text',
value: i === lines.length - 1 ? line : line + '\n',
position: {
start: { line: startLineNum + i, column: 1 },
end: { line: startLineNum + i, column: 1 },
},
})
}
}
startLineNum = startLineNum + numLines
return result
}
if (Object.prototype.hasOwnProperty.call(node, 'children')) {
const initialLineNum = startLineNum
// @ts-ignore
node.children = addNodePosition(node.children, startLineNum)
result.push(node)
node.position = {
start: { line: initialLineNum, column: 1 },
end: { line: startLineNum, column: 1 },
}
return result
}
result.push(node)
return result
}, [])
}
return addNodePosition
}
/**
* Rehype prism plugin generator that highlights code blocks with refractor (prismjs)
*
* Pass in your own refractor object with the required languages registered:
* https://github.com/wooorm/refractor#refractorregistersyntax
*
* @param {import('refractor/lib/core').Refractor} refractor
* @return {import('unified').Plugin<[Options?], Root>}
*/
const rehypePrismGenerator = (refractor) => {
return (options = {}) => {
return (tree) => {
visit(tree, 'element', visitor)
}
/**
* @param {Element} node
* @param {number} index
* @param {Element} parent
*/
function visitor(node, index, parent) {
if (!parent || parent.tagName !== 'pre' || node.tagName !== 'code') {
return
}
let meta = node.data && node.data.meta ? /** @type {string} */ (node.data.meta) : ''
// Coerce className to array
if (node.properties.className) {
if (typeof node.properties.className === 'boolean') {
node.properties.className = []
} else if (!Array.isArray(node.properties.className)) {
node.properties.className = [node.properties.className]
}
} else {
node.properties.className = []
}
node.properties.className.push('code-highlight')
const lang = getLanguage(node)
/** @type {Element} */
let refractorRoot
// Syntax highlight
if (lang) {
try {
let rootLang
if (lang?.includes('diff-')){
rootLang=lang.split('-')[1]
} else{
rootLang=lang
}
// @ts-ignore
refractorRoot = refractor.highlight(toString(node), rootLang)
// @ts-ignore className is already an array
parent.properties.className = (parent.properties.className || []).concat(
'language-' + rootLang
)
} catch (err) {
if (options.ignoreMissing && /Unknown language/.test(err.message)) {
refractorRoot = node
} else {
throw err
}
}
} else {
refractorRoot = node
}
refractorRoot.children = addNodePositionClosure()(refractorRoot.children)
// Add position info to root
if (refractorRoot.children.length > 0) {
refractorRoot.position = {
start: { line: refractorRoot.children[0].position.start.line, column: 0 },
end: {
line: refractorRoot.children[refractorRoot.children.length - 1].position.end.line,
column: 0,
},
}
} else {
refractorRoot.position = {
start: { line: 0, column: 0 },
end: { line: 0, column: 0 },
}
}
const shouldHighlightLine = calculateLinesToHighlight(meta)
const startingLineNumber = calculateStartingLine(meta)
const codeLineArray = createLineNodes(refractorRoot.position.end.line)
const falseShowLineNumbersStr = [
'showlinenumbers=false',
'showlinenumbers="false"',
'showlinenumbers={false}',
]
for (const [i, line] of codeLineArray.entries()) {
// Default class name for each line
line.properties.className = ['code-line']
// Syntax highlight
const treeExtract = filter(
refractorRoot,
(node) => node.position.start.line <= i + 1 && node.position.end.line >= i + 1
)
line.children = treeExtract.children
// Line number
if (
(meta.toLowerCase().includes('showLineNumbers'.toLowerCase()) ||
options.showLineNumbers) &&
!falseShowLineNumbersStr.some((str) => meta.toLowerCase().includes(str))
) {
line.properties.line = [(i + startingLineNumber).toString()]
line.properties.className.push('line-number')
}
// Line highlight
if (shouldHighlightLine(i)) {
line.properties.className.push('highlight-line')
}
// Diff classes
if ((lang === 'diff' || lang?.includes('diff-')) && toString(line).substring(0, 1) === '-') {
line.properties.className.push('deleted')
} else if ((lang === 'diff' || lang?.includes('diff-')) && toString(line).substring(0, 1) === '+') {
line.properties.className.push('inserted')
}
}
// Remove possible trailing line when splitting by \n which results in empty array
if (
codeLineArray.length > 0 &&
toString(codeLineArray[codeLineArray.length - 1]).trim() === ''
) {
codeLineArray.pop()
}
node.children = codeLineArray
}
}
}
export default rehypePrismGenerator