Skip to content

Commit 3d61619

Browse files
elecmonkeybrc-dd
andauthored
feat(markdown): support custom display-name for fenced code blocks (#4960)
--------- Co-authored-by: Divyansh Singh <[email protected]>
1 parent 612c458 commit 3d61619

File tree

5 files changed

+48
-11
lines changed

5 files changed

+48
-11
lines changed

docs/en/guide/markdown.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export default {
365365

366366
A [list of valid languages](https://shiki.style/languages) is available on Shiki's repository.
367367

368-
You may also customize syntax highlight theme in app config. Please see [`markdown` options](../reference/site-config#markdown) for more details.
368+
You may also customize syntax highlight theme, configure language aliases, and set custom language labels in app config. Please see [`markdown` options](../reference/site-config#markdown) for more details.
369369

370370
## Line Highlighting in Code Blocks
371371

docs/zh/guide/markdown.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ export default {
343343

344344
在 Shiki 的代码仓库中,可以找到[合法的编程语言列表](https://shiki.style/languages)
345345

346-
还可以全局配置中自定义语法高亮主题。有关详细信息,参见 [`markdown` 选项](../reference/site-config#markdown)得到更多信息。
346+
还可以在全局配置中自定义语法高亮主题、配置语言别名和自定义语言标签。有关详细信息,参见 [`markdown` 选项](../reference/site-config#markdown)得到更多信息。
347347

348348
## 在代码块中实现行高亮 {#line-highlighting-in-code-blocks}
349349

src/node/markdown/markdown.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,37 @@ export interface MarkdownOptions extends Options {
8787
*/
8888
languages?: (LanguageInput | BuiltinLanguage)[]
8989
/**
90-
* Custom language aliases.
90+
* Custom language aliases for syntax highlighting.
91+
* Maps custom language names to existing languages.
92+
* Alias lookup is case-insensitive and underscores in language names are displayed as spaces.
93+
*
94+
* @example
95+
*
96+
* Maps `my_lang` to use Python syntax highlighting.
97+
* ```js
98+
* { 'my_lang': 'python' }
99+
* ```
100+
*
101+
* Usage in markdown:
102+
* ````md
103+
* ```My_Lang
104+
* # This will be highlighted as Python code
105+
* # and will show "My Lang" as the language label
106+
* print("Hello, World!")
107+
* ```
108+
* ````
91109
*
92-
* @example { 'my-lang': 'js' }
93110
* @see https://shiki.style/guide/load-lang#custom-language-aliases
94111
*/
95112
languageAlias?: Record<string, string>
113+
/**
114+
* Custom language labels for display.
115+
* Overrides the default language label shown in code blocks.
116+
* Keys are case-insensitive.
117+
*
118+
* @example { 'vue': 'Vue SFC' }
119+
*/
120+
languageLabel?: Record<string, string>
96121
/**
97122
* Show line numbers in code blocks
98123
* @default false
@@ -249,7 +274,10 @@ export async function createMarkdownRenderer(
249274
// custom plugins
250275
md.use(componentPlugin, { ...options.component })
251276
.use(highlightLinePlugin)
252-
.use(preWrapperPlugin, { codeCopyButtonTitle })
277+
.use(preWrapperPlugin, {
278+
codeCopyButtonTitle,
279+
languageLabel: options.languageLabel
280+
})
253281
.use(snippetPlugin, srcDir)
254282
.use(containerPlugin, options.container)
255283
.use(imagePlugin, options.image)

src/node/markdown/plugins/highlight.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,18 @@ export async function highlight(
5959
codeTransformers: userTransformers = []
6060
} = options
6161

62+
const langAlias = Object.fromEntries(
63+
Object.entries(options.languageAlias || {}) //
64+
.map(([k, v]) => [k.toLowerCase(), v])
65+
)
66+
6267
const highlighter = await createHighlighter({
6368
themes:
6469
typeof theme === 'object' && 'light' in theme && 'dark' in theme
6570
? [theme.light, theme.dark]
6671
: [theme],
67-
langs: [
68-
...(options.languages || []),
69-
...Object.values(options.languageAlias || {})
70-
],
71-
langAlias: options.languageAlias
72+
langs: [...(options.languages || []), ...Object.values(langAlias)],
73+
langAlias
7274
})
7375

7476
await options?.shikiSetup?.(highlighter)

src/node/markdown/plugins/preWrapper.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ import type { MarkdownItAsync } from 'markdown-it-async'
22

33
export interface Options {
44
codeCopyButtonTitle: string
5+
languageLabel?: Record<string, string>
56
}
67

78
export function preWrapperPlugin(md: MarkdownItAsync, options: Options) {
9+
const langLabel = Object.fromEntries(
10+
Object.entries(options.languageLabel || {}) //
11+
.map(([k, v]) => [k.toLowerCase(), v])
12+
)
13+
814
const fence = md.renderer.rules.fence!
915
md.renderer.rules.fence = (...args) => {
1016
const [tokens, idx] = args
@@ -17,11 +23,12 @@ export function preWrapperPlugin(md: MarkdownItAsync, options: Options) {
1723
token.info = token.info.replace(/ active$/, '').replace(/ active /, ' ')
1824

1925
const lang = extractLang(token.info)
26+
const label = langLabel[lang.toLowerCase()] || lang.replace(/_/g, ' ')
2027

2128
return (
2229
`<div class="language-${lang}${active}">` +
2330
`<button title="${options.codeCopyButtonTitle}" class="copy"></button>` +
24-
`<span class="lang">${lang}</span>` +
31+
`<span class="lang">${label}</span>` +
2532
fence(...args) +
2633
'</div>'
2734
)

0 commit comments

Comments
 (0)