From 795755bf9b895843fb6572cedfcfb25ac9dd772e Mon Sep 17 00:00:00 2001 From: David Legrand <1110600+davlgd@users.noreply.github.com> Date: Tue, 13 Feb 2024 01:21:09 +0100 Subject: [PATCH 1/5] 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 --- exampleSite/hugo.yaml | 1 + layouts/partials/components/last-updated.html | 21 ++++++++++++++++++- layouts/partials/toc.html | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/exampleSite/hugo.yaml b/exampleSite/hugo.yaml index 0da0ad99..5235f23f 100644 --- a/exampleSite/hugo.yaml +++ b/exampleSite/hugo.yaml @@ -142,6 +142,7 @@ params: editURL: enable: true + where: "single" # single | toc base: "https://github.com/imfing/hextra/edit/main/exampleSite/content" blog: diff --git a/layouts/partials/components/last-updated.html b/layouts/partials/components/last-updated.html index 7918fb12..22905e65 100644 --- a/layouts/partials/components/last-updated.html +++ b/layouts/partials/components/last-updated.html @@ -1,9 +1,28 @@ {{- $lastUpdated := (T "lastUpdated") | default "Last updated on" -}} +{{- $editThisPage := (T "editThisPage") | default "Edit this page"}} {{- if site.Params.displayUpdatedDate -}} +
+ {{- if and (site.Params.editURL.enable) (eq site.Params.editURL.where "single") -}} + {{- $editURL := site.Params.editURL.base | default "" -}} + {{- with .Params.editURL -}} + {{/* if `editURL` is set in the front matter */}} + {{- $editURL = . -}} + {{- else -}} + {{- with .File -}} + {{/* `.FileInfo.Meta.SourceRoot` is a Hugo internal field, e.g. `/path/to/repo/content/en/` */}} + {{- $sourceDir := replace (strings.TrimPrefix .FileInfo.Meta.BaseDir .FileInfo.Meta.SourceRoot) "\\" "/" -}} + {{- $sourceDir = strings.TrimPrefix "/content" $sourceDir -}} + {{- $path := replace .Path "\\" "/" -}} + {{- $editURL = urls.JoinPath $editURL $sourceDir $path -}} + {{- end -}} + {{- end -}} + {{ $editThisPage }} + {{- end -}} {{- with .Lastmod -}} {{ $datetime := (time.Format "2006-01-02T15:04:05.000Z" .) }} -
{{ $lastUpdated }}
+
{{ $lastUpdated }}
+
{{- else -}}
{{- end -}} diff --git a/layouts/partials/toc.html b/layouts/partials/toc.html index 02cf98da..4c25671f 100644 --- a/layouts/partials/toc.html +++ b/layouts/partials/toc.html @@ -24,7 +24,7 @@ {{/* TOC bottom part */}}
- {{- if site.Params.editURL.enable -}} + {{- if and (site.Params.editURL.enable) (eq site.Params.editURL.where "toc") -}} {{- $editURL := site.Params.editURL.base | default "" -}} {{- with .Params.editURL -}} {{/* if `editURL` is set in the front matter */}} From f87f19fb74cd3d91055e4454c6228e9377577b64 Mon Sep 17 00:00:00 2001 From: David Legrand <1110600+davlgd@users.noreply.github.com> Date: Mon, 1 Apr 2024 13:13:06 +0200 Subject: [PATCH 2/5] Fixes after review - `where` > `location` - Edit link > in a partial --- exampleSite/hugo.yaml | 2 +- layouts/partials/components/edit-url.html | 22 +++++++++++++++++++ layouts/partials/components/last-updated.html | 19 ++-------------- layouts/partials/toc.html | 17 +------------- 4 files changed, 26 insertions(+), 34 deletions(-) create mode 100644 layouts/partials/components/edit-url.html diff --git a/exampleSite/hugo.yaml b/exampleSite/hugo.yaml index 5235f23f..9bc7c5d2 100644 --- a/exampleSite/hugo.yaml +++ b/exampleSite/hugo.yaml @@ -142,7 +142,7 @@ params: editURL: enable: true - where: "single" # single | toc + location: "toc" # single | toc base: "https://github.com/imfing/hextra/edit/main/exampleSite/content" blog: diff --git a/layouts/partials/components/edit-url.html b/layouts/partials/components/edit-url.html new file mode 100644 index 00000000..11546de8 --- /dev/null +++ b/layouts/partials/components/edit-url.html @@ -0,0 +1,22 @@ +{{- $context := .context -}} +{{- $editThisPage := (T "editThisPage") | default "Edit this page"}} +{{- $linkClass := "mb-4 block text-xs text-gray-500 ltr:text-right rtl:text-left dark:text-gray-400" -}} +{{- if and (site.Params.editURL.enable) (eq site.Params.editURL.location .source) -}} + {{- $editURL := site.Params.editURL.base | default "" -}} + {{- with .Params.editURL -}} + {{/* if `editURL` is set in the front matter */}} + {{- $editURL = . -}} + {{- else -}} + {{- with $context.File -}} + {{/* `.FileInfo.Meta.SourceRoot` is a Hugo internal field, e.g. `/path/to/repo/content/en/` */}} + {{- $sourceDir := replace (strings.TrimPrefix .FileInfo.Meta.BaseDir .FileInfo.Meta.SourceRoot) "\\" "/" -}} + {{- $sourceDir = strings.TrimPrefix "/content" $sourceDir -}} + {{- $path := replace .Path "\\" "/" -}} + {{- $editURL = urls.JoinPath $editURL $sourceDir $path -}} + {{- end -}} + {{- end -}} + {{- if eq site.Params.editURL.location "toc" -}} + {{- $linkClass = "hx-text-xs hx-font-medium hx-text-gray-500 hover:hx-text-gray-900 dark:hx-text-gray-400 dark:hover:hx-text-gray-100 contrast-more:hx-text-gray-800 contrast-more:dark:hx-text-gray-50" -}} + {{- end -}} + {{ $editThisPage }} +{{- end -}} diff --git a/layouts/partials/components/last-updated.html b/layouts/partials/components/last-updated.html index 22905e65..013881b6 100644 --- a/layouts/partials/components/last-updated.html +++ b/layouts/partials/components/last-updated.html @@ -2,23 +2,8 @@ {{- $editThisPage := (T "editThisPage") | default "Edit this page"}} {{- if site.Params.displayUpdatedDate -}} -
- {{- if and (site.Params.editURL.enable) (eq site.Params.editURL.where "single") -}} - {{- $editURL := site.Params.editURL.base | default "" -}} - {{- with .Params.editURL -}} - {{/* if `editURL` is set in the front matter */}} - {{- $editURL = . -}} - {{- else -}} - {{- with .File -}} - {{/* `.FileInfo.Meta.SourceRoot` is a Hugo internal field, e.g. `/path/to/repo/content/en/` */}} - {{- $sourceDir := replace (strings.TrimPrefix .FileInfo.Meta.BaseDir .FileInfo.Meta.SourceRoot) "\\" "/" -}} - {{- $sourceDir = strings.TrimPrefix "/content" $sourceDir -}} - {{- $path := replace .Path "\\" "/" -}} - {{- $editURL = urls.JoinPath $editURL $sourceDir $path -}} - {{- end -}} - {{- end -}} - {{ $editThisPage }} - {{- end -}} +
+{{ partial "components/edit-url.html" (dict "source" "single" "context" .) }} {{- with .Lastmod -}} {{ $datetime := (time.Format "2006-01-02T15:04:05.000Z" .) }}
{{ $lastUpdated }}
diff --git a/layouts/partials/toc.html b/layouts/partials/toc.html index 4c25671f..d1733434 100644 --- a/layouts/partials/toc.html +++ b/layouts/partials/toc.html @@ -24,22 +24,7 @@ {{/* TOC bottom part */}}
- {{- if and (site.Params.editURL.enable) (eq site.Params.editURL.where "toc") -}} - {{- $editURL := site.Params.editURL.base | default "" -}} - {{- with .Params.editURL -}} - {{/* if `editURL` is set in the front matter */}} - {{- $editURL = . -}} - {{- else -}} - {{- with .File -}} - {{/* `.FileInfo.Meta.SourceRoot` is a Hugo internal field, e.g. `/path/to/repo/content/en/` */}} - {{- $sourceDir := replace (strings.TrimPrefix .FileInfo.Meta.BaseDir .FileInfo.Meta.SourceRoot) "\\" "/" -}} - {{- $sourceDir = strings.TrimPrefix "/content" $sourceDir -}} - {{- $path := replace .Path "\\" "/" -}} - {{- $editURL = urls.JoinPath $editURL $sourceDir $path -}} - {{- end -}} - {{- end -}} - {{ $editThisPage }} - {{- end -}} + {{ partial "components/edit-url.html" (dict "source" "toc" "context" .) }} {{/* Scroll To Top */}}