Templater icon indicating copy to clipboard operation
Templater copied to clipboard

Is there a way to have a hotkey triggered template exclude the frontmatter in the template note?

Open ReaderGuy42 opened this issue 3 years ago • 3 comments

I'm looking for a way to have the template be input through a hotkey, but not include the frontmatter.

Specifically, I'm using a dataview type thing that counts pages etc. but the template is showing up in the list. However, when I try to use the method to exclude the template from the list, by giving it the yaml key "status" with the value "exclude", this frontmatter then also shows up when the template is triggered/input.

(Note: with the dataview thing I can also use tags to exclude specific documents. However, the issue I have with that is that the tags in the frontmatter show up in preview mode which carries over when I turn them into pdf files or word files.)

Is there a way around this? Thank you!

ReaderGuy42 avatar Dec 21 '21 11:12 ReaderGuy42

You could add a command that removes the frontmatter after the template is inserted.

At the bottom of the template note do something like:

<%*
const file = tp.file.find_tfile(tp.file.title)
const content = tp.file.content.split("\n")
let inFrontmatter = false
const newContent = content.reduce((acc, line) => {
    if (!inFrontmatter && line === "---") {
        inFrontmatter = true
        return acc
    }
    if (inFrontmatter) {
        if (line === "---")
        {
            InFrontmatter = false
        }
        return acc
    }
    acc.concat(line)
    return acc
},"")
await app.vault.modify(file, newContent)
%>

shabegom avatar Jan 13 '22 03:01 shabegom

@shabegom Hmm strange, this solution doesn't work anymore.

<%* const file = tp.file.find_tfile(tp.file.title) const content = tp.file.content.split("\n") let inFrontmatter = false const newContent = content.reduce((acc, line) => { if (!inFrontmatter && line === "---") { inFrontmatter = true return acc } if (inFrontmatter) { if (line === "---") { InFrontmatter = false } return acc } acc.concat(line) return acc },"") await app.vault.modify(file, newContent) %>

ryee-dev avatar Mar 22 '22 23:03 ryee-dev

Hmm strange, this solution doesn't work anymore.

How does it fail? what error appears in the console?

shabegom avatar Mar 23 '22 14:03 shabegom