Skip to content

Commit

Permalink
Merge pull request #43 from shikijs/weakmap
Browse files Browse the repository at this point in the history
Use a weakmap for the highlighters
  • Loading branch information
orta authored Jun 26, 2021
2 parents 04f7989 + 2c63c9c commit 4df3f98
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 30 deletions.
4 changes: 2 additions & 2 deletions packages/docusaurus-preset-shiki-twoslash/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -22,7 +22,7 @@
"lint": "echo 'NOOP'"
},
"dependencies": {
"remark-shiki-twoslash": "1.4.7",
"remark-shiki-twoslash": "1.4.8",
"typescript": ">3"
}
}
4 changes: 2 additions & 2 deletions packages/eleventy-plugin-shiki-twoslash/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -21,7 +21,7 @@
},
"dependencies": {
"deasync": "^0.1.21",
"remark-shiki-twoslash": "1.4.7",
"remark-shiki-twoslash": "1.4.8",
"typescript": ">3"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-remark-shiki-twoslash/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/hexo-shiki-twoslash/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -21,7 +21,7 @@
"lint": "echo 'NOOP'"
},
"dependencies": {
"remark-shiki-twoslash": "1.4.7",
"remark-shiki-twoslash": "1.4.8",
"typescript": ">3"
}
}
4 changes: 2 additions & 2 deletions packages/markdown-it-shiki-twoslash/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/remark-shiki-twoslash/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
14 changes: 6 additions & 8 deletions packages/remark-shiki-twoslash/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IThemeRegistration, Highlighter>()
const highlighterCache = new WeakMap<UserConfigSettings, Highlighter[]>()

/** 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.has(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) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/twoslash-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "twoslash-cli",
"version": "1.2.4",
"version": "1.2.5",
"main": "index.js",
"bin": {
"twoslash": "./bin/twoslash.js",
Expand All @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions packages/vuepress-plugin-shiki-twoslash/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -22,7 +22,7 @@
"lint": "echo 'NOOP'"
},
"dependencies": {
"remark-shiki-twoslash": "1.4.7",
"remark-shiki-twoslash": "1.4.8",
"typescript": ">3"
}
}
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4df3f98

Please sign in to comment.