Replace html content from Hugo Pipes?

Is it possible to use Hugo Pipes to replace some content, for example convert some spaces to non-breakable ones, or have I to keep this kind of tasks in say Gulp (parsing all html files)?

See gulpfile example.

You could use the replace or replaceRE functions in your templates

I saw these functions, but how can I pass the whole page as the INPUT of the function?

You can pass the content using .Content

  • and perform multiple replacement at once then return the modified content, just as a markdown parser for example or a gulp task?

Try it out and see :slight_smile:

Yes I’ll try tomorrow, thank you! :slight_smile:

This should help me too:
https://discourse.gohugo.io/t/refactor-multiple-replace-statements/9994

Do you have any idea why
{{ replace .Content "A" "B" | safeHTML }}
is working, but
{{ .Content | replace "A" "B" | safeHTML }}
returns
A
?

source file

I think both are working, but work in another way as you expect.
{{ replace .Content "A" "B" }} Replaces every β€œA” inside .Content with β€œB”

But {{ .Content | replace "A" "B" }} Replaces every occurrence of β€œB” it finds in β€œA” with the value of .Content. And as it does not find any B in β€œA” it could replace, it leaves it as β€œA”.

So the following two will do the Same:

{{ replace "ABC" "A" "X" }}  outputs XBC
{{ "X" | replace "ABC" "A"}} outputs XBC
1 Like

Indeed thank you for pointing this out!
However then I can’t pipe multiple replace on .Content…

I think I will keep this kind of replacement in my gulpfile :sweat_smile:

You could do it like this

{{ $content := replace .Content "A" "B" }}
{{ $content = replace $content "C" "D" }}
...
{{ $content }}
1 Like

This works, thank you! :slight_smile: