# Add/replace text inside shortcodes

**URL:** <https://discourse.gohugo.io/t/add-replace-text-inside-shortcodes/42103>\
**Category:** support\
**Created:** [December 26, 2022, 11:18pm UTC](https://discourse.gohugo.io/t/add-replace-text-inside-shortcodes/42103 "2022-12-26T23:18:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![idarek](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/idarek/32/26229_2.png) [@idarek](https://discourse.gohugo.io/u/idarek)\
**Post date:** [December 26, 2022, 11:18pm UTC](https://discourse.gohugo.io/t/add-replace-text-inside-shortcodes/42103/1 "2022-12-26T23:18:08Z")

</div>

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

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

```

and shortcode

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

```

This will generate as

```auto
<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

```auto
{{ 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

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

```

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

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [December 26, 2022, 11:29pm UTC](https://discourse.gohugo.io/t/add-replace-text-inside-shortcodes/42103/2 "2022-12-26T23:29:32Z")

</div>

Markdown attributes…

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

```

No need to mess with the shortcode.

---

<div class="post-metadata">

**Author:** ![idarek](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/idarek/32/26229_2.png) [@idarek](https://discourse.gohugo.io/u/idarek)\
**Post date:** [December 26, 2022, 11:39pm UTC](https://discourse.gohugo.io/t/add-replace-text-inside-shortcodes/42103/3 "2022-12-26T23:39:08Z")

</div>

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

> **[Markdown Render Hooks](https://gohugo.io/templates/render-hooks/#heading-link-example)**
>
> Render Hooks allow custom templates to override markdown rendering functionality.

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

```

Am I missing something?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [December 26, 2022, 11:40pm UTC](https://discourse.gohugo.io/t/add-replace-text-inside-shortcodes/42103/4 "2022-12-26T23:40:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![idarek](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/idarek/32/26229_2.png) [@idarek](https://discourse.gohugo.io/u/idarek)\
**Post date:** [December 26, 2022, 11:41pm UTC](https://discourse.gohugo.io/t/add-replace-text-inside-shortcodes/42103/5 "2022-12-26T23:41:25Z")

</div>

Yes, exactly that.

---

<div class="post-metadata">

**Author:** ![idarek](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/idarek/32/26229_2.png) [@idarek](https://discourse.gohugo.io/u/idarek)\
**Post date:** [December 26, 2022, 11:49pm UTC](https://discourse.gohugo.io/t/add-replace-text-inside-shortcodes/42103/6 "2022-12-26T23:49:27Z")

</div>

Found it.

Added the following to my `render-heading.html`

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

```

like described here:

> <https://github.com/gohugoio/hugo/blob/34d1150d927adfba97399f70fb046fbfc5c8cd7a/markup/goldmark/integration_test.go>

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [December 26, 2022, 11:51pm UTC](https://discourse.gohugo.io/t/add-replace-text-inside-shortcodes/42103/7 "2022-12-26T23:51:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [December 28, 2022, 11:51pm UTC](https://discourse.gohugo.io/t/add-replace-text-inside-shortcodes/42103/8 "2022-12-28T23:51:19Z")

</div>

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