Exclude internal broken links from {{.Content}}

Each month, I send out a newsletter which I also republish on my website. The newsletter contains internal and external links. I have several years worth of newsletters published.

With time, several of the internal links have been removed, but the newsletter content hasn’t been updated to reflect this.

Is there some logic I can use so that {{.Content}} excludes the internal links that don’t exist?

content > newsletters

*[Link title](/internal-link/)
*[Link title](/internal-link/)
*[Link title](/internal-link/)
layout > newsletters

{{.Content}}

Sure. Use a link render hook, and render nothing if .Page.GetPage returns nil.

I’ve tried the following:

create a render link:

layout > newsletters > _markup > render-link.html

which looks like this:

{{if .Page.GetPage nil}}
  <a href="{{.Destination | safeURL}}">{{- .Text | safeHTML -}}</a>
{{end}}

But I get the following error message: "cannot assign nil to string".

Can you provide any further pointers, please?

Start with Hugo’s embedded link render hook (source code here), then add logic to skip rendering (and warn) if the page doesn’t exist.

GetPage expects a string parameter, which you don’t pass

Alright, thanks. Still struggling here, but I’ll figure it out.

Here’s an example:

git clone --single-branch -b hugo-forum-topic-49503 https://github.com/jmooring/hugo-testing hugo-forum-topic-49503
cd hugo-forum-topic-49503
hugo server
1 Like

Your code works, thank you.

I haven’t marked this as solved yet, as I’d please like to ask a follow-up question.

It’s taken me time to figure out, but the reason I couldn’t get my attempts or your code to work initially is because of how I’ve structured the links in my post.

Each product page in it’s front matter has a params url that looks like this: "/category/product-name/". Category can have several values (ex. /electronics/ , /music/ etc).

In my newsletter posts I reference each product as:

- [Product Name](/category/product-name/)

The problem I’ve found is that for the code to work I need to modify the links and replace /category/ with the section products, like so:

- [Product Name](/products/product-name/)

Any attempt to do some form of URL manipulation with $u generates an error such as the object becomes a string. I also can’t use .Section or something like this as it’s a render-link. Please correct me if I’m wrong as I’m new to this.

An easy fix is to modify each link in my newsletter posts, but I thought I would ask if there’s a better way.

In order to “get” a page as we do in the render hook, you have to pass .Page.GetPage a logical path. For example:

Kind File path Logical path
page content/foo/bar.md /foo/bar
page content/foo/bar/index.md /foo/bar
section content/foo/bar/_index.md /foo/bar

Your example is the very reason why a render hook like this is preferred. Regardless of what you do to the url or slug in front matter, or what you do with permalinks in site configuration, or whether your baseURL includes a subdirectory, this will always resolve the logical path to the correct URL.

1 Like

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