Skip to content

Commit 88d1a5c

Browse files
authored
feat(rehype): add langAlias option (#1015)
1 parent f38148e commit 88d1a5c

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

packages/rehype/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@ export type RehypeShikiOptions = RehypeShikiCoreOptions
1616
* @default Object.keys(bundledLanguages)
1717
*/
1818
langs?: Array<LanguageInput | BuiltinLanguage>
19+
/**
20+
* Alias of languages
21+
* @example { 'my-lang': 'javascript' }
22+
*/
23+
langAlias?: Record<string, string>
1924
}
2025

2126
const rehypeShiki: Plugin<[RehypeShikiOptions], Root> = function (
2227
options = {} as RehypeShikiOptions,
2328
) {
2429
const themeNames = ('themes' in options ? Object.values(options.themes) : [options.theme]).filter(Boolean) as BuiltinTheme[]
2530
const langs = options.langs || Object.keys(bundledLanguages)
31+
const langAlias = options.langAlias || {}
2632

2733
let getHandler: Promise<any>
2834

@@ -31,6 +37,7 @@ const rehypeShiki: Plugin<[RehypeShikiOptions], Root> = function (
3137
getHandler = getSingletonHighlighter({
3238
themes: themeNames,
3339
langs,
40+
langAlias,
3441
})
3542
.then(highlighter => rehypeShikiFromHighlighter.call(this, highlighter, options))
3643
}

packages/rehype/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/rehype/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/rehype/test/index.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@ import { unified } from 'unified'
77
import { expect, it } from 'vitest'
88
import rehypeShiki from '../src'
99

10+
it('lang-alias', async () => {
11+
const file = await unified()
12+
.use(remarkParse)
13+
.use(remarkRehype)
14+
.use(rehypeShiki, {
15+
theme: 'vitesse-light',
16+
addLanguageClass: true,
17+
langAlias: {
18+
mylang: 'javascript',
19+
mylang2: 'js', // nested alias
20+
},
21+
})
22+
.use(rehypeStringify)
23+
.process(await fs.readFile(new URL('./fixtures/lang-alias.md', import.meta.url)))
24+
25+
await expect(file.toString()).toMatchFileSnapshot('./fixtures/lang-alias.out.html')
26+
})
27+
1028
it('run', async () => {
1129
const file = await unified()
1230
.use(remarkParse)

0 commit comments

Comments
 (0)