From eaabbfd6ef202ac4c55ee6bd228082c27b290b53 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=B6ren=20Weber?= <mail@soeren-weber.de>
Date: Sat, 27 Jan 2024 12:06:39 +0100
Subject: [PATCH] topbar: support editURL in frontmatter #764

---
 exampleSite/config/_default/params.toml       | 12 +++---
 .../content/basics/migration/_index.en.md     |  2 +
 .../content/cont/frontmatter/_index.en.md     | 13 +++++++
 layouts/partials/topbar/button/edit.html      | 37 +++++++++++++------
 4 files changed, 47 insertions(+), 17 deletions(-)

diff --git a/exampleSite/config/_default/params.toml b/exampleSite/config/_default/params.toml
index ab0c583817..86f4e575a2 100644
--- a/exampleSite/config/_default/params.toml
+++ b/exampleSite/config/_default/params.toml
@@ -157,11 +157,13 @@ disableNextPrev = false
 # The URL prefix to edit a page.
 # Default: not set
 # If set, an edit button will be shown in the topbar. If the button is hidden,
-# also the keyboard shortcuts are disabled. The given URL is prepended to the
-# relative file path of a the displayed page. The URL must end with a `/`.
-# This is useful if you wnat to give the opportunity for people to create merge
-#  request for your content.
-editURL = "https://github.com/McShelby/hugo-theme-relearn/edit/main/exampleSite/content/"
+# also the keyboard shortcuts are disabled. The value can contain the macro
+# `${FilePath}` which will be replaced by the file path of your displayed page.
+# If no `${FilePath}` is given in the value, the value is treated as if the
+# `${FilePath}` was appended at the end of the value. This can be overridden
+# in the pages frontmatter. This is useful if you want to give the opportunity
+# for people to create merge request for your content.
+editURL = "https://github.com/McShelby/hugo-theme-relearn/edit/main/exampleSite/content/${FilePath}"
 
 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 # Menu
diff --git a/exampleSite/content/basics/migration/_index.en.md b/exampleSite/content/basics/migration/_index.en.md
index 5bd7248bf7..5c4c4f8665 100644
--- a/exampleSite/content/basics/migration/_index.en.md
+++ b/exampleSite/content/basics/migration/_index.en.md
@@ -40,6 +40,8 @@ This document shows you what's new in the latest release and flags it with one o
 
 - {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} Improvements for accessibility when tabbing thru the page for images, links and tab handles.
 
+- {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} The `editURL` config parameter is now [overwritable in your pages frontmatter](cont/frontmatter). In addition it received more versatility by letting you control where to put the file path into the URL. This is achieved by replacing the variable `${FilePath}` in your URL by the pages file path. You don't need to change anything in your existing configuration as the old way without the replacement variable still works.
+
 - {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} The themes [config](basics/configuration) and [frontmatter](cont/frontmatter) options received a comprehensive documentation update. In addition the theme switched from `config.toml` to `hugo.toml`.
 
 - {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} Restored compatibility with Hugo versions {{% badge color="fuchsia" icon="fab fa-hackerrank" title=" " %}}0.121.0{{% /badge %}} or higher for the [`highlight` shortcode](shortcodes/highlight). This does not change the minimum required Hugo version.
diff --git a/exampleSite/content/cont/frontmatter/_index.en.md b/exampleSite/content/cont/frontmatter/_index.en.md
index f8fbce88ce..ab0b5c165b 100644
--- a/exampleSite/content/cont/frontmatter/_index.en.md
+++ b/exampleSite/content/cont/frontmatter/_index.en.md
@@ -61,6 +61,19 @@ disableBreadcrumb = false
 # disabled.
 disableNextPrev = false
 
+# The URL prefix to edit a page.
+# Default: not set
+# If set, an edit button will be shown in the topbar. If the button is hidden,
+# also the keyboard shortcuts are disabled. The value can contain the macro
+# `${FilePath}` which will be replaced by the file path of your displayed page.
+# If not set, the set value of your site's hugo.toml is used. If the global
+# parameter is given but you want to hide the button for the displayed page,
+# you can set the value to an empty string. If instead of hiding you want to have
+# an disabled button, you can set the value to a string containing just spaces.
+# This is useful if you want to give the opportunity for people to create merge
+# request for your content.
+editURL = ""
+
 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 # Menu
 # These options modify the menu apperance.
diff --git a/layouts/partials/topbar/button/edit.html b/layouts/partials/topbar/button/edit.html
index 1b78cca9d0..b5c43ba48e 100644
--- a/layouts/partials/topbar/button/edit.html
+++ b/layouts/partials/topbar/button/edit.html
@@ -4,17 +4,30 @@
 {{- with .page }}
 	{{- $format := partial "get-format.hugo" . }}
 	{{- $outputFormat := partial "output-format.hugo" (dict "page" . "format" $format) }}
-	{{- if and (eq $outputFormat "html") (or .Site.Params.editURL .Params.editURL) .File }}
-		{{- $href := or .Params.editURL (printf "%s%s%s" .Site.Params.editURL (strings.TrimLeft "/" (replace .File.Dir "\\" "/")) .File.LogicalName) }}
-		{{- partial "topbar/func/button.html" (dict
-			"page" .
-			"class" "topbar-button-edit"
-			"href" $href
-			"icon" "pen"
-			"onwidths" $onwidths
-			"onwidthm" $onwidthm
-			"onwidthl" $onwidthl
-			"hint" (printf "%s (CTRL+ALT+w)" (T "Edit-this-page"))
-		)}}
+	{{- if and (eq $outputFormat "html") .File }}
+		{{- $filePath := printf "%s%s" (strings.TrimLeft "/" (replace .File.Dir "\\" "/")) .File.LogicalName }}
+		{{- $href := "" }}
+		{{- if .Site.Params.editURL }}
+			{{- $href = .Site.Params.editURL }}
+			{{- if not (strings.Contains $href "${FilePath}") }}
+				{{ $href = printf "%s%s" $href "${FilePath}" }}
+			{{- end }}
+		{{- end }}
+		{{- if isset .Params "editurl" }}
+			{{- $href = .Params.editURL }}
+		{{- end }}
+		{{- $href = strings.Replace $href "${FilePath}" $filePath }}
+		{{- if $href }}
+			{{- partial "topbar/func/button.html" (dict
+				"page" .
+				"class" "topbar-button-edit"
+				"href" $href
+				"icon" "pen"
+				"onwidths" $onwidths
+				"onwidthm" $onwidthm
+				"onwidthl" $onwidthl
+				"hint" (printf "%s (CTRL+ALT+w)" (T "Edit-this-page"))
+			)}}
+		{{- end }}
 	{{- end }}
 {{- end }}
\ No newline at end of file