Skip to content

Commit

Permalink
feat(plugin-shiki): migrate to esm-based shikiji (close #12)
Browse files Browse the repository at this point in the history
  • Loading branch information
nruffing committed Dec 24, 2023
1 parent 93e6a04 commit 5395bca
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@
"globby",
"gtag",
"mdit",
"nord",
"nprogress",
"prefetch",
"preload",
"prismjs",
"shiki",
"shikiji",
"slugify",
"tsbuildinfo",
"unmount",
"vuejs",
"vuepress",
Expand Down
2 changes: 1 addition & 1 deletion plugins/plugin-shiki/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"dependencies": {
"@vuepress/core": "2.0.0-rc.0",
"shiki": "^0.14.5"
"shikiji": "^0.9.11"
},
"publishConfig": {
"access": "public"
Expand Down
44 changes: 38 additions & 6 deletions plugins/plugin-shiki/src/node/shikiPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,58 @@
import type { Plugin } from '@vuepress/core'
import { getHighlighter } from 'shiki'
import type { HighlighterOptions } from 'shiki'
import {


getHighlighter





} from 'shikiji'
import type {BundledLanguage, BundledTheme, LanguageInput, SpecialLanguage, StringLiteralUnion, ThemeRegistration, ThemeRegistrationRaw} from 'shikiji';

const defaultTheme = 'nord'

export type ShikiPluginTheme =
| ThemeRegistration
| ThemeRegistrationRaw
| StringLiteralUnion<BundledTheme>

/**
* Options of @vuepress/plugin-shiki
*/
export type ShikiPluginOptions = Pick<HighlighterOptions, 'theme' | 'langs'>
export interface ShikiPluginOptions {
theme?: ShikiPluginTheme
lightTheme?: ShikiPluginTheme
darkTheme?: ShikiPluginTheme
langs?: (
| LanguageInput
| StringLiteralUnion<BundledLanguage>
| SpecialLanguage
)[]
}

export const shikiPlugin = ({
theme = 'nord',
theme,
lightTheme,
darkTheme,
langs,
}: ShikiPluginOptions = {}): Plugin => ({
name: '@vuepress/plugin-shiki',

extendsMarkdown: async (md) => {
const highlighter = await getHighlighter({
theme,
themes: [theme, lightTheme, darkTheme].filter(
(t) => t !== undefined,
) as ShikiPluginTheme[],
langs,
})
md.options.highlight = (code, lang) =>
highlighter.codeToHtml(code, {
lang,
themes: {
light: lightTheme ?? theme ?? defaultTheme,
dark: darkTheme ?? theme ?? defaultTheme,
},
})
},
})
32 changes: 11 additions & 21 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 5395bca

Please sign in to comment.