create_from_template
Generate new notes in Obsidian by applying templates with customizable variables for consistent formatting and structure.
Instructions
Create a new note from an existing template note
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| templatePath | Yes | Path to the template note | |
| newPath | Yes | Path for the new note | |
| variables | No | Variables to replace in template (e.g., {{title}} -> 'My Note') |
Implementation Reference
- src/index.ts:800-837 (handler)The main handler function that reads a template note, replaces variables like {{title}} and built-in {{date}}, {{time}}, {{datetime}}, ensures directories, and writes the new note.async function handleCreateFromTemplate(args: { templatePath: string; newPath: string; variables?: Record<string, string>; }): Promise<string> { const templateFullPath = resolvePath(args.templatePath); const newFullPath = resolvePath(args.newPath); if (!(await fileExists(templateFullPath))) { throw new Error(`Template not found at ${args.templatePath}`); } if (await fileExists(newFullPath)) { throw new Error(`Note already exists at ${args.newPath}`); } let content = await fs.readFile(templateFullPath, "utf-8"); // Replace variables if (args.variables) { for (const [key, value] of Object.entries(args.variables)) { const pattern = new RegExp(`\\{\\{${key}\\}\\}`, "g"); content = content.replace(pattern, value); } } // Replace built-in variables const now = new Date(); content = content .replace(/\{\{date\}\}/g, now.toISOString().split("T")[0]) .replace(/\{\{time\}\}/g, now.toTimeString().split(" ")[0]) .replace(/\{\{datetime\}\}/g, now.toISOString()); await ensureDir(newFullPath); await fs.writeFile(newFullPath, content, "utf-8"); return `Successfully created note at ${args.newPath} from template ${args.templatePath}`; }
- src/index.ts:319-341 (schema)The tool schema definition including input schema with properties for templatePath, newPath, and optional variables object.{ name: "create_from_template", description: "Create a new note from an existing template note", inputSchema: { type: "object", properties: { templatePath: { type: "string", description: "Path to the template note", }, newPath: { type: "string", description: "Path for the new note", }, variables: { type: "object", description: "Variables to replace in template (e.g., {{title}} -> 'My Note')", }, }, required: ["templatePath", "newPath"], }, },
- src/index.ts:935-943 (registration)The switch case in the CallToolRequestSchema handler that dispatches to the handleCreateFromTemplate function.case "create_from_template": result = await handleCreateFromTemplate( args as { templatePath: string; newPath: string; variables?: Record<string, string>; } ); break;