Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save GitMurf/fa9043565ca83ee94cf1783008ad7be5 to your computer and use it in GitHub Desktop.
Save GitMurf/fa9043565ca83ee94cf1783008ad7be5 to your computer and use it in GitHub Desktop.

Revisions

  1. GitMurf created this gist Jul 31, 2022.
    53 changes: 53 additions & 0 deletions obsidian.templater.help.anthony-baker.meeting-template.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    <%*
    // v0.1

    /* START OF USER DEFINED SETTINGS */
    // template to use
    const templateFolder = "templates";
    const templateName = "template_anthony.md";
    // creating the new file
    const newFileFolder = "test/meetings";
    const openNewFile = false;
    const replaceWithLink = true;
    const embedLink = false;
    // other settings
    const newFileNamePrefix = `${tp.date.now('YYYY-MM-DD')} `;
    /* END OF USER DEFINED SETTINGS */

    // replace trailing and leading slashes
    const cleanTemplateFolder = templateFolder.replace(/^\//, "").replace(/\/$/, "");
    const templateLocation = `${cleanTemplateFolder}/${templateName}`;
    const foundFile = app.vault.getAbstractFileByPath(templateLocation);
    if(!foundFile) {
    new Notice(`Could not find the Template: ${templateLocation}`, 5000);
    return;
    }

    // replace trailing and leading slashes
    const cleanNewFolder = newFileFolder.replace(/^\//, "").replace(/\/$/, "");
    const foundFolder = app.vault.getAbstractFileByPath(cleanNewFolder);
    if(!foundFolder) {
    new Notice(`Could not find the Folder: ${cleanNewFolder}`, 5000);
    return;
    }

    const cmEditorAct = app.workspace.activeLeaf.view.editor;
    // if nothing is selected then select the current active line and use it
    if(tp.file.selection() === "") {
    const curLine = cmEditorAct.getCursor().line;
    cmEditorAct.setSelection({ line: curLine, ch: 0 }, { line: curLine, ch: cmEditorAct.getLine(curLine).length });
    }
    let selectedContent = newFileNamePrefix + tp.file.selection();

    let newFileName = await tp.system.prompt("Name of new note", selectedContent);
    newFileName = newFileName.replace(/[\.#\*""\/\\<>\:\|\[\]\?]/gim, '').slice(0, 255);

    if(replaceWithLink) {
    tR = `[` + `[${newFileName}]]`;
    if(embedLink) { tR = `!${tR}` }
    } else {
    tR = selectedContent;
    }

    tp.file.create_new(foundFile, newFileName, openNewFile, foundFolder);
    %>