2
votes

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:

  • Only using Jekyll to render some fixed and basic components (backward compatible with the environments that do not support JS).
  • Use Jekyll to output meta data, e.g. XML or JSON.
  • Use client side JS and the meta data to provide richer functions.

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:

  • I need a ruby-to-JSON/XML converter in JS. (or a parser to directly read this as a JS data structure).
  • Some fields are not output correctly, e.g. the date.

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:

2
Do you want to create one big JSON file that contains the YAML (as JSON) of all pages, or a separate file for each page (the first is possible just with jekyll, the latter not so much)?Polygnome
@Polygnome , I originally mean the latter one but the former one can also be a workaround (with worse performance because the client JS would read non-relevant information). When the fields used in the frontmatter are fixed, this is not a problem. We can iterate over all pages and then over all intended fields. I tried to build a intra-site search index in this way. However, when pages have their self-defined front-matter fields, I don't know how to dump them all.Pili Hu
Sorry for coming late.... I just found the comment notification of SF. Sigh.Pili Hu

2 Answers

10
votes

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 }}
0
votes

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