Commit 10bc6133 authored by Miguel Angel Reina Ortega's avatar Miguel Angel Reina Ortega
Browse files

Prepare mermaid diagrams

parent 95139850
Loading
Loading
Loading
Loading
Loading
+21 −1
Changes for generateBaseline/mermaid-filter.js: 21 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -70,6 +70,26 @@ function figureCaptionInlines(el) {
	return out;
}

// stateDiagram-v2 treats unquoted "A --> B : label" as mermaid syntax.
// HTML <br /> and extra ":" in labels (e.g. "IPE: send_goal") must be quoted.
function prepareMermaidSource(content) {
	let source = String(content).replace(/<\s*br\s*\/?\s*>/gi, '<br/>');
	source = source.replace(
		/^(\s*(?:\[\*\]|[A-Za-z_][\w]*)\s*-->\s*(?:\[\*\]|[A-Za-z_][\w]*)\s*:\s*)(.*)$/gm,
		function (_match, prefix, label) {
			const trimmed = label.replace(/\s+$/, '');
			if (!trimmed || (trimmed.charAt(0) === '"' && trimmed.charAt(trimmed.length - 1) === '"')) {
				return prefix + trimmed;
			}
			if (/<|&|:/.test(trimmed)) {
				return prefix + '"' + trimmed.replace(/\\/g, '\\\\').replace(/"/g, '#quot;') + '"';
			}
			return prefix + trimmed;
		}
	);
	return source;
}

function renderMermaid(el, caption) {
	const attrs = el.c[0];
	const content = el.c[1];
@@ -77,7 +97,7 @@ function renderMermaid(el, caption) {
	const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'mermaid-'));
	const input = path.join(tmp, 'in.mmd');
	const output = path.join(tmp, 'out.png');
	fs.writeFileSync(input, content, 'utf8');
	fs.writeFileSync(input, prepareMermaidSource(content), 'utf8');

	const args = [
		'-i', input,