Skip to content

Commit 97ff4b3

Browse files
authored
feat(markdown-it): add langAlias option (#1018)
1 parent 951c7fd commit 97ff4b3

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

packages/markdown-it/src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,25 @@ export type MarkdownItShikiOptions = MarkdownItShikiSetupOptions & {
1313
* @default Object.keys(bundledLanguages)
1414
*/
1515
langs?: Array<LanguageInput | BuiltinLanguage>
16+
17+
/**
18+
* Alias of languages
19+
* @example { 'my-lang': 'javascript' }
20+
*/
21+
langAlias?: Record<string, string>
1622
}
1723

1824
export default async function markdownItShiki(options: MarkdownItShikiOptions) {
1925
const themeNames = ('themes' in options
2026
? Object.values(options.themes)
2127
: [options.theme]).filter(Boolean) as BuiltinTheme[]
28+
const langs = options.langs || Object.keys(bundledLanguages)
29+
const langAlias = options.langAlias || {}
2230

2331
const highlighter = await createHighlighter({
2432
themes: themeNames,
25-
langs: options.langs || Object.keys(bundledLanguages) as BuiltinLanguage[],
33+
langs,
34+
langAlias,
2635
})
2736

2837
return function (markdownit: MarkdownIt) {

packages/markdown-it/test/fixtures/lang-alias.md

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/markdown-it/test/fixtures/lang-alias.out.html

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/markdown-it/test/index.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,19 @@ it('run for default language', { timeout: 10_000 }, async () => {
6464

6565
await expect(result).toMatchFileSnapshot('./fixtures/c.out.html')
6666
})
67+
68+
it('lang-alias', async () => {
69+
const md = MarkdownIt()
70+
md.use(await Shiki({
71+
langs: ['js'],
72+
theme: 'vitesse-light',
73+
langAlias: {
74+
mylang: 'javascript',
75+
mylang2: 'js', // nested alias
76+
},
77+
}))
78+
79+
const result = md.render(await fs.readFile(new URL('./fixtures/lang-alias.md', import.meta.url), 'utf-8'))
80+
81+
await expect(result).toMatchFileSnapshot('./fixtures/lang-alias.out.html')
82+
})

0 commit comments

Comments
 (0)