Crossreferences to index-pages in section not working

I tried to solve this by setting a specific shortcode for relref to override the default one, based off of an example mentioned in How to link to section pages with ref/relref shortcodes?. See the code and location to put it here: https://github.com/vjeantet/hugo-theme-docdock/blob/master/layouts/shortcodes/relref.html. However, this has issues in a number of cases where it doesn’t work and prints the error string into the template:

{{- if in (.Get 0) "/_index.md" -}}
    {{- $path := (strings.TrimSuffix "_index.md" (.Get 0)) -}}
    {{- with .Site.GetPage "section" (trim $path "/") -}}
        {{- .URL -}}
    {{- else -}}
        {{- printf "MUST-DEFINE-TOP-LEVEL-SECTION" -}}
    {{- end -}}
{{- else -}}
    {{- .Get 0 | relref .Page -}}
{{- end -}}