Index and list URLs from content

I actually just implemented something similar in my theme to tell the browser to preload any images in the content:

{{ if .Content -}}
    {{ $urls := findRE "<img src=\"[^\"|\\\"]*\"" .Content -}}
    {{ range $url := $urls -}}
      {{ $url := (strings.TrimPrefix "<img src=\"" $url) -}}
      {{ $url := strings.TrimSuffix "\"" $url -}}
      <link rel="preload" href="{{ $url | htmlUnescape | safeHTML }}" as="image" />
    {{ end -}}
{{ end -}}

If you’re looking for all linked URLs (i.e. all <a> tags), you should be able to use something like:

{{/* This isn't tested */}}
{{ if .Content -}}
    {{ $.Scratch.Set "count" 0 }}
    {{ $urls := findRE "<a href=\"[^\"|\\\"]*\"" .Content -}}
    {{ range $url := $urls -}}
      {{ $.Scratch.Add "count" 1 }}
      {{ $url := (strings.TrimPrefix "<a href=\"" $url) -}}
      {{ $url := strings.TrimSuffix "\"" $url -}}
      [{{ $.Scratch.Get "count" }}]: {{ $url }}
    {{ end -}}
{{ end -}}

Alternatively, you can manually create footnotes in markdown with the following syntax:

This is a footnote.[^1]

[^1]: the footnote text.
1 Like