Split markdown content in two files, but don't render shortcodes as raw text

markdown

+++
title = "Test"
date = 2021-03-31T08:58:49-07:00
draft = false
+++
This is the content for the **first** section.

{{< p >}}
I am _emphasized_ text.
{{< /p >}}

[SECTION-BREAK]

This is the content for the **second** section.

layouts/_default/single.html

{{ define "main" }}
  <h1>{{ .Title }}</h1>

  {{ $sectionBreak := "<p>[SECTION-BREAK]</p>" }}
  {{ $sections := split .Content $sectionBreak }}
  
  {{ if eq (len $sections) 1 }}
    {{ .Content }}
  {{ else }}
    {{ range $k, $v := $sections }}
      {{ $id := printf "section-%d" (add 1 $k) }}
      <div id="{{ $id }}">
        {{ $v | strings.TrimSpace | safeHTML }}
      </div>
    {{ end }}
  {{ end }}

{{ end }}

And the shortcode for testing (layouts/shortcodes/p.html)

<p>{{ .Inner | .Page.RenderString }}</p>

In the original solution I used .RawContent so that we could use an HTML comment to split the sections, without having to set markup.goldmark.renderer.unsafe to true in the site configuration file. So I let the choice of delimiter dictate the approach.

In this revision, we’ve changed the delimiter to something that is not recognized as HTML, so we can use .Content (fully rendered) instead of .RawContent.

1 Like