This repository was archived by the owner on Sep 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
97 lines (78 loc) · 2.7 KB
/
index.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
const shiki = require('shiki')
const visit = require('unist-util-visit')
const rangeParser = require('parse-numeric-range')
const { renderToHtml } = require('./renderer')
const FALLBACK_LANGUAGE = 'text'
const LANG_TEXT = ['text', 'txt', 'plaintext']
const resolveLanguage = (lang, aliases) => (aliases && aliases[lang] ? aliases[lang] : lang)
const parseMeta = meta => {
const opts = meta.slice(1, -1)
const linesToHighlight = rangeParser(opts)
return {
linesToHighlight: linesToHighlight && linesToHighlight.length > 0 ? linesToHighlight : null
}
}
const highlight = ({ value, lang, meta }, highlighter, options) => {
const htmlRendererOptions = options.htmlRendererOptions || {}
if (lang.includes('{') && lang.endsWith('}')) {
const sep = lang.split(/(?={)/)
lang = sep[0]
meta = sep[1]
}
const language = resolveLanguage(lang, options.aliases)
const index = shiki.BUNDLED_LANGUAGES.findIndex(
x => x.id === language || (x.aliases && x.aliases.includes(language))
)
if (options.showLanguage) {
htmlRendererOptions.langId = language
}
const lines =
index >= 0 || LANG_TEXT.includes(language)
? highlighter.codeToThemedTokens(value, language)
: highlighter.codeToThemedTokens(value, FALLBACK_LANGUAGE)
let metaOptions
if (meta) {
metaOptions = parseMeta(meta)
}
if (lines.length > 1) {
if (options.showLineNumbers) {
const maxWidth = `${lines.length}`.length
htmlRendererOptions.showLineNumbers = true
htmlRendererOptions.lineNumberFormatter = lineNumber => `${lineNumber}`.padStart(maxWidth)
}
htmlRendererOptions.linesToHighlight =
options.highlightLines && metaOptions && metaOptions.linesToHighlight
? metaOptions.linesToHighlight
: null
} else {
htmlRendererOptions.showLineNumbers = false
}
return renderToHtml(lines, htmlRendererOptions)
}
const traverse = (tree, tokenType, highlighter, options) => {
visit(tree, tokenType, node => {
node.type = 'html'
try {
node.value = highlight(node, highlighter, options)
} catch (e) {
node.value = '<code>ERROR Rendering Code Block</code>'
}
})
}
module.exports = options => {
const theme = options.theme ? options.theme : 'nord'
options.aliases = options.aliases ? options.aliases : {}
return async tree => {
const highlighter = await shiki.getHighlighter({ theme })
try {
const { fg, bg } = highlighter.getTheme()
options.htmlRendererOptions = { fg, bg }
traverse(tree, 'code', highlighter, options)
if (!options.skipInline) {
traverse(tree, 'inlineCode', highlighter, options)
}
} catch (e) {
console.error(`Failed to load Shiki theme ${theme}`)
}
}
}