link: support linkable output formats #850 #1008

This commit is contained in:
Sören Weber 2025-02-08 16:05:12 +01:00
parent 1ebb5451c3
commit c0c2b83059
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
9 changed files with 97 additions and 59 deletions

View file

@ -1,57 +1,65 @@
{{- $href := partial "permalink.gotmpl" (dict "to" .linkObject) }}
{{- $silent := .silent | default false }}
{{- $hideFilepath := .hideFilepath | default false }}
{{- $u := urls.Parse .url }}
{{- $hideFilepath := .hideFilepath | default false }}
{{- $type := printf "%T" .linkObject }}
{{- $isPage := eq $type "*hugolib.pageState" }}
{{- $href := partial "permalink.gotmpl" (dict "to" .linkObject) }}
{{- if $isPage }}
{{- $format := $u.Query.Get "format" }}
{{- if $format }}
{{- $of := .linkObject.OutputFormats.Get $format }}
{{- if $of }}
{{- $href = partial "permalink.gotmpl" (dict "to" $of) }}
{{- else }}
{{- $msg := printf "output format %q not found" $of }}
{{- partial "partials/inline/print-error" (dict "url" .url "page" .page "param" .param "msg" $msg "linkObject" .linkObject "hideFilepath" $hideFilepath) }}
{{- end }}
{{- end }}
{{- end }}
{{- with $u.RawQuery }}
{{- $href = printf "%s?%s" $href . }}
{{- end }}
{{- with $u.Fragment }}
{{- $href = printf "%s#%s" $href . }}
{{- if not $silent }}
{{- partial "inline/validate-fragment.html" (dict "origPage" $.page "page" $.linkObject "parsedURL" $u "url" $.url "param" $.param "hideFilepath" $hideFilepath) }}
{{- if $isPage }}
{{- partial "inline/validate-fragment.html" (dict "page" $.page "linkObject" $.linkObject "parsedURL" $u "url" $.url "param" $.param "hideFilepath" $hideFilepath) }}
{{- end }}
{{- end }}
{{- $href = replaceRE `&lang=\w*\b` "" $href }}
{{- $href = replaceRE `\?lang=\w*&\b` "?" $href }}
{{- $href = replaceRE `\?lang=\w*\b` "" $href }}
{{- $href = replaceRE `&format=\w*\b` "" $href }}
{{- $href = replaceRE `\?format=\w*&\b` "?" $href }}
{{- $href = replaceRE `\?format=\w*\b` "" $href }}
{{- return $href }}
{{- define "partials/inline/validate-fragment.html" }}
{{- /*
Validates the fragment portion of a link destination.
*/}}
{{- /* Initialize. */}}
{{- $errorLevel := .errorLevel }}
{{- $origPage := .origPage }}
{{- $p := .page }}
{{- $url := .url }}
{{- $u := .parsedURL }}
{{- $param := .param }}
{{- $hideFilepath := .hideFilepath }}
{{- /* Validate. */}}
{{- with $u.Fragment }}
{{- if $p.Fragments.Identifiers.Contains . }}
{{- if gt ($p.Fragments.Identifiers.Count .) 1 }}
{{- $filepath := "[virtual file]" }}{{ with and $origPage $origPage.File $origPage.File.Filename }}{{ $filepath = . }}{{ end }}
{{- if $.linkObject.Fragments.Identifiers.Contains . }}
{{- if gt ($.linkObject.Fragments.Identifiers.Count .) 1 }}
{{- $filepath := "[virtual file]" }}{{ with and $.page $.page.File $.page.File.Filename }}{{ $filepath = . }}{{ end }}
{{- $msg := printf "%q: duplicate heading ID %q found" $filepath . }}
{{- partial "_relearn/urlErrorReport.gotmpl" (dict "url" $url "page" $origPage "param" $param "msg" $msg) }}
{{- partial "_relearn/urlErrorReport.gotmpl" (dict "url" $.url "page" $.page "param" $.param "msg" $msg) }}
{{- end }}
{{- else }}
{{- /* Determine target path for warning and error message. */}}
{{- $targetPath := "" }}
{{- with $p.File }}
{{- $targetPath = .Path }}
{{- else }}
{{- $targetPath = .Path }}
{{- end }}
{{- $filepath := "[virtual file]" }}{{ with and $origPage $origPage.File $origPage.File.Filename }}{{ $filepath = . }}{{ end }}
{{- $msg := printf "heading ID %q not found" . }}
{{- if not $hideFilepath }}
{{- $msg = printf "%q: %s" $filepath $msg }}
{{- end }}
{{- if or $hideFilepath (ne $origPage $p) }}
{{- $msg = printf "%s in %q" $msg $targetPath }}
{{- end }}
{{- partial "_relearn/urlErrorReport.gotmpl" (dict "url" $url "page" $origPage "param" $param "msg" $msg) }}
{{- partial "partials/inline/print-error" (dict "url" $.url "page" $.page "param" $.param "msg" $msg "linkObject" $.linkObject "hideFilepath" $.hideFilepath) }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- define "partials/inline/print-error" }}
{{- $targetpath := "" }}
{{- with .linkObject.File }}
{{- $targetpath = .Path }}
{{- else }}
{{- $targetpath = .Path }}
{{- end }}
{{- $filepath := "[virtual file]" }}{{ with and .page .page.File .page.File.Filename }}{{ $filepath = . }}{{ end }}
{{- $msg := .msg }}
{{- if not .hideFilepath }}
{{- $msg = printf "%q: %s" $filepath $msg }}
{{- end }}
{{- if or .hideFilepath (ne .page .linkObject) }}
{{- $msg = printf "%s in %q" $msg $targetpath }}
{{- end }}
{{- partial "_relearn/urlErrorReport.gotmpl" (dict "url" .url "page" .page "param" .param "msg" $msg) }}
{{- end }}

