Can you use Markdown files for default content in a theme?

I’m currently working on a theme for my own personal use. I’d like to include a particular page by default on any site that uses this theme, so I created a Markdown file in my theme’s content directory with the page’s text. At first everything appeared to be working properly, but then I found that this seems to be causing problems with hugo new. Consider this command:

$ hugo new posts/test.md
panic: [BUG] no Page found for "/path/to/site/themes/mytheme/content/posts/test.md"

goroutine 1 [running]:
github.com/gohugoio/hugo/create.(*contentBuilder).applyArcheType(0xc0008e4000, {0xc0008c4910, 0x4a}, {0xc00070ed10, 0xa})
	github.com/gohugoio/hugo/create/content.go:253 +0x354
github.com/gohugoio/hugo/create.(*contentBuilder).buildFile(0xc0008e4000)
	github.com/gohugoio/hugo/create/content.go:222 +0x1ad
github.com/gohugoio/hugo/create.NewContent(0xc0001dd1e0, {0x0, 0x0}, {0x7ffeefbffc39, 0x41a9385})
	github.com/gohugoio/hugo/create/content.go:98 +0x80d
github.com/gohugoio/hugo/commands.(*newCmd).newContent(0xc000090cf0, 0xc000980c80, {0xc00035bc10, 0x1, 0x0})
	github.com/gohugoio/hugo/commands/new.go:83 +0x150
github.com/spf13/cobra.(*Command).execute(0xc000980c80, {0xc00035bbe0, 0x1, 0x1})
	github.com/spf13/cobra@v1.2.1/command.go:856 +0x60e
github.com/spf13/cobra.(*Command).ExecuteC(0xc000989180)
	github.com/spf13/cobra@v1.2.1/command.go:974 +0x3bc
github.com/gohugoio/hugo/commands.Execute({0xc0001840a0, 0x2, 0x2})
	github.com/gohugoio/hugo/commands/hugo.go:92 +0xb4
main.main()
	github.com/gohugoio/hugo/main.go:23 +0x58

Despite the error, Hugo does manage to create a new file at /path/to/site/themes/mytheme/content/posts/test.md with the following text:

---
title: "Content Placeholder"
_build:
  render: never
  list: never
  publishResources: false
---

It looks like Hugo is trying—and failing—to create a new post in my theme’s content directory rather than in my site’s content directory. Is this expected behavior? If so, is there any other way to process Markdown files in a theme?

To answer your question: Yes you can.

The error you get is a bug (see issue below) and I will fix it. I rewrote that feature for the latest release, and I seem to have missed something vs. themes.

Question: Dir you run “hugo new posts/test.md” from the theme or from the project?

1 Like

Got it! Good to know that this is a known issue. :+1:

Oh, sorry I wasn’t clear: I ran hugo new from the site’s top-level directory.

Just out of curiosity, is hugo new supposed to generate a new post in the theme’s content directory if it is run from within a theme, instead?

Well, it wasn’t a known issue until you told me about it.

That said, I’m having problems reproducing it on my MacBook. What OS are you running? Do you have a simple step-by-step variant that fails like this?

I believe I have reproduced and identified the config causing the issue in https://github.com/gohugoio/hugo/issues/9129#issuecomment-962103220.

HTH.

I do! I’ll post step-by-step instructions in the GitHub issue.