fix(cli): preserve literal evidence in Markdown output

This commit is contained in:
morluto committed 2026-10-27 09:04:11 +08:00
1 parent bf7721080f
commit 6d7e627783
4 files changed
+78 -11

No files matched your search

+20 -6
View File
@@ -66,6 +66,19 @@ const escapeTableCell = (value: string): string =>
.replace(/[\\|`*[\]~]|_+/g, escapeInlineSyntax)
.replace(/\r\n|\r|\n/g, "<br>");
/** Heading and paragraph facts are literal data, including block syntax. */
const escapeMarkdownText = (value: string): string =>
escapeTableCell(value)
.replace(/#/g, "\\#")
.replace(/^(\s*)([-+=])/, "$1\\$2")
.replace(/^(\s*\d{1,9})([.)])(?=\s)/, "$1\\$2")
.replace(/^[ \t]+/, (indent) =>
indent.replace(/ /g, "&#32;").replace(/\t/g, "&#9;"),
);
const markdownHeading = (path: readonly string[]): string =>
`## ${escapeMarkdownText(path.join("."))}\n\n`;
const renderTable = (
headers: readonly string[],
rows: readonly string[][],
@@ -138,13 +151,13 @@ const formatMarkdown = (
): string => {
if (value == null && path.length === 0) return "";
if (isScalar(value)) {
if (path.length === 0) return String(value);
return `## ${path.join(".")}\n\n${String(value)}`;
if (path.length === 0) return escapeMarkdownText(String(value));
return `${markdownHeading(path)}${escapeMarkdownText(String(value))}`;
}
if (Array.isArray(value)) {
if (isObjectArray(value)) {
const table = formatObjectTable(value);
return path.length === 0 ? table : `## ${path.join(".")}\n\n${table}`;
return path.length === 0 ? table : `${markdownHeading(path)}${table}`;
}
return formatMarkdown(markdownCell(value), path);
}
@@ -163,8 +176,9 @@ const formatMarkdown = (
return entries
.map(([key, child]) => {
const childPath = [...path, key];
const heading = `## ${childPath.join(".")}\n\n`;
if (isScalar(child)) return `${heading}${String(child)}`;
const heading = markdownHeading(childPath);
if (isScalar(child))
return `${heading}${escapeMarkdownText(String(child))}`;
if (isObjectArray(child)) return `${heading}${formatObjectTable(child)}`;
if (
typeof child === "object" &&
@@ -176,7 +190,7 @@ const formatMarkdown = (
? `${heading}${formatKeyValueTable(nested)}`
: formatMarkdown(nested, childPath);
}
return `${heading}${markdownCell(child)}`;
return `${heading}${escapeMarkdownText(markdownCell(child))}`;
})
.join("\n\n");
};