TBB
TBB

Reputation: 1227

Grunt Task > Minify inline Json-ld script in html file > Jekyll Build

SHORT VERSION

Is there a grunt task which will minify inline json wrapped in <script type="application/ld+json"> tags in an html file?


LONG VERSION

I am using jekyll and grunt.

The Jekyll build creates static html files with page/post specific inline json-ld.

The json-ld is wrapped in <script type="application/ld+json"> tags.

I want to minify the entire HTML file.

I have run grunt-contrib-htmlmin (htmlmin) on the files.

Htmlmin has nicely minified all content:

  1. before the <script type="application/ld+json"> tag into 1 line; and

  2. after the <script type="application/ld+json"> tag into 1 line.

The <script type="application/ld+json"> tag and its contents has been ignored.

Htmlmin has an option called MinifyJS to "Minify Javascript in script elements and on* attributes (uses UglifyJS)".

I have tried the option as both true and false, and without the option altogether. The json-ld tag is still ignored.

I have tested the json-ld syntax in an online minifier. It returned the code (~100 lines) as one line.

I have searched for other Grunt tasks. It looks like there are a few other html minifiers out there but none seem as well developed and supported as grunt-contrib-htmlmin. I suspect that they will have the same issue but I will consider testing some others if I do not get a response.

I am new to Grunt.

Can anyone suggest a Grunt task that can handle this?

Or provide guidance on how to do this?

Thank you for any assistance in advance! :)

Upvotes: 1

Views: 406

Answers (1)

TBB
TBB

Reputation: 1227

I won a tumble weed award for this (yay), but even better: I have an answer!

Grunt-contrib-htmlmin has an option called processScripts to specify script types for processing. By specifying application/ld+json, you will force it to process the Json-LD too.

This is probably obvious to most, and it is to me now, but it wasn't to me when I first asked this. Goes to show that once you understand something you are better addressed at coming up with solutions - and my steep "coding" learning curve is finally paying off!

Upvotes: 2

Related Questions