Nope. I was looking for a solution that would work for any random link (plug-and-play like shortcodes). e.g. If I add an external link in my post I should be able to add rel=“external” attribute to the along with the href while other links in the same post don’t get the same attribute.
So, {{< link "foobar.com" "external" "external link" >}} renders the correct a tag for this specific link.
Is there any other way to achieve the same thing without cluttering the post content too much?
And if I have to stick with this, is there any way to shorten this thing? Like may be declare a variable in the beginning of the content and then just enter that in the actual content?
{{/* Split .Title into two parts, title and attributes. */}}
{{- $parts := split . "{" -}}
{{- $parts = apply $parts "trim" "." " " -}}
{{- $parts = apply $parts "trim" "." "}" -}}
{{/* Extract title into a string. */}}
{{- $title := index $parts 0 -}}
{{/* Extract attributes into a dictionary. */}}
{{- $temp := index $parts 1 -}}
{{- $temp = split $temp "'" -}}
{{- $temp = first (sub (len $temp) 1) $temp -}}
{{- $temp = apply $temp "replace" "." "=" "" -}}
{{- $temp = apply $temp "trim" "." " " -}}
{{- $attributes := dict -}}
{{- if $temp -}}
{{- range (seq 0 2 (sub (len $temp) 1)) -}}
{{- $attributes = merge $attributes (dict (index $temp . ) (index $temp (add 1 .))) -}}
{{- end -}}
{{- end -}}
{{- $parsedTitle := dict "title" $title "attributes" $attributes -}}
{{- return $parsedTitle -}}
This works with all of these:
[Hugo Forum](https://discourse.gohugo.io/ "The Hugo Forum {rel='nofollow' foo='bar'}")
[Hugo Forum](https://discourse.gohugo.io/ "The Hugo Forum {rel='nofollow'}")
[Hugo Forum](https://discourse.gohugo.io/ "{rel='nofollow'}")
[Hugo Forum](https://discourse.gohugo.io/ "Hello World")
[Hugo Forum](https://discourse.gohugo.io/ " ")
[Hugo Forum](https://discourse.gohugo.io/ "")
[Hugo Forum](https://discourse.gohugo.io/)
Because I am abusing an HTML attribute, overloading it with stuff it shouldn’t contain. Ideally we would be able to add attributes to links and images in the same way that we can attributes to headings, code fences, and blocks.
When we did this https://github.com/gohugoio/hugo/issues/8270, the implementation was limited to headings. Doing the same thing for image and links is complicated. This is somewhat related: