Skip to content

Commit 19f2e72

Browse files
committed
Support non-md files as archetype files
It now properly uses the extension of the target file to determine archetype file. Fixes #3597 Fixes #3618
1 parent 0f40e1f commit 19f2e72

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

‎create/content.go‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ import (
3030
func NewContent(
3131
ps *helpers.PathSpec,
3232
siteFactory func(filename string, siteUsed bool) (*hugolib.Site, error), kind, targetPath string) error {
33+
ext := helpers.Ext(targetPath)
3334

34-
jww.INFO.Println("attempting to create ", targetPath, "of", kind)
35+
jww.INFO.Printf("attempting to create %q of %q of ext %q", targetPath, kind, ext)
3536

36-
archetypeFilename := findArchetype(ps, kind)
37+
archetypeFilename := findArchetype(ps, kind, ext)
3738

3839
f, err := ps.Fs.Source.Open(archetypeFilename)
3940
if err != nil {
@@ -84,7 +85,7 @@ func NewContent(
8485
// FindArchetype takes a given kind/archetype of content and returns an output
8586
// path for that archetype. If no archetype is found, an empty string is
8687
// returned.
87-
func findArchetype(ps *helpers.PathSpec, kind string) (outpath string) {
88+
func findArchetype(ps *helpers.PathSpec, kind, ext string) (outpath string) {
8889
search := []string{ps.AbsPathify(ps.Cfg.GetString("archetypeDir"))}
8990

9091
if ps.Cfg.GetString("theme") != "" {
@@ -100,13 +101,16 @@ func findArchetype(ps *helpers.PathSpec, kind string) (outpath string) {
100101
// If the new content isn't in a subdirectory, kind == "".
101102
// Therefore it should be excluded otherwise `is a directory`
102103
// error will occur. github.com/gohugoio/hugo/issues/411
103-
var pathsToCheck []string
104+
var pathsToCheck = []string{"default"}
104105

105-
if kind == "" {
106-
pathsToCheck = []string{"default.md", "default"}
107-
} else {
108-
pathsToCheck = []string{kind + ".md", kind, "default.md", "default"}
106+
if ext != "" {
107+
if kind != "" {
108+
pathsToCheck = append([]string{kind + ext, "default" + ext}, pathsToCheck...)
109+
} else {
110+
pathsToCheck = append([]string{"default" + ext}, pathsToCheck...)
111+
}
109112
}
113+
110114
for _, p := range pathsToCheck {
111115
curpath := filepath.Join(x, p)
112116
jww.DEBUG.Println("checking", curpath, "for archetypes")

‎create/content_test.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func TestNewContent(t *testing.T) {
4444
expected []string
4545
}{
4646
{"post", "post/sample-1.md", []string{`title = "Post Arch title"`, `test = "test1"`, "date = \"2015-01-12T19:20:04-07:00\""}},
47+
{"post", "post/org-1.org", []string{`#+title: ORG-1`}},
4748
{"emptydate", "post/sample-ed.md", []string{`title = "Empty Date Arch title"`, `test = "test1"`}},
4849
{"stump", "stump/sample-2.md", []string{`title = "Sample 2"`}}, // no archetype file
4950
{"", "sample-3.md", []string{`title = "Sample 3"`}}, // no archetype
@@ -111,6 +112,10 @@ func initFs(fs *hugofs.Fs) error {
111112
path: filepath.Join("archetypes", "post.md"),
112113
content: "+++\ndate = \"2015-01-12T19:20:04-07:00\"\ntitle = \"Post Arch title\"\ntest = \"test1\"\n+++\n",
113114
},
115+
{
116+
path: filepath.Join("archetypes", "post.org"),
117+
content: "#+title: {{ .BaseFileName | upper }}",
118+
},
114119
{
115120
path: filepath.Join("archetypes", "product.md"),
116121
content: `+++

‎helpers/path.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,12 @@ func GetDottedRelativePath(inPath string) string {
287287
return dottedPath
288288
}
289289

290+
// Ext takes a path and returns the extension, including the delmiter, i.e. ".md".
291+
func Ext(in string) string {
292+
_, ext := fileAndExt(in, fpb)
293+
return ext
294+
}
295+
290296
// Filename takes a path, strips out the extension,
291297
// and returns the name of the file.
292298
func Filename(in string) (name string) {

0 commit comments

Comments
 (0)