Auto replace text to link

Hello. I want to replace some phrases with other phrases, for example, “old text” -> “new text”. How can I do that?

Rather depends whether that is a one-off activity or ongoing.

I would use my text editor to do a search and replace.

Replacements need to be done each time the XTML version is generated

Confusing, so you want to change some text after the Hugo build process?

I think that Hugo can use the Go regular expression replace function which might help as it would do the replace during build as long as you can find out where to put the replace.

You could also replace a simple Hugo build with a build script that runs a command-line replace after the build.

However, it would probably be easier to help if we knew why you wanted to do the replace on each build instead of just once in the source - I can only assume it relates to some dynamic data feed. If we knew what, there might be better approaches.

I want to do a re-linking on the site. There are a lot of texts and I would like to automatically change the phrases for links.

It would help us help you if you give a concrete example… Can you share a site source (even a minimal would be fine) and then point to a link in there, and show us how that should look before and after replacements? From where do you get the replacement text? Etc.

I want to replace some phrases with other phrases

So something like

{{ replace .Content "old text" "new text" }}

This may be limitted, so look at


as well

e.g. I have:

{{ .Content | replaceRE `(?s:</div>\s?</p>)` `</div>` | replaceRE `(?s:</pre>\s?</p>)` `</pre>` | replaceRE `(?s:</figure>\s*</p>)` `</figure>` | safeHTML }}

in _default/baseof.html

This is a good example.
Can we have something global? I mean, I want to replace phrases in TPL too.

1 Like