update_frontmatter
Modify or add YAML frontmatter properties in Obsidian notes to organize metadata, update tags, or manage document attributes.
Instructions
Update or add frontmatter (YAML) properties in a note
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Path to the note | |
| properties | Yes | Key-value pairs to set in frontmatter. Use null to delete a property. |
Implementation Reference
- src/index.ts:772-798 (handler)The handler function that reads the note, parses its frontmatter, merges/updates the provided properties (deleting if null), serializes new frontmatter, and writes back to the file.async function handleUpdateFrontmatter(args: { path: string; properties: Record<string, unknown>; }): Promise<string> { const fullPath = resolvePath(args.path); if (!(await fileExists(fullPath))) { throw new Error(`Note not found at ${args.path}`); } const content = await fs.readFile(fullPath, "utf-8"); const { frontmatter, body } = parseFrontmatter(content); const newFrontmatter = { ...(frontmatter || {}), ...args.properties }; // Remove null values for (const [key, value] of Object.entries(newFrontmatter)) { if (value === null) { delete newFrontmatter[key]; } } const newContent = serializeFrontmatter(newFrontmatter) + body; await fs.writeFile(fullPath, newContent, "utf-8"); return `Successfully updated frontmatter in ${args.path}`; }
- src/index.ts:303-317 (schema)Input schema defining the parameters for the update_frontmatter tool: path to the note and properties object to update/add/delete.inputSchema: { type: "object", properties: { path: { type: "string", description: "Path to the note", }, properties: { type: "object", description: "Key-value pairs to set in frontmatter. Use null to delete a property.", }, }, required: ["path", "properties"], },
- src/index.ts:930-934 (registration)Registration in the tool dispatch switch statement that calls the handler based on tool name.case "update_frontmatter": result = await handleUpdateFrontmatter( args as { path: string; properties: Record<string, unknown> } ); break;
- src/index.ts:300-318 (registration)Tool registration in the tools array used for listing available tools.{ name: "update_frontmatter", description: "Update or add frontmatter (YAML) properties in a note", inputSchema: { type: "object", properties: { path: { type: "string", description: "Path to the note", }, properties: { type: "object", description: "Key-value pairs to set in frontmatter. Use null to delete a property.", }, }, required: ["path", "properties"], }, },
- src/index.ts:381-419 (helper)Helper function to parse frontmatter YAML from note content into object and extract body.function parseFrontmatter(content: string): { frontmatter: Record<string, unknown> | null; body: string; raw: string; } { const match = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/); if (match) { try { // Simple YAML parsing for common cases const yaml: Record<string, unknown> = {}; const lines = match[1].split("\n"); for (const line of lines) { const colonIndex = line.indexOf(":"); if (colonIndex > 0) { const key = line.slice(0, colonIndex).trim(); let value: unknown = line.slice(colonIndex + 1).trim(); // Handle arrays if (value === "") { value = []; } else if ( typeof value === "string" && value.startsWith("[") && value.endsWith("]") ) { value = value .slice(1, -1) .split(",") .map((v) => v.trim()); } yaml[key] = value; } } return { frontmatter: yaml, body: match[2], raw: match[1] }; } catch { return { frontmatter: null, body: content, raw: "" }; } } return { frontmatter: null, body: content, raw: "" }; }
- src/index.ts:421-432 (helper)Helper function to serialize a frontmatter object back to YAML string format.function serializeFrontmatter(obj: Record<string, unknown>): string { const lines: string[] = []; for (const [key, value] of Object.entries(obj)) { if (value === null || value === undefined) continue; if (Array.isArray(value)) { lines.push(`${key}: [${value.join(", ")}]`); } else { lines.push(`${key}: ${value}`); } } return `---\n${lines.join("\n")}\n---\n`; }