|
1 | 1 | // Markdown Core module implements frontmatter behavior. |
2 | 2 | import { isRecord } from "@openclaw/normalization-core/record-coerce"; |
3 | | -import { isMap, isNode, isScalar, parseDocument } from "yaml"; |
| 3 | +import { isAlias, isMap, isNode, isScalar, parseDocument } from "yaml"; |
4 | 4 |
|
5 | 5 | type ParsedFrontmatter = Record<string, string>; |
6 | 6 |
|
@@ -80,28 +80,48 @@ function parseLineFrontmatter(block: string): ParsedFrontmatter { |
80 | 80 | return result; |
81 | 81 | } |
82 | 82 |
|
83 | | -function normalizeFreeformDescription(block: string): string { |
| 83 | +const FREEFORM_TEXT_FIELDS = new Set(["description", "read_when", "summary"]); |
| 84 | + |
| 85 | +function normalizeFreeformFieldAtError(block: string): string { |
84 | 86 | const doc = parseDocument(block, { schema: "core", prettyErrors: false }); |
85 | 87 | if (!isMap(doc.contents)) { |
86 | 88 | return block; |
87 | 89 | } |
88 | | - const descriptionPair = doc.contents.items.find( |
89 | | - (pair) => isScalar(pair.key) && pair.key.value === "description", |
| 90 | + const error = doc.errors.find((candidate) => candidate.pos?.[0] !== undefined); |
| 91 | + // Aliases fail during toJS without adding a document error position. |
| 92 | + const descriptionAlias = doc.contents.items.find( |
| 93 | + (candidate) => |
| 94 | + isScalar(candidate.key) && candidate.key.value === "description" && isAlias(candidate.value), |
90 | 95 | ); |
91 | | - const keyStart = isNode(descriptionPair?.key) ? descriptionPair.key.range?.[0] : undefined; |
92 | | - if (keyStart === undefined) { |
| 96 | + const pos = |
| 97 | + error?.pos?.[0] ?? |
| 98 | + (isNode(descriptionAlias?.key) ? descriptionAlias.key.range?.[0] : undefined); |
| 99 | + if (pos === undefined) { |
93 | 100 | return block; |
94 | 101 | } |
95 | | - const lineStart = block.lastIndexOf("\n", keyStart - 1) + 1; |
96 | | - const lineEnd = block.indexOf("\n", keyStart); |
| 102 | + const lineStart = block.lastIndexOf("\n", pos) + 1; |
| 103 | + const lineEnd = block.indexOf("\n", pos); |
97 | 104 | const end = lineEnd === -1 ? block.length : lineEnd; |
98 | 105 | const line = block.slice(lineStart, end); |
99 | | - const match = line.match(/^(?:description|"description"|'description'):\s*(.*)$/); |
100 | | - const rawValue = match?.[1]?.trim(); |
101 | | - if (!rawValue || /^[|>](?:[1-9][+-]?|[+-][1-9]?)?$/.test(rawValue)) { |
| 106 | + const match = line.match(/^(?:([\w-]+)|"([\w-]+)"|'([\w-]+)'):\s*(.*)$/); |
| 107 | + const keyName = match?.[1] ?? match?.[2] ?? match?.[3]; |
| 108 | + const rawValue = match?.[4]?.trim(); |
| 109 | + const isTopLevelField = doc.contents.items.some( |
| 110 | + (pair) => isScalar(pair.key) && pair.key.value === keyName && pair.key.range?.[0] === lineStart, |
| 111 | + ); |
| 112 | + // Keep shipped description recovery; other text fields only recover colon-rich parser errors. |
| 113 | + const recoverColonRichText = error?.code === "BLOCK_AS_IMPLICIT_KEY" && rawValue?.includes(": "); |
| 114 | + if ( |
| 115 | + !keyName || |
| 116 | + !rawValue || |
| 117 | + !FREEFORM_TEXT_FIELDS.has(keyName) || |
| 118 | + !isTopLevelField || |
| 119 | + (keyName !== "description" && !recoverColonRichText) || |
| 120 | + /^[|>](?:[1-9][+-]?|[+-][1-9]?)?$/.test(rawValue) |
| 121 | + ) { |
102 | 122 | return block; |
103 | 123 | } |
104 | | - const replacement = `description: ${JSON.stringify(stripQuotes(rawValue))}`; |
| 124 | + const replacement = `${keyName}: ${JSON.stringify(stripQuotes(rawValue))}`; |
105 | 125 | return `${block.slice(0, lineStart)}${replacement}${block.slice(end)}`; |
106 | 126 | } |
107 | 127 |
|
@@ -184,7 +204,20 @@ function parseYamlFrontmatter(block: string): ParsedFrontmatterBlockResult { |
184 | 204 | if (parsed.issues.length === 0) { |
185 | 205 | return parsed; |
186 | 206 | } |
187 | | - const recoveredBlock = normalizeFreeformDescription(block); |
| 207 | + // Recover one error-located field per iteration, retrying parse each time, |
| 208 | + // so multiple colon-rich fields are fixed without rewriting valid siblings. |
| 209 | + let recoveredBlock = block; |
| 210 | + for (let i = 0; i < FREEFORM_TEXT_FIELDS.size; i += 1) { |
| 211 | + const next = normalizeFreeformFieldAtError(recoveredBlock); |
| 212 | + if (next === recoveredBlock) { |
| 213 | + break; |
| 214 | + } |
| 215 | + recoveredBlock = next; |
| 216 | + const reparsed = parseYamlFrontmatterOnce(recoveredBlock, fallback); |
| 217 | + if (reparsed.issues.length === 0) { |
| 218 | + return reparsed; |
| 219 | + } |
| 220 | + } |
188 | 221 | return recoveredBlock === block ? parsed : parseYamlFrontmatterOnce(recoveredBlock, fallback); |
189 | 222 | } |
190 | 223 |
|
|
0 commit comments