Hi, I noticed that I use .GetPage too much when really “ref” suffices, as it takes care of anchors automatically.
But in this piece of code in render-link.html it fails.
{{- if $isRemote }}
{{if not (strings.HasPrefix $destination "http") }}
{{$destination = print "https://" $destination}}
{{end}}
{{ .Page.Store.Add "all_sources" (slice .Destination (plainify .Text))}}
{{- else }}
{{- with ref $.Page . }}
{{- $destination = .}}
{{- else -}}
{{ with resources.Get .Destination }}
{{ $destination = .Permalink}}
{{ end}}
{{- end }}
{{- end }}
I have this error message:
execute of template failed: template: _default/_markup/render-link.html:19:11: executing “_default/_markup/render-link.html” at <ref $.Page .>: error calling ref: unable to cast goldmark.linkContext{page:(*hugolib.pageForRenderHooks)(0xc0013f6cc0), destination:“History”, title:“”, text:“allegued”, plainText:“allegued”, AttributesHolder:(*attributes.AttributesHolder)(0x3ba8ca0)} of type goldmark.linkContext to string
But it’s the same ref $.Page . syntax that I used elsewhere with success to get a Permalink with an anchor (or not).
{{- $destination := .Destination }}
{{ if eq $destination “ADD_REF” }}
{{- warnf “Missing reference for link %s in page %s” (.Text|htmlUnescape) $destination .Page.Path }}
{{.Text}}
{{ else if eq $destination “#”}} {{with (.Text|htmlUnescape) }}{{if eq . “…”}}{{.}}{{else}}[ - {{.}} - ]{{end}}{{end}}
{{else if eq $destination “DFN”}}
<dfn {{with .Title}}alt=“{{.}}”{{end}}>{{.Text}}
{{else if eq $destination “ABBR”}}
{{.Text}}
{{else}}
{{- $isRemote := or (strings.Contains $destination “://”) (strings.HasPrefix $destination “www”) }}
{{- if $isRemote }}
{{if not (strings.HasPrefix $destination “http”) }}
{{$destination = print “https://” $destination}}
{{end}}
{{ .Page.Store.Add “all_sources” (slice .Destination (plainify .Text))}}
{{- else }}
{{- with ref $.Page . }}
{{- $destination = .}}
{{- else -}}
{{ with resources.Get .Destination }}
{{ $destination = .Permalink}}
{{ end}}
{{- end }}
{{- end }}
<a href=“{{ $destination }}”{{ with .Title}} title=“{{ . }}”{{ end }}{{if $isRemote}} rel=“nofollow external” target=“_blank”{{ end }}>{{.Text}}{{end -}}
As you suggested, I reversed the arguments, and this is the result:
ERROR 2023/05/31 23:23:11 Rebuild failed: “/home/drm/WEBSITE/content/docs/Love/spirituality.md:1:1”: “/home/drm/WEBSITE/layouts/_default/_markup/render-link.html:19:11”: execute of template failed at <ref . $.Page>: error calling ref: invalid Page received in Ref
Total in 115 ms
Ok I see what you mean.
it should be something like {{ref $.Page .Destination}}, but it wrecks linking resources completely.
How would you simplify the following by using “ref” ?
I have very strong opinions about how a link render hook should function, and am not terribly interested in troubleshooting other approaches. This is the drop-in I use everywhere. Take the best, leave the rest.
Ahahah. Ok, I’ll see about it, if I manage to understand that code, because it’s by far the worst (most complex) I ever read, despite the commentaries.
ah sorry I didn’t mean to be disparaging. it’s certainly valid and efficient.
I just don’t want to insert something I don’t understand, as it’s usually a recipe for disaster down the line. Until I digested it, I’ll keep to the saying “if it’s broken, don’t fix it”