fix: normalise YAML block scalars in LLM-generated frontmatter - #561
fix: normalise YAML block scalars in LLM-generated frontmatter#561AndrewDongminYoo wants to merge 3 commits into
Conversation
chuenchen309
left a comment
There was a problem hiding this comment.
This is clean — nice that it round-trips through js-yaml with a fast-exit guard instead of regex-munging the values. One correctness issue I think is worth fixing before merge:
The + 4 offsets in the reconstruction assume the opening fence is exactly ---\n, but fmRe matches more than that. ^---\s*\r?\n also matches ---\r\n (CRLF) and --- \n (trailing space), where the opening delimiter is 5+ chars. In those cases:
content.slice(0, m.index! + 4) + newPayload + content.slice(m.index! + 4 + payload.length)slices mid-delimiter — for CRLF input the prefix becomes ---\r (the \n is dropped) and the suffix start is off by one, so the last payload character gets duplicated. Net result is corrupted frontmatter on any Windows-line-ending or trailing-whitespace source. Repro is the existing folded-scalar test with \r\n line endings.
Since the exact fence text is already in the match, deriving its length (or capturing the opening/closing fences as regex groups) avoids the hardcoded 4. A CRLF case in the test suite would lock it in — the current tests all use \n so this slips through.
Minor / by design: collapsing |- literal scalars to spaces (per the test) is lossy for fields where line breaks are meaningful. Fine for description; just flagging in case any field is meant to preserve them.
c042965 to
50699b2
Compare
50699b2 to
52b5633
Compare
|
Fixed — the reconstruction now captures and reuses the exact opening and closing fence text instead of assuming I also added a regression test covering CRLF line endings and whitespace-padded fences, which verifies the frontmatter boundary and body remain intact. |
|
Looks great — reusing |
|
Circling back on the second point, which I left unanswered earlier — the
If a genuinely multi-line field is ever added, this normalizer would need a per-key exemption — that's the right moment to add one, not now. |
What
Normalize top-level YAML block scalars in LLM-generated frontmatter without reserializing unrelated YAML.
Why
LLMs sometimes emit folded or literal block scalars that downstream rendering does not support. Parsing and dumping the entire frontmatter fixed the scalar but also removed comments and rewrote quoting, arrays, line endings, and other author formatting.
How
Validation
npm run test:mocks -- --run src/lib/ingest-sanitize.test.ts(28 passed)npm run typechecknpm run test:mocks(118 files, 1743 tests passed)npm run build