Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ditch deasync in 11ty plugin #189

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/witty-needles-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eleventy-plugin-shiki-twoslash": patch
---

Ditch deasync in 11ty plugin
24 changes: 8 additions & 16 deletions packages/eleventy-plugin-shiki-twoslash/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
// @ts-check
const { setupForFile, transformAttributesToHTML } = require("remark-shiki-twoslash")
const { sleep } = require("deasync")

/**
* @param {*} eleventyConfig
* @param {import("shiki-twoslash").UserConfigSettings} options
*/

let highlighters = undefined

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.on("eleventy.before", async () => {
const h = await setupForFile(options)
highlighters = await h.highlighters;
});

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)
})
}
}
1 change: 0 additions & 1 deletion packages/eleventy-plugin-shiki-twoslash/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"build": "pnpm exec tsc index.js --declaration --allowJs --emitDeclarationOnly --target es2018 -moduleResolution node"
},
"dependencies": {
"deasync": "^0.1.21",
"remark-shiki-twoslash": "3.1.7",
"typescript": ">3",
"@typescript/vfs": "1.4.0",
Expand Down