foambubble / foam

A personal knowledge management and sharing system for VSCode
https://foambubble.github.io/
Other
15.37k stars 659 forks source link

Auto apply template based on placeholder path / name #1218

Open gorootde opened 1 year ago

gorootde commented 1 year ago

Is your feature request related to a problem? Please describe.

It is annoying to be forced to create pages for placeholders manually, because Ctrl+click does not apply the matching template.

My current workflow looks way to often like this :

  1. Write a placeholder link [[users/example]]
  2. Cmd+click on the link
  3. Find that the created file doesn't have my users template applied and also resides in / instead of /users
  4. Delete the file
  5. Use command palette to manually create a note from template User

Describe the solution you'd like

As a user I'd expect placeholders to apply the matching template automatically upon Ctrl+click.

  1. Create a placeholder wiki-link e.g. to [[users/example]]
  2. Ctrl+click on the link
  3. The template with the frontmatter containing filepath: users/$FOAM_SLUG is automatically chosen and I am prompted for the $FOAM_TITLE

To do so, foam would require a way to configure a "template schema" that is used for looking up the correct template in step 3.). The schema ideally would be a regex / glob pattern either configured as setting or within the frontmatter of templates.

Describe alternatives you've considered

Eventhough #850 would reduce the pain, it would certainly not help with creating the files in the correct path.

Screenshots or Videos

No response

riccardoferretti commented 1 year ago

I was just thinking that a couple of days ago. In my case, clicking on a [[2023-04-12]] placeholder will not create the daily note, but simply use the generic template.

First of all, there is a pretty simple solution to your problem: if you hover on a placeholder you will see a "Create note from template" in the hover, which will allow you to select the template to apply.

At the same time I was thinking of a way to make this simpler and more automated. My current thinking is adding a property to templates called from_placeholder (or something like that), which would include a regex that will be matched against the placeholder text. E.g.

---
foam_template:
  filepath: "📆 daily-notes/$FOAM_DATE_YEAR-$FOAM_DATE_MONTH-$FOAM_DATE_DATE.md"
  from_placeholder: ^(19|20)\d\d-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$
  name: "Daily note"
---

# ${FOAM_DATE_YEAR}-${FOAM_DATE_MONTH}-${FOAM_DATE_DATE}

Still thinking about the details on how to go about it, happy to get feedback on it.

gorootde commented 1 year ago

This is exactly what I was describing. I'd love to see it implemented like that! Upon index creation we may just index the from_placeholder fields as well, and use the first matching entry of that list to choose the template to apply.