Minify the output of a partial

Is it possible to pipe the output of a partial to another command? For example, minify?

I tried this:

{{- partial "seo_schema" . | minify }}

The minify flag is meant for use in the CLI like so: hugo --minify

The Asset Minifcation syntax is:

{{ $css := resources.Get "css/main.css" }}
{{ $style := $css | resources.Minify }}

However as far as I know a partial cannot be fetched as a Resource.

A partial’s output will be minified along with the final rendered output of your Hugo project only when you use the above CLI flag. See the Release Notes for Hugo 0.47

minify is like an alias for resources.Minify (Ref). So, minify can be used instead, though, as you said, only for “resources” in the assets/ dir (or the ones created on the fly using resources.FromString and such).

Ensure that the partial “outputs” a string. After that, you can do something like below:

{{ with "html/foo.html" }}      <!-- This path doesn't matter much -->
    {{ $vars := ("<div>\n\n\nHello World\n\n\n</div>" | resources.FromString .) | minify }}
    {{ $vars.Content | safeHTML }}
{{ end }}

Above outputs <div>Hello World</div> in the HTML.

(Replace "<div>\n\n\nHello World\n\n\n</div>" with your partial call.)

Ref


Though, unless you have a reason to minify just this partial, just do hugo --minify as @alexandros suggested.

Sure.

I thought that since this question is about minifying a partial then the CLI flag is what needs to be used.

Thanks for the links. Maybe the OP was looking for these.

It doesn’t have to be, right? Because there’s also resources.ExecuteAsTemplate (doc page).

(This is more a theoretical question to deepen my understanding. I don’t think it will help Zinefer in his use case given the easier ways already listed above.)

I think that partials outputs value of type “template.HTML” (don’t quote me as I could be wrong; not a Go programmer). Now “template.HTML” is like a string, but not exactly. But it can be converted to one by using a printf at the very end of the partial that outputs the exact string you want.

I don’t have the exact issue at the top of my head at the moment, but some function had an issue when using a partial to provide its input because it expected “string” and not “template.HTML”. Putting a printf at the end fixed that issue.


About resources.ExecuteAsTemplate, I haven’t yet used that. It looks like those would be used when you want to process the Go Template syntax in a physical css/scss/etc. asset file.

1 Like

Thanks for the replies everyone! I am already using --minify but this one partial doesn’t get white-space stripped. I’m starting to think the mixed content of my partial is the culprit. Here’s the partial:

<script type="application/ld+json">
{
  "@context" : "http://schema.org",
  "@type" : "BlogPosting",
  "mainEntityOfPage": {
    "@type": "WebPage",
    "@id": "{{ .Site.BaseURL }}"
  },
  "articleSection" : "{{ .Section }}",
  "name" : "{{ .Title }}",
  "headline" : "{{ .Title }}",
  "description" : "{{ if .Description }}{{ .Description }}{{ else }}{{if .IsPage}}{{ .Summary }}{{ end }}{{ end }}",
  "inLanguage" : "en-US",
  "author" : {{ range .Site.Params.Author }} { "@type": "Person", "name": "{{ . }}" } {{ end }},
  "creator" : {{ range .Site.Params.Author }} { "@type": "Person", "name": "{{ . }}" } {{ end }},
  "publisher": { "@type": "Organization", "name": "{{ index .Site.Params.Publisher }}", "logo": {"@type": "ImageObject", "url": "https://google.com/logo.jpg", "width": 60, "height": 60 } },
  "accountablePerson" : {{ range .Site.Params.Author }} { "@type": "Person", "name": "{{ . }}" } {{ end }},
  "copyrightHolder" : {{ range .Site.Params.Author }} { "@type": "Person", "name": "{{ . }}" } {{ end }},
  "copyrightYear" : "{{ .Date.Format "2006" }}",
  {{ with or (index (.Resources.Match "thumb.*") 0) (index (.Resources.ByType "image") 0) }}"image": "{{ .Permalink }}",{{ end }}
  "datePublished": "{{ .Date }}",
  "dateModified" : "{{ .Date }}",
  "url" : "{{ .Permalink }}",
  "wordCount" : "{{ .WordCount }}",
  "keywords" : [ {{ if isset .Params "tags" }}{{ range .Params.tags }}"{{ . }}",{{ end }}{{ end }}"Blog" ]
}
</script>

EDIT: I reworked my partial to use jsonify and now everything is quite minimal.

hi @zinefer I just ran into the same problem as you just now, when I hugo --minify everything is minified but the json-ld for the structured data is not when viewed in the public folder.

How did you minify he partial? When I use {{ partial "header" . | jsonify }} I get no errors but when I view the page in the public folder I see: u003e\n\n\u003chr\u003e\n\n\n\n\u003chr\u003e and other strange characters?! I also copied your code but also saw the same characters?!

Thank you.

I think Hugo and/or minify uses the type attribute to decide if that HTML tag should be minified. And probably “ld+json” types are not whitelisted for minification? @bep? @tdewolff?

This is what I have for now.

2 Likes

Oh like that… now I see, thank you for sharing!

We use the type attribute. In Hugo 0.48 we try every defined MimeType that has the json defined as a suffix. This means that you can create a custom MIME type for application/ld+json and it should work.

For JavaScript we also try this regexp: “^(application|text)/(x-)?(java|ecma)script$”

I’ve seen a little activity on this post and just wanted to let everyone know that as of 0.49 ld+json tags should be minified with the --minify flag.

1 Like