Skip to content

Commit b63e4ee

Browse files
committed
create: Preserve shortcodes in archetype templates
Fixes #3623
1 parent bfa336d commit b63e4ee

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

‎create/content_template_handler.go‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package create
1616
import (
1717
"bytes"
1818
"fmt"
19+
"strings"
1920
"time"
2021

2122
"github.com/gohugoio/hugo/helpers"
@@ -57,6 +58,20 @@ draft: true
5758
`
5859
)
5960

61+
var (
62+
archetypeShortcodeReplacementsPre = strings.NewReplacer(
63+
"{{<", "{x{<",
64+
"{{%", "{x{%",
65+
">}}", ">}x}",
66+
"%}}", "%}x}")
67+
68+
archetypeShortcodeReplacementsPost = strings.NewReplacer(
69+
"{x{<", "{{<",
70+
"{x{%", "{{%",
71+
">}x}", ">}}",
72+
"%}x}", "%}}")
73+
)
74+
6075
func executeArcheTypeAsTemplate(s *hugolib.Site, kind, targetPath, archetypeFilename string) ([]byte, error) {
6176

6277
var (
@@ -86,6 +101,10 @@ func executeArcheTypeAsTemplate(s *hugolib.Site, kind, targetPath, archetypeFile
86101

87102
}
88103

104+
// The archetype template may contain shortcodes, and these does not play well
105+
// with the Go templates. Need to set some temporary delimiters.
106+
archetypeTemplate = []byte(archetypeShortcodeReplacementsPre.Replace(string(archetypeTemplate)))
107+
89108
// Reuse the Hugo template setup to get the template funcs properly set up.
90109
templateHandler := s.Deps.Tmpl.(tpl.TemplateHandler)
91110
templateName := "_text/" + helpers.Filename(archetypeFilename)
@@ -100,7 +119,7 @@ func executeArcheTypeAsTemplate(s *hugolib.Site, kind, targetPath, archetypeFile
100119
return nil, fmt.Errorf("Failed to process archetype file %q: %s", archetypeFilename, err)
101120
}
102121

103-
archetypeContent = buff.Bytes()
122+
archetypeContent = []byte(archetypeShortcodeReplacementsPost.Replace(buff.String()))
104123

105124
if !bytes.Contains(archetypeContent, []byte("date")) || !bytes.Contains(archetypeContent, []byte("title")) {
106125
// TODO(bep) remove some time in the future.

‎create/content_test.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ func TestNewContent(t *testing.T) {
4949
{"stump", "stump/sample-2.md", []string{`title: "Sample 2"`}}, // no archetype file
5050
{"", "sample-3.md", []string{`title: "Sample 3"`}}, // no archetype
5151
{"product", "product/sample-4.md", []string{`title = "SAMPLE-4"`}}, // empty archetype front matter
52+
{"shortcodes", "shortcodes/go.md", []string{
53+
`title = "GO"`,
54+
"{{< myshortcode >}}",
55+
"{{% myshortcode %}}",
56+
"{{</* comment */>}}\n{{%/* comment */%}}"}}, // shortcodes
5257
}
5358

5459
for _, c := range cases {
@@ -126,6 +131,24 @@ title = "{{ .BaseFileName | upper }}"
126131
path: filepath.Join("archetypes", "emptydate.md"),
127132
content: "+++\ndate =\"\"\ntitle = \"Empty Date Arch title\"\ntest = \"test1\"\n+++\n",
128133
},
134+
// #3623x
135+
{
136+
path: filepath.Join("archetypes", "shortcodes.md"),
137+
content: `+++
138+
title = "{{ .BaseFileName | upper }}"
139+
+++
140+
141+
{{< myshortcode >}}
142+
143+
Some text.
144+
145+
{{% myshortcode %}}
146+
{{</* comment */>}}
147+
{{%/* comment */%}}
148+
149+
150+
`,
151+
},
129152
} {
130153
f, err := fs.Source.Create(v.path)
131154
if err != nil {

0 commit comments

Comments
 (0)