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.
<%*
// 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);
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment