From c3915c66c02de4d3e71fdd43adc5b1ec1ae23d78 Mon Sep 17 00:00:00 2001 From: Orta Date: Sat, 26 Jun 2021 15:07:11 +0100 Subject: [PATCH 1/2] Use a weakmap for the highlighters --- .../docusaurus-preset-shiki-twoslash/package.json | 4 ++-- .../eleventy-plugin-shiki-twoslash/package.json | 4 ++-- packages/gatsby-remark-shiki-twoslash/package.json | 4 ++-- packages/hexo-shiki-twoslash/package.json | 4 ++-- packages/markdown-it-shiki-twoslash/package.json | 4 ++-- packages/remark-shiki-twoslash/package.json | 2 +- packages/remark-shiki-twoslash/src/index.ts | 14 ++++++-------- packages/twoslash-cli/package.json | 4 ++-- .../vuepress-plugin-shiki-twoslash/package.json | 4 ++-- pnpm-lock.yaml | 14 +++++++------- 10 files changed, 28 insertions(+), 30 deletions(-) diff --git a/packages/docusaurus-preset-shiki-twoslash/package.json b/packages/docusaurus-preset-shiki-twoslash/package.json index 51e8c59..e6cc72c 100644 --- a/packages/docusaurus-preset-shiki-twoslash/package.json +++ b/packages/docusaurus-preset-shiki-twoslash/package.json @@ -1,6 +1,6 @@ { "name": "docusaurus-preset-shiki-twoslash", - "version": "1.1.2", + "version": "1.1.3", "license": "MIT", "homepage": "https://github.com/shikijs/twoslash", "repository": { @@ -22,7 +22,7 @@ "lint": "echo 'NOOP'" }, "dependencies": { - "remark-shiki-twoslash": "1.4.7", + "remark-shiki-twoslash": "1.4.8", "typescript": ">3" } } diff --git a/packages/eleventy-plugin-shiki-twoslash/package.json b/packages/eleventy-plugin-shiki-twoslash/package.json index 59e81cb..5b71c2c 100644 --- a/packages/eleventy-plugin-shiki-twoslash/package.json +++ b/packages/eleventy-plugin-shiki-twoslash/package.json @@ -1,6 +1,6 @@ { "name": "eleventy-plugin-shiki-twoslash", - "version": "1.0.6", + "version": "1.0.7", "license": "MIT", "homepage": "https://github.com/shikijs/twoslash", "repository": { @@ -21,7 +21,7 @@ }, "dependencies": { "deasync": "^0.1.21", - "remark-shiki-twoslash": "1.4.7", + "remark-shiki-twoslash": "1.4.8", "typescript": ">3" }, "devDependencies": { diff --git a/packages/gatsby-remark-shiki-twoslash/package.json b/packages/gatsby-remark-shiki-twoslash/package.json index 1c4f4d9..540e321 100644 --- a/packages/gatsby-remark-shiki-twoslash/package.json +++ b/packages/gatsby-remark-shiki-twoslash/package.json @@ -1,6 +1,6 @@ { "name": "gatsby-remark-shiki-twoslash", - "version": "3.0.2", + "version": "3.0.3", "license": "MIT", "homepage": "https://github.com/shikijs/twoslash", "repository": { @@ -23,7 +23,7 @@ "build": "" }, "dependencies": { - "remark-shiki-twoslash": "1.4.7", + "remark-shiki-twoslash": "1.4.8", "typescript": ">3", "unist-util-visit": "^2.0.0" }, diff --git a/packages/hexo-shiki-twoslash/package.json b/packages/hexo-shiki-twoslash/package.json index dffcb36..4678224 100644 --- a/packages/hexo-shiki-twoslash/package.json +++ b/packages/hexo-shiki-twoslash/package.json @@ -1,6 +1,6 @@ { "name": "hexo-shiki-twoslash", - "version": "1.0.4", + "version": "1.0.5", "license": "MIT", "homepage": "https://github.com/shikijs/twoslash", "repository": { @@ -21,7 +21,7 @@ "lint": "echo 'NOOP'" }, "dependencies": { - "remark-shiki-twoslash": "1.4.7", + "remark-shiki-twoslash": "1.4.8", "typescript": ">3" } } diff --git a/packages/markdown-it-shiki-twoslash/package.json b/packages/markdown-it-shiki-twoslash/package.json index 22a8956..1a6eade 100644 --- a/packages/markdown-it-shiki-twoslash/package.json +++ b/packages/markdown-it-shiki-twoslash/package.json @@ -1,6 +1,6 @@ { "name": "markdown-it-shiki-twoslash", - "version": "1.0.6", + "version": "1.0.7", "license": "MIT", "homepage": "https://github.com/shikijs/twoslash", "repository": { @@ -25,7 +25,7 @@ "prepublishOnly": "npm run build; npm run bootstrap" }, "dependencies": { - "remark-shiki-twoslash": "1.4.7" + "remark-shiki-twoslash": "1.4.8" }, "devDependencies": { "@types/deasync": "^0.1.1", diff --git a/packages/remark-shiki-twoslash/package.json b/packages/remark-shiki-twoslash/package.json index 6779d74..49f4923 100644 --- a/packages/remark-shiki-twoslash/package.json +++ b/packages/remark-shiki-twoslash/package.json @@ -1,6 +1,6 @@ { "name": "remark-shiki-twoslash", - "version": "1.4.7", + "version": "1.4.8", "license": "MIT", "homepage": "https://github.com/shikijs/twoslash", "repository": { diff --git a/packages/remark-shiki-twoslash/src/index.ts b/packages/remark-shiki-twoslash/src/index.ts index 103d553..8cb8902 100755 --- a/packages/remark-shiki-twoslash/src/index.ts +++ b/packages/remark-shiki-twoslash/src/index.ts @@ -59,29 +59,27 @@ export const runTwoSlashOnNode = (code: string, lang: string, meta: string, sett } // To make sure we only have one highlighter per theme in a process -const highlighterCache = new Map() +const highlighterCache = new WeakMap() /** Sets up the highlighters, and cache's for recalls */ export const highlightersFromSettings = async (settings: UserConfigSettings) => { const themes = settings.themes || (settings.theme ? [settings.theme] : ["light-plus"]) + if (highlighterCache.get(settings)) return highlighterCache.get(settings)! - return await Promise.all( + const highlighters = await Promise.all( themes.map(async theme => { // You can put a string, a path, or the JSON theme obj const themeName = (theme as any).name || theme - const cached = highlighterCache.get(themeName) - if (cached) { - return cached - } - const highlighter = await getHighlighter({ ...settings, theme, themes: undefined }) // @ts-ignore - https://github.com/shikijs/shiki/pull/162 will fix this highlighter.customName = themeName - highlighterCache.set(themeName, highlighter) return highlighter }) ) + + highlighterCache.set(settings, highlighters) + return highlighters } const amendSettingsForDefaults = (settings: UserConfigSettings) => { diff --git a/packages/twoslash-cli/package.json b/packages/twoslash-cli/package.json index 237893c..5ad8ba5 100644 --- a/packages/twoslash-cli/package.json +++ b/packages/twoslash-cli/package.json @@ -1,6 +1,6 @@ { "name": "twoslash-cli", - "version": "1.2.4", + "version": "1.2.5", "main": "index.js", "bin": { "twoslash": "./bin/twoslash.js", @@ -15,7 +15,7 @@ "hast-util-to-html": "^7.1.2", "mdast-util-to-hast": "^10.0.0", "remark": "^13.0.0", - "remark-shiki-twoslash": "1.4.7", + "remark-shiki-twoslash": "1.4.8", "unist-util-visit": "^3.1.0" }, "devDependencies": { diff --git a/packages/vuepress-plugin-shiki-twoslash/package.json b/packages/vuepress-plugin-shiki-twoslash/package.json index 3d06303..c7b5ac1 100644 --- a/packages/vuepress-plugin-shiki-twoslash/package.json +++ b/packages/vuepress-plugin-shiki-twoslash/package.json @@ -1,6 +1,6 @@ { "name": "vuepress-plugin-shiki-twoslash", - "version": "1.0.4", + "version": "1.0.5", "license": "MIT", "homepage": "https://github.com/shikijs/twoslash", "repository": { @@ -22,7 +22,7 @@ "lint": "echo 'NOOP'" }, "dependencies": { - "remark-shiki-twoslash": "1.4.7", + "remark-shiki-twoslash": "1.4.8", "typescript": ">3" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 057def1..0ccc7b4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -28,7 +28,7 @@ importers: packages/docusaurus-preset-shiki-twoslash: specifiers: - remark-shiki-twoslash: 1.4.7 + remark-shiki-twoslash: 1.4.8 typescript: '>3' dependencies: remark-shiki-twoslash: link:../remark-shiki-twoslash @@ -37,7 +37,7 @@ importers: packages/eleventy-plugin-shiki-twoslash: specifiers: deasync: ^0.1.21 - remark-shiki-twoslash: 1.4.7 + remark-shiki-twoslash: 1.4.8 shiki: 0.9.3 shiki-twoslash: 1.5.0 typescript: '>3' @@ -51,7 +51,7 @@ importers: packages/gatsby-remark-shiki-twoslash: specifiers: - remark-shiki-twoslash: 1.4.7 + remark-shiki-twoslash: 1.4.8 tslib: ^1.10.0 typescript: '>3' unist-util-visit: ^2.0.0 @@ -64,7 +64,7 @@ importers: packages/hexo-shiki-twoslash: specifiers: - remark-shiki-twoslash: 1.4.7 + remark-shiki-twoslash: 1.4.8 typescript: '>3' dependencies: remark-shiki-twoslash: link:../remark-shiki-twoslash @@ -77,7 +77,7 @@ importers: '@types/markdown-it': ^12.0.1 deasync: ^0.1.21 markdown-it: ^12.0.6 - remark-shiki-twoslash: 1.4.7 + remark-shiki-twoslash: 1.4.8 shiki: 0.9.3 shiki-twoslash: 1.5.0 tslib: ^1.10.0 @@ -167,7 +167,7 @@ importers: hast-util-to-html: ^7.1.2 mdast-util-to-hast: ^10.0.0 remark: ^13.0.0 - remark-shiki-twoslash: 1.4.7 + remark-shiki-twoslash: 1.4.8 typescript: ^3 unist-util-visit: ^3.1.0 dependencies: @@ -184,7 +184,7 @@ importers: packages/vuepress-plugin-shiki-twoslash: specifiers: - remark-shiki-twoslash: 1.4.7 + remark-shiki-twoslash: 1.4.8 typescript: '>3' dependencies: remark-shiki-twoslash: link:../remark-shiki-twoslash From 2c63c9cdced8fedc4f075f988cff889fbde2704a Mon Sep 17 00:00:00 2001 From: Orta Date: Sat, 26 Jun 2021 15:07:46 +0100 Subject: [PATCH 2/2] Use the right func to verify --- packages/remark-shiki-twoslash/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/remark-shiki-twoslash/src/index.ts b/packages/remark-shiki-twoslash/src/index.ts index 8cb8902..8b0f864 100755 --- a/packages/remark-shiki-twoslash/src/index.ts +++ b/packages/remark-shiki-twoslash/src/index.ts @@ -64,7 +64,7 @@ const highlighterCache = new WeakMap() /** Sets up the highlighters, and cache's for recalls */ export const highlightersFromSettings = async (settings: UserConfigSettings) => { const themes = settings.themes || (settings.theme ? [settings.theme] : ["light-plus"]) - if (highlighterCache.get(settings)) return highlighterCache.get(settings)! + if (highlighterCache.has(settings)) return highlighterCache.get(settings)! const highlighters = await Promise.all( themes.map(async theme => {