-
-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(markdown-it): support
markdown-it-async
integration (#902)
- Loading branch information
Showing
11 changed files
with
213 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import type { MarkdownItAsync } from 'markdown-it-async' | ||
import type { CodeToHastOptions, ShikiTransformer } from 'shiki' | ||
import type { MarkdownItShikiSetupOptions } from './core' | ||
|
||
export type { MarkdownItShikiExtraOptions, MarkdownItShikiSetupOptions } from './common' | ||
|
||
export function setupMarkdownWithCodeToHtml( | ||
markdownit: MarkdownItAsync, | ||
codeToHtml: (code: string, options: CodeToHastOptions<any, any>) => Promise<string>, | ||
options: MarkdownItShikiSetupOptions, | ||
): void { | ||
const { | ||
parseMetaString, | ||
trimEndingNewline = true, | ||
defaultLanguage = 'text', | ||
} = options | ||
|
||
markdownit.options.highlight = async (code, lang = 'text', attrs) => { | ||
if (lang === '') { | ||
lang = defaultLanguage as string | ||
} | ||
const meta = parseMetaString?.(attrs, code, lang) || {} | ||
const codeOptions: CodeToHastOptions = { | ||
...options, | ||
lang, | ||
meta: { | ||
...options.meta, | ||
...meta, | ||
__raw: attrs, | ||
}, | ||
} | ||
|
||
const builtInTransformer: ShikiTransformer[] = [] | ||
|
||
builtInTransformer.push({ | ||
name: '@shikijs/markdown-it:block-class', | ||
code(node) { | ||
node.properties.class = `language-${lang}` | ||
}, | ||
}) | ||
|
||
if (trimEndingNewline) { | ||
if (code.endsWith('\n')) | ||
code = code.slice(0, -1) | ||
} | ||
|
||
return await codeToHtml( | ||
code, | ||
{ | ||
...codeOptions, | ||
transformers: [ | ||
...builtInTransformer, | ||
...codeOptions.transformers || [], | ||
], | ||
}, | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* Create a markdown-it-async plugin from a codeToHtml function. | ||
* | ||
* This plugin requires to be installed against a markdown-it-async instance. | ||
*/ | ||
export function fromAsyncCodeToHtml( | ||
codeToHtml: (code: string, options: CodeToHastOptions<any, any>) => Promise<string>, | ||
options: MarkdownItShikiSetupOptions, | ||
) { | ||
return async function (markdownit: MarkdownItAsync) { | ||
return setupMarkdownWithCodeToHtml(markdownit, codeToHtml, options) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { | ||
BuiltinLanguage, | ||
BuiltinTheme, | ||
CodeOptionsMeta, | ||
CodeOptionsThemes, | ||
LanguageInput, | ||
TransformerOptions, | ||
} from 'shiki' | ||
|
||
export interface MarkdownItShikiExtraOptions { | ||
/** | ||
* Custom meta string parser | ||
* Return an object to merge with `meta` | ||
*/ | ||
parseMetaString?: ( | ||
metaString: string, | ||
code: string, | ||
lang: string, | ||
) => Record<string, any> | undefined | null | ||
|
||
/** | ||
* markdown-it's highlight function will add a trailing newline to the code. | ||
* | ||
* This integration removes the trailing newline to the code by default, | ||
* you can turn this off by passing false. | ||
* | ||
* @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 = | ||
& CodeOptionsThemes<BuiltinTheme> | ||
& TransformerOptions | ||
& CodeOptionsMeta | ||
& MarkdownItShikiExtraOptions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import fs from 'node:fs/promises' | ||
import MarkdownItAsync from 'markdown-it-async' | ||
import { codeToHtml } from 'shiki' | ||
import { expect, it } from 'vitest' | ||
import { fromAsyncCodeToHtml } from '../src/async' | ||
|
||
it('async', { timeout: 10_000 }, async () => { | ||
const md = MarkdownItAsync() | ||
md.use(fromAsyncCodeToHtml(codeToHtml, { | ||
themes: { | ||
light: 'vitesse-light', | ||
dark: 'vitesse-dark', | ||
}, | ||
})) | ||
|
||
const result = await md.renderAsync(await fs.readFile(new URL('./fixtures/a.md', import.meta.url), 'utf-8')) | ||
|
||
await expect(result).toMatchFileSnapshot('./fixtures/a.async.out.html') | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters