Skip to content

Commit 3281bf5

Browse files
authored
feat(markdown-it): support default and fallback lang #687 (#689)
Co-authored-by: winnliu <@waw981109@>
1 parent a397882 commit 3281bf5

File tree

6 files changed

+75
-3
lines changed

6 files changed

+75
-3
lines changed

packages/markdown-it/src/core.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import type MarkdownIt from 'markdown-it'
2-
import type { BuiltinTheme, CodeOptionsMeta, CodeOptionsThemes, CodeToHastOptions, HighlighterGeneric, ShikiTransformer, TransformerOptions } from 'shiki'
2+
import type {
3+
BuiltinLanguage,
4+
BuiltinTheme,
5+
CodeOptionsMeta,
6+
CodeOptionsThemes,
7+
CodeToHastOptions,
8+
HighlighterGeneric,
9+
LanguageInput,
10+
ShikiTransformer,
11+
TransformerOptions,
12+
} from 'shiki'
313

414
export interface MarkdownItShikiExtraOptions {
515
/**
@@ -21,6 +31,18 @@ export interface MarkdownItShikiExtraOptions {
2131
* @default true
2232
*/
2333
trimEndingNewline?: boolean
34+
35+
/**
36+
* When lang of code block is empty string, it will work.
37+
*
38+
* @default 'text'
39+
*/
40+
defaultLanguage?: LanguageInput | BuiltinLanguage
41+
42+
/**
43+
* When lang of code block is not included in langs of options, it will be as a fallback lang.
44+
*/
45+
fallbackLanguage?: LanguageInput | BuiltinLanguage
2446
}
2547

2648
export type MarkdownItShikiSetupOptions =
@@ -37,9 +59,17 @@ export function setupMarkdownIt(
3759
const {
3860
parseMetaString,
3961
trimEndingNewline = true,
62+
defaultLanguage = 'text',
63+
fallbackLanguage,
4064
} = options
41-
65+
const langs = highlighter.getLoadedLanguages()
4266
markdownit.options.highlight = (code, lang = 'text', attrs) => {
67+
if (lang === '') {
68+
lang = defaultLanguage as string
69+
}
70+
if (fallbackLanguage && !langs.includes(lang)) {
71+
lang = fallbackLanguage as string
72+
}
4373
const meta = parseMetaString?.(attrs, code, lang) || {}
4474
const codeOptions: CodeToHastOptions = {
4575
...options,

packages/markdown-it/test/fixtures/b.md

Lines changed: 3 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/b.out.html

Lines changed: 1 addition & 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/c.md

Lines changed: 3 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/c.out.html

Lines changed: 1 addition & 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: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import MarkdownIt from 'markdown-it'
44
import { transformerMetaHighlight } from '@shikijs/transformers'
55
import Shiki from '../src'
66

7-
it('run', async () => {
7+
it('run for base', async () => {
88
const md = MarkdownIt()
99
md.use(await Shiki({
1010
themes: {
@@ -20,3 +20,37 @@ it('run', async () => {
2020

2121
expect(result).toMatchFileSnapshot('./fixtures/a.out.html')
2222
})
23+
it('run for fallback language', async () => {
24+
const md = MarkdownIt()
25+
md.use(await Shiki({
26+
themes: {
27+
light: 'vitesse-light',
28+
dark: 'vitesse-dark',
29+
},
30+
fallbackLanguage: 'js',
31+
transformers: [
32+
transformerMetaHighlight(),
33+
],
34+
}))
35+
36+
const result = md.render(await fs.readFile(new URL('./fixtures/b.md', import.meta.url), 'utf-8'))
37+
38+
expect(result).toMatchFileSnapshot('./fixtures/b.out.html')
39+
})
40+
it('run for default language', async () => {
41+
const md = MarkdownIt()
42+
md.use(await Shiki({
43+
themes: {
44+
light: 'vitesse-light',
45+
dark: 'vitesse-dark',
46+
},
47+
defaultLanguage: 'js',
48+
transformers: [
49+
transformerMetaHighlight(),
50+
],
51+
}))
52+
53+
const result = md.render(await fs.readFile(new URL('./fixtures/c.md', import.meta.url), 'utf-8'))
54+
55+
expect(result).toMatchFileSnapshot('./fixtures/c.out.html')
56+
})

0 commit comments

Comments
 (0)