HUGO

Published path of alternate output formats

What I’m looking for though is outputting formats from other sections into a specific directory/path.

Example:

root/
├─ content/
│  ├─ section1/
│  │  ├─ post1.md
│  ├─ section2/
│  │  ├─ post2.md
├─ public/
│  ├─ index.html
│  ├─ section1/
│  │  ├─ post1.html
│  ├─ section2/
│  │  ├─ post2.html
│  ├─ output/
│  │  ├─ section1/
│  │  │  ├─ post1.json
│  │  ├─ section2/
│  │  │  ├─ post2.json

Is this possible?

@jmooring

This site configuration:

uglyURLs = true

[outputFormats.html]
noUgly = true

[outputFormats.json]
path = 'output'

Will produce this structure:

public
├── output/
│   ├── section1/
│   │   └── post1.json
│   └── section2/
│       └── post2.json
├── section1/
│   ├── post1/
│   │   └── index.html
│   └── index.html
├── section2/
│   ├── post2/
│   │   └── index.html
│   └── index.html
└── index.html

This assumes:

  1. You have a JSON template (e.g., layouts/_default/single.json.json), and
  2. You have enabled JSON output at the site, section, or page level
1 Like

When do you sleep, @jmooring? :rofl:

Would there be a method for a list.json version of this, as well?

layouts/_default/list.json.json

Strange one there, I have a layouts/_default/list.json.json. Bupkis.

public/
├── output/
│   ├── section-1/
│   │   └── post1.json
│   └── section-2/
│       └── post2.json
├── section-1/
│   ├── post1/
│   │   └── index.html
│   ├── index.html
│   └── output.json     <-- from list.json.json
├── section-2/
│   ├── post2/
│   │   └── index.html
│   ├── index.html
│   └── output.json     <-- from list.json.json
└── index.html

https://gohugo.io/templates/output-formats/#configure-output-formats

path

sub path to save the output files.

Try it:

git clone --single-branch -b hugo-forum-topic-23945 https://github.com/jmooring/hugo-testing hugo-forum-topic-23945
cd hugo-forum-topic-23945
hugo && tree public

I’ll look at it and try to see the difference. Single files come out fine… the listing doesn’t. So, I may be doing something wrong… will check now and get back to you.

Joe, I see it working in your example… I don’t know why it’s working for you and not for me though. Singles printed out fine, but not the list. Which I thought should have happened since a template was available.

The repository is updated with the config, you can see the list items are there and even view the output of the single files, just not the list… :man_shrugging:t4:

No, it’s there…

rm -rf public/ && hugo && find public/ -name api.json
public/reports/api.json
public/contact-us/api.json
public/about-project/api.json
public/become-sponsor/api.json
public/privacy-policy/api.json
public/updates/api.json

Ok… was looking for it in the api folder generated. I think, I miscommunicated then. Is there a way to have the list print in the external directory just the same as the singles? So, to api/ not api.json?

So, like api/index.json?

No, there isn’t. I don’t much care for the way it works either.

If it were me, I would configure like this:

# uglyURLs = true

# [outputFormats.html]
# noUgly = true

# [outputFormats.json]
# path = 'output'

[outputs]
page = ['html','json']
section = ['html','json']

And then I would configure URL rewrites (redirects) on the server. Hopefully you have some control of that.

Hey, you can’t always get what you want…

Soo… I can work that, Netlify has a rewrite option that I’ll try out.

It would have been cleaner if it was in HUGO, but it’s not that much of an extra.

While I have you here, is there a way to reduce the number of JSON files created… i.e. in other directories where not needed?

Yes.

Currently you have this:

[outputs]
page = ['html','json']
section = ['html','json']

Which renders JSON files for every page and every section. Get rid of that bit.

To enable JSON output for a single page (let’s say a report), place this in front matter:

outputs = ['html','json']

Or, to enable it for a section, see:
https://gohugo.io/content-management/front-matter#front-matter-cascade

You can define the cascade in a section page (e.g., /content/section-1/_index.md) or in site configuration.

Super helpful.

I lost out on the rewrite with Netlify, so, I’m stuck with what I got

Thanks for all the help. You are a beast, bro.

1 Like

Hey, @jmooring… what if I created a content folder? Would I be able to piece together an index.json that way? Like the index file pulls from sections?

I tried it but not sure, what adjustment would need to be made… I’m thinking it out… I’ll build it out upload it to Github for you to check out.

Yes, and that works if you don’t want a section page in HTML format.

git clone --single-branch -b hugo-forum-topic-23945 https://github.com/jmooring/hugo-testing hugo-forum-topic-23945
cd hugo-forum-topic-23945
rm -rf public/ && hugo && tree public/
public/
├── api/
│   ├── section-1/
│   │   ├── index.json
│   │   ├── post-1-1.json
│   │   └── post-1-2.json
│   └── section-2/
│       ├── index.json
│       ├── post-2-1.json
│       └── post-2-2.json
├── section-1/
│   ├── post-1-1/
│   │   └── index.html
│   └── post-1-2/
│       └── index.html
├── section-2/
│   ├── post-2-1/
│   │   └── index.html
│   └── post-2-2/
│       └── index.html
└── index.html

The limitation is that the url parameter in front matter applies to all output formats for a page.

1 Like

Correction. Pull the repo again and take a look. I tried something that I didn’t think would work: define a directory in the front matter url, excluding the filename. It worked.

Related to my “I don’t much care for the way it works” comment:

1 Like

@jmooring, that is exactly what I was looking for. I would say this issue is closed on that note. I’ll proceed along those lines… I’ll message once everything has been updated.

1 Like