import type { InterviewProfile } from "./types"; export interface FrontmatterOptions { id: string; title: string; noteType: string; interviewProfile: string; defaults?: Record; frontmatterWhitelist: string[]; } /** * Write frontmatter YAML from options. * Only includes defaults fields that are in frontmatterWhitelist. */ export function writeFrontmatter(opts: FrontmatterOptions): string { const { id, title, noteType, interviewProfile, defaults, frontmatterWhitelist } = opts; const lines: string[] = []; lines.push("---"); // Required fields lines.push(`id: ${escapeYamlValue(id)}`); lines.push(`title: ${escapeYamlValue(title)}`); lines.push(`type: ${escapeYamlValue(noteType)}`); lines.push(`interview_profile: ${escapeYamlValue(interviewProfile)}`); // Whitelisted defaults if (defaults) { for (const [key, value] of Object.entries(defaults)) { if (frontmatterWhitelist.includes(key)) { lines.push(`${key}: ${escapeYamlValue(value)}`); } } } lines.push("---"); return lines.join("\n"); } /** * Escape YAML value for frontmatter. * Handles strings, numbers, booleans, null. */ function escapeYamlValue(value: unknown): string { if (value === null || value === undefined) { return "null"; } if (typeof value === "boolean") { return value ? "true" : "false"; } if (typeof value === "number") { return String(value); } if (typeof value === "string") { // If string contains special characters, quote it if ( value.includes(":") || value.includes("#") || value.includes("|") || value.includes(">") || value.includes("<") || value.includes("&") || value.includes("*") || value.includes("!") || value.includes("%") || value.includes("@") || value.includes("`") || value.includes('"') || value.includes("'") || value.includes("\n") || value.trim() !== value || value.startsWith("[") || value.startsWith("{") ) { // Escape quotes and use double quotes const escaped = value.replace(/"/g, '\\"'); return `"${escaped}"`; } return value; } // For objects/arrays, serialize as JSON string (quoted) try { const json = JSON.stringify(value); return `"${json.replace(/"/g, '\\"')}"`; } catch { return '""'; } }