Add/replace text inside shortcodes

Hi,
I got a following text in markdown surrounded by shortcode

{{% a-shortcode %}}
## Heading section
Paragraph text
{{% /a-shortcode %}}

and shortcode

<span itemprop="xyz">
 {{ .Inner }}
</span>

This will generate as

<span itemprop="xyz">
<h2 id="heading-section">Heading section</h2>
<p>Paragraph text</p>
</span>

I would like to replace/add text to achieve from this
<h2 id="heading-section">
this
<h2 itemprop="text" id="heading-section">

My idea was

{{ replaceRE `<h2` `<h2 itemprop="text"` .Inner }}

but noticed that .Inner is still in markdown form hence replaceRE doesn’t see H2 yet.

Appreciate your help with findRE or replaceRE or other approach, as this is not my best side in Hugo.

This is working partly

{{ replaceRE `<h2` `<h2 itemprop="text"` (.Inner | markdownify) }}

byt the output is displayed in plain HTML on page, not in proper HTML.

Markdown attributes…

{{% a-shortcode %}}
## Heading section {itemprop=text}
Paragraph text
{{% /a-shortcode %}}

No need to mess with the shortcode.

2 Likes

Wow, so simple. It’s working on the plain Hugo site without modifications. Thank you for that, but

it’s not working when using customised render-heading.html like described here

<h{{ .Level }} id="{{ .Anchor | safeURL }}">{{ .Text | safeHTML }} <a href="#{{ .Anchor | safeURL }}">¶</a></h{{ .Level }}>

Am I missing something?

I assume you are using the {{% %}} notation so that the headings are included in .TableOfContents?

Yes, exactly that.

Found it.

Added the following to my render-heading.html

{{- range $k, $v := .Attributes -}}
    {{- printf " %s=%q" $k $v | safeHTMLAttr -}}
  {{- end -}}

like described here:

Just don’t mess with the heading IDs in the render hook:
https://github.com/gohugoio/hugo/issues/8383

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.