Use getJSON on all links in .md and return an array

The task is to use getJSON on every link in content files (.md) (In content itself, not frontmatter) and get an array of results at the end

For now, I can:

  • use _markup/render-link.html approach to travel through every link, but I can only modify links, not collect them all
  • use getJSON on a single custom link

Now I need to understand how to connect these approaches to get an array of values at the end of the post.

P.S. In case you wondering, the goal is to build a Google remarketing snippet that requires an array of ID’s. stored in remote website headers

P.P.S I apologize for my English and not very elegant explanation

This example uses the markdown render hook to display and collect anchors:

layouts/_default/_markup/render-link.html

<a href="{{ .Destination }}" {{- with .Title }} title="{{ . }}" {{- end }}>{{ .Text | safeHTML }}</a>
{{ $anchor := dict "href" .Destination "content" (.Text | safeHTML) "title" .Title }}
{{ .Page.Store.SetInMap "anchors" (now.UnixNano | string) $anchor }}

layouts/_default/single.html

{{ $noop := .Content }}
{{ with .Store.GetSortedMapValues "anchors" }}
  <pre>{{ jsonify (dict "indent" "  ") . }}</pre>
{{ end }}
1 Like

@jmooring This actually helped a lot, many thanks.

Unfortunately, I completely forgot that the server on the other end would not allow 2k+ requests in 1806 ms :sweat_smile:

Maybe you can run your query offline, storing the results in data as either a single file or multiple files, then query with site.Data.foo when building the site.

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