Hello! I am trying to surround the h1, h2, h3, h4, h5, h6 HTML heading values within the {{ .Content }}page variable with an anchor tag linking to itself.
For example, if I had a markdown file containing:
# Introduction
This is my post.
The outputted HTML currently shows:
<h1 id="introduction">Introduction</h1>
<p>This is my post.</p>
What I would like to see is:
<h1 id="introduction">
<a href="#introduction">Introduction</a>
</h1>
<p>This is my post.</p>
How would I go about achieving this? Thank you!
EDIT: I’ve looked into Goldmark Render Hooks, but creating layouts/_default/_markup/render-h1.html appears to have no effect on the output.
I think you could accomplish this with a custom shortcode.
You might have parameters called level and text, then the template would contain that full desired html example you provided, but substituting in the header number and content text where appropriate. You’d call “anchorize” on the header text to get the link and ID values.
Sorry, on mobile, so not able to actually try this out… Your TOC problem reminds me of the recommendation here:
Using {{% and %}} instead of {{< and >}} causes things to get rendered differently, potentially including them in TOC. I tested this out a bit ago and got confusing results with Goldmark… So I’m not sure exactly what’ll happen if you try this.
You may also have to drop the parameter into the template without calling markdownify on it? I’m really not sure.
Thanks for the quick reply! I just re-read the documentation for shortcodes.
If further rendering is required, use {{% and %}}.
If no further rendering is required (shortcode outputs html?), use {{< and >}}.