kedron
January 14, 2021, 3:06pm
1
what you were doing or what you tried
I’m trying to include the full content from a Markdown file inside <content:encoded><![CDATA[
tag for RSS. I’ve tried this:
{{ "<content:encoded><![CDATA[" | safeHTML }}{{ .Content | safeHTML }}{{"]]></content:encoded>" | safeHTML}}
what you expected
I expected the .Content to render as HTML within the <content:encoded><![CDATA[
brackets.
what actually happened
<![CDATA[
is removed, and the first <
of each HTML tag is escaped:
<content:encoded><div>Test div</div>
<p>Test p</p></content:encoded>
My Hugo details:
Hugo Static Site Generator v0.80.0/extended darwin/amd64 BuildDate: unknown
GOOS=“darwin”
GOARCH=“amd64”
GOVERSION=“go1.15.6”
[markup]
defaultMarkdownHandler = "goldmark"
[markup.goldmark]
[markup.goldmark.renderer]
unsafe = true
Thanks so much for any light you can shed on what I’m doing wrong.
Basically you are trying to encode Content in RSS 1.0 .
RSS 1.0 is a RDF Site Summary
RSS 2.0 is a dialect of XML
The internal Hugo RSS Template uses RSS 2.0.
Hugo uses Go template packages (text, HTML, XML) that automatically secure output.
You will have to do any escaping yourself in your RSS 1.0 template.
The internal template -in the above link- uses such escaping:
{{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
You will most likely need to use some or all of the following functions:
printf
, safeHTML
, safeHTMLAttr
1 Like
kedron
January 14, 2021, 5:06pm
3
Thank you! You pointed me in the right direction! Here is what I landed on:
<content:encoded>
{{ printf "<![CDATA[" | safeHTML }}
{{- printf "%s" .Content | safeHTML }}
{{- printf "]]>" | safeHTML }}
</content:encoded>
Thank you!
1 Like
Instead of calling printf
3 times, which is completely unnecessary, use the following construct:
<content:encoded>{{ printf "<![CDATA[%s]]" .Content | safeHTML }}</content:encoded>
The verb %s prints the uninterpreted bytes of the string or slice
) (in this case .Content
) and whatever is entered before or after the verb will wrap its output.
More on the printing verbs can be found at the doc for: fmt - The Go Programming Language
1 Like
kedron
January 14, 2021, 5:43pm
5
Thanks again! That worked (with one little change):
<content:encoded>{{ printf "<![CDATA[%s]]>" .Content | safeHTML }}</content:encoded>
I truly appreciate your help!
1 Like
kedron
January 15, 2021, 10:15pm
6
Welp, I had it working yesterday. I must have jacked something up along the way. I’m back to the same results.
<content:encoded>{{ printf "<![CDATA[%s]]>" .Content | safeHTML }}</content:encoded>
That results in:
content:encoded><p class="lead">Test title here!</p></content:encoded>
Removes the <![CDATA[ and first < from .Content
Try placing everything within the funtion:
{{ printf "<content:encoded><![CDATA[%s]]></content:encoded>" .Content | safeHTML }}
kedron
January 16, 2021, 4:00pm
8
Thanks for the suggestion. I tried that as well, and it produces the same results. So close… Here is the entire file:
kedron
January 29, 2021, 1:48am
9
I think I’ve found the issue. Before sending my site to my host, I run hugo --gc --minify -d public
- and I believe the --minify is stripping out the bits I need for XML.
Is there a way to minify everything except XML?
kedron
January 29, 2021, 1:51am
10
Sorry for the noise. I found this:
Everything works as expected…
This topic was automatically closed after 8 hours. New replies are no longer allowed.