Skip to content

Commit 0ec7dfa

Browse files
committed
fix(skills): preserve strict frontmatter recovery
1 parent 4dbf59f commit 0ec7dfa

2 files changed

Lines changed: 38 additions & 20 deletions

File tree

‎packages/markdown-core/src/frontmatter.test.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,23 @@ summary: handles formats: pdf and docx
122122
const result = parseFrontmatterBlockResult(`---
123123
name: sample-skill
124124
read_when: signals: user announcing a conversion task
125-
metadata: [broken
125+
metadata: owner: value
126126
---`);
127127

128-
expect(result.issues[0]).toMatchObject({ code: "BAD_INDENT" });
128+
expect(result.issues[0]).toMatchObject({ code: "BLOCK_AS_IMPLICIT_KEY" });
129+
});
130+
131+
it("preserves valid structured siblings when pure-text fields are normalized", () => {
132+
const result = parseFrontmatterBlockResult(`---
133+
name: sample-skill
134+
read_when: signals: user announcing a conversion task
135+
metadata:
136+
openclaw:
137+
emoji: beaker
138+
---`);
139+
140+
expect(result.frontmatter.metadata).toBe('{"openclaw":{"emoji":"beaker"}}');
141+
expect(result.issues).toEqual([]);
129142
});
130143

131144
it("does not rewrite valid sibling values when recovering a malformed field", () => {

‎packages/markdown-core/src/frontmatter.ts‎

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Markdown Core module implements frontmatter behavior.
22
import { isRecord } from "@openclaw/normalization-core/record-coerce";
3-
import { isMap, isNode, parseDocument } from "yaml";
3+
import { isAlias, isMap, isNode, isScalar, parseDocument } from "yaml";
44

55
type ParsedFrontmatter = Record<string, string>;
66

@@ -80,40 +80,45 @@ function parseLineFrontmatter(block: string): ParsedFrontmatter {
8080
return result;
8181
}
8282

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`.
8683
const FREEFORM_TEXT_FIELDS = new Set(["description", "read_when", "summary"]);
8784

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.
9285
function normalizeFreeformFieldAtError(block: string): string {
9386
const doc = parseDocument(block, { schema: "core", prettyErrors: false });
94-
if (doc.errors.length === 0 || !isMap(doc.contents)) {
87+
if (!isMap(doc.contents)) {
9588
return block;
9689
}
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),
9995
);
100-
const pos = error?.pos?.[0];
96+
const pos =
97+
error?.pos?.[0] ??
98+
(isNode(descriptionAlias?.key) ? descriptionAlias.key.range?.[0] : undefined);
10199
if (pos === undefined) {
102100
return block;
103101
}
104102
const lineStart = block.lastIndexOf("\n", pos) + 1;
105103
const lineEnd = block.indexOf("\n", pos);
106104
const end = lineEnd === -1 ? block.length : lineEnd;
107105
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.
110106
const match = line.match(/^(?:([\w-]+)|"([\w-]+)"|'([\w-]+)'):\s*(.*)$/);
111107
const keyName = match?.[1] ?? match?.[2] ?? match?.[3];
112-
if (!keyName || !FREEFORM_TEXT_FIELDS.has(keyName)) {
113-
return block;
114-
}
115108
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+
) {
117122
return block;
118123
}
119124
const replacement = `${keyName}: ${JSON.stringify(stripQuotes(rawValue))}`;

0 commit comments

Comments
 (0)