fix: resolve REG-04 — frontmatter inline array parser now respects quoted commas (#1695)
Fixes #1694 The inline array parser used .split(',') which ignored quote boundaries, splitting "a, b" into two items. Replaced with a quote-aware splitter that tracks single/double quote state. Updated REG-04 test to assert correct behavior and added coverage for single-quoted and mixed-quote inline arrays.
This commit is contained in:
@@ -8,6 +8,38 @@ const { safeReadFile, normalizeMd, output, error } = require('./core.cjs');
|
||||
|
||||
// ─── Parsing engine ───────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Split a YAML inline array body on commas, respecting quoted strings.
|
||||
* e.g. '"a, b", c' → ['a, b', 'c']
|
||||
*/
|
||||
function splitInlineArray(body) {
|
||||
const items = [];
|
||||
let current = '';
|
||||
let inQuote = null; // null | '"' | "'"
|
||||
|
||||
for (let i = 0; i < body.length; i++) {
|
||||
const ch = body[i];
|
||||
if (inQuote) {
|
||||
if (ch === inQuote) {
|
||||
inQuote = null;
|
||||
} else {
|
||||
current += ch;
|
||||
}
|
||||
} else if (ch === '"' || ch === "'") {
|
||||
inQuote = ch;
|
||||
} else if (ch === ',') {
|
||||
const trimmed = current.trim();
|
||||
if (trimmed) items.push(trimmed);
|
||||
current = '';
|
||||
} else {
|
||||
current += ch;
|
||||
}
|
||||
}
|
||||
const trimmed = current.trim();
|
||||
if (trimmed) items.push(trimmed);
|
||||
return items;
|
||||
}
|
||||
|
||||
function extractFrontmatter(content) {
|
||||
const frontmatter = {};
|
||||
// Find ALL frontmatter blocks at the start of the file.
|
||||
@@ -53,8 +85,8 @@ function extractFrontmatter(content) {
|
||||
// Push new context for potential nested content
|
||||
stack.push({ obj: current.obj[key], key: null, indent });
|
||||
} else if (value.startsWith('[') && value.endsWith(']')) {
|
||||
// Inline array: key: [a, b, c]
|
||||
current.obj[key] = value.slice(1, -1).split(',').map(s => s.trim().replace(/^["']|["']$/g, '')).filter(Boolean);
|
||||
// Inline array: key: [a, b, c] — quote-aware split (REG-04 fix)
|
||||
current.obj[key] = splitInlineArray(value.slice(1, -1));
|
||||
current.key = null;
|
||||
} else {
|
||||
// Simple key: value
|
||||
|
||||
Reference in New Issue
Block a user