diff --git a/src/node/markdown/markdown.ts b/src/node/markdown/markdown.ts index 7e6d09e8..9cdaff85 100644 --- a/src/node/markdown/markdown.ts +++ b/src/node/markdown/markdown.ts @@ -88,11 +88,24 @@ export interface MarkdownOptions extends Options { languages?: (LanguageInput | BuiltinLanguage)[] /** * Custom language aliases. + * Maps custom language names to existing languages for syntax highlighting. + * Language identifiers with underscores will automatically display with spaces. + * Alias lookup is case-insensitive. + * + * @example { 'my_lang': 'python' } + * + * ```My_Lang uses Python highlighting, displays "My Lang" * - * @example { 'my-lang': 'js' } * @see https://shiki.style/guide/load-lang#custom-language-aliases */ languageAlias?: Record + /** + * Custom language labels for display. + * Overrides the default language label shown in code blocks. + * + * @example { 'vue': 'Vue SFC' } + */ + languageLabel?: Record /** * Show line numbers in code blocks * @default false @@ -249,7 +262,10 @@ export async function createMarkdownRenderer( // custom plugins md.use(componentPlugin, { ...options.component }) .use(highlightLinePlugin) - .use(preWrapperPlugin, { codeCopyButtonTitle }) + .use(preWrapperPlugin, { + codeCopyButtonTitle, + languageLabel: options.languageLabel + }) .use(snippetPlugin, srcDir) .use(containerPlugin, options.container) .use(imagePlugin, options.image) diff --git a/src/node/markdown/plugins/highlight.ts b/src/node/markdown/plugins/highlight.ts index f1285ed7..4e62d3d1 100644 --- a/src/node/markdown/plugins/highlight.ts +++ b/src/node/markdown/plugins/highlight.ts @@ -68,7 +68,10 @@ export async function highlight( ...(options.languages || []), ...Object.values(options.languageAlias || {}) ], - langAlias: options.languageAlias + langAlias: options.languageAlias ? + Object.fromEntries( + Object.entries(options.languageAlias).map(([key, value]) => [key.toLowerCase(), value]) + ) : undefined }) await options?.shikiSetup?.(highlighter) diff --git a/src/node/markdown/plugins/preWrapper.ts b/src/node/markdown/plugins/preWrapper.ts index e7d553b3..d68043a1 100644 --- a/src/node/markdown/plugins/preWrapper.ts +++ b/src/node/markdown/plugins/preWrapper.ts @@ -2,6 +2,7 @@ import type { MarkdownItAsync } from 'markdown-it-async' export interface Options { codeCopyButtonTitle: string + languageLabel?: Record } export function preWrapperPlugin(md: MarkdownItAsync, options: Options) { @@ -17,11 +18,12 @@ export function preWrapperPlugin(md: MarkdownItAsync, options: Options) { token.info = token.info.replace(/ active$/, '').replace(/ active /, ' ') const lang = extractLang(token.info) + const langLabel = getLangLabel(lang, options.languageLabel) return ( `
` + `` + - `${lang}` + + `${langLabel}` + fence(...args) + '
' ) @@ -46,3 +48,11 @@ function extractLang(info: string) { .replace(/^vue-html$/, 'template') .replace(/^ansi$/, '') } + +function getLangLabel(lang: string, languageLabel?: Record): string { + if (languageLabel && languageLabel[lang]) { + return languageLabel[lang] + } + + return lang.replace(/_/g, ' ') +}