Invalid YAML with Templater JS

Using

  • Calendar
  • Periodic Notes
  • Templater

I managed to create this marvelous template below, for my daily notes. It’s activated, when I click on any day of the Calendar pane on the right. And the corresponding note is created, absolutely perfect.

BUT: Switching from Reading to Edit Mode, Obsidian shows the harsh truth, in pastell red:

Invalid YAML.

On Windows, everything works fine, nevertheless.

On Android, nothing works but the error message, template could not been processed, or something like that.

What I’m trying to do

---
<%*
// moment.locale('de');
let m = moment(tp.file.title);
let dayNow = m.format("dddd, D. MMMM YYYY");
let dayWeek = m.format("dddd");
let dayPrev = m.subtract(1, 'd').format('YYYY MM DD');
let dayNext = m.add(1, 'd').format('YYYY MM DD');
let week = m.format('gggg [W]WW');
let month = m.format('YYYY MM');
let quarter = m.format('YYYY [Q]Q');
let year = m.format('YYYY');
-%>
Datum: <% dayNow %>
Wochentag: <% dayWeek %>
Woche: <% week %>
Monat: <% month %>
Quartal: <% quarter %>
Jahr: <% year %>
---
# <% tp.file.title %>
Datum: <% dayNow %>
Wochentag: <% dayWeek %>
Woche: <% week %>
Monat: <% month %>
Quartal: <% quarter %>
Jahr: <% year %>

The TP embedded JS part alone is valid. As soon as I add any Key, Value pair, I’m lost.

Of course, I’d like to understand, what’s going wrong here, on the YAML, Templater, JS side.

AND I’d like to learn, why it makes a difference, running my vault on Windows vs. Android.

Any hint is welcome.

Thank you ever so much in advance!

1 Like

SOLVED.

With a little help from TfT Hacker on Twitter: Frontmatter with hack

The great idea: let Templater triple-dash your YAML, and everything is fine.

Nothing invalid, according to Obsidian.

And, surprise: no Metadata box, as well.

Just as you’d expect. At least: me :slight_smile:

My Daily Note template from above is now:

<% "---" %>
<%*
// moment.locale('de');
let m = moment(tp.file.title);
let dayNow = m.format("dddd, D. MMMM YYYY");
let dayWeek = m.format("dddd");
let dayPrev = m.subtract(1, 'd').format('YYYY MM DD');
let dayNext = m.add(1, 'd').format('YYYY MM DD');
let week = m.format('gggg [W]WW');
let month = m.format('YYYY MM');
let quarter = m.format('YYYY [Q]Q');
let year = m.format('YYYY');
-%>
Datum: <% dayNow %>
Wochentag: <% dayWeek %>
Woche: <% week %>
Monat: <% month %>
Quartal: <% quarter %>
Jahr: <% year %>
<% "---" %>

The three dashes are replaced by <% “—” %>.

That’s it.

After sync with Android: even there, no problem at all. Any click on a calendar day nicely creates or opens its note, perfectly rendered.

So: is it a hack?

Some kind of “illegal”?

Is there an “official” solution?

1 Like