feat: expose `shikijiSetup` hook (#3344)

pull/3345/head
Anthony Fu 1 year ago committed by GitHub
parent 00eec7a4ef
commit d12e23ddf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -31,7 +31,8 @@ import type {
ThemeRegistration,
BuiltinTheme,
LanguageInput,
ShikijiTransformer
ShikijiTransformer,
Highlighter
} from 'shikiji'
export type { Header } from '../shared'
@ -103,6 +104,10 @@ export interface MarkdownOptions extends MarkdownIt.Options {
* @see https://github.com/antfu/shikiji#hast-transformers
*/
codeTransformers?: ShikijiTransformer[]
/**
* Setup Shikiji instance
*/
shikijiSetup?: (shikiji: Highlighter) => void | Promise<void>
/* ==================== Markdown It Plugins ==================== */
@ -176,16 +181,7 @@ export const createMarkdownRenderer = async (
const md = MarkdownIt({
html: true,
linkify: true,
highlight:
options.highlight ||
(await highlight(
theme,
options.languages,
options.defaultHighlightLang,
logger,
options.codeTransformers,
options.languageAlias
)),
highlight: options.highlight || (await highlight(theme, options, logger)),
...options
})

@ -1,6 +1,6 @@
import { customAlphabet } from 'nanoid'
import c from 'picocolors'
import type { LanguageInput, ShikijiTransformer } from 'shikiji'
import type { ShikijiTransformer } from 'shikiji'
import {
bundledLanguages,
getHighlighter,
@ -9,7 +9,7 @@ import {
isSpecialLang
} from 'shikiji'
import type { Logger } from 'vite'
import type { ThemeOptions } from '../markdown'
import type { MarkdownOptions, ThemeOptions } from '../markdown'
import {
transformerCompactLineOptions,
transformerNotationDiff,
@ -55,21 +55,25 @@ const attrsToLines = (attrs: string): TransformerCompactLineOption[] => {
export async function highlight(
theme: ThemeOptions,
languages: LanguageInput[] = [],
defaultLang: string = '',
logger: Pick<Logger, 'warn'> = console,
userTransformers: ShikijiTransformer[] = [],
languageAlias: Record<string, string> = {}
options: MarkdownOptions,
logger: Pick<Logger, 'warn'> = console
): Promise<(str: string, lang: string, attrs: string) => string> {
const {
defaultHighlightLang: defaultLang = '',
codeTransformers: userTransformers = []
} = options
const highlighter = await getHighlighter({
themes:
typeof theme === 'string' || 'name' in theme
? [theme]
: [theme.light, theme.dark],
langs: [...Object.keys(bundledLanguages), ...languages],
langAlias: languageAlias
langs: [...Object.keys(bundledLanguages), ...(options.languages || [])],
langAlias: options.languageAlias
})
await options?.shikijiSetup?.(highlighter)
const transformers: ShikijiTransformer[] = [
transformerNotationDiff(),
transformerNotationFocus({

Loading…
Cancel
Save