<%*
/*
*/
const view = app.workspace.activeLeaf.view;
const editor = view.editor;
const curLineNum = editor.getCursor().line;
const curLineText = editor.getLine(curLineNum);
const title = tp.file.title;
const today = title.match(/\d{4}\-\d{2}\-\d{2} .+/) //are we on the DNP?
? null //if on the DNP, today is set to null
: moment(Date.now()).format("YYYY-MM-DD dddd"); //set today
let newLineText = curLineText.replace(
/!?\[\[([^\]\|]*\/)?([^\]\|\/]+)\|?([^\]]*)?\]\]/,
(match,p1,p2,p3) => `${today?"###":"##"} ${p3??p2}\n![[${(p1??"")+p2}#${(today?today+" ":"") + title}]]`
);
editor.setLine(curLineNum, newLineText);
let fname = newLineText.match(/!\[\[(.*?)#.*?]]/)[1];
let file = app.metadataCache.getFirstLinkpathDest(fname,view.file.path);
if(!file) {
if(!fname.endsWith(".md")) fname=fname+".md";
file = await app.vault.create(fname,"# Notes\n");
}
const data = await app.vault.read(file);
const parts = data.split(/# Notes(?:\n|\r\n|\r)/);
newLineText = `## ${today?"[["+today+"]], ":""}[[${title}]]`;
if(parts.length === 2) {
await app.vault.modify(file,parts[0]+"# Notes\n"+newLineText+"\n\n"+parts[1]);
} else {
await app.vault.modify(file,data+"# Notes\n"+newLineText+"\n\n");
}
await app.workspace.openLinkText(fname, view.file.path);
let i=0;
const lineCount = editor.lineCount();
while(editor.getLine(i)!==newLineText && i<lineCount) i++;
editor.setCursor(i+1);
%>
This is all very interesting, and I can't wait for editable transclusions. However, I think meeting-node.md might have a bug; when running the template from my daily note, the Templater plugin sometimes uses the existing
# Notes
section in the linked topic page and inserts the new subsection at the top (as expected), but it sometimes creates a redundant# Notes
section at the bottom of the page regardless. I haven't figured out why yet, if it is cursor position or spacing or what.Edit: looking further, the cause is the line
const parts = data.split("# Notes\n")
not detecting the existing string for some reason, so the subsequent if statement evaluates to false. I haven't found a deterministic way to replicate it but it sometimes detects it and sometimes doesn't when I move around spacing and the cursor. The file is 3300 words long and has about 16 subheadings already below the # Notes, and I can't replicate this in a much shorter test Markdown file with an existing# Notes
and only a few other test headings in the way, so possibly it has to do with how long files are read in.