Skip to content

Commit

Permalink
hugolib: Add .Name as a shortcode variable
Browse files Browse the repository at this point in the history
Fixes #5546
  • Loading branch information
bep committed Dec 21, 2018
1 parent 6f069e5 commit 1021714
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
3 changes: 3 additions & 0 deletions docs/content/en/variables/shortcodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 2 additions & 1 deletion hugolib/shortcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion hugolib/shortcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -1042,6 +1042,7 @@ title: "Hugo Rocks!"
{{< s1 >}}
`).WithTemplatesAdded("layouts/shortcodes/s1.html", `
Name: {{ .Name }}
{{ with .Position }}
File: {{ .Filename }}
Offset: {{ .Offset }}
Expand All @@ -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",
)

}
Expand Down

0 comments on commit 1021714

Please sign in to comment.