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?
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
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.