Spaces:
Running
Running
File size: 4,174 Bytes
ac5fcb4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | /**
* Mermaid widget 执行环境构建器(备忘录 142)
*
* 背景:CWS MV3 远程托管代码政策(违规参考 ID Blue Argon)对扩展包做静态
* 扫描,任何指向包外的 <script src> 字符串——即使仅作为渲染端载荷、扩展自身
* 从不执行——都判违规。因此扩展只传 mermaid 定义信封
* {"_xwxMermaidDef": "<定义原文>"},执行环境由本模块在渲染端构建。
*
* 契约(三端必须保持一致,导出位图与 3.2.4 前逐像素同语义):
* - 库版本:mermaid 10.9.1 本地 vendor(v11 ER 图 themeVariables
* rowEven/rowOdd/nodeBorder 缺陷,server.js 旧格式 v11→v10 降级同款规避;
* 桌面端 BatchExporter widget_renderer.rs 同版本 vendor)
* - initialize 参数:{ startOnLoad: false, theme: 'default',
* flowchart: { htmlLabels: true }, securityLevel: 'loose' }
* - 渲染:mermaid.render('xwx-mmd', def) → #widget-container innerHTML;
* 失败置 document.title = 'MERMAID_RENDER_ERROR'(桌面端轮询 SVG in DOM,
* 标题信号保留与旧扩展模板一致)
* - 桌面端 BatchExporter widget_renderer.rs 的 mermaid 定义分支为本文件
* 的 Rust 同构实现,改参数须两端同步
*
* 信封识别(parseMermaidDefEnvelope):html 传输字段历史上有两种载荷——
* 完整执行 HTML(≤3.2.4 旧扩展)与本信封(≥3.2.5)。完整 HTML 以 <!DOCTYPE
* 或 < 开头,信封以 { 开头且可解析出 _xwxMermaidDef 字符串字段,判据互斥。
*/
const fs = require('fs');
const path = require('path');
/** vendor 库文本缓存(进程内只读一次;3.3MB 字符串常驻内存) */
let _mermaidLibJs = null;
/**
* 读取本地 vendor 的 mermaid 10.9.1 并做内联转义。
* 内联进 <script> 的标准转义:'</script' 会提前闭合脚本标签;'<!--' 会把
* HTML 解析器置入 script-data-escaped 状态。二者在 JS 字符串/正则/注释内
* 转义后(\/ 与 \!)语义不变,属于 esbuild/webpack 内联脚本的同款处理。
* @returns {string}
*/
function getMermaidVendorJs() {
if (_mermaidLibJs === null) {
const vendorPath = path.join(__dirname, '..', 'vendor', 'mermaid-10.9.1.min.js');
const raw = fs.readFileSync(vendorPath, 'utf8');
_mermaidLibJs = raw
.replace(/<\/script/gi, '<\\/script')
.replace(/<!--/g, '<\\!--');
}
return _mermaidLibJs;
}
/**
* 识别 mermaid 定义信封载荷。
* @param {string} widgetHtml - 传输字段原文(可能是完整 HTML 或信封 JSON)
* @returns {string|null} 定义原文;非信封载荷返回 null(走旧格式完整 HTML 路径)
*/
function parseMermaidDefEnvelope(widgetHtml) {
if (typeof widgetHtml !== 'string') return null;
const s = widgetHtml.trim();
if (!s.startsWith('{') || !s.includes('_xwxMermaidDef')) return null;
try {
const obj = JSON.parse(s);
const def = obj && typeof obj === 'object' ? obj._xwxMermaidDef : undefined;
return (typeof def === 'string' && def.trim().length > 0) ? def : null;
} catch {
return null;
}
}
/**
* mermaid 定义 → 可执行 widget 代码(不含外层文档——渲染模板
* templates/widget-render.html 已提供 #widget-container 容器,本代码作为
* %%WIDGET_CODE%% 注入该容器内,getElementById 命中的即容器本身)。
* @param {string} definition - mermaid 图定义(flowchart/sequenceDiagram/…)
* @returns {string}
*/
function buildMermaidWidgetCode(definition) {
const defJson = JSON.stringify(String(definition));
return `<script>${getMermaidVendorJs()}</script>` +
`<script>` +
`mermaid.initialize({ startOnLoad: false, theme: 'default', flowchart: { htmlLabels: true }, securityLevel: 'loose' });` +
`mermaid.render('xwx-mmd', ${defJson}).then(function (r) {` +
`document.getElementById('widget-container').innerHTML = r.svg;` +
`}).catch(function (e) { document.title = 'MERMAID_RENDER_ERROR'; document.getElementById('widget-container').textContent = String(e); });` +
`<\/script>`;
}
module.exports = { parseMermaidDefEnvelope, buildMermaidWidgetCode };
|