fix: normalise YAML block scalars in LLM-generated frontmatter#561
fix: normalise YAML block scalars in LLM-generated frontmatter#561AndrewDongminYoo wants to merge 1 commit 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
Add a
normalizeBlockScalarsInFrontmatterstep tosanitizeIngestedFileContentso LLM-generated frontmatter never carries YAML block scalars the renderer can't handle.Why
LLMs sometimes emit
>-(folded) or|-(literal) block scalars in frontmatter. LLM Wiki's renderer doesn't support them, so those pages render incorrectly.How
lineWidth: -1so js-yaml never reintroduces block scalars.Testing
ingest-sanitize.test.ts— 22 pass / 0 fail (7 new cases covering folded/literal scalars, clean-page fast-exit, and composition with existing repairs).tsc --buildclean.