1+ import type { MarkdownOptions , UserConfig } from 'vitepress'
12import { parseVipMermaidFenceInfo } from './mermaid-theme'
23
3- interface MarkdownItLike {
4- renderer : {
5- rules : {
6- fence ?: (
7- tokens : Array < { info : string , content : string } > ,
8- idx : number ,
9- options : unknown ,
10- env : unknown ,
11- self : { renderToken : ( tokens : unknown , idx : number , options : unknown ) => string } ,
12- ) => string
13- }
14- }
15- }
4+ /** 与 `markdown.config(md)` 回调参数类型保持一致,避免手写精简接口 */
5+ type MarkdownIt = Parameters < NonNullable < MarkdownOptions [ 'config' ] > > [ 0 ]
166
177/**
188 * Markdown-it 插件:```mermaid / ```mmd → <VipMermaid>
199 * 需配合主题注册 VipMermaid(defineVipExtendsTheme 已内置)
2010 */
21- export function vipMermaidMarkdown ( md : MarkdownItLike ) : void {
11+ export function vipMermaidMarkdown ( md : MarkdownIt ) : void {
2212 const defaultFence = md . renderer . rules . fence ?. bind ( md . renderer . rules )
2313
2414 md . renderer . rules . fence = ( tokens , idx , options , env , self ) => {
@@ -39,6 +29,38 @@ export function vipMermaidMarkdown(md: MarkdownItLike): void {
3929 }
4030}
4131
32+ const MERMAID_OPTIMIZE_DEPS = [ 'mermaid' , '@braintree/sanitize-url' , 'copy-to-clipboard' ] as const
33+
34+ function mergeOptimizeDepsInclude (
35+ existing : string | string [ ] | undefined ,
36+ ) : string [ ] {
37+ const base = existing == null
38+ ? [ ]
39+ : Array . isArray ( existing )
40+ ? [ ...existing ]
41+ : [ existing ]
42+
43+ for ( const dep of MERMAID_OPTIMIZE_DEPS ) {
44+ if ( ! base . includes ( dep ) ) {
45+ base . push ( dep )
46+ }
47+ }
48+ return base
49+ }
50+
51+ /** 启用 Mermaid 时合并 Vite 预构建配置,修复 sanitize-url 等依赖的 CJS/ESM 互操作问题 */
52+ export function mergeVipMermaidViteConfig (
53+ vite : UserConfig [ 'vite' ] = { } ,
54+ ) : NonNullable < UserConfig [ 'vite' ] > {
55+ return {
56+ ...vite ,
57+ optimizeDeps : {
58+ ...vite . optimizeDeps ,
59+ include : mergeOptimizeDepsInclude ( vite . optimizeDeps ?. include ) ,
60+ } ,
61+ }
62+ }
63+
4264export type { VipMermaidOptions , VipMermaidTheme } from './mermaid-theme'
4365export {
4466 configureVipMermaid ,
0 commit comments