How to use backticks inside a multiline shortcode parameter

I just made a figure shortcode which can embed a page resource and be called several ways (if I made the syntax awkward, advice is welcome)

{{< figure src="foo.webp" caption="Awesome words" />}}

{{< figure caption="Awesome words" >}}
  {{< resource "foo.webp" >}}
{{< /figure >}}

{{< figure "Awesome words" >}}
  <!-- in case I want to make a sort of "subfigure" setup by nesting -->
  {{< resource "foo.webp" >}}
{{< /figure >}}

{{< figure src="foo.webp" />}} <!-- fallback to .Title of the resource -->

The captions are rendered markdown, and it works great. But sometimes I have a caption that’s long enough to span multiple lines. I also sometimes need to put markdown code blocks inside the caption. I couldn’t see if there was a way to escape backticks inside a multiline string.

{{< figure src="words.webp" caption=`
  My very awesome and long caption. Use
  a \`command spelled precisely like this\`. Need
  another line because this is too long.
`/>}}

This is what I was hoping for, but it doesn’t work and seems to just end the string.

according to this : raw string literal there is no way to include a backtick in a raw (multiline) string.

you could do something like that:

layouts/_shortcodes/backtick.html

{{- $page := .Page -}}
{{ with .Get "caption" }}
   <p>
      {{ $page.RenderString (. | replaceRE `(?s)<backtick>(.*?)</backtick>` "`$1`") }}
   </p>
{{ end }}

Now you may call your shortcode like that:

{{< backtick caption="Hello **world**" >}}

{{< ml caption=`Laborum voluptate <backtick>pariatu</backtick> ex culpa magna nostrud est incididunt fugiat pariatur do dolor ipsum enim. Consequat tempor
do dolor eu. Non id id anim anim excepteur excepteur pariatur nostrud qui irure ullamco.` >}}

{{< backtick caption=`
My very awesome and long caption. Use
a <backtick>command spelled precisely like this</backtick>. Need
another line because this is too long.
` >}}

{{< backtick caption=`
That works also if the <backtick>backtick is not closed
on the same line</backtick> and with multiple instances.My very awesome and long caption. Use
a <backtick>command spelled precisely like this</backtick>. Need
another line because this is too long.
` >}}

which will render as:

   <p>
      Hello <strong>world</strong>
   </p>
   <p>
      Laborum voluptate <code>pariatu</code> ex culpa magna nostrud est incididunt fugiat pariatur do dolor ipsum enim. Consequat tempor
do dolor eu. Non id id anim anim excepteur excepteur pariatur nostrud qui irure ullamco.
   </p>
   <p>
      My very awesome and long caption. Use
a <code>command spelled precisely like this</code>. Need
another line because this is too long.
   </p>
   <p>
      That works also if the <code>backtick is not closed on the same line</code> and with multiple instances.My very awesome and long caption. Use
a <code>command spelled precisely like this</code>. Need
another line because this is too long.
   </p>

Thanks, that works. I ended up just doing this in my figure shortcode since \bt is the easiest and fastest thing to type that I could come up with:

<figure>
    {{ $resource_title := "" }}
    {{ with .Get "src" }}
        {{ with $.Page.Resources.Get . }}
            {{ $resource_title = .Title }}
            {{ partial "embed-resource.html" . }}
        {{ else }}
            {{ errorf "resource %q not found" . }}
        {{ end }}
    {{ else }}
        {{ .Inner }}
    {{ end }}

    {{ with or (.Get "caption") (.Get 0) $resource_title }}
        <figcaption>{{ (replace . "\\bt" "`") | markdownify }}</figcaption>
    {{ end }}
</figure>

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