Files
mindnet_obsidian/src/interview/writeFrontmatter.ts
T
Lars bab84549e2
Node.js build / build (20.x) (push) Waiting to run
Node.js build / build (22.x) (push) Waiting to run
Enhance interview functionality and settings; add YAML dependency
- Introduced profile selection modal for creating notes from interview profiles.
- Added settings for interview configuration path, auto-starting interviews, and intercepting unresolved link clicks.
- Updated package files to include YAML dependency for configuration handling.
- Enhanced CSS for profile selection and interview wizard UI elements.
2026-01-16 12:27:44 +01:00

94 lines
2.3 KiB
TypeScript

import type { InterviewProfile } from "./types";
export interface FrontmatterOptions {
id: string;
title: string;
noteType: string;
interviewProfile: string;
defaults?: Record<string, unknown>;
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 '""';
}
}