Is it possible to process YAML data with ERB inside Markdown content?

61 views
Skip to first unread message

Dave Everitt

unread,
Jun 7, 2021, 11:21:04 AM6/7/21
to nanoc
All my content is in Markdown/HTML for tags MD can't process.

I now want to process the YAML metadata in each Markdown content file (not in a layout) so the result appears in the rendered Markdown?

ATM I'm trying an include:

/content/myfile.md (has YAML)
/content/_inc/myinclude.md
OR
/layouts/partials/myinclude.html

But the include code in the myfile.md Markdown file gets curly quotes and renders literally:

<%= @items[‘/_inc/myinclude/’].compiled_content %>

I came across this issue previously so will dig around, but if anyone has a solution in the meantime…

Dave Everitt

unread,
Jun 7, 2021, 11:41:30 AM6/7/21
to nanoc
Update. Curly quotes fixed by explicitly processing MD files in question with ERB first.

Issue: how can I access and process the YAML (YAML to Ruby) in /content/myfile.md from within the /_inc/myinclude.md file?

Dave Everitt

unread,
Jun 7, 2021, 11:58:51 AM6/7/21
to nanoc
UPDATE: only solution I can find is to put ERB in the Markdown rather than an include, but then repeat the YAML data (an array) as a Ruby array in the ERB - I can't directly access the YAML from the top of the Markdown within the ERB?

Denis Defreyne

unread,
Jun 7, 2021, 12:01:07 PM6/7/21
to noreply-spamdigest via nanoc
Hey Dave,

I’m having some issue in visualising the problem you’re facing. Do you have a (demo) repository I could take a look at?

Denis Defreyne
[dəˈni] • pronouns: he/him

--
You received this message because you are subscribed to the Google Groups "nanoc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nanoc+un...@googlegroups.com.

Dave Everitt

unread,
Jun 7, 2021, 1:30:29 PM6/7/21
to nanoc
Hi Denis

I've temporarily made the repo public and invited you as a collaborator (although I'm fine if you just need to look, not contribute!).



I'll make it private again once you've seen the code.

Dave Everitt

unread,
Jun 7, 2021, 1:35:11 PM6/7/21
to nanoc
The page in question is http://127.0.0.1:3000/planets/venus/ tab: "dignities"

Denis Defreyne

unread,
Jun 7, 2021, 3:57:33 PM6/7/21
to noreply-spamdigest via nanoc
Hey Dave,

I think the way I’d go about this is using partials, using the Rendering helper. You’re already using the rendering helper, but I think the missing piece you need is passing in the item to the partial, e.g. using <%= render '/partials/dignities/', item: @item %>. The extra argument passes the item to the partial, so that the partial itself also has access to @item. Also refer to the second example in the Rendering helper documentation.

You’ll indeed still need to run the erb filter first — the Markdown filters won’t know how to deal with erb and will all mess it up I believe.

With this approach, you’ll have partials that have access to the original item (and thus its metadata) and you can split up pieces of code, reuse it elsewhere, and not duplicate any code.

One more thing: I spotted <%= @item[:dignities[0]] %> in content/_inc/dignities.md. This is probably not what you intend, because dignities[0]is evaluated first, which evaluates to "d", so that piece of code is effectively equal to <%= @item["d"] %>

Let me know how it goes!

Denis Defreyne
[dəˈni] • pronouns: he/him



Dave Everitt

unread,
Jun 11, 2021, 10:40:49 AM6/11/21
to nanoc
Many thanks Denis - that's done it! I'll remove you from the repo - yes, the <%= @item[:dignities[0]] %> was a remnant from my desperate hacking :-)

Be good to have the <%= render '/partials/whatever/', item: @item %> in the docs, as I couldn't find it anywhere. Happy to send a pull request to the relevant page in the docs repo, maybe here: https://nanoc.app/doc/reference/helpers/#rendering

Reply all
Reply to author
Forward
0 new messages