# Exclude shortcode content from jsonify

**URL:** <https://discourse.gohugo.io/t/exclude-shortcode-content-from-jsonify/28442>\
**Category:** support\
**Created:** [September 23, 2020, 9:18pm UTC](https://discourse.gohugo.io/t/exclude-shortcode-content-from-jsonify/28442 "2020-09-23T21:18:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hrishikesh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hrishikesh/32/19176_2.png) [@Hrishikesh](https://discourse.gohugo.io/u/Hrishikesh)\
**Post date:** [September 23, 2020, 9:18pm UTC](https://discourse.gohugo.io/t/exclude-shortcode-content-from-jsonify/28442/1 "2020-09-23T21:18:13Z")

</div>

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

```auto
{{ $.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](http://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>`):

```auto
<script>
  document.addEventListener("turbolinks:load", function()
    {
      loadMedia();
    });
</script>

```

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?

---

<div class="post-metadata">

**Author:** ![ju52](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ju52/32/6749_2.png) [@ju52](https://discourse.gohugo.io/u/ju52)\
**Post date:** [September 24, 2020, 6:20am UTC](https://discourse.gohugo.io/t/exclude-shortcode-content-from-jsonify/28442/2 "2020-09-24T06:20:57Z")

</div>

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" .}}
{{end}}
```

---

<div class="post-metadata">

**Author:** ![Hrishikesh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hrishikesh/32/19176_2.png) [@Hrishikesh](https://discourse.gohugo.io/u/Hrishikesh)\
**Post date:** [September 24, 2020, 6:30am UTC](https://discourse.gohugo.io/t/exclude-shortcode-content-from-jsonify/28442/3 "2020-09-24T06:30:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [September 24, 2020, 11:30am UTC](https://discourse.gohugo.io/t/exclude-shortcode-content-from-jsonify/28442/4 "2020-09-24T11:30:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Hrishikesh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hrishikesh/32/19176_2.png) [@Hrishikesh](https://discourse.gohugo.io/u/Hrishikesh)\
**Post date:** [September 24, 2020, 11:35am UTC](https://discourse.gohugo.io/t/exclude-shortcode-content-from-jsonify/28442/5 "2020-09-24T11:35:49Z")

</div>

> [@davidsneighbour](#):
>
> It’s also better for performance

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.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [September 26, 2020, 11:35am UTC](https://discourse.gohugo.io/t/exclude-shortcode-content-from-jsonify/28442/6 "2020-09-26T11:35:59Z")

</div>

Closed on September 26, 2020, 11:35am UTC

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