You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.8 KiB
39 lines
1.8 KiB
/**
|
|
* mermaid 空实现(stub)
|
|
*
|
|
* TDesign Chat 组件的 markdown-content 在模块顶层执行
|
|
* `Cherry.usePlugin(CherryCodeBlockMermaidPlugin, { mermaid, ... })`,
|
|
* 而 mermaid 插件构造函数要求 mermaid API 存在,否则抛
|
|
* `code-block-mermaid-plugin[init]: Package mermaid or mermaidAPI not found`,
|
|
* 导致整个 IIFE 加载失败。
|
|
*
|
|
* SDK 选择不打包 mermaid(省约 1.2MB gzip,且原 SDK 的 marked 渲染器本就不支持 mermaid 图)。
|
|
* 用此 stub 提供最小 API,模拟「同步版 mermaid」(render 为 3 参数签名):
|
|
* - 构造函数先取 `n.mermaidAPI`,再经 `isAsyncRenderVersion()`(判定 render.length === 3)
|
|
* 后回退到顶层 `n`,最终调用 `initialize()` —— 均需存在;
|
|
* - 顶层 `render` 在渲染 mermaid 代码块时抛异常,由插件的 syncRender try/catch 捕获,
|
|
* 降级为普通代码块展示。
|
|
*
|
|
* 宿主如需 mermaid 图,可在引入 SDK 前自行加载 mermaid(本 stub 仅作兜底占位)。
|
|
*/
|
|
const mermaidStub = {
|
|
// 顶层 mermaid:sync 分支下被赋值给 mermaidAPIRefs,需提供 initialize + render
|
|
initialize(): void {
|
|
// no-op:插件初始化占位
|
|
},
|
|
// 3 参数签名 → isAsyncRenderVersion() 判定为同步版本,走 syncRender 的 try/catch
|
|
render(_code: string, _element: unknown, _callback: unknown): void {
|
|
throw new Error('mermaid 未加载,代码块降级为普通代码展示');
|
|
},
|
|
// mermaidAPI 子对象:构造函数取值链 `n.mermaidAPI` 命中它,isAsyncRenderVersion 读其 render.length
|
|
mermaidAPI: {
|
|
initialize(): void {
|
|
// no-op
|
|
},
|
|
render(_code: string, _element: unknown, _callback: unknown): void {
|
|
throw new Error('mermaid 未加载,代码块降级为普通代码展示');
|
|
},
|
|
},
|
|
};
|
|
|
|
export default mermaidStub;
|