Performing functions on a captured group in replaceRE

Hello,

Thank you so much for Hugo. I love how fast it is and how it allows me to use Go in more places. :heart:

I’m attempting to add the ability for titles within my generated pages to be linked to. Before stumbling upon the BlackFriday extension that will do this for me automatically, I was attempting to accomplish this using replaceRE in my template:

{{ .Content | replaceRE “(?U)<h2>(.*)</h2>” “<h2 id=’{{$1 | anchorize}}’>$1</h2>” | safeHTML }}

This did not do what I was hoping for and instead resulted in {{My Title | anchorize}} being rendered as the title instead.

I will be changing my approach to allow BlackFriday to do this for me, but I’m curious if this approach could be made to work. It might be a useful tool to have in my toolbelt going forward. :slightly_smiling_face:

Thanks!

See

May be you just need to change $1 (and so on) to ${1}? I know for fact that this works :sunglasses:

(because it looks like you are doing something similar Looks like you are doing something else … not sure why you are inserting the id, because Hugo/Blackfriday is already doing that for you)


Snippet from above link:

{{ $content = $content | replaceRE "(<h[2-9] id=\"([^\"]+)\".+)(</h[2-9]+>)" `${1}&nbsp;<a class="headline-
1 Like

Thanks for the replies.

You’re right, @kaushalmodi, BlackFriday will indeed generate the ids for me automatically. I was trying to do this myself as I was confused at the time as I shot myself in the foot with a custom shortcode. I realize now that I don’t get the benefits of BlackFriday while in a shortcode. It makes perfect sense now that @maiki helped me through it.

With that said, I’m still curious if it’s possible to do what I was trying to do. The regex works fine and I’m able to print the captured group perfectly fine. What I’m not sure is feasible or not is to perform some functions on $1 or if I’m limited to straight up insertion into the replacement string. I played with it some more and thought maybe something like this would work:

{{ .Content | replaceRE "(?U)<h2>(.*)</h2>" (printf "<h2 id='%s'>%s</h2>" (anchorize $1) $1) | safeHTML }}

But $1 is not defined in this context. I’m guessing it’s only known to replaceRE itself and therefore it’s simply not possible.

It’s just a curiosity really so no big deal if I can’t. I’m mainly just interested to see what can and cannot be done in Hugo templates.

I think it’s not possible to do what you want in exactly the same manner. But you can “extract” each subgroup into a different var and work on that … (below is not tested … but you will get the idea):

{{ $subgroup1 := .Content | replaceRE "(?U)<h2>(.*)</h2>" "${1}" }}
{{ .Content | replaceRE "(?U)<h2>(.*)</h2>" (printf "<h2 id='%s'>%s</h2>" (anchorize $subgroup1) $1) | safeHTML }}

It’s not efficient, but it seems to be at least doable.


I am actually doing something like that in one of my themes:

{{ $hugo_version_string_1 := hugo.Version }}
{{ with hugo.CommitHash }} <!-- Example value: "975bb629:chroma-d5ca12b" -->
    {{ $hugo_commit_string := . | replaceRE "^([0-9a-f]{7,}).*" "${1}" }}
    {{ $hugo_version_string_1 = printf `<a href="%s/commit/%s">%s</a>` $site_params.urls.hugo $hugo_commit_string $hugo_version_string_1 }}
{{ end }}
{{ $hugo_version_string := printf `<span class="nobr">Hugo %s</span>` $hugo_version_string_1 }}