-
Notifications
You must be signed in to change notification settings - Fork 54
/
index.js
30 lines (27 loc) · 1.11 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
// @ts-check
const { setupForFile, transformAttributesToHTML } = require("remark-shiki-twoslash")
const { sleep } = require("deasync")
/**
* @param {*} eleventyConfig
* @param {import("shiki-twoslash").UserConfigSettings} options
*/
module.exports = function (eleventyConfig, options = {}) {
/** @type {import("shiki").Highlighter[]} */
let highlighters = undefined
setupForFile(options).then(h => (highlighters = h.highlighters))
if (!highlighters) {
let count = 10000 / 200
while (!highlighters) {
sleep(200)
count -= 1
if (count <= 0)
throw new Error(
"Could not get Shiki loaded async via 'deasync'. 11ty doesn't have an API for async plugins, and Shiki needs this for the WASM syntax highlighter. You can try using a different version of node, or requesting APIs at https://github.com/11ty/eleventy"
)
}
}
eleventyConfig.addMarkdownHighlighter((code, lang, fence) => {
code = code.replace(/\r?\n$/, "") // strip trailing newline fed during code block parsing
return transformAttributesToHTML(code, [lang, fence].join(" "), highlighters, options)
})
}