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): export bundled languages and their names and improve performance #163

Merged
merged 2 commits into from
May 29, 2024
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
1 change: 1 addition & 0 deletions plugins/plugin-shiki/src/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { shikiPlugin } from './shikiPlugin.js'

export { bundledLanguageNames, bundledLanguages } from './resolveHighlight.js'
export * from './shikiPlugin.js'
export * from './types.js'
/** @deprecated Use named export instead */
Expand Down
30 changes: 12 additions & 18 deletions plugins/plugin-shiki/src/node/resolveHighlight.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { transformerCompactLineOptions } from '@shikijs/transformers'
import {
bundledLanguages,
getHighlighter,
isPlainLang,
isSpecialLang,
} from 'shiki'
import { bundledLanguages, getHighlighter, isSpecialLang } from 'shiki'
import { colors, logger } from 'vuepress/utils'
import { getTransformers } from './transformers/getTransformers.js'
import type { ShikiHighlightOptions } from './types.js'
import { attrsToLines, nanoid, resolveLanguage } from './utils.js'

const DEFAULT_LANGS = Object.keys(bundledLanguages)
export { bundledLanguages } from 'shiki'
export const bundledLanguageNames = Object.keys(bundledLanguages)

const MUSTACHE_REG = /\{\{[^]*?\}\}/g

export const resolveHighlight = async ({
langs = DEFAULT_LANGS,
langs = bundledLanguageNames,
theme = 'nord',
themes,
defaultHighlightLang = '',
Expand All @@ -32,20 +28,18 @@ export const resolveHighlight = async ({
await options.shikiSetup?.(highlighter)

const transformers = getTransformers(options)
const loadedLanguages = highlighter.getLoadedLanguages()

return (str, language, attrs) => {
let lang = resolveLanguage(language)

if (lang) {
const langLoaded = highlighter.getLoadedLanguages().includes(lang as any)
if (!langLoaded && !isPlainLang(lang) && !isSpecialLang(lang)) {
logger.warn(
colors.yellow(
`\nThe language '${lang}' is not loaded, falling back to '${defaultHighlightLang || 'txt'}' for syntax highlighting.`,
),
)
lang = defaultHighlightLang
}
if (lang && !loadedLanguages.includes(lang) && !isSpecialLang(lang)) {
logger.warn(
colors.yellow(
`\nThe language '${lang}' is not loaded, falling back to '${defaultHighlightLang || 'txt'}' for syntax highlighting.`,
),
)
lang = defaultHighlightLang
}

const codeMustaches = new Map<string, string>()
Expand Down
Loading