* Parses YAML frontmatter string into a QuartoFrontmatter object. * Uses the yaml package to properly handle nested objects, arrays, and all YAML features.
(yamlString: string)
| 142 | * Uses the yaml package to properly handle nested objects, arrays, and all YAML features. |
| 143 | */ |
| 144 | function parseYamlFrontmatter(yamlString: string): QuartoFrontmatter { |
| 145 | // Handle empty input |
| 146 | if (!yamlString || yamlString.trim() === '') { |
| 147 | return {} |
| 148 | } |
| 149 | |
| 150 | try { |
| 151 | const parsed = parseYaml(yamlString) |
| 152 | |
| 153 | // If parsing returns null or undefined, return empty object |
| 154 | if (parsed === null || parsed === undefined) { |
| 155 | return {} |
| 156 | } |
| 157 | |
| 158 | // Ensure the result is an object (not a primitive or array) |
| 159 | if (typeof parsed !== 'object' || Array.isArray(parsed)) { |
| 160 | return {} |
| 161 | } |
| 162 | |
| 163 | return parsed as QuartoFrontmatter |
| 164 | } catch { |
| 165 | // If YAML parsing fails, return empty object |
| 166 | // In production, you might want to log this error |
| 167 | return {} |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Parses Quarto cell options from #| lines. |
no test coverage detected