fix: quote YAML-special and numeric frontmatter values
formatValue now detects YAML-special tokens (true/false/null/yes/no/on/off/y/n/~) and numeric-looking strings, quoting them via JSON.stringify so YAML parsers cannot misread them as booleans or numbers. ISO date strings (YYYY-MM-DD) are exempt and remain bare for readability. Adds four covering tests. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -47,6 +47,10 @@ export function serializeFrontmatter(pairs: Pair[]): string {
|
||||
|
||||
function formatValue(v: string): string {
|
||||
if (v === "") return '""';
|
||||
const yamlSpecial = /^(true|false|null|yes|no|on|off|y|n|~)$/i.test(v);
|
||||
const isoDate = /^\d{4}-\d{2}-\d{2}$/.test(v);
|
||||
const numericLike = !isoDate && /^[+-]?(\d|\.\d)/.test(v);
|
||||
if (yamlSpecial || numericLike) return JSON.stringify(v);
|
||||
const bareSafe = /^[A-Za-z0-9_./-][A-Za-z0-9_./ -]*$/.test(v) && !/^\s|\s$/.test(v);
|
||||
return bareSafe ? v : JSON.stringify(v);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user