Skip to content

Commit

Permalink
feat(markdown-it): support default and fallback lang #687 (#689)
Browse files Browse the repository at this point in the history
Co-authored-by: winnliu <@waw981109@>
  • Loading branch information
Only566 committed Jun 2, 2024
1 parent a397882 commit 3281bf5
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 3 deletions.
34 changes: 32 additions & 2 deletions packages/markdown-it/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import type MarkdownIt from 'markdown-it'
import type { BuiltinTheme, CodeOptionsMeta, CodeOptionsThemes, CodeToHastOptions, HighlighterGeneric, ShikiTransformer, TransformerOptions } from 'shiki'
import type {
BuiltinLanguage,
BuiltinTheme,
CodeOptionsMeta,
CodeOptionsThemes,
CodeToHastOptions,
HighlighterGeneric,
LanguageInput,
ShikiTransformer,
TransformerOptions,
} from 'shiki'

export interface MarkdownItShikiExtraOptions {
/**
Expand All @@ -21,6 +31,18 @@ export interface MarkdownItShikiExtraOptions {
* @default true
*/
trimEndingNewline?: boolean

/**
* When lang of code block is empty string, it will work.
*
* @default 'text'
*/
defaultLanguage?: LanguageInput | BuiltinLanguage

/**
* When lang of code block is not included in langs of options, it will be as a fallback lang.
*/
fallbackLanguage?: LanguageInput | BuiltinLanguage
}

export type MarkdownItShikiSetupOptions =
Expand All @@ -37,9 +59,17 @@ export function setupMarkdownIt(
const {
parseMetaString,
trimEndingNewline = true,
defaultLanguage = 'text',
fallbackLanguage,
} = options

const langs = highlighter.getLoadedLanguages()
markdownit.options.highlight = (code, lang = 'text', attrs) => {
if (lang === '') {
lang = defaultLanguage as string
}
if (fallbackLanguage && !langs.includes(lang)) {
lang = fallbackLanguage as string
}
const meta = parseMetaString?.(attrs, code, lang) || {}
const codeOptions: CodeToHastOptions = {
...options,
Expand Down
3 changes: 3 additions & 0 deletions packages/markdown-it/test/fixtures/b.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/markdown-it/test/fixtures/b.out.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/markdown-it/test/fixtures/c.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/markdown-it/test/fixtures/c.out.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 35 additions & 1 deletion packages/markdown-it/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import MarkdownIt from 'markdown-it'
import { transformerMetaHighlight } from '@shikijs/transformers'
import Shiki from '../src'

it('run', async () => {
it('run for base', async () => {
const md = MarkdownIt()
md.use(await Shiki({
themes: {
Expand All @@ -20,3 +20,37 @@ it('run', async () => {

expect(result).toMatchFileSnapshot('./fixtures/a.out.html')
})
it('run for fallback language', async () => {
const md = MarkdownIt()
md.use(await Shiki({
themes: {
light: 'vitesse-light',
dark: 'vitesse-dark',
},
fallbackLanguage: 'js',
transformers: [
transformerMetaHighlight(),
],
}))

const result = md.render(await fs.readFile(new URL('./fixtures/b.md', import.meta.url), 'utf-8'))

expect(result).toMatchFileSnapshot('./fixtures/b.out.html')
})
it('run for default language', async () => {
const md = MarkdownIt()
md.use(await Shiki({
themes: {
light: 'vitesse-light',
dark: 'vitesse-dark',
},
defaultLanguage: 'js',
transformers: [
transformerMetaHighlight(),
],
}))

const result = md.render(await fs.readFile(new URL('./fixtures/c.md', import.meta.url), 'utf-8'))

expect(result).toMatchFileSnapshot('./fixtures/c.out.html')
})

0 comments on commit 3281bf5

Please sign in to comment.