summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-12-21 09:51:15 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-12-21 09:51:15 +0100
commit1021714449a05ef85b2fdfaf65b354cbdee44f23 (patch)
treec92c11867d9af5a78cade6d7554387accdf789ef
parent6f069e549b869c976cb6576a64812df5e7b7c809 (diff)
hugolib: Add .Name as a shortcode variable
Fixes #5546
-rw-r--r--docs/content/en/variables/shortcodes.md3
-rw-r--r--hugolib/shortcode.go3
-rw-r--r--hugolib/shortcode_test.go4
3 files changed, 8 insertions, 2 deletions
diff --git a/docs/content/en/variables/shortcodes.md b/docs/content/en/variables/shortcodes.md
index 10b779396..7462deec7 100644
--- a/docs/content/en/variables/shortcodes.md
+++ b/docs/content/en/variables/shortcodes.md
@@ -20,6 +20,9 @@ toc: false
[Shortcodes][shortcodes] have access to parameters delimited in the shortcode declaration via [`.Get`][getfunction], page- and site-level variables, and also the following shortcode-specific fields:
+.Name
+: Shortcode name.
+
.Ordinal
: Zero-based ordinal in relation to its parent. If the parent is the page itself, this ordinal will represent the position of this shortcode in the page content.
diff --git a/hugolib/shortcode.go b/hugolib/shortcode.go
index db7bf2c62..6d87414a7 100644
--- a/hugolib/shortcode.go
+++ b/hugolib/shortcode.go
@@ -58,6 +58,7 @@ type ShortcodeWithPage struct {
Inner template.HTML
Page *PageWithoutContent
Parent *ShortcodeWithPage
+ Name string
IsNamedParams bool
// Zero-based ordinal in relation to its parent. If the parent is the page itself,
@@ -401,7 +402,7 @@ func renderShortcode(
return "", nil
}
- data := &ShortcodeWithPage{Ordinal: sc.ordinal, posOffset: sc.pos, Params: sc.params, Page: p, Parent: parent}
+ data := &ShortcodeWithPage{Ordinal: sc.ordinal, posOffset: sc.pos, Params: sc.params, Page: p, Parent: parent, Name: sc.name}
if sc.params != nil {
data.IsNamedParams = reflect.TypeOf(sc.params).Kind() == reflect.Map
}
diff --git a/hugolib/shortcode_test.go b/hugolib/shortcode_test.go
index 3a1656e26..9545301ea 100644
--- a/hugolib/shortcode_test.go
+++ b/hugolib/shortcode_test.go
@@ -1027,7 +1027,7 @@ ordinal: 4 scratch ordinal: 5 scratch get ordinal: 4`)
}
-func TestShortcodePosition(t *testing.T) {
+func TestShortcodeVariables(t *testing.T) {
t.Parallel()
assert := require.New(t)
@@ -1042,6 +1042,7 @@ title: "Hugo Rocks!"
{{< s1 >}}
`).WithTemplatesAdded("layouts/shortcodes/s1.html", `
+Name: {{ .Name }}
{{ with .Position }}
File: {{ .Filename }}
Offset: {{ .Offset }}
@@ -1059,6 +1060,7 @@ String: {{ . | safeHTML }}
filepath.FromSlash("File: content/page.md"),
"Line: 7", "Column: 4", "Offset: 40",
filepath.FromSlash("String: \"content/page.md:7:4\""),
+ "Name: s1",
)
}