1

I'm trying to loop through my Content Tree and pull out metadata from various markdown files. I saw the Paginator plugin example, but I feel like my use case is much more simple than needed from the Paginator.

If my contents dir structure looks like this:

projects/
  project-name-1/
    index.md
  project-name-2/
    index.md
  project-name-3/
    index.md

I want to loop through my contents and retrieve all of the "title" metadata properties. If I try a simple loop in jade, such as:

each project in content.projects
  - console.log(project.metadata.title);

I log undefined.

I tried moving this kind of logic into a simple plugin as inspired by the Paginator (written in js):

module.exports = function(env, callback){
  _ = require('underscore');
  var options = {projects: 'projects'};
  function getProjects(contents) {
    //helper that returns a list of projects found in *contents*
    //note that each article is assumed to have its own directory in the articles directory
    // console.dir(contents[options.projects]);
    _.each(contents[options.projects], function(i){console.log(i);});
    var projects = {};//@todo create a new collection containing each metadata object
    return projects;
  }
  //add the article helper to the environment so we can use it later
  env.helpers.getProjects = getProjects;

  //tell the plugin manager we are done
  callback();
};

and while I'm able to see my metadata inside the logged object, I'm not entirely sure how to access it from here. Is there a simpler way to pull out metadata from the content tree like this? Any help is much appreciated!

1 Answers1

0

The problem you're having is that you are iterating over a object and not an array.

You can solve it in 2 ways, either use wintersmith's "content groups" that are arrays.

each project in contents.projects._.directories
  - console.log(project.index.metadata.title)

Note that we are iterating over the directories here since that reflects your setup, if you had your projects in just one directory we would iterate over the 'pages' content group.

The other option is to use 'for in' enumeration. In jade it looks like this:

each name, content in contents.projects
  - console.log(content.index.metadata.title)

The problem with this approach is that if you put something other than a directory inside of 'projects' it will break.

Johan Nordberg
  • 356
  • 4
  • 8