Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print warning if system can't resolve link to a markdown file #691

Merged
merged 1 commit into from
Nov 26, 2023

Conversation

stereobooster
Copy link
Contributor

Small improvement:

if user uses link, like [post](/content/posts/other-post.md) and this file doesn't exist (e.g. broken link), Hugo will print a warning. Which is nice way to prevent 404 errors

Copy link

netlify bot commented Oct 31, 2023

Deploy Preview for hugo-congo ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit e70c968
🔍 Latest deploy log https://app.netlify.com/sites/hugo-congo/deploys/654123e994be76000892fdc8
😎 Deploy Preview https://deploy-preview-691--hugo-congo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@wolfspyre
Copy link
Contributor

wolfspyre commented Oct 31, 2023

wouldn't also emitting the page this occurs at by using .Page.Path:
{{- warnf "DEBUG We are disabled, but were invoked by %s " .Page.Path -}}

be helpful?

also, might wanna have this be a toggle like: (totes written off the cuff and prolly wont work as is)

{{- $.Scratch.Set "linkLiveness"  "" }}
{{- $.Scratch.Set "deadLinkErr"  false }}
{{- $linkLiveness := (.Site.Params.Warnings.LinkLiveness | default false)  }}
{{- if not (in (slice "warn" "error" "disabled")) $linkLiveness }}
{{-   $linkLiveness = "disabled" }}
{{- end }}
{{- $.Scratch.Set "linkLiveness" $linkLiveness }}

... pseudocode that I'm sure won't work

{{- with {dead link path} }}
{{-    $.Scratch.Set "deadLinkErr"  (printf "dead link on page %s to %s" .Page.Path .) }}
{{- end }}
...
{{- if and   $.Scratch.Get "linkLiveness" (eq ($.Scratch.Get "linkLiveness") "error" ) -}}
{{-   errorf "%s" $.Scratch.Get "deadLinkErr" -}}
{{- else if and   $.Scratch.Get "linkLiveness" (eq ($.Scratch.Get "linkLiveness") "warn" ) -}}
{{-  /* warn dont fail */ -}}
{{-   warnf "%s" $.Scratch.Get "deadLinkErr" -}}
{{- else  -}}
...
{{- end  -}}

or you could use the errorf variant that has an excludable errnumber mechanism that allows the user to ignore errors of type _ ....

@jpanther
Copy link
Owner

Looking at the code for this warning, doesn't this prevent any links to *.md URLs? What happens if the user is wanting to link to a Markdown file?

@stereobooster
Copy link
Contributor Author

stereobooster commented Oct 31, 2023

Looking at the code for this warning, doesn't this prevent any links to *.md URLs? What happens if the user is wanting to link to a Markdown file?

Yes it will print warning too. It would be something like this:

/posts/slug-with.md

e.g. if slug ends with .md. Not sure this can be fixed somehow. Related gohugoio/hugo#10585

PS it will not print warning for external links, like

https://github.com/jpanther/congo/blob/dev/README.md

@jpanther jpanther added the enhancement New feature or request label Nov 25, 2023
@jpanther
Copy link
Owner

The only that's stopping me from merging this is that there may be edge cases that will be triggered here that we're missing. And I wonder if throwing warnings when people want to link to Markdown files directly is an issue. I'm not sure if this is a prevalent use case or not.

@stereobooster
Copy link
Contributor Author

stereobooster commented Nov 25, 2023

Hard to tell. It will break nothing. People will see some warnings which doesn't stop them from publishing it as is anyway. We can implement it and see if somebody complains or gets confused by it.

Other way would be to add config which would disable this check in case somebody gets a lot of false warnings.

@wolfspyre
Copy link
Contributor

wolfspyre commented Nov 25, 2023

if that’s your only reservation, perhaps changing the verbiage of the warning to indicate a potential red herring or a reference to this PR with a request to comment if an edge case pops up, and leave it as a potential nit for FutureUs™️? and then fix forward if/when it bites us in the butt as we’ll have a better sense of what edge case arose at that point?

@jpanther
Copy link
Owner

I'm probably overthinking this one. I'll merge it in and we can address any issues that arise if and as they appear.

@jpanther jpanther merged commit 041fc30 into jpanther:dev Nov 26, 2023
@stereobooster stereobooster deleted the portable-links-2 branch November 26, 2023 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants