Write up and down arrows on internal links to headings

Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
This commit is contained in:
Danila Fedorin 2024-06-23 12:46:07 -07:00
parent 60041d1d37
commit 7b3ef86744
3 changed files with 24 additions and 3 deletions

View File

@ -150,7 +150,7 @@ a {
transition: color 0.25s;
}
&.external-link {
&.external-link, &.same-page-link {
.feather {
fill: none;
margin-left: 0.25rem;

View File

@ -0,0 +1,4 @@
{{- .Page.Scratch.SetInMap "definedSections" (printf "#%s" .Anchor) true -}}
<h{{ .Level }} id="{{ .Anchor }}">
{{- .Text | safeHTML -}}
</h{{ .Level }}>

View File

@ -1,13 +1,30 @@
{{- $scratch := newScratch -}}
{{- $absoluteDest := absLangURL .Destination -}}
{{- $siteRootUrl := absLangURL "" -}}
{{- $isExternal := not (hasPrefix $absoluteDest $siteRootUrl) -}}
{{- $isSamePage := hasPrefix .Destination "#" -}}
{{- if $isSamePage -}}
{{- $scratch.Set "class" "same-page-link" -}}
{{- if index (.Page.Scratch.Get "definedSections") .Destination -}}
{{- $scratch.Set "icon" "arrow-up" -}}
{{- else -}}
{{- $scratch.Set "icon" "arrow-down" -}}
{{- end -}}
{{- else if $isExternal -}}
{{- $scratch.Set "class" "external-link" -}}
{{- $scratch.Set "icon" "external-link" -}}
{{- end -}}
<a href="{{ .Destination | safeURL }}"
{{- with .Title }} title="{{ . }}"{{ end -}}
{{- with $scratch.Get "class" -}}
class="{{ . }}"
{{- end -}}
{{- if $isExternal -}}
class="external-link" target="_blank" rel="noopener noreferrer"
target="_blank" rel="noopener noreferrer"
{{- end -}}
>
{{- with .Text | safeHTML }}{{ . }}{{ end -}}
{{- if $isExternal -}}{{- partial "icon.html" "external-link" -}}{{- end -}}
{{- with $scratch.Get "icon" -}}{{- partial "icon.html" . -}}{{- end -}}
</a>
{{- /* chomp trailing newline */ -}}