Pulling from YAML properties into note for export

Hi guys,

Looking to create a template or have some sort of functionality where my note will pull from the frontmatter and put it in the same note, near the top. So in my YAML frontmatter i have a property called “type:” — and i want whatever is present for that field to be included at the top of the note. I also have a “creation date:” property, and i want that at the top of my notes, also.

Then, when i export the note to PDF or whatever format, these YAML frontmatter properties will be displayed — that is the goal.

Is this possible or is there a plugin that does this?

Thanks

You can use Templater community plugin and its frontmatter module. Templater offers text snippets and file creation. If you don’t want to manually place the cursor then QuickAdd’s capture choices may be your option. QuickAdd uses different modules to access frontmatter through inline scripts (possibly Dataview which is other community plugin). Dataview alone cannot do same things that Templater and QuickAdd does.

I use templater. I followed the directions but when i flip to read view, nothing? Is this working as intended?

Run command

Templater: Open insert template modal
or
Templater: Replace templates in the active file

You’re touching in on a greater theme here on when is what done, and in order to better answer you I need to talk a little about different concepts.

So for this post I’m going to use static about actual text being present in your note, and dynamic about text which when displayed in various modes replaces the actual text you wrote in the note.


Dynamic queries

When you’re working within Obsidian you can use various plugins to achieve different dynamic effects. Like displaying information from the frontmatter (YAML or properties) in the main note. To do this it could be as simple as doing `= this.type`. This will display the type property from the current file when you’re in either reading mode or in live preview (with your cursor on another line).

In my initial tests, this will also be shown when you do Export to PDF, so maybe it’s just as easy as including lines similar to the one above into your documents.

However, I’m not sure if such a dynamic query would survive all kinds of exports, and I do believe it’ll not work if you publish your stuff to a website (unless it use the reading mode rendering before publishing).

Similar effect to the simple line above, can also be done using plugins like Meta bind and their view commands. But we’re still depending on that plugin to be included when presenting the text to you.

Using Templater for static insertion

Templater is used for one time insertions of a text according to the template. It’s mostly used to setup a base note with various information so that it’s easier to fill in the needed information, or to insert a section into your current document.

You can use it to do a one time insertion of your type and/or creation date, but once this text is inserted it’s static and you need to manually change it yourself if you want it changed. In other words, you loose the link between the frontmatter and the inserted text.

Using QuickAdd for static insertion or changes

QuickAdd can also be used similar to Templater for static insertion of a given text based on some criteria. In addition you can use it to manipulate/edit your document in various ways, with a little bit of scripting. (This can partially also be done with Templater but it do require scripting)

The point I’m trying to make is that you could insert a static placeholder into your document, and use scripting to replace the content of that placeholder depending on your frontmatter. Imagine you’ve got the following static text in your note:

<span id="type"> </span>

Then you could write something in QuickAdd (or in fact also in Templater or Dataview) to add the value of type into that <span> element. And depending on how of often you call that script, it’ll reflect the correct value of the property.

Note that since this solution now updates a static placeholder, there will be static text which would survive any export to PDF or publish to any site the same way any other other static text would work.

Using Linter to make the static be dynamic…

Another option, which would work also in the context of publish/export, is to utilise some tool like Linter to create a custom rule for you to run a script to update a static placeholder on every save of your note. That way it would always make sure that the value from the frontmatter, is statically inserted into your note and as such is always present in the publish/export.


I hope this make sense to you, if not ask and I’ll try to explain it better. But the take away is firstly that you need to verify whether the simpler solution of a dynamic query using either Dataview or Meta bind will be sufficient for your export needs. If its not, you’ll need to look into the other part on how to semi-dynamically insert the value from the frontmatter to be part of the static text of your note.

1 Like