* Pre-processes frontmatter text to quote values that contain special YAML characters. * This allows glob patterns like **\/*.{ts,tsx} to be parsed correctly.
(frontmatterText: string)
| 83 | * This allows glob patterns like **\/*.{ts,tsx} to be parsed correctly. |
| 84 | */ |
| 85 | function quoteProblematicValues(frontmatterText: string): string { |
| 86 | const lines = frontmatterText.split('\n') |
| 87 | const result: string[] = [] |
| 88 | |
| 89 | for (const line of lines) { |
| 90 | // Match simple key: value lines (not indented, not list items, not block scalars) |
| 91 | const match = line.match(/^([a-zA-Z_-]+):\s+(.+)$/) |
| 92 | if (match) { |
| 93 | const [, key, value] = match |
| 94 | if (!key || !value) { |
| 95 | result.push(line) |
| 96 | continue |
| 97 | } |
| 98 | |
| 99 | // Skip if already quoted |
| 100 | if ( |
| 101 | (value.startsWith('"') && value.endsWith('"')) || |
| 102 | (value.startsWith("'") && value.endsWith("'")) |
| 103 | ) { |
| 104 | result.push(line) |
| 105 | continue |
| 106 | } |
| 107 | |
| 108 | // Quote if contains special YAML characters |
| 109 | if (YAML_SPECIAL_CHARS.test(value)) { |
| 110 | // Use double quotes and escape any existing double quotes |
| 111 | const escaped = value.replace(/\\/g, '\\\\').replace(/"/g, '\\"') |
| 112 | result.push(`${key}: "${escaped}"`) |
| 113 | continue |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | result.push(line) |
| 118 | } |
| 119 | |
| 120 | return result.join('\n') |
| 121 | } |
| 122 | |
| 123 | export const FRONTMATTER_REGEX = /^---\s*\n([\s\S]*?)---\s*\n?/ |
| 124 |
no test coverage detected