HUGO

Is there a faster way to escape or filter out the Shortcode?

Hi All,
I am trying to use <meta name="description" content="{{ .Description }}">, if I do not set a description to the page, I hope to use the contents in the page. However, if my page has shortcodes, the shortcodes will also be included in the meta.

I have read this Escaping Meta Tag Content? Help - #23 by rhcarvalho but it’s mainly about HTML tag.

I know I can try to find all “{{< >}}” then remove them by my customized function, but I wonder if there is already a built-in fast way/function to do so?

Any help is appreciated, thanks in advance!

I do it like this in my Zen theme:

<meta name="description" content="{{ with (.Description | default (replaceRE "\n" " " (.Plain | truncate 170))) }}{{ . }}{{ end }}">

2 Likes

Thank you so much for your share!

But with truncate, it’s not always the same number how much I want to truncate. But it is a nice idea! Thanks!

You can always pass an int to truncate from a parameter, when needed.
e.g.

+++
num = 200
+++

And then

{{ $num := .Params.num }}
{{ $num = $num | default 170 }}
{{ $plain := .Plain | truncate $num }}
1 Like

Thanks!
Then if I just want to remove the shortcodes in the middle of the contents while keeping other contents,

for example:

some text...
{{< shortcodes >}}
some other text...

If I just want to filter out shortcodes and use the plain text, I think I need to append the rest contents.

I also find trim might be useful, but if we have several shortcodes within the contents, it’s a little bit more complex to filter.

like:

some text...
{{< shortcodes >}}
some other text...
{{< anotherShortcodes >}}
some more text...

But for now, truncate and trim seems good. It would be great if it can detect all the shortcodes I am using, just like what it does to filter out all HTML tags.

Thank you again for all your help!

On list pages you can hide the rendered shortcode content with a condition like:
{{ if eq $.Page.Kind "page" }} -from within the shortcode-.

To hide the rendered shortcodes in a description meta tag at the head of the content file that contains them, you would have to resort to RegEx -as you know-:

{{ .RawContent | replaceRE `{{.*?}}` `` }}

In the above, the .RawContent variable fetches the raw markdown content with the unprocessed shortcodes and the replaceRE function will remove everything between the curled brackets.

1 Like

This works nice! Thank you very much!

I have another issue then, seems .RawContent only works with Page. If mine is:
{{ with .Description | default .Summary }}

Cannot with them. When I use .Description or .Summary, it’s already a string that cannot be parsed by RawContent. I probably need to think of other ways how I want to use it.

Thanks a lot!

.Description is a string set in front matter.
.Summary is the already processed and truncated .Content.

Therefore it is logical that .RawContent has nothing to do with either.

Your only option is to set manual summaries, if you are not OK with the default .Summary.
As posted above you can achieve this with truncate.

1 Like

thanks for your share and advice!