|
1 |
| -export * from './env' |
2 |
| -export * from './markdown' |
| 1 | +import { componentPlugin } from '@mdit-vue/plugin-component' |
| 2 | +import { |
| 3 | + frontmatterPlugin, |
| 4 | + type FrontmatterPluginOptions |
| 5 | +} from '@mdit-vue/plugin-frontmatter' |
| 6 | +import { |
| 7 | + headersPlugin, |
| 8 | + type HeadersPluginOptions |
| 9 | +} from '@mdit-vue/plugin-headers' |
| 10 | +import { sfcPlugin, type SfcPluginOptions } from '@mdit-vue/plugin-sfc' |
| 11 | +import { titlePlugin } from '@mdit-vue/plugin-title' |
| 12 | +import { tocPlugin, type TocPluginOptions } from '@mdit-vue/plugin-toc' |
| 13 | +import { slugify } from '@mdit-vue/shared' |
| 14 | +import MarkdownIt from 'markdown-it' |
| 15 | +import anchorPlugin from 'markdown-it-anchor' |
| 16 | +import attrsPlugin from 'markdown-it-attrs' |
| 17 | +import emojiPlugin from 'markdown-it-emoji' |
| 18 | +import type { ILanguageRegistration, IThemeRegistration } from 'shiki' |
| 19 | +import type { Logger } from 'vite' |
| 20 | +import { containerPlugin } from './plugins/containers' |
| 21 | +import { highlight } from './plugins/highlight' |
| 22 | +import { highlightLinePlugin } from './plugins/highlightLines' |
| 23 | +import { imagePlugin } from './plugins/image' |
| 24 | +import { lineNumberPlugin } from './plugins/lineNumbers' |
| 25 | +import { linkPlugin } from './plugins/link' |
| 26 | +import { preWrapperPlugin } from './plugins/preWrapper' |
| 27 | +import { snippetPlugin } from './plugins/snippet' |
| 28 | + |
| 29 | +export type { Header } from '../shared' |
| 30 | + |
| 31 | +export type ThemeOptions = |
| 32 | + | IThemeRegistration |
| 33 | + | { light: IThemeRegistration; dark: IThemeRegistration } |
| 34 | + |
| 35 | +export interface MarkdownOptions extends MarkdownIt.Options { |
| 36 | + lineNumbers?: boolean |
| 37 | + preConfig?: (md: MarkdownIt) => void |
| 38 | + config?: (md: MarkdownIt) => void |
| 39 | + anchor?: anchorPlugin.AnchorOptions |
| 40 | + attrs?: { |
| 41 | + leftDelimiter?: string |
| 42 | + rightDelimiter?: string |
| 43 | + allowedAttributes?: string[] |
| 44 | + disable?: boolean |
| 45 | + } |
| 46 | + defaultHighlightLang?: string |
| 47 | + frontmatter?: FrontmatterPluginOptions |
| 48 | + headers?: HeadersPluginOptions | boolean |
| 49 | + sfc?: SfcPluginOptions |
| 50 | + theme?: ThemeOptions |
| 51 | + languages?: ILanguageRegistration[] |
| 52 | + toc?: TocPluginOptions |
| 53 | + externalLinks?: Record<string, string> |
| 54 | + cache?: boolean |
| 55 | +} |
| 56 | + |
| 57 | +export type MarkdownRenderer = MarkdownIt |
| 58 | + |
| 59 | +export const createMarkdownRenderer = async ( |
| 60 | + srcDir: string, |
| 61 | + options: MarkdownOptions = {}, |
| 62 | + base = '/', |
| 63 | + logger: Pick<Logger, 'warn'> = console |
| 64 | +): Promise<MarkdownRenderer> => { |
| 65 | + const theme = options.theme ?? { light: 'github-light', dark: 'github-dark' } |
| 66 | + const hasSingleTheme = typeof theme === 'string' || 'name' in theme |
| 67 | + |
| 68 | + const md = MarkdownIt({ |
| 69 | + html: true, |
| 70 | + linkify: true, |
| 71 | + highlight: |
| 72 | + options.highlight || |
| 73 | + (await highlight( |
| 74 | + theme, |
| 75 | + options.languages, |
| 76 | + options.defaultHighlightLang, |
| 77 | + logger |
| 78 | + )), |
| 79 | + ...options |
| 80 | + }) |
| 81 | + |
| 82 | + md.linkify.set({ fuzzyLink: false }) |
| 83 | + |
| 84 | + if (options.preConfig) { |
| 85 | + options.preConfig(md) |
| 86 | + } |
| 87 | + |
| 88 | + // custom plugins |
| 89 | + md.use(componentPlugin) |
| 90 | + .use(highlightLinePlugin) |
| 91 | + .use(preWrapperPlugin, { hasSingleTheme }) |
| 92 | + .use(snippetPlugin, srcDir) |
| 93 | + .use(containerPlugin, { hasSingleTheme }) |
| 94 | + .use(imagePlugin) |
| 95 | + .use( |
| 96 | + linkPlugin, |
| 97 | + { target: '_blank', rel: 'noreferrer', ...options.externalLinks }, |
| 98 | + base |
| 99 | + ) |
| 100 | + .use(lineNumberPlugin, options.lineNumbers) |
| 101 | + |
| 102 | + // 3rd party plugins |
| 103 | + if (!options.attrs?.disable) { |
| 104 | + md.use(attrsPlugin, options.attrs) |
| 105 | + } |
| 106 | + md.use(emojiPlugin) |
| 107 | + |
| 108 | + // mdit-vue plugins |
| 109 | + md.use(anchorPlugin, { |
| 110 | + slugify, |
| 111 | + permalink: anchorPlugin.permalink.linkInsideHeader({ |
| 112 | + symbol: '​', |
| 113 | + renderAttrs: (slug, state) => { |
| 114 | + // Find `heading_open` with the id identical to slug |
| 115 | + const idx = state.tokens.findIndex((token) => { |
| 116 | + const attrs = token.attrs |
| 117 | + const id = attrs?.find((attr) => attr[0] === 'id') |
| 118 | + return id && slug === id[1] |
| 119 | + }) |
| 120 | + // Get the actual heading content |
| 121 | + const title = state.tokens[idx + 1].content |
| 122 | + return { |
| 123 | + 'aria-label': `Permalink to "${title}"` |
| 124 | + } |
| 125 | + } |
| 126 | + }), |
| 127 | + ...options.anchor |
| 128 | + } as anchorPlugin.AnchorOptions).use(frontmatterPlugin, { |
| 129 | + ...options.frontmatter |
| 130 | + } as FrontmatterPluginOptions) |
| 131 | + |
| 132 | + if (options.headers) { |
| 133 | + md.use(headersPlugin, { |
| 134 | + level: [2, 3, 4, 5, 6], |
| 135 | + slugify, |
| 136 | + ...(typeof options.headers === 'boolean' ? undefined : options.headers) |
| 137 | + } as HeadersPluginOptions) |
| 138 | + } |
| 139 | + |
| 140 | + md.use(sfcPlugin, { |
| 141 | + ...options.sfc |
| 142 | + } as SfcPluginOptions) |
| 143 | + .use(titlePlugin) |
| 144 | + .use(tocPlugin, { |
| 145 | + ...options.toc |
| 146 | + } as TocPluginOptions) |
| 147 | + |
| 148 | + // apply user config |
| 149 | + if (options.config) { |
| 150 | + options.config(md) |
| 151 | + } |
| 152 | + |
| 153 | + return md |
| 154 | +} |
0 commit comments