我正在尝试遍历我的内容树并从各种降价文件中提取元数据。我看到了 Paginator 插件示例,但我觉得我的用例比 Paginator 所需的要简单得多。
如果我的内容目录结构如下所示:
projects/
project-name-1/
index.md
project-name-2/
index.md
project-name-3/
index.md
我想遍历我的内容并检索所有“标题”元数据属性。如果我在玉中尝试一个简单的循环,例如:
each project in content.projects
- console.log(project.metadata.title);
我记录未定义。
我尝试将这种逻辑移动到一个简单的插件中,灵感来自 Paginator(用 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();
};
虽然我能够在记录的对象中看到我的元数据,但我不完全确定如何从这里访问它。有没有更简单的方法可以像这样从内容树中提取元数据?任何帮助深表感谢!