Loop Through a List of String ID's

All of my notes have a unique identifier, which functions as the note id as well as the filename. For example, the filename might be 20210706211400.md and in the frontmatter of that note, amongst other key/values, there will be an id key/value that matches the filename:

id: 20210706211400

I reference other notes using these ID’s with a so-called Wiki-Link syntax. So in reference to another note I might write [[20210615200150]] and my template will link to that note.

What I am trying to do is find all of the Wiki-Links in a post, loop through them, and read the .Title and/or .Summary. With the aim of then being able to use that information in a tooltip or popover when a Wiki-Link is clicked on. I’ve not had any luck doing so.

Here is what I am doing to make the Wiki-Links work (the popover works just fine, but the content obviously doesn’t yet reflect the actual content of the post the Wiki-Link refers to:

        replaceRE "\\[\\[([0-9]+)\\]\\]" "<a class=\"wikilink\" tabindex=\"0\" role=\"button\" data-trigger=\"focus\" data-content=\"<content placeholder>\" title=\"<title placeholder>\">$1</a>" 

This is what I’m trying to play around with to get the title and summary from the list of ID’s. I’m hoping I can then create some variables from it that I can then pass into the above block.

I don’t know how I can tell Hugo that each string in the below list is a post, and to lookup the values I need from it.

    {{ $entryLinks := findRE "\\[\\[([0-9]+)\\]\\]" .Content }}
    {{ range $entryLinks }}
        <li>{{ . }}</li>
    {{ end }}

This prints out the ID’s, but obviously it’s just a list of strings and Hugo has no idea that there’s a post behind the ID that I want to access.


Any help would be much appreciated, do ask if anything isn’t clear.

With a content structure like this:

└── post/
    ├── 20210623095113.md
    ├── 20210624101730.md
    └── 20210706103710.md

And markdown like this:

Proident duis quis [[20210623095113]] sit in [[20210624101730]] enim 
nostrud velit [[20210706103710]] officia consequat.

You can use this instead of .Content in your templates:

{{ $content := .Content }}
{{ $anchorFormatString := `<a href=%q data-content=%q title=%q class=%q tabindex=%q role=%q data-trigger=%q>%s</a>` }}
{{ $entryLinks := findRE `\[\[(\d+)\]\]` .Content }}
{{ range $entryLinks }}
  {{ $id := trim . "[]" }}
  {{ $page := site.GetPage $id }}
  {{ $anchor := printf $anchorFormatString $page.RelPermalink $page.Summary $page.Title "wikilink" "0" "button" "focus" . }}
  {{ $content = replace $content . $anchor }}
{{ end }}
{{ $content | safeHTML }}

There’s no reason to include an id in the frontmatter.

Thanks very much @jmooring that works really well. Exactly what I was after.

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