View file

@ -174,7 +174,7 @@
{{- $url = .Params.menuPageRef }}
{{- $linkObject := partial "_relearn/refObject.gotmpl" (dict "page" . "objectRef" .Params.menuPageRef) }}
{{- if $linkObject }}
{{- $url = partial "_relearn/decoratedLink.gotmpl" (dict "url" .Params.menuPageRef "page" . "linkObject" $linkObject "param" "link" "silent" true) }}
{{- $url = partial "_relearn/decoratedLink.gotmpl" (dict "url" .Params.menuPageRef "page" . "linkObject" $linkObject "param" "link") }}
{{- end }}
{{- else if .Params.menuUrl }}
{{- $isCrosslink = true }}

View file

@ -8,10 +8,11 @@
{{- $show = not (partial "_relearn/pageIsSpecial.gotmpl" .) }}
{{- end }}
{{- if $show }}
{{- $href := printf "%s?format=markdown" .Path }}
{{- partial "topbar/func/button.html" (dict
"page" .
"class" "topbar-button-markdown"
"href" $format.RelPermalink
"href" $href
"icon" "fa-fw fab fa-markdown"
"onwidths" $onwidths
"onwidthm" $onwidthm

View file

@ -10,10 +10,14 @@
{{- end }}
{{- $next := partialCached "_relearn/pageNext.gotmpl" . .Path }}
{{- $nextTitle := partial "title.gotmpl" (dict "page" $next "outputFormat" "html") }}
{{- $href := "" }}
{{- with (and $next $next.Path) }}
{{- $href = . }}
{{- end }}
{{- partial "topbar/func/button.html" (dict
"page" .
"class" "topbar-button-next"
"href" (partial "permalink.gotmpl" (dict "to" $next))
"href" $href
"icon" "chevron-right"
"onwidths" $onwidths
"onwidthm" $onwidthm

View file

@ -10,10 +10,14 @@
{{- end }}
{{- $prev := partialCached "_relearn/pagePrev.gotmpl" . .Path }}
{{- $prevTitle := partial "title.gotmpl" (dict "page" $prev "outputFormat" "html") }}
{{- $href := "" }}
{{- with (and $prev $prev.Path) }}
{{- $href = . }}
{{- end }}
{{- partial "topbar/func/button.html" (dict
"page" .
"class" "topbar-button-prev"
"href" (partial "permalink.gotmpl" (dict "to" $prev))
"href" $href
"icon" "chevron-left"
"onwidths" $onwidths
"onwidthm" $onwidthm

View file

@ -8,10 +8,11 @@
{{- $show = not (partial "_relearn/pageIsSpecial.gotmpl" .) }}
{{- end }}
{{- if $show }}
{{- $href := printf "%s?format=print" .Path }}
{{- partial "topbar/func/button.html" (dict
"page" .
"class" "topbar-button-print"
"href" $format.RelPermalink
"href" $href
"icon" "print"
"onwidths" $onwidths
"onwidthm" $onwidthm

View file

@ -8,10 +8,11 @@
{{- $show = and (not (partial "_relearn/pageIsSpecial.gotmpl" .)) .File .File.Filename }}
{{- end }}
{{- if $show }}
{{- $href := printf "%s?format=source" .Path }}
{{- partial "topbar/func/button.html" (dict
"page" .
"class" "topbar-button-source"
"href" $format.RelPermalink
"href" $href
"icon" "code"
"onwidths" $onwidths
"onwidthm" $onwidthm

View file

@ -5,8 +5,15 @@
{{- $onwidths := cond (isset . "onwidths") .onwidths "show" }}
{{- $onwidthm := cond (isset . "onwidthm") .onwidthm "show" }}
{{- $onwidthl := cond (isset . "onwidthl") .onwidthl "show" }}
{{- $target := .target | default "" }}
{{- $attributes := .attributes | default dict }}
{{- $type := .type | default "" }}
{{- $title := (trim .title " ") | default "" }}
{{- $hint := (trim .hint " ") | default "" }}
{{- $icon := (trim .icon " ") | default "" }}
{{- if and $icon (not (findRE ".*?\\bfa-\\w.*?" $icon)) }}
{{- $icon = printf "fa-fw fas fa-%s" $icon }}
{{- end }}
{{- $content := .content }}
{{- $isDisabled := not $href }}
{{- $isButton := false }}
{{- if or (not $href) (strings.HasPrefix $href "javascript:") }}
@ -15,26 +22,38 @@
{{- if not $type }}
{{- $type = "button" }}
{{- end }}
{{- else if and (eq (len $target) 0) (or (strings.HasPrefix $href "http://") (strings.HasPrefix $href "https://") ) }}
{{- $target = "_blank" }}
{{- if isset $page.Site.Params "externallinktarget" }}
{{- $target = $page.Site.Params.externalLinkTarget }}
{{- else }}
{{- $u := urls.Parse .href }}
{{- if $u.IsAbs }}
{{- partialCached "_relearn/urlExists.gotmpl" (dict "url" .href "page" $page "type" "link") $u.String }}
{{- else }}
{{- $linkObject := partial "_relearn/linkObject.gotmpl" (dict "url" .href "page" $page) }}
{{- if $linkObject }}
{{- $href = partial "_relearn/decoratedLink.gotmpl" (dict "url" .href "page" $page "linkObject" $linkObject "param" "link") }}
{{- else }}
{{- $filepath := "[virtual file]" }}{{ with and $page $page.File $page.File.Filename }}{{ $filepath = . }}{{ end }}
{{- $msg := printf "%q: link '%s' is not a page or a resource" $filepath .href }}
{{- partial "_relearn/urlErrorReport.gotmpl" (dict "url" .href "page" $page "param" "link" "msg" $msg) }}
{{- end }}
{{- end }}
{{- $attributes = merge $attributes (partial "_relearn/linkAttributes.gotmpl" (dict "url" $href "page" $page "attributes" (dict "class" "topbar-control" "title" $hint))) -}}
{{- end }}
{{- $title := (trim .title " ") | default "" }}
{{- $hint := (trim .hint " ") | default "" }}
{{- $icon := (trim .icon " ") | default "" }}
{{- if and $icon (not (findRE ".*?\\bfa-\\w.*?" $icon)) }}
{{- $icon = printf "fa-fw fas fa-%s" $icon }}
{{- end }}
{{- $content := .content }}
<div class="topbar-button {{ $class }}" data-content-empty="{{ $onempty }}" data-width-s="{{ $onwidths }}" data-width-m="{{ $onwidthm }}" data-width-l="{{ $onwidthl }}">
{{- if $isDisabled -}}
<span class="topbar-control">
{{- else if $isButton -}}
<button class="topbar-control"{{ if $href }} onclick="{{ $href | safeJS }}"{{ end }}{{ if gt (len $type) 0 }} type="{{ $type }}"{{ end }} title="{{ $hint }}">
{{- else -}}
<a class="topbar-control"{{ if $href }} href="{{ $href }}"{{ if gt (len $target) 0 }} target="{{ $target }}"{{ end }}{{ end }} title="{{ $hint }}">
<a
{{- range $k, $v := $attributes }}
{{- if $v }}
{{- if eq (printf "%T" $v) "bool" }}
{{- printf " %s" $k | safeHTMLAttr }}
{{- else }}
{{- printf " %s=%q" $k $v | safeHTMLAttr }}
{{- end }}
{{- end }}
{{- end }}>
{{- end }}
{{- if and $icon }}<i class="{{ $icon }}"></i>{{ end }}
{{- if and $icon $title }} {{ end }}

View file

@ -1 +1 @@
7.3.2+00590db6bbc0743775238176a01124a654be9887
7.3.2+1ebb5451c366e5c592ed18d9b9454804f1d9e624