Pili Hu
Pili Hu

Reputation: 151

Jekyll -- Convert Full YAML Front-matter to XML/JSON

I have been playing around with Jekyll for some time. In some scenarios, I feel the functionality is limited. So I plan to augment my site in this way:

When the required fields are fixed, the task is easy. e.g. may people use Jekyll to generate RSS (xml) feeds; some people use Jekyll to generate a search index (xml with "title", "date", etc).

Now I want to give more freedom to end users of my template so I do not limit what fields are used in the YAML front-matter. Is there a convenient way to convert the full YAML front-matter to JSON to XML format?

I did not find the support of dumping full YAML front-matter in Jekyll or Liquid manual. If I output {{ page }} directly, this is the result:

{"name"=>"Google", "date"=>#, "link"=>"http://google.com/", "open_source"=>false, "open_format"=>false, "rate_use"=>10, "rate_quality"=>9, "tags"=>["Google", "Search Engine", "Information Retrieval"], "language"=>"en", "layout"=>"default", "mathjax"=>false, "url"=>"/sample/google.html", "content"=>"The search engine I use everyday. \n\n"}

At least two problems:

Hope someone can point me to relevant resources and previous trials like this.

Many thanks.

(this is a redirect from jekyll-rb Google Group to get quicker response)

Related questions:

Upvotes: 2

Views: 2837

Answers (2)

Chris Dolphin
Chris Dolphin

Reputation: 1598

For anyone coming across this trying to find a way to convert any of the Jekyll's YAML data (either from Frontmatter or from files in _data) to JSON in their templates:

Jekyll has a filter called jsonify. Super easy and straightforward. https://jekyllrb.com/docs/templates/

Example from the docs:

 {{ site.data.projects | jsonify }}

Upvotes: 12

Scott Luptowski
Scott Luptowski

Reputation: 1

I had the same problem as you and managed to find a Jekyll plugin to help out. It's not perfect (I don't know if there is array support) but it works pretty well.

Here's the link: https://github.com/matthewowen/jekyll-json

Basically just put the ruby file inside your /_plugins directory. I've also discovered that you must input at least one value inside the _config.yml file, which will then be added to each of the objects you create for your respective post pages

Upvotes: 0

Related Questions