Markdown Reference Links in Shortcodes

I have a shortcode named ‘q.html’ that looks like this:

 <blockquote>
 {{ .Inner }}
 </blockquote>

and the markdown content is:

[test][testlink]
{{%q%}}This is a [test][testlink].{{%/q%}}

[testlink]: https://google.com 

It renders incorrectly as

<p><a href="https://google.com">test</a></p>
<blockquote>
This is a [test][testlink].
</blockquote>

If I change the shortcode to:

<blockquote>
{{ markdownify (delimit (slice .Inner "[testlink]: https://google.com") "\n") }}
</blockquote>

Then it renders correctly:

<p><a href="https://google.com">test</a></p>
<blockquote>
This is a <a href="https://google.com">test</a>.
</blockquote>

Which seems to indicate that when processing shortcodes reference-style links defined in the content markdown aren’t being passed to the Blackfriday processor.

Is there a way to make the defined reference links available to shortcodes rendered separately by Blackfriday?

The workaround I’m currently looking at is to define the links in the front matter and then use a shortcode to retrieve the link when needed.

Workaround shortcode (link.html):

{{ index .Page.Params.links (.Get 0) }}

Invoking the shortcode:

---
links:
  testlink: http://google.com
---

[test]({{<link testlink>}})
{{%q%}}This is a [test]({{<link testlink>}}).{{%/q%}}

Which renders correctly.

Is there a better workaround (assuming there’s no way to pass the reference links to Blackfriday)?

2 Likes

The shortcodes and the surrounding content share no common (Blackfriday) context, so no way to pass the ref to Blackfriday that I can think of. (and to stop a follow up question: I don’t see how we could easily do that with some code change, either).

Thanks for the quick reply. That was the answer I expected and I agree that it would be a non-trivial bit of work to somehow merge the contexts.

If anyone has other workarounds or approaches to avoid writing a URL multiple times in a way that works with shortcodes I’d appreciate it. My workaround works, but is kind of clunky. Maybe if there was an elegant way to reference the parameters defined in the front matter in the markdown…

This one seems like it’s pretty disastrous. I am using the expand shortcode from the learn theme - https://learn.netlify.com/en/shortcodes/expand/ and all of the markdown reference links break.

Is there a way to change the shortcode so that it passes its contents through Blackfriday, and still has access to the reference links in the parent markdown file? I have files with about 50 links - my other solution will be to just not use shortcodes, which seems like it’s not the point.

With multiple links on one page I use the following syntax:

Markup:

Read here [W3C][] or here [W3C][].

[W3C]: https://w3c.github.io/webappsec/specs/subresourceintegrity "Subresource Integrity"

https://daringfireball.net/projects/markdown/syntax#link