|
1 | | -/** |
2 | | - * Entry frontmatter and body helpers, shared by build.mjs and validate.mjs so |
3 | | - * the two can never read the same file differently. (They used to carry a |
4 | | - * copy each, and the copies had drifted: one undid quote escapes and the |
5 | | - * other did not.) |
6 | | - * |
7 | | - * The parser reads exactly the YAML subset entries use: `key: value`, quoted |
8 | | - * scalars, flow lists (`[a, "b, c"]`), block lists (`key:` then ` - item`) |
9 | | - * and `#` comment lines. Anything else, and a key given twice, is reported |
10 | | - * with its line number rather than guessed at. |
11 | | - */ |
12 | | -import { renderInline } from "./markdown.mjs"; |
13 | | -import { unquoteScalar } from "./shared.mjs"; |
14 | | - |
15 | | -/** Splits the inside of a flow list on commas that are not inside quotes. */ |
16 | | -function splitFlowList(inner) { |
17 | | - const parts = []; |
18 | | - let cur = ""; |
19 | | - let quote = null; |
20 | | - for (let i = 0; i < inner.length; i += 1) { |
21 | | - const ch = inner[i]; |
22 | | - if (quote) { |
23 | | - cur += ch; |
24 | | - if (quote === '"' && ch === "\\" && i + 1 < inner.length) cur += inner[++i]; |
25 | | - else if (ch === quote) quote = null; |
26 | | - } else if (ch === '"' || ch === "'") { |
27 | | - quote = ch; |
28 | | - cur += ch; |
29 | | - } else if (ch === ",") { |
30 | | - parts.push(cur); |
31 | | - cur = ""; |
32 | | - } else cur += ch; |
33 | | - } |
34 | | - parts.push(cur); |
35 | | - return parts.map((p) => unquoteScalar(p.trim())); |
36 | | -} |
37 | | - |
38 | | -export function parseScalar(raw) { |
39 | | - const v = raw.trim(); |
40 | | - if (v === "true") return true; |
41 | | - if (v === "false") return false; |
42 | | - if (v === "null" || v === "~" || v === "") return null; |
43 | | - if (v.length >= 2 && ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'")))) { |
44 | | - return unquoteScalar(v); |
45 | | - } |
46 | | - if (v.startsWith("[") && v.endsWith("]")) { |
47 | | - const inner = v.slice(1, -1).trim(); |
48 | | - return inner ? splitFlowList(inner) : []; |
49 | | - } |
50 | | - return v; |
51 | | -} |
52 | | - |
53 | | -/** |
54 | | - * `{ meta, body, errors }`, or null when the text has no frontmatter block. |
55 | | - * `errors` are "line N: ..." strings; the caller prefixes the file. |
56 | | - */ |
57 | | -export function parseFrontmatter(text) { |
58 | | - // A byte-order mark is invisible in an editor; ignore it. |
59 | | - const match = String(text) |
60 | | - .replace(/^/, "") |
61 | | - .match(/^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n([\s\S]*))?$/); |
62 | | - if (!match) return null; |
63 | | - const meta = {}; |
64 | | - const errors = []; |
65 | | - const lines = match[1].split(/\r?\n/); |
66 | | - let listKey = null; |
67 | | - lines.forEach((line, i) => { |
68 | | - const lineNo = i + 2; // line 1 is the opening --- |
69 | | - if (!line.trim() || line.trim().startsWith("#")) return; |
70 | | - const item = line.match(/^\s+-\s+(.*)$/); |
71 | | - if (item) { |
72 | | - if (listKey === null) errors.push(`line ${lineNo}: list item without a key above it`); |
73 | | - else meta[listKey].push(parseScalar(item[1])); |
74 | | - return; |
75 | | - } |
76 | | - listKey = null; |
77 | | - const idx = line.indexOf(":"); |
78 | | - if (idx === -1 || /^\s/.test(line)) { |
79 | | - errors.push(`line ${lineNo}: "${line.trim()}" is not a key: value line`); |
80 | | - return; |
81 | | - } |
82 | | - const key = line.slice(0, idx).trim(); |
83 | | - if (Object.prototype.hasOwnProperty.call(meta, key)) { |
84 | | - errors.push(`line ${lineNo}: key "${key}" is given twice`); |
85 | | - return; |
86 | | - } |
87 | | - const value = parseScalar(line.slice(idx + 1)); |
88 | | - const next = lines[i + 1]; |
89 | | - if (value === null && next !== undefined && /^\s+-\s/.test(next)) { |
90 | | - meta[key] = []; |
91 | | - listKey = key; |
92 | | - return; |
93 | | - } |
94 | | - meta[key] = value; |
95 | | - }); |
96 | | - return { meta, body: match[2] || "", errors }; |
97 | | -} |
98 | | - |
99 | | -/** The `## Evidence` section of an entry body, up to the next `## ` heading, or null. */ |
100 | | -export function evidenceSection(body) { |
101 | | - const idx = body.search(/^## Evidence\s*$/m); |
102 | | - if (idx === -1) return null; |
103 | | - const rest = body.slice(idx); |
104 | | - const next = rest.search(/\n## (?!Evidence\s*$)/m); |
105 | | - return next === -1 ? rest : rest.slice(0, next); |
106 | | -} |
107 | | - |
108 | | -const ENTITIES = { "&": "&", "<": "<", ">": ">", """: '"', "'": "'" }; |
109 | | - |
110 | | -/** |
111 | | - * Plain text of a line of entry markdown. It goes through the same inline |
112 | | - * renderer as the entry page and drops the tags, so it strips exactly the |
113 | | - * markup the page renders: underscores and `#` stay literal (Rig_Medium, C#). |
114 | | - */ |
115 | | -export function plainText(markdown) { |
116 | | - const html = renderInline(markdown, (href) => ({ href, external: true })); |
117 | | - return html |
118 | | - .replace(/<[^>]+>/g, "") |
119 | | - .replace(/&(amp|lt|gt|quot|#39);/g, (m) => ENTITIES[m]) |
120 | | - .replace(/\s+/g, " ") |
121 | | - .trim(); |
122 | | -} |
123 | | - |
124 | | -/** The first paragraph of an entry body as plain text, at most 220 characters. */ |
125 | | -export function summaryFromBody(body) { |
126 | | - const first = |
127 | | - String(body) |
128 | | - .split(/\n\s*\n/) |
129 | | - .map((p) => p.trim()) |
130 | | - .find((p) => p && !p.startsWith("#")) || ""; |
131 | | - const clean = plainText(first); |
132 | | - return clean.length > 220 ? `${clean.slice(0, 217)}…` : clean; |
133 | | -} |
| 1 | +/** |
| 2 | + * Entry frontmatter and body helpers, shared by build.mjs and validate.mjs so |
| 3 | + * the two can never read the same file differently. (They used to carry a |
| 4 | + * copy each, and the copies had drifted: one undid quote escapes and the |
| 5 | + * other did not.) |
| 6 | + * |
| 7 | + * The parser reads exactly the YAML subset entries use: `key: value`, quoted |
| 8 | + * scalars, flow lists (`[a, "b, c"]`), block lists (`key:` then ` - item`) |
| 9 | + * and `#` comment lines. Anything else, and a key given twice, is reported |
| 10 | + * with its line number rather than guessed at. |
| 11 | + */ |
| 12 | +import { renderInline } from "./markdown.mjs"; |
| 13 | +import { unquoteScalar } from "./shared.mjs"; |
| 14 | + |
| 15 | +/** Splits the inside of a flow list on commas that are not inside quotes. */ |
| 16 | +function splitFlowList(inner) { |
| 17 | + const parts = []; |
| 18 | + let cur = ""; |
| 19 | + let quote = null; |
| 20 | + for (let i = 0; i < inner.length; i += 1) { |
| 21 | + const ch = inner[i]; |
| 22 | + if (quote) { |
| 23 | + cur += ch; |
| 24 | + if (quote === '"' && ch === "\\" && i + 1 < inner.length) cur += inner[++i]; |
| 25 | + else if (ch === quote) quote = null; |
| 26 | + } else if (ch === '"' || ch === "'") { |
| 27 | + quote = ch; |
| 28 | + cur += ch; |
| 29 | + } else if (ch === ",") { |
| 30 | + parts.push(cur); |
| 31 | + cur = ""; |
| 32 | + } else cur += ch; |
| 33 | + } |
| 34 | + parts.push(cur); |
| 35 | + return parts.map((p) => unquoteScalar(p.trim())); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Drops a trailing ` # comment`. As in YAML, a `#` starts a comment only after |
| 40 | + * whitespace (or at the start) and outside quotes, so `C#` and `#fff` are |
| 41 | + * content. A quote opens only where a scalar can start (the start, or after |
| 42 | + * `[`, `,` or whitespace), so an apostrophe in `Kay's` is content too. |
| 43 | + */ |
| 44 | +function stripComment(raw) { |
| 45 | + let quote = null; |
| 46 | + for (let i = 0; i < raw.length; i += 1) { |
| 47 | + const ch = raw[i]; |
| 48 | + if (quote) { |
| 49 | + if (quote === '"' && ch === "\\") i += 1; |
| 50 | + else if (ch === quote) quote = null; |
| 51 | + } else if ((ch === '"' || ch === "'") && (i === 0 || /[\s[,]/.test(raw[i - 1]))) { |
| 52 | + quote = ch; |
| 53 | + } else if (ch === "#" && (i === 0 || /\s/.test(raw[i - 1]))) { |
| 54 | + return raw.slice(0, i); |
| 55 | + } |
| 56 | + } |
| 57 | + return raw; |
| 58 | +} |
| 59 | + |
| 60 | +export function parseScalar(raw) { |
| 61 | + const v = stripComment(raw).trim(); |
| 62 | + if (v === "true") return true; |
| 63 | + if (v === "false") return false; |
| 64 | + if (v === "null" || v === "~" || v === "") return null; |
| 65 | + if (v.length >= 2 && ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'")))) { |
| 66 | + return unquoteScalar(v); |
| 67 | + } |
| 68 | + if (v.startsWith("[") && v.endsWith("]")) { |
| 69 | + const inner = v.slice(1, -1).trim(); |
| 70 | + return inner ? splitFlowList(inner) : []; |
| 71 | + } |
| 72 | + return v; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * `{ meta, body, errors }`, or null when the text has no frontmatter block. |
| 77 | + * `errors` are "line N: ..." strings; the caller prefixes the file. |
| 78 | + */ |
| 79 | +export function parseFrontmatter(text) { |
| 80 | + // A byte-order mark is invisible in an editor; ignore it. |
| 81 | + const match = String(text) |
| 82 | + .replace(/^/, "") |
| 83 | + .match(/^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n([\s\S]*))?$/); |
| 84 | + if (!match) return null; |
| 85 | + const meta = {}; |
| 86 | + const errors = []; |
| 87 | + const lines = match[1].split(/\r?\n/); |
| 88 | + let listKey = null; |
| 89 | + lines.forEach((line, i) => { |
| 90 | + const lineNo = i + 2; // line 1 is the opening --- |
| 91 | + if (!line.trim() || line.trim().startsWith("#")) return; |
| 92 | + const item = line.match(/^\s+-\s+(.*)$/); |
| 93 | + if (item) { |
| 94 | + if (listKey === null) errors.push(`line ${lineNo}: list item without a key above it`); |
| 95 | + else meta[listKey].push(parseScalar(item[1])); |
| 96 | + return; |
| 97 | + } |
| 98 | + listKey = null; |
| 99 | + const idx = line.indexOf(":"); |
| 100 | + if (idx === -1 || /^\s/.test(line)) { |
| 101 | + errors.push(`line ${lineNo}: "${line.trim()}" is not a key: value line`); |
| 102 | + return; |
| 103 | + } |
| 104 | + const key = line.slice(0, idx).trim(); |
| 105 | + if (Object.prototype.hasOwnProperty.call(meta, key)) { |
| 106 | + errors.push(`line ${lineNo}: key "${key}" is given twice`); |
| 107 | + return; |
| 108 | + } |
| 109 | + const value = parseScalar(line.slice(idx + 1)); |
| 110 | + // A block list may start after blank or comment lines under its key. |
| 111 | + let j = i + 1; |
| 112 | + while (j < lines.length && (!lines[j].trim() || lines[j].trim().startsWith("#"))) j += 1; |
| 113 | + const next = lines[j]; |
| 114 | + if (value === null && next !== undefined && /^\s+-\s/.test(next)) { |
| 115 | + meta[key] = []; |
| 116 | + listKey = key; |
| 117 | + return; |
| 118 | + } |
| 119 | + meta[key] = value; |
| 120 | + }); |
| 121 | + return { meta, body: match[2] || "", errors }; |
| 122 | +} |
| 123 | + |
| 124 | +/** The `## Evidence` section of an entry body, up to the next `## ` heading, or null. */ |
| 125 | +export function evidenceSection(body) { |
| 126 | + const idx = body.search(/^## Evidence\s*$/m); |
| 127 | + if (idx === -1) return null; |
| 128 | + const rest = body.slice(idx); |
| 129 | + const next = rest.search(/\n## (?!Evidence\s*$)/m); |
| 130 | + return next === -1 ? rest : rest.slice(0, next); |
| 131 | +} |
| 132 | + |
| 133 | +const ENTITIES = { "&": "&", "<": "<", ">": ">", """: '"', "'": "'" }; |
| 134 | + |
| 135 | +/** |
| 136 | + * Plain text of a line of entry markdown. It goes through the same inline |
| 137 | + * renderer as the entry page and drops the tags, so it strips exactly the |
| 138 | + * markup the page renders: underscores and `#` stay literal (Rig_Medium, C#). |
| 139 | + */ |
| 140 | +export function plainText(markdown) { |
| 141 | + const html = renderInline(markdown, (href) => ({ href, external: true })); |
| 142 | + return html |
| 143 | + .replace(/<[^>]+>/g, "") |
| 144 | + .replace(/&(amp|lt|gt|quot|#39);/g, (m) => ENTITIES[m]) |
| 145 | + .replace(/\s+/g, " ") |
| 146 | + .trim(); |
| 147 | +} |
| 148 | + |
| 149 | +/** The first paragraph of an entry body as plain text, at most 220 characters. */ |
| 150 | +export function summaryFromBody(body) { |
| 151 | + const first = |
| 152 | + String(body) |
| 153 | + .split(/\n\s*\n/) |
| 154 | + .map((p) => p.trim()) |
| 155 | + .find((p) => p && !p.startsWith("#")) || ""; |
| 156 | + const clean = plainText(first); |
| 157 | + return clean.length > 220 ? `${clean.slice(0, 217)}…` : clean; |
| 158 | +} |
0 commit comments