Generate pages from yml data files

I have several yml files that contain data that I need to transform into pages.
Let’s say:

/data/people.yml

---
people:
  - name: Jane
    email: jane@doe.com
    description: hello
  - name: Joe
    email: joe@black.com
    description: hi
---

I want something that generates these files:

/content/people/jane.md
/content/people/joe.md

And each file should look like:

---
title: Jane
email: jane@doe.com
---
Hello

Is there any official way to do this?

I normally do this with a ruby script, but I would like to do this with a node package or at least a gulp task.

I read some old posts that say that there is no way, but since they’re old, I’m wondering if there is a new way.

I was going to say Hugo doesn’t do that, but as I was typing, I remembered that there is such a thing as Custom Output formats https://gohugo.io/templates/output-formats/

Check that out. You “might” be able to make a custom output format for the markdown. HOWEVER, I doubt this would be made into html pages. So, not sure what you are trying to accomplish here.

hugo doesn’t do this, so you can create any script or task you prefer to create those files.

yea if you want that markdown rendered into a page, you need to do it apart from hugo. but if you literally want to create markdown files as an output, you can do that with the custom outputs.

That’s… kinda weird to say. Because you still need source markdown files to generate the output.

So perhaps I misunderstood the OP. @guayom, you wanted those markdown files in the people section of your content folder in order to generate web pages, ne?

I’m trying to do like a generator that literally creates .md files. I’m not sure hat I can use custom outputs for that, but I’ll check it out.

I don’t have any .md files to begin with. I want them to be generated automatically. I think you understood correctly @maiki.

Okay, cool, then you know what you need to do (use a separate task/script to generate those in content).

I will add one thing: you can be really terse with the markdown files. For instance, you can generate a content file for each record in your data file, and make the title of the content match a value in the data. Then in that section’s single template you can pull the rest of info over.

So you’d have a file at /content/people/jane.md read:

---
title: Jane
date: 2007-11-02T02:06:46+00:00
---

And then in your /layouts/section/people.html pull in the other info, via named value.

I suggest this, because then you can edit your people info in one file, and the creation task should be a little simpler. :slight_smile:

1 Like

Yea I’m wrong. Can’t build a page with any output without an md file to start the process

@maiki I kinda like your idea for some things. That’s a good idea.

For the specific task I have in mind, what I’m trying to do is basically import the content pulled from another database. After that, I will just have to maintain this new files that I generated. I could basically delete the yml databases.

Oh cool. Yeah, you’ll need to convert it to the markdown files in content, but it is only a one time deal, so you are on the right track.

Make sure to share yer script in #tips-tricks once you’ve go it working! :slight_smile:

@guayom Also @budparr 's excellent article might be a source of inspiration:

https://www.thenewdynamic.org/article/migrate-from-any-cms-to-markdown/

1 Like

Some very good tips there! Nice article! Thanks a lot man!

1 Like