Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%*
- // ============================================================
- // TASK COMPLETION TRIGGER — Listener
- // by sqeptyk
- // ============================================================
- //
- // SETUP:
- // 1. Place this file anywhere in your vault.
- // 2. In Templater settings, add this file to "Startup Templates".
- // 3. Edit WATCH_FOLDER to the folder containing your task notes.
- // 4. Edit TASK_TAG_MAP to map your tags to your templates.
- //
- // TAG FORMAT: Tasks must use #yourtag somewhere in the task line.
- // Example: - [ ] Take Loki for a walk #mornwalk
- //
- // TEMPLATE PATH: Path relative to vault root, no .md extension.
- // Example: "Templates/MyTemplate"
- //
- // To add new tag → template mappings without editing this file,
- // use the companion add-task-trigger.md template.
- // ============================================================
- // -- CONFIGURATION -------------------------------------------
- // Folder to watch for task completions (vault-root relative)
- const WATCH_FOLDER = "TASKS";
- // Map of tags to templates
- // Add or remove entries as needed
- const TASK_TAG_MAP = {
- // "#yourtag": { template: "Templates/YourTemplate" },
- // "#anothertag": { template: "Templates/AnotherTemplate" },
- };
- // ------------------------------------------------------------
- const fileCache = new Map();
- const watchPrefix = WATCH_FOLDER + "/";
- const plugin = tp.app.plugins.getPlugin("templater-obsidian");
- function getCompletedTag(oldContent, newContent) {
- const oldSet = new Set(oldContent.split("\n"));
- const newLines = newContent.split("\n");
- const tags = Object.keys(TASK_TAG_MAP);
- const pattern = new RegExp("^- \[x\]");
- for (let i = 0; i < newLines.length; i++) {
- const line = newLines[i];
- if (pattern.test(line) && !oldSet.has(line)) {
- for (let j = 0; j < tags.length; j++) {
- if (line.indexOf(tags[j]) !== -1) return tags[j];
- }
- }
- }
- return null;
- }
- let processingTask = false;
- (async function init() {
- const allFiles = tp.app.vault.getFiles();
- for (let i = 0; i < allFiles.length; i++) {
- if (allFiles[i].path.indexOf(watchPrefix) === 0) {
- const c = await tp.app.vault.read(allFiles[i]);
- fileCache.set(allFiles[i].path, c);
- }
- }
- plugin.registerEvent(
- tp.app.vault.on("modify", async function(file) {
- if (file.path.indexOf(watchPrefix) !== 0) return;
- if (processingTask) return;
- const newContent = await tp.app.vault.read(file);
- const oldContent = fileCache.get(file.path) || "";
- fileCache.set(file.path, newContent);
- const tag = getCompletedTag(oldContent, newContent);
- if (!tag) return;
- const config = TASK_TAG_MAP[tag];
- processingTask = true;
- try {
- const templateFile = tp.app.vault.getAbstractFileByPath(config.template + ".md");
- if (!templateFile) {
- new Notice("Task Trigger: Template not found: " + config.template);
- return;
- }
- await plugin.templater.create_new_note_from_template(templateFile, undefined, undefined, false);
- } catch (e) {
- console.error("Task Trigger error:", e);
- new Notice("Task Trigger: Failed - check console.");
- } finally {
- processingTask = false;
- }
- })
- );
- })();
- %>
Advertisement
Add Comment
Please, Sign In to add comment