Skip to content

Commit 795755b

Browse files
committed
feat: add configurable edit link position
- Add a `where` parameter to `editURL` - Allow user to choose between `toc` (default) and `single` - Add to single through last-updated partial
1 parent 0d6cbba commit 795755b

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

exampleSite/hugo.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ params:
142142

143143
editURL:
144144
enable: true
145+
where: "single" # single | toc
145146
base: "https://github.com/imfing/hextra/edit/main/exampleSite/content"
146147

147148
blog:

layouts/partials/components/last-updated.html

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
{{- $lastUpdated := (T "lastUpdated") | default "Last updated on" -}}
2+
{{- $editThisPage := (T "editThisPage") | default "Edit this page"}}
23

34
{{- if site.Params.displayUpdatedDate -}}
5+
<div class="hx-mt-12 hx-mb-8 hx-block hx-text-xs hx-text-gray-500 ltr:hx-text-right rtl:hx-text-left dark:hx-text-gray-400">
6+
{{- if and (site.Params.editURL.enable) (eq site.Params.editURL.where "single") -}}
7+
{{- $editURL := site.Params.editURL.base | default "" -}}
8+
{{- with .Params.editURL -}}
9+
{{/* if `editURL` is set in the front matter */}}
10+
{{- $editURL = . -}}
11+
{{- else -}}
12+
{{- with .File -}}
13+
{{/* `.FileInfo.Meta.SourceRoot` is a Hugo internal field, e.g. `/path/to/repo/content/en/` */}}
14+
{{- $sourceDir := replace (strings.TrimPrefix .FileInfo.Meta.BaseDir .FileInfo.Meta.SourceRoot) "\\" "/" -}}
15+
{{- $sourceDir = strings.TrimPrefix "/content" $sourceDir -}}
16+
{{- $path := replace .Path "\\" "/" -}}
17+
{{- $editURL = urls.JoinPath $editURL $sourceDir $path -}}
18+
{{- end -}}
19+
{{- end -}}
20+
<a class="mb-4 block text-xs text-gray-500 ltr:text-right rtl:text-left dark:text-gray-400" href="{{ $editURL }}" target="_blank" rel="noreferer">{{ $editThisPage }}</a>
21+
{{- end -}}
422
{{- with .Lastmod -}}
523
{{ $datetime := (time.Format "2006-01-02T15:04:05.000Z" .) }}
6-
<div class="hx-mt-12 hx-mb-8 hx-block hx-text-xs hx-text-gray-500 ltr:hx-text-right rtl:hx-text-left dark:hx-text-gray-400">{{ $lastUpdated }} <time datetime="{{ $datetime }}">{{ partial "utils/format-date" . }}</time></div>
24+
<div class="hx-mt-4">{{ $lastUpdated }} <time datetime="{{ $datetime }}">{{ partial "utils/format-date" . }}</time></div>
25+
</div>
726
{{- else -}}
827
<div class="hx-mt-16"></div>
928
{{- end -}}

layouts/partials/toc.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
{{/* TOC bottom part */}}
2626
<div class="{{ $borderClass }} hx-sticky hx-bottom-0 hx-flex hx-flex-col hx-items-start hx-gap-2 hx-pb-8 dark:hx-border-neutral-800 contrast-more:hx-border-t contrast-more:hx-border-neutral-400 contrast-more:hx-shadow-none contrast-more:dark:hx-border-neutral-400">
27-
{{- if site.Params.editURL.enable -}}
27+
{{- if and (site.Params.editURL.enable) (eq site.Params.editURL.where "toc") -}}
2828
{{- $editURL := site.Params.editURL.base | default "" -}}
2929
{{- with .Params.editURL -}}
3030
{{/* if `editURL` is set in the front matter */}}

0 commit comments

Comments
 (0)