# Link validation inside render-link hook?

**URL:** <https://discourse.gohugo.io/t/link-validation-inside-render-link-hook/44071>\
**Category:** support\
**Created:** [April 25, 2023, 2:30pm UTC](https://discourse.gohugo.io/t/link-validation-inside-render-link-hook/44071 "2023-04-25T14:30:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lbliii](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/lbliii/32/9902_2.png) [@lbliii](https://discourse.gohugo.io/u/lbliii)\
**Post date:** [April 25, 2023, 2:30pm UTC](https://discourse.gohugo.io/t/link-validation-inside-render-link-hook/44071/1 "2023-04-25T14:30:13Z")

</div>

I’m trying to come up with a way to get some link validation without needing a site crawler or anything and I thought it’d be possible to get Hugo to take the .Destination and use it to do one of these:

- .GetPage
- .ReadFile
- .FileExists

etc but I’m not having much luck.

I think what I want is for it to simply fail and give me the page it fails on if the link’s destination doesn’t exist. the Relref/ref functionality seems to do that somewhat, but I ran into some weird issues where it would only find top-level pages and broke if I tried to go deeper.

I think because a lot of my subpages use relative links, the fileExists solution won’t work.

Has anyone else been able to build some sort of link validation into this hook?

```auto
{{/* Convert destination to string */}}

{{$x := printf "\"%s.md\"" .Destination }}

{{/* Check if site pages exist */}}

{{ if strings.HasPrefix .Destination "http" }} 
{{else if strings.HasPrefix .Destination "https" }}
{{else}}
{{os.FileExists $x }}

{{/* Maybe print in terminal if possible? or idk. */}}

{{end}}

<a href="{{ .Destination | safeURL }}"{{ with .Title }} title="{{ . }}"{{ end }}{{ if strings.HasPrefix .Destination "http" }} target="_blank" rel="noopener"{{ end }}>{{ .Text | safeHTML }}</a>

```

also, sidenote: is it just me or is it actually pretty frustrating to validate links in general? It seems like most solutions don’t understand how hugo links work. For example in VS Code, whenever I click a simple relative link on my markdown pages I get the error “The editor could not be opened because the file was not found.” – not sure if this is a setting I can change, but it’s been a pain for a while. I’ve even had some link checkers give me bad advice and tell me links were broken that weren’t.

---

<div class="post-metadata">

**Author:** ![fekete-robert](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/fekete-robert/32/13363_2.png) [@fekete-robert](https://discourse.gohugo.io/u/fekete-robert)\
**Post date:** [April 25, 2023, 3:40pm UTC](https://discourse.gohugo.io/t/link-validation-inside-render-link-hook/44071/2 "2023-04-25T15:40:09Z")

</div>

Link validation is a pain indeed. Regarding the VS Code issue, there is a plugin called Open File that uses some search patterns to look for the file at the cursor when you press a keyboard shortcut, and it usually finds the file in my projects - that helps a lot to me.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [April 25, 2023, 6:09pm UTC](https://discourse.gohugo.io/t/link-validation-inside-render-link-hook/44071/3 "2023-04-25T18:09:06Z")

</div>

At a high level, your question is:

> How do I use render hooks to **validate** markdown link and image destinations?

Let’s reframe the question:

> How do I use render hooks to **accurately resolve** markdown link and image destinations?

If we focus on **resolution** , we get **validation** for free. [More reading…](https://www.veriphor.com/articles/link-and-image-render-hooks/)

---

<div class="post-metadata">

**Author:** ![lbliii](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/lbliii/32/9902_2.png) [@lbliii](https://discourse.gohugo.io/u/lbliii)\
**Post date:** [April 25, 2023, 9:45pm UTC](https://discourse.gohugo.io/t/link-validation-inside-render-link-hook/44071/4 "2023-04-25T21:45:06Z")

</div>

This is AMAZING and is exactly what I needed. Thank you!! I definitely wouldn’t have been able to figure this out on my own, but I’m going to study this solution and learn from it. 🙂

---

<div class="post-metadata">

**Author:** ![jeremie](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jeremie](https://discourse.gohugo.io/u/jeremie)\
**Post date:** [May 25, 2023, 8:51pm UTC](https://discourse.gohugo.io/t/link-validation-inside-render-link-hook/44071/5 "2023-05-25T20:51:14Z")

</div>

@jmooring, thanks for the great article!

At the end, you indicate

> To perform any of these operations, you can “hook” into this render hook with a partial template, after the render hook has captured the resource.

Would you mind providing an example. I am having a hard time figuring out how to do that. Here is my render-image for now. How do I include this in a partial template and where?

```auto
{{ if .Title }}
  <figure>
    <img src="{{ .Destination | safeURL }}" alt="{{ .Text }}">
    <figcaption>{{ .Title }}</figcaption>
  </figure>
{{ else }}
  <img src="{{ .Destination | safeURL }}" alt="{{ .Text }}">
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [May 25, 2023, 10:27pm UTC](https://discourse.gohugo.io/t/link-validation-inside-render-link-hook/44071/6 "2023-05-25T22:27:58Z")

</div>

This is a simple example, where we call an image processing partial that resizes and converts to WebP.

```plaintext
git clone --single-branch -b hugo-forum-topic-44071 https://github.com/jmooring/hugo-testing hugo-forum-topic-44071
cd hugo-forum-topic-44071
hugo server

```

Files of interest:

- layouts/\_default/\_markup/render-image.html
- layouts/partials/process-image.html

We take the attributes and the resource for a round trip between the two.

You could also take a one-way trip from the hook to the partial, and do the rendering in the partial. That’s the approach to use if you’re going to generate multiple image sizes, a picture element, etc.

---

<div class="post-metadata">

**Author:** ![lbliii](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/lbliii/32/9902_2.png) [@lbliii](https://discourse.gohugo.io/u/lbliii)\
**Post date:** [February 16, 2024, 8:38pm UTC](https://discourse.gohugo.io/t/link-validation-inside-render-link-hook/44071/7 "2024-02-16T20:38:34Z")

</div>

Sorry to revive an old thread, but do you happen to have any guidance on how I could extend this to handle api/sdk reference docs that would sit under `/assets/references` or `static/references`?

I noticed the `resources.Get` section throws an error that it can’t handle directories. Currently links to say `/references/some-package/client.html#some-thing` return a warning and get very noisy unless I filter them out.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 16, 2024, 9:14pm UTC](https://discourse.gohugo.io/t/link-validation-inside-render-link-hook/44071/8 "2024-02-16T21:14:13Z")

</div>

This [link render hook](https://www.veriphor.com/articles/link-and-image-render-hooks/#link-render-hook) can resolve links to global resources (files in the assets directory), but cannot validate fragments within those resources. Fragment validation is only possible within content pages.

I suppose you could try to do it with some ugly regex stuff but… yuck.
