Is there a way to minify Page.Content for Atom feeds?

I noticed that page content in HTML output is minimized as expected:

                        <p>Result:</p>
                        <div class=paige-shortcode-code>
                            <div class=highlight>
                                <pre tabindex=0 class=chroma>
                                    <code class=language-plaintext data-lang=plaintext>
                                        <span class=line>
                                            <span class=cl>q = &#39;q = %r; print(q %% q)&#39;; print(q % q)</span>
                                        </span>
                                    </code>
                                </pre>
                            </div>
                        </div>
                        <hr>
                        <p>Code:</p>

but page content in Atom XML output is not:

<?xml version="1.0" encoding="utf-8"?><feed xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom">[...snip...]<entry>[...snip...]
</span></span></code></pre></div><p>Result:</p>









<div class="paige-shortcode-code"><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-plaintext" data-la
ng="plaintext"><span class="line"><span class="cl">q = &#39;q = %r; print(q %% q)&#39;; print(q % q)</span></span></code></pre></d
iv></div>

<hr>
<p>Code:</p>

Is there a way to do that? The minify func only works on resources, so that doesn’t seem right.

mmh, shown code is NOT minified

The blank lines come from paige/code commands {{ }}. if you use {{- -}} they are gone.

Looks like the minification does not handle the content of (at least) your shortcode.
if you call the shortcode using {{% code .. %}}notation the blank lines vanish

Maybe due to this in Shortcodes

Hugo processes the shortcode separately, merging the output into the page content after Markdown rendering

and there’s a statement about removing whitespace here Shortcode templates (maybe related)

If a shortcode might be used inline, remove the surrounding whitespace by using template action delimiters with hyphens

Yes.

layouts/partials/minify.html
{{/*
Returns a minified copy of the given string.

Note that this partial template returns a string, so you may have to pass the
result through one of the safe functions (e.g., safe.HTML, safe.CSS).

@param {string} text The string to minify.
@param {string} [format=html] The format of the string to minify, one of css, html, js, json, svg, or xml.

@returns {string}

@example: {{ partial "minify.html" (dict "text" .Content) }}
@example: {{ partial "minify.html" (dict "text" .Content "format" "html") }}
*/}}
{{ $partialName := "minify.html" }}
{{ $result := "" }}

{{ $text := or .text "" }}
{{ $format := or .format "html" }}
{{ $supportedFormats := slice
  "css"
  "html"
  "js"
  "json"
  "svg"
  "xml"
}}

{{ if in $supportedFormats $format }}
  {{ with $.text }}
    {{ $cacheKey := printf "%s.%s" (hash.XxHash .) $format }}
    {{ with resources.FromString $cacheKey . }}
      {{ with . | resources.Minify }}
        {{ $result = .Content }}
      {{ end }}
    {{ end }}
  {{ end }}
{{ else }}
  {{ errorf "The %q partial does not support the %q format." $partialName $format  }}
{{ end }}

{{ return $result }}

layouts/_default/list.rss.xml
...
{{- range $pages }}
  <item>
    <title>{{ .Title }}</title>
    <link>{{ .Permalink }}</link>
    <pubDate>{{ .PublishDate.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate>
    {{- with $authorEmail }}<author>{{ . }}{{ with $authorName }} ({{ . }}){{ end }}</author>{{ end }}
    <guid>{{ .Permalink }}</guid>
    {{- $content := partial "minify.html" (dict "text" .Content "format" "html") }}
    <description>{{ printf "<![CDATA[ %s ]]>" $content | safeHTML }}</description>
  </item>
{{- end }}
...

Would a template function be easier to use?

{{ .Content | transform.Minify "html" }} ⟶ string

Certainly, but I’m not sure how often it would be used. This is the first time that I can remember anyone asking for this capabilty.

1 Like

Ah! That makes sense. Thank you!

For future readers, it’s important that the temporary resource have a file extension matching the content type, otherwise the minify function will fail.

Right, that’s why we do this:

{{ $cacheKey := printf "%s.%s" (hash.XxHash .) $format }}
1 Like

I didn’t see why that was necessary, so I tried leaving it out, and then learned the hard way. :slight_smile:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.