Copy tags from title to front matter template when creating new notes #610
Replies: 3 comments 2 replies
-
You can actually use For the correct name, you could give yourself a prompt with |
Beta Was this translation helpful? Give feedback.
-
Hi @AB1908. I do not think I understood your answer. So let me ask some questions to try clarify: What do you mean by "hoists" the tags to the top ?
Wouldn't you need to enter the title twice then? First in the prompt dialog, and then in the editor title text field? If I insert this:
at the top of the frontmatter template file, and then try create a new note. Then the system prompt dialog will be executed first, then the template will be populated into the editor, then focus is given to the editor title bar where it expects me to type in the title again.. I think I am missing something, because this does not seem like a good solution to me.. |
Beta Was this translation helpful? Give feedback.
-
I think maybe you misunderstood my question? What I would like to do is to get the tags field in the frontmatter filled out based on what I typed in the title. In order to clarify, I created this small snippet: private async insertTemplate(new_file: TFile, app: App) {
if (new_file !== null) {
const view = app.workspace.getActiveViewOfType(MarkdownView);
if (view !== null) {
// Only update blank files
if (view.data === '') {
const templ_file = app.vault.getAbstractFileByPath("templates/frontmatter.md");
if (templ_file instanceof TFile) {
let str = await app.vault.read(templ_file);
const creation_date = window.moment(new_file.stat.ctime).format("YYYY-MM-DD HH:mm");
str = str.replace(/<%\s+tp.file.creation_date\(\s*\)\s+%>/, creation_date);
const last_mod_date = window.moment(new_file.stat.mtime).format("YYYY-MM-DD HH:mm");
str = str.replace(/<%\s+tp.file.last_modified_date\(.*?\)\s+%>/, last_mod_date);
const cache = app.metadataCache;
const all_tags = [...Object.keys(cache.getTags())].map((p) => p.split("#").pop()) as string[];
const title_words = new_file.basename.split(/\s+/).map(x => { return x.toLowerCase();});
const title_tags = title_words.filter(word => {return all_tags.includes(word);});
str = str.replace(/<%\s+tp.title.tags\(\s*\)\s+%>/, title_tags.join(", "));
const editor = view.editor;
const doc = editor.getDoc();
const cursor = doc.getCursor();
doc.replaceRange(str, cursor);
doc.setCursor(doc.lastLine());
}
}
}
}
} This does what I want, if I call it on a file rename event like this: this.registerEvent(
this.app.vault.on('rename', (file: TFile, _old_file: TFile) => {
this.insertTemplate(file, this.app);
})
); and with a frontmatter template file
As you can see from the above snippets, I used an unimplemented feature What do you think? |
Beta Was this translation helpful? Give feedback.
-
Hi. Thanks for creating this awesome plugin! I have a question. Or maybe a feature request. When I create a new note (
CTRL+N
), I tend to use certain tags also when typing-in the title in the text field at the top of the editor pane. I would like to have these tags automatically filled into the front matter of the new file.I have already set up in the templater settings: “Trigger Templater on new file creation” and "Enable folder templates", where I have added a folder template that inserts a front matter into the new file. The frontmatter template might look like this:
Currently this works fine. Except that I would also like to have the tags field in the frontmatter filled out based on what I typed in the title.
This plugin uses a function called
app.metadataCache.getTags()
to get the currently defined tags, so it should be possible to extract those and compare each word in the title with those to determine which of the title words is a tag.I also see another issue that might need to be addressed. Currently templater inserts the template before I have finished typing in the title of the new note. I guess it has to wait until the user presses enter in the text field at the top of the editor pane before inserting the template. Otherwise it cannot determine the tags.
Beta Was this translation helpful? Give feedback.
All reactions