( app: App, file: TFile, templatePropertyVars: Map<string, unknown>, )
| 186 | * (see coerceYamlValue in utils/yamlValues.ts for implementation details) |
| 187 | */ |
| 188 | export async function postProcessFrontMatter( |
| 189 | app: App, |
| 190 | file: TFile, |
| 191 | templatePropertyVars: Map<string, unknown>, |
| 192 | ): Promise<void> { |
| 193 | // Validate structured variables before processing |
| 194 | const validation = validateStructuredVariables(templatePropertyVars); |
| 195 | |
| 196 | // Log any validation warnings |
| 197 | if (validation.warnings.length > 0) { |
| 198 | for (const warning of validation.warnings) { |
| 199 | log.logWarning(`Structured variable validation warning: ${warning}`); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // If validation found errors, log them and skip post-processing |
| 204 | if (!validation.isValid) { |
| 205 | const errorSummary = validation.errors.join("; "); |
| 206 | log.logError( |
| 207 | `Cannot post-process front matter for file ${file.path} due to validation errors: ${errorSummary}. ` + |
| 208 | `The file was created successfully, but some structured variables may not be properly formatted. ` + |
| 209 | `Please check the variable values and ensure they don't contain circular references, ` + |
| 210 | `exceed nesting depth of ${VALIDATION_LIMITS.MAX_NESTING_DEPTH}, or contain unsupported types (functions, symbols).` |
| 211 | ); |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | try { |
| 216 | log.logMessage(`Post-processing front matter for ${file.path} with ${templatePropertyVars.size} structured variables`); |
| 217 | log.logMessage(`Variable types: ${Array.from(templatePropertyVars.entries()) |
| 218 | .map(([k, v]) => `${k}:${typeof v}`).join(', ')}`); |
| 219 | |
| 220 | await app.fileManager.processFrontMatter(file, (frontmatter) => { |
| 221 | for (const [key, value] of templatePropertyVars) { |
| 222 | const pathSegments = key.includes(TemplatePropertyCollector.PATH_SEPARATOR) |
| 223 | ? key.split(TemplatePropertyCollector.PATH_SEPARATOR) |
| 224 | : [key]; |
| 225 | const coerced = coerceYamlValue(value); |
| 226 | assignFrontmatterValue(frontmatter, pathSegments, coerced); |
| 227 | } |
| 228 | }); |
| 229 | |
| 230 | log.logMessage(`Successfully post-processed front matter for ${file.path}`); |
| 231 | } catch (err) { |
| 232 | // Improved error message with actionable information |
| 233 | log.logError( |
| 234 | `Failed to post-process front matter for file ${file.path}: ${err}. ` + |
| 235 | `The file was created successfully, but structured variables may not be properly formatted. ` + |
| 236 | `This usually happens when variable values contain unexpected types or when Obsidian's YAML processor encounters an issue. ` + |
| 237 | `Check the console for more details about which variables caused the problem.` |
| 238 | ); |
| 239 | // Don't throw - the file was still created successfully |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Key segments that become a prototype-pollution gadget only when a value is |
no test coverage detected