Syntax issue with using "ref" instead of (.GetPage ......blablabl).Permalink

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).

I’m not sure what the context (the dot) is, but it looks like you’ve reversed the arguments. See docs.

This is the complete code.

{{- $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

Think about it. What’s the dot? The ref function wants a path. You are passing the entire render hook context.

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” ?

{{- $url := urls.Parse .Destination }}
  {{- $path := strings.TrimSuffix "/_index.md" $url.Path }}
  {{- $path = strings.TrimSuffix "/_index" $path }}
  {{- $path = strings.TrimSuffix ".md" $path }}
  {{- $page := site.GetPage $path }}
  {{- if $page }}
   {{- $destination = $page.Permalink }}
    {{- with $url.Fragment }}
	{{- $destination = print $destination "#" $url.Fragment }}
      {{- end }}
	  {{- else -}}
	{{ with resources.Get .Destination }}
	{{ $destination = .Permalink}}
  {{ end}}
{{- end }}

the syntax of my page links are only ever like this: to access a file named “instincto”, wherever it is in the tree, it [text](instincto).

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.

Perhaps what you meant to say was, “Thank you for sharing your approach with the Hugo community.”

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” :sweat_smile:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.