-
-
Save softplus/8d4f09264cc3f579e11e91e4eb0234c4 to your computer and use it in GitHub Desktop.
Nofollow link rendering + archive.org integration for Hugo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{- $linkurl := .Destination }} | |
{{- $relval := ""}} | |
{{- $targetval := ""}} | |
{{- $classval := "" }} | |
{{- if strings.HasPrefix $linkurl "http" }} | |
{{- $targetval = printf "%s %s" $targetval "_blank" }} | |
{{- $relval = printf "%s %s" $relval "noopener" }} | |
{{- $classval = printf "%s %s" $classval "external" }} | |
{{- end -}} | |
{{- if strings.HasSuffix $linkurl "#?" }} | |
{{- $relval = printf "%s %s" $relval "nofollow" }} | |
{{- $urlparts := split $linkurl "#" -}} | |
{{- $linkurl = index $urlparts 0}} | |
{{- end -}} | |
<a href="{{ $linkurl | safeURL }}" | |
{{- with .Title}} title="{{ trim . " " }}"{{ end -}} | |
{{- with $relval}} rel="{{ trim . " " }}"{{ end -}} | |
{{- with $targetval}} target="{{ trim . " " }}"{{ end -}} | |
{{- with $classval}} class="{{ trim . " " }}"{{ end -}} | |
>{{ .Text | safeHTML }}</a> | |
{{- if strings.HasPrefix $linkurl "http" }} | |
{{- if gt (sub now.Year .Page.Date.Year) 5 }} | |
{{- $relval = printf "%s %s" $relval "nofollow" }} | |
(<a href="https://web.archive.org/web/{{ .Page.Date.Year }}*/{{ $linkurl }}" | |
{{- with $relval}} rel="{{ trim . " " }}"{{ end -}} | |
{{- with $targetval}} target="{{ trim . " " }}"{{ end -}} | |
{{- with $classval}} class="{{ trim . " " }}"{{ end -}} | |
>archive.org</a>) | |
{{- end -}} | |
{{- end -}} |
Author
softplus
commented
Apr 23, 2021
- Links like http://example.com/page#? are rendered with rel=nofollow
- External links are class=external, target=_blank, rel=noopener
- Links on posts older than 5 years additionally get an archive.org link for external links
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment