{{ $currRellink := substr .RelPermalink 0 }}
{{ $currRellink := printf `"%s"` $currRellink }} <!-- adds quotes around outside to prevent finding common words as links like about -->
{{ $currContent := .Content }}
{{ $backlinks := slice }}
{{ $forwardlinks := slice }}
{{ $pages := .Site.RegularPages}}
{{ range $pages }}
{{ $currPagelink := substr .RelPermalink 0 }} <!-- Get link of page -->
{{ $found := findRE $currRellink .Content 1 }}
{{ $foundCourses := findRE `courses` $currPagelink }}<!-- Check if link of page has "courses "-->
{{ if and (not $foundCourses) $found}} <!-- only append if backlink doesn't come from courses -->
{{ $backlinks = $backlinks | append . }}
{{ else }}
{{ $rellink := substr .RelPermalink 0 }}
{{ $rellink := printf `"%s"` $rellink }} <!-- adds quotes around outside to prevent finding common links like about -->
{{ $found = findRE $rellink $currContent 1 }}
{{ if $found }}
{{ $forwardlinks = $forwardlinks | append . }}
{{ end }}
{{ end }}
{{ end }}
{{ $related := append $backlinks $forwardlinks }}
<!-- Modified from https://brainbaking.com/post/2022/04/true-backlink-support-in-hugo/ -->
<hr>
{{ if gt (len $backlinks) 0 }}
<div class="bl-section">
<h4>Links to this note</h4>
<div class="backlinks">
<ul>
{{ range $backlinks | first 10}}
<li>
<a href="{{ .RelPermalink }}">{{ .Title }}</a>
</li>
{{ end }}
</ul>
</div>
</div>
{{ else }}
<div class="bl-section">
<h4 class="small text-muted font-weight-light">No notes link to this note</h4>
</div>
{{ end }}
<hr>
{{ if gt (len $forwardlinks) 0 }}
<div class="bl-section">
<h4>This note links to</h4>
<div class="backlinks">
<ul>
{{ range $forwardlinks | first 10}}
<li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
{{ end }}
</ul>
</div>
</div>
{{ else }}
<div class="bl-section">
<h4 class="small text-muted font-weight-light">This note does not link to other notes</h4>
</div>
{{ end }}
It give this error code sometimes when building on GitHub Pages but not always. It also stopped making any backlinks, although it did before.
Error: error building site: render: failed to render pages: render of "page" failed: "/home/runner/work/whatmakeart.github.io/whatmakeart.github.io/themes/basicTheme/layouts/_default/single.html:68:15": execute of template failed: template: _default/single.html:68:15: executing "main" at <partial "backlinks.html" .>: error calling partial: partial "backlinks.html" timed out after 30s. This is most likely due to infinite recursion. If this is just a slow template, you can try to increase the 'timeout' config setting.
Is there something obvious that is causing the infinite recursion? I am not sure why it is not listing any forward or backlinks now.
I think it is likely due to the content includes I have in the render-image.html markdown render hook, although I tried to not parse the pages in the /courses/ directory and it still didn’t work. It could also be the code in the render-link.html that tries to search for links in a included markdown page so they work.
It turns out my render-link.html and my orginal modification of Wouter Groeneveld’s code was the problem.
The links produced from my render-link.html do not have a trailing / even though I tried to add a trailing / with {{ $link = path.Join $link "/" }} after using {{ $link = path.Clean $link }}
The original code from Wouter already had the solution that I previously removed to get working with my links at the time. The original backlinks code used substr to remove trailing / from the link search with {{ $currRellink := substr .RelPermalink 0 -1 }}
I added that back in and now the backlinks are working. The forward links are still not working but it is likely a similar fix.
Current backlinks working partial
{{ $currRellink := substr .RelPermalink 0 -1 }}<!-- needed for removing the trailing slash for search unless it can be added back in with path.Join -->
{{ $currRellink := printf `"%s"` $currRellink }} <!-- adds quotes around outside to prevent finding common words as links like about -->
{{ $currContent := .Content }}
{{ $backlinks := slice }}
{{ $forwardlinks := slice }}
{{ $pages := where .Site.RegularPages ".Section" "not in" (slice "courses" ) }}<!-- regular non list pages not in section courses -->
{{ range $pages }}
{{ $found := findRE $currRellink .Content 1 }}
{{ if $found}} <!-- only append if backlink doesn't come from courses -->
{{ $backlinks = $backlinks | append . }}
{{ else }}
{{ $rellink := substr .RelPermalink 0 -1}}<!-- needed for removing the trailing slash for search unless it can be added back in with path.Join -->
{{ $rellink := printf `"%s"` $rellink }} <!-- adds quotes around outside to prevent finding common links like about -->
{{ $found = findRE $rellink $currContent 1 }}
{{ if $found }}
{{ $forwardlinks = $forwardlinks | append . }}
{{ end }}
{{ end }}
{{ end }}
<!-- Modified from https://brainbaking.com/post/2022/04/true-backlink-support-in-hugo/ -->
<hr>
{{ if gt (len $backlinks) 0 }}
<div class="bl-section">
<h4>Links to this note</h4>
<div class="backlinks">
<ul>
{{ range $backlinks | first 10}}
<li>
<a href="{{ .RelPermalink }}">{{ .Title }}</a>
</li>
{{ end }}
</ul>
</div>
</div>
{{ else }}
<div class="bl-section">
<h4 class="small text-muted font-weight-light">No notes link to this note</h4>
</div>
{{ end }}
<hr>
{{ if gt (len $forwardlinks) 0 }}
<div class="bl-section">
<h4>This note links to</h4>
<div class="backlinks">
<ul>
{{ range $forwardlinks | first 10}}
<li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
{{ end }}
</ul>
</div>
</div>
{{ else }}
<div class="bl-section">
<h4 class="small text-muted font-weight-light">This note does not link to other notes</h4>
</div>
{{ end }}
The forward links work on some pages but not others. This page has forward links and backlinks but this page should as well but only has backlinks.
---- Solution ----
The original code had an {{ else }} after the first if, I assume this want to prevent duplicate forward and backlinks but I wanted to get all the forward links as well even if they were also backlinks. Changing this to an {{ end }} and having a second if did the trick.
Current final Backlinks Partial
{{ $currRellink := substr .RelPermalink 0 -1 }}<!-- needed for removing the trailing slash for search unless it can be added back in with path.Join -->
{{ $currRellink := printf `"%s"` $currRellink }} <!-- adds quotes around outside to prevent finding common words as links like about -->
{{ $currContent := .Content }}
{{ $backlinks := slice }}
{{ $forwardlinks := slice }}
{{ $pages := where .Site.RegularPages ".Section" "not in" (slice "courses" ) }}<!-- regular non list pages not in section courses -->
{{ range $pages }}
{{ $found := findRE $currRellink .Content 1 }}
{{ if $found}} <!-- only append if backlink doesn't come from courses -->
{{ $backlinks = $backlinks | append . }}
{{ end }} <!-- needs to be an end instead of an else to also get all forward links -->
{{ $rellink := substr .RelPermalink 0 -1}}<!-- needed for removing the trailing slash for search unless it can be added back in with path.Join -->
{{ $rellink := printf `"%s"` $rellink }} <!-- adds quotes around outside to prevent finding common links like about -->
{{ $found := findRE $rellink $currContent 1 }}
{{ if $found }}
{{ $forwardlinks = $forwardlinks | append . }}
{{ end }}
{{ end }}
<!-- Modified from https://brainbaking.com/post/2022/04/true-backlink-support-in-hugo/ -->
<hr>
{{ if gt (len $backlinks) 0 }}
<div class="bl-section">
<h4>Links to this note</h4>
<div class="backlinks">
<ul>
{{ range $backlinks | first 10}}
<li>
<a href="{{ .RelPermalink }}">{{ .Title }}</a>
</li>
{{ end }}
</ul>
</div>
</div>
{{ else }}
<div class="bl-section">
<h4 class="small text-muted font-weight-light">No notes link to this note</h4>
</div>
{{ end }}
<hr>
{{ if gt (len $forwardlinks) 0 }}
<div class="bl-section">
<h4>This note links to</h4>
<div class="backlinks">
<ul>
{{ range $forwardlinks | first 10}}
<li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
{{ end }}
</ul>
</div>
</div>
{{ else }}
<div class="bl-section">
<h4 class="small text-muted font-weight-light">This note does not link to other notes</h4>
</div>
{{ end }}