Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,45 @@
|
||||
// 只呈现编译器已经确定的控制边、失败出口与调用位置,不按名字猜业务流程。
|
||||
const { pathToFileURL } = require('node:url');
|
||||
function escape(value) {
|
||||
return value.replaceAll('&', '#38;').replaceAll('"', '#34;').replaceAll('<', '#60;')
|
||||
.replaceAll('>', '#62;').replaceAll('|', '#124;').replaceAll('\n', ' ').replaceAll(String.fromCharCode(96), '#96;');
|
||||
}
|
||||
function workflow(index, root) {
|
||||
const labels = [];
|
||||
const diagram = (index.flows || []).map(flow => render(flow, index, root, labels)).join('\n');
|
||||
return { diagram, labels };
|
||||
}
|
||||
function render(flow, index, root, labels) {
|
||||
const fence = String.fromCharCode(96).repeat(3);
|
||||
const lines = ['## 执行流程', '', '图显示可能的执行路径。点击节点打开源码,展开调用查看子流程。', '', fence + 'mermaid', 'flowchart TD'];
|
||||
const full = [];
|
||||
for (const [id, node] of flow.nodes.entries()) {
|
||||
const [left, right] = node.kind === 'branch' ? ['{', '}'] : node.kind === 'end' ? ['([', '])'] : ['[', ']'];
|
||||
const characters = [...node.label];
|
||||
let label = node.label;
|
||||
if (characters.length > 48) {
|
||||
const prefix = characters.slice(0, 42).join('');
|
||||
const args = node.kind === 'branch' ? -1 : prefix.indexOf('(');
|
||||
label = (args > 0 ? prefix.slice(0, args) + '(…)' : prefix + '…') + ' [' + (id + 1) + ']';
|
||||
full.push(`节点 ${id + 1}`, '', ...node.label.split('\n').map(line => ' ' + line), '');
|
||||
}
|
||||
lines.push(' n' + id + left + '"' + escape(label) + '"' + right);
|
||||
if (node.site) {
|
||||
const target = pathToFileURL(index.files[node.site.file].path);
|
||||
target.hash = 'L' + (node.site.start.line + 1) + ',' + (node.site.start.character + 1);
|
||||
lines.push(' click n' + id + ' href "' + target.href + '" "打开源码"');
|
||||
}
|
||||
}
|
||||
for (const edge of flow.edges) lines.push(' n' + edge.from + ' -->' + (edge.label ? '|"' + escape(edge.label) + '"|' : '') + ' n' + edge.to);
|
||||
lines.push(fence, '');
|
||||
if (full.length) {
|
||||
lines.push('带编号节点的原文见「实现细节」。', '');
|
||||
labels.push('### ' + index.symbols.find(symbol => symbol.id === flow.symbol).name, '', ...full);
|
||||
}
|
||||
for (const node of flow.nodes) if (node.target) {
|
||||
const target = 'command:dever.expandFlow?' + encodeURIComponent(JSON.stringify([root, node.target]));
|
||||
lines.push('[展开 ' + node.target + '](' + target + ')', '');
|
||||
}
|
||||
return lines.join('\n');
|
||||
}
|
||||
module.exports = { workflow };
|
||||
Reference in new issue
Block a user