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

feat(plugin-shiki): migrate to esm-based shikiji (close #12) #13

Merged
merged 7 commits into from
Dec 28, 2023
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
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.