github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/docs/themes/hugo-theme-relearn/layouts/partials/shortcodes/link.html (about) 1 {{- $page := .page }} 2 {{- if and (not $page) .context }} 3 {{- $page = .context }} 4 {{- warnf "%q: DEPRECATED parameter 'context' for shortcode 'link' found, use 'page' instead; see https://mcshelby.github.io/hugo-theme-relearn/basics/migration#5180" $page.File.Filename }} 5 {{- end }} 6 {{- $url := .url }} 7 {{- if strings.HasPrefix $url "HAHAHUGOSHORTCODE" }} 8 {{- warnf "%q: WARNING you must call the ref / relref shortcode with '%% %%' instead of '< >' to work correctly for the anchor target attribute" $page.File.Filename }} 9 {{- end }} 10 {{- $attributes := dict }} 11 {{- $title := .title | default "" }} 12 {{- $title = trim $title " " }} 13 {{- $attributes = $attributes | merge (dict "title" $title) }} 14 {{- $content := .content }} 15 {{- $target := .target | default "" }} 16 {{- $u := urls.Parse $url }} 17 {{- $href := $u.String }} 18 {{- $path := $u.Path }} 19 {{- if $u.IsAbs }} 20 {{- $attributes = merge $attributes (dict "rel" "external") }} 21 {{- $target = "_blank" }} 22 {{- if isset $page.Site.Params "externallinktarget" }} 23 {{- $target = $page.Site.Params.externalLinkTarget }} 24 {{- end }} 25 {{- $attributes = $attributes | merge (dict "target" $target) }} 26 {{- else }} 27 {{- $linkPage := "" }} 28 {{- if $path }} 29 {{- with or 30 ($page.Page.GetPage $path) 31 ($page.Page.GetPage (strings.TrimRight "/" $path)) 32 ($page.Page.Resources.Get $path) 33 (and (ne $page.Page.BundleType "leaf") ($page.Page.CurrentSection.Resources.Get $path)) 34 (resources.Get $path) 35 }} 36 {{- $linkPage = . }} 37 {{- else }} 38 {{- /* is it a link into another translation? */}} 39 {{- if strings.HasPrefix $path "/" }} 40 {{- range $page.AllTranslations }} 41 {{- $lang := .Language.Lang }} 42 {{- $prefix := printf "/%s" $lang }} 43 {{- $suffix := strings.TrimPrefix $prefix $path | default "/" }} 44 {{- /* with the second check we check if the prefix was finished; 45 eg. /pir/index.html vs. /pirate/index.html, were the latter is 46 an external address outside of this site */}} 47 {{- if and (strings.HasPrefix $path $prefix) (strings.HasPrefix $suffix "/") }} 48 {{- with or 49 (.GetPage $suffix) 50 (.GetPage (strings.TrimRight "/" $suffix)) 51 (.Resources.Get $suffix) 52 (and (ne .BundleType "leaf") (.CurrentSection.Resources.Get $suffix)) 53 (resources.Get $suffix) 54 }} 55 {{- $linkPage = . }} 56 {{- break }} 57 {{- end }} 58 {{- end }} 59 {{- end }} 60 {{- end }} 61 {{- end }} 62 {{- else }} 63 {{- $linkPage = $page.Page }} 64 {{- end }} 65 {{- with $linkPage }} 66 {{- $href = partial "relLangPrettyUglyURL.hugo" (dict "to" .) }} 67 {{- with $u.RawQuery }} 68 {{- $href = printf "%s?%s" $href . }} 69 {{- end }} 70 {{- with $u.Fragment }} 71 {{- $ctx := dict 72 "contentPath" $page.File.Filename 73 "errorLevel" $page.Site.Params.link.errorlevel 74 "page" $linkPage 75 "parsedURL" $u 76 "renderHookName" "link" 77 }} 78 {{- partial "inline/h-rh-l/validate-fragment.html" $ctx }} 79 {{- $href = printf "%s#%s" $href . }} 80 {{- end }} 81 {{- else }} 82 {{- if eq $page.Site.Params.link.errorlevel "warning" }} 83 {{- warnf "%q: link '%s' is not a page but linked anyways" $page.File.Filename $url }} 84 {{- else if eq $page.Site.Params.link.errorlevel "error" }} 85 {{- errorf "%q: link '%s' is not a page" $page.File.Filename $url }} 86 {{- end }} 87 {{- end }} 88 {{- end }} 89 {{- $attributes = $attributes | merge (dict "href" $href) -}} 90 <a 91 {{- range $k, $v := $attributes }} 92 {{- if $v }} 93 {{- printf " %s=%q" $k $v | safeHTMLAttr }} 94 {{- end }} 95 {{- end }}>{{ $content | safeHTML }}</a> 96 97 {{- define "partials/inline/h-rh-l/validate-fragment.html" }} 98 {{- /* 99 Validates the fragment portion of a link destination. 100 101 @context {string} contentPath The page containing the link. 102 @context {string} errorLevel The error level when unable to resolve destination; ignore (default), warning, or error. 103 @context {page} page The page corresponding to the link destination 104 @context {struct} parsedURL The link destination parsed by urls.Parse. 105 @context {string} renderHookName The name of the render hook. 106 */}} 107 108 {{- /* Initialize. */}} 109 {{- $contentPath := .contentPath }} 110 {{- $errorLevel := .errorLevel }} 111 {{- $p := .page }} 112 {{- $u := .parsedURL }} 113 {{- $renderHookName := .renderHookName }} 114 115 {{- /* Validate. */}} 116 {{- with $u.Fragment }} 117 {{- if $p.Fragments.Identifiers.Contains . }} 118 {{- if gt ($p.Fragments.Identifiers.Count .) 1 }} 119 {{- $msg := printf "%q: duplicate heading ID %q found" $contentPath . }} 120 {{- if eq $errorLevel "warning" }} 121 {{- warnf $msg }} 122 {{- else if eq $errorLevel "error" }} 123 {{- errorf $msg }} 124 {{- end }} 125 {{- end }} 126 {{- else }} 127 {{- /* Determine target path for warning and error message. */}} 128 {{- $targetPath := "" }} 129 {{- with $p.File }} 130 {{- $targetPath = .Path }} 131 {{- else }} 132 {{- $targetPath = .Path }} 133 {{- end }} 134 {{- /* Set common message. */}} 135 {{- $msg := printf "%q: heading ID %q not found in %q" $contentPath . $targetPath }} 136 {{- if eq $targetPath $contentPath }} 137 {{- $msg := printf "%q: heading ID %q not found" $contentPath . }} 138 {{- end }} 139 {{- /* Throw warning or error. */}} 140 {{- if eq $errorLevel "warning" }} 141 {{- warnf $msg }} 142 {{- else if eq $errorLevel "error" }} 143 {{- errorf $msg }} 144 {{- end }} 145 {{- end }} 146 {{- end }} 147 {{- end }}