This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
131 lines (100 loc) · 3.6 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
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
/**
* @typedef {import('mdast').Root} Root
*/
import { createStarryNight, all } from '@wooorm/starry-night'
import fenceparser from '@microflash/fenceparser'
import { toHtml } from 'hast-util-to-html'
import { visit } from 'unist-util-visit'
const defaults = {
showLanguage: true,
showLineNumbers: true,
highlightLines: true,
showCaption: true,
aliases: {}
}
function decorate(code, scope, lang, options, metadata) {
const lines = code.split(/\r\n|\r|\n/)
const prompts = metadata && metadata.prompt ? metadata.prompt : null
if (options.showLineNumbers && lines && lines.length > 1) {
const maxWidth = `${lines.length}`.length
const highlightLines = options.highlightLines && metadata && metadata.highlight && metadata.highlight.length
code = lines.map((line, index) => {
const lineClasses = highlightLines && metadata.highlight.includes(index + 1) ? `line line-highlighted` : 'line'
const lineNumber = `${index + 1}`.padStart(maxWidth)
let decoratedLine = `<span class="${lineClasses}">`
decoratedLine += `<span class="line-number" aria-hidden="true">${lineNumber}</span>`
if (prompts && prompts.includes(index + 1)) {
decoratedLine += `<span class="line-prompt" aria-hidden="true">$</span>`
}
decoratedLine += line
decoratedLine += `</span>`
return decoratedLine
})
.join('\r\n')
} else {
let decoratedLine = `<span class="line line-standalone">`
if (prompts && prompts.includes(1)) {
decoratedLine += `<span class="line-prompt" aria-hidden="true">$</span>`
}
decoratedLine += code
decoratedLine += `</span>`
code = decoratedLine
}
const langToken = scope ? scope.replace(/^source\./, '').replace(/\./g, '-') : lang
let template = ''
template += `<div class="highlight highlight-${langToken}">`
const showCaption = options.showCaption && metadata && metadata.caption
const showHeader = options.showLanguage || showCaption
if (showHeader) {
template += `<div class="highlight-header">`
if (options.showLanguage && lang) {
template += `<div class="highlight-language">${lang}</div>`
}
if (showCaption) {
template += `<div class="highlight-caption">${metadata.caption}</div>`
}
template += `</div>`
}
template += `<pre><code tabindex="0">${code}</code></pre>`
template += `</div>`
return template
}
/**
* Plugin to highlight codeblocks with Starry Night
*
* @type {import('unified').Plugin<Array<void>, Root>}
*/
export default function remarkStarryNight(userOptions) {
const options = Object.assign({}, defaults, userOptions)
const { aliases } = options
const grammars = options.grammars || all
const starryNightPromise = createStarryNight(grammars)
return async function (tree) {
const starryNight = await starryNightPromise
visit(tree, 'code', function (node) {
const { value, lang = 'text', meta } = node
if (!value) {
return
}
const metadata = meta ? fenceparser(meta).metadata : null
const langId = aliases && aliases[lang] ? aliases[lang] : lang
const scope = starryNight.flagToScope(langId)
let code = value
if (scope) {
const highlighted = starryNight.highlight(value, scope)
code = toHtml(highlighted)
.replace(/[\r\n|\r|\n]*$/, '') // remove the last newline character
} else {
console.warn(`Grammar unavailable for ${langId}; rendering the code fence as text`)
}
let codeblock
if (options.disableDecorations) {
codeblock = `<div class="highlight highlight-${langToken}"><pre><code tabindex="0">${code}</code></pre></div>`
} else {
codeblock = decorate(code, scope, lang, options, metadata)
}
node.type = 'html'
node.value = codeblock
})
}
}