How to convert blog posts into data/posts.json?

Goal:
On build, convert blog’s front matter into data/posts.json so that I can iterate over data.posts.json with my shiny MVCofTheWeek.js

Background
Using the middleman-blog gem, trying to convert blog front matter into a json file that’s stored in data/posts.json

Problem I can’t seem to access the blog.articles, page_articles, etc in config.rb the way I can in a view (They seem to be on Middleman::Application::MiddlemanApplication1). I can’t tell if I’m missing the proper helper method or need some extra configuration to get access to blog posts in config.rb

Attempts So far, I’ve tried the following but neither have blog in scope. :

  1. Explored building a custom extension and trying the activate
  2. Accessing the posts in the activate :blog bloc but that is a different class (Middleman::Configuration::ConfigurationManager) and doesn’t have #articles

Any ideas?

Hello @wwwoodall

Have you try to hack middleman navtree ?

@wwwoodall is this something you are planning to release as a gem? I was recently wondering if something like this existed for the very same reason (bring articles into Ember.js). Would be happy to help out if you are and any help needed.

@flexbox,
I did see that in my search and it looks interesting. I starred it.
But thanks for pointing that out! :smile:

@gordonbisnor,
I ended up taking a different approach to solve the initial problem. Which was to skip data and just grab blog.articles and put direclyt in init.js.erb files. I also used a helper method to clean it up some.

// init.js.erb

var app = app || {};

$(function() {
  'use strict';
  var data = <%= blog_posts_as_json %>; // get data as json

  var posts = _.map(data, function(p) {
    return JSON.parse(p) // parse json and put in var posts
  });
});
## config

helpers do

  def blog_posts_as_json
   blog.articles.map { |b|
      b.data.to_json
    }
  end
end

However, this doesn’t let me add things like the article summary to the hash so there are some issues to be worked out.

@wwwoodall Good idea! Thanks for the update! And so I guess you could add slug and summary etc to your frontmatter to get whatever details you might need in the JSON using that method.