summaryrefslogtreecommitdiffstats
path: root/tpl/strings/strings.go
diff options
context:
space:
mode:
authorJoe Mooring <joe.mooring@veriphor.com>2024-04-01 19:26:15 -0700
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2024-04-02 18:25:44 +0200
commit6624979e1bd6bdd7edc8e627085fd400ab3950d9 (patch)
treed43c157777e0e9b70be7cf4e1c8545e4d49ec743 /tpl/strings/strings.go
parent983b8d537c6af7b9790dc8ab31817f066309d5bd (diff)
tpl/strings: Create strings.Diff template function
Closes #12330
Diffstat (limited to 'tpl/strings/strings.go')
-rw-r--r--tpl/strings/strings.go10
1 files changed, 10 insertions, 0 deletions
diff --git a/tpl/strings/strings.go b/tpl/strings/strings.go
index cd233b0a4..e6f7aed80 100644
--- a/tpl/strings/strings.go
+++ b/tpl/strings/strings.go
@@ -27,6 +27,7 @@ import (
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/tpl"
+ "github.com/rogpeppe/go-internal/diff"
"github.com/spf13/cast"
)
@@ -172,6 +173,15 @@ func (ns *Namespace) ContainsNonSpace(s any) bool {
return false
}
+// Diff returns an anchored diff of the two texts old and new in the “unified
+// diff” format. If old and new are identical, Diff returns an empty string.
+func (ns *Namespace) Diff(oldname string, old any, newname string, new any) string {
+ oldb := []byte(cast.ToString(old))
+ newb := []byte(cast.ToString(new))
+
+ return string(diff.Diff(oldname, oldb, newname, newb))
+}
+
// HasPrefix tests whether the input s begins with prefix.
func (ns *Namespace) HasPrefix(s, prefix any) (bool, error) {
ss, err := cast.ToStringE(s)