Skip to content

Commit

Permalink
feat(plugin-shiki): migrate to esm-based shikiji (close #12) (#13)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: now `langs` option is required, you need to set the languages list explicitly

Co-authored-by: meteorlxy <[email protected]>
  • Loading branch information
nruffing and meteorlxy authored Dec 28, 2023
1 parent 6f3f865 commit df11c04
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 27 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.7"
"shikiji": "^0.9.12"
},
"publishConfig": {
"access": "public"
Expand Down
58 changes: 53 additions & 5 deletions plugins/plugin-shiki/src/node/shikiPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,74 @@
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'

export type ShikiLang =
| LanguageInput
| StringLiteralUnion<BundledLanguage>
| SpecialLanguage

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

/**
* Options of @vuepress/plugin-shiki
*/
export type ShikiPluginOptions = Pick<HighlighterOptions, 'theme' | 'langs'>
export interface ShikiPluginOptions {
/**
* Languages to be loaded.
*
* Shikiji does not preload any languages to avoid extra overhead. So you need
* to specify the languages you want to use.
*
* @see https://shikiji.netlify.app/languages
*/
langs?: ShikiLang[]

/**
* The single theme to use
*
* @see https://shikiji.netlify.app/themes
*/
theme?: ShikiTheme

/**
* The dark and light themes to use
*
* @see https://shikiji.netlify.app/themes
*/
themes?: {
dark: ShikiTheme
light: ShikiTheme
}
}

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

extendsMarkdown: async (md) => {
const highlighter = await getHighlighter({
theme,
langs,
themes: themes ? [themes.dark, themes.light] : [theme],
})

md.options.highlight = (code, lang) =>
highlighter.codeToHtml(code, {
lang,
...(themes ? { themes } : { theme }),
})
},
})
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 df11c04

Please sign in to comment.