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

Use a weakmap for the highlighters #43

Merged
merged 2 commits into from
Jun 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.