Exclude shortcode content from jsonify

I’m trying to add site search using Lunr and so, generating index like this:

{{ $.Scratch.Add "pagesIndex" slice }}

{{ range $index, $page := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }}

  {{ $pageData := (dict "title" $page.Title "href" $page.RelPermalink "tags" (delimit $page.Params.tags " ; ") "content" $page.Plain) }}
  {{ $.Scratch.Add "pagesIndex" $pageData }}

{{ end }}

{{ $.Scratch.Get "pagesIndex" | jsonify }}

The problem is, the generated JSON also includes the <script> tags that I used in my shortcodes.

For example, in my video shortcode, I have used Plyr.io as my video player and so, I’m initializing the player using (while the loadMedia function exists in my script that exists in my document’s <head>):

  document.addEventListener("turbolinks:load", function()

The generated JSON also includes the text document.addEventListener("turbolinks:load", function(){loadMedia();});.

Is there any way I can exclude text content from my shortcodes while including the text that I’ve written in markdown?

Cut your shortcode in parts

I trigger some head / meta includes with an empty shortcode

shortcode tex.html
<!-- does nothing - only activates mathjax -->

part of my head partial

{{ if .HasShortcode "tex" }}
    {{ partialCached "js/mathjax" .}}

I wasn’t aware about .HasShortcode. I now added that condition in my baseof.html to load the required script. Thanks!

It’s also better for performance. If you have two players on a page, it would run loadMedia() twice. Putting it in a header template is much better.

You could improve on that procedure by doing the following:

  • surround your range (after range and before end) with a div or span that has a class is--player
  • add a script that does an each loop over .is--player and initializes them. in jQuery, this would be $('.is--player').loadMedia(); depending on how you scripted loadMedia.

Yeah, I was aware of that, but, I thought, I had no option. Turns out, I had. Now, the script loads all videos and audio in one call. Thanks.

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