I am trying to use the link render hook, specifically this nice implementation. It works great for the most part, very useful stuff, but it stumbles upon links that don’t follow the folder structure inside content
.
I might be not phrasing that in correct terms, so here’s an example minimal project, just in case.
The content folder structure is:
./content/
├── about
│ └── index.md
└── posts
├── my-first-post
│ └── index.md
└── my-second-post
└── index.md
The permalinks format in the config is:
[permalinks]
posts = "/posts/:year/:month/:day/:filename/"
And here comes the problem. In this Markdown source of an article:
Testing links:
- matches the `content` folder structure: [/posts/my-first-post](/posts/my-first-post)
- matches the custom `permalinks` structure: [/posts/2024/11/07/my-first-post](/posts/2024/11/07/my-first-post)
both links are rendered with a correct href
value, so I can click on any of them, and the correct page will open. But the link render hook will print a warning/error for the second link:
WARN The link render hook was unable to resolve the destination "/posts/2024/11/07/my-first-post" in posts/my-second-post/index.md
and also that link’s <a>
element will get appended with broken
CSS class (in development
environment):
So I figured I need to extend the link render hook code, as it apparently doesn’t cover a scenario of custom permalinks, but there I encountered a problem that the GetPage
call does not find the page if it is passed in a custom permalink format:
/* this one doesn't find it */
site.GetPage "/posts/2024/11/07/my-first-post"
/* and this one does find it, but I need to use the permalink format */
/* site.GetPage "/posts/my-first-post" */
So then my question: is it possible to verify that a linked page exists, using its custom permanent link format, like the one I have in this example? It doesn’t have to be with GetPage
, I’ll take any other option that would work inside the link render hook.