|
1 | 1 | // Markdown Core module implements frontmatter behavior. |
2 | 2 | import { isRecord } from "@openclaw/normalization-core/record-coerce"; |
3 | | -import { isMap, isNode, parseDocument } from "yaml"; |
| 3 | +import { isAlias, isMap, isNode, isScalar, parseDocument } from "yaml"; |
4 | 4 |
|
5 | 5 | type ParsedFrontmatter = Record<string, string>; |
6 | 6 |
|
@@ -80,40 +80,45 @@ function parseLineFrontmatter(block: string): ParsedFrontmatter { |
80 | 80 | return result; |
81 | 81 | } |
82 | 82 |
|
83 | | -// Pure-text scalar fields whose inline values may legitimately contain a |
84 | | -// colon+space. Restoring them keeps legacy single-line frontmatter loadable |
85 | | -// without loosening strict parsing for structured fields like `metadata`. |
86 | 83 | const FREEFORM_TEXT_FIELDS = new Set(["description", "read_when", "summary"]); |
87 | 84 |
|
88 | | -// Recover only the field whose value actually triggers BLOCK_AS_IMPLICIT_KEY, |
89 | | -// located via the YAML error position. Valid sibling scalars (quoted values |
90 | | -// with escape sequences, commented values) are not rewritten, preserving |
91 | | -// their YAML decode semantics. |
92 | 85 | function normalizeFreeformFieldAtError(block: string): string { |
93 | 86 | const doc = parseDocument(block, { schema: "core", prettyErrors: false }); |
94 | | - if (doc.errors.length === 0 || !isMap(doc.contents)) { |
| 87 | + if (!isMap(doc.contents)) { |
95 | 88 | return block; |
96 | 89 | } |
97 | | - const error = doc.errors.find( |
98 | | - (candidate) => candidate.code === "BLOCK_AS_IMPLICIT_KEY" && candidate.pos?.[0] !== undefined, |
| 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), |
99 | 95 | ); |
100 | | - const pos = error?.pos?.[0]; |
| 96 | + const pos = |
| 97 | + error?.pos?.[0] ?? |
| 98 | + (isNode(descriptionAlias?.key) ? descriptionAlias.key.range?.[0] : undefined); |
101 | 99 | if (pos === undefined) { |
102 | 100 | return block; |
103 | 101 | } |
104 | 102 | const lineStart = block.lastIndexOf("\n", pos) + 1; |
105 | 103 | const lineEnd = block.indexOf("\n", pos); |
106 | 104 | const end = lineEnd === -1 ? block.length : lineEnd; |
107 | 105 | const line = block.slice(lineStart, end); |
108 | | - // Match unquoted, double-quoted, and single-quoted freeform keys. Current |
109 | | - // main recovers all three for `description`; preserve that compatibility. |
110 | 106 | const match = line.match(/^(?:([\w-]+)|"([\w-]+)"|'([\w-]+)'):\s*(.*)$/); |
111 | 107 | const keyName = match?.[1] ?? match?.[2] ?? match?.[3]; |
112 | | - if (!keyName || !FREEFORM_TEXT_FIELDS.has(keyName)) { |
113 | | - return block; |
114 | | - } |
115 | 108 | const rawValue = match?.[4]?.trim(); |
116 | | - if (!rawValue || /^[|>](?:[1-9][+-]?|[+-][1-9]?)?$/.test(rawValue) || !rawValue.includes(": ")) { |
| 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 | + ) { |
117 | 122 | return block; |
118 | 123 | } |
119 | 124 | const replacement = `${keyName}: ${JSON.stringify(stripQuotes(rawValue))}`; |
|
0 commit comments