除了 Markdown 编辑器,你还需要会用程序来处理它( 二 )


front matter
markdown 写文章确实很方便 , 简单容易上手 , 但是 markdown 不能保存元数据 , 例如作者 , 日期 , 类型这样的结构化的数据 , 如果都生成 html 标签的话提取的时候又稍微麻烦了点 ,还得借助 cheerio才能完成 。
所以 , 为了能方便的保存文章的元数据 , 几乎所有的静态网站生成器都使用 front matter 格式来保存文章 。
front matter 文件通常分为头部和正文部分 , 头部一般使用 yaml、toml和 json 三种格式 , front matter 解析工具需要识别这三种格式的文件头 。 正文部分就是普通的 markdown 内容 。
front-matterfront-matter 也是用 node.js 开发的 , 相比 markdown 解析器来说 , fornt-matter 解析器要简单很多 。
示例文件 example.md
---title: Just hack'ndescription: Nothing to see here---This is some text about some stuff that happened sometime ago解析代码
var fs = require('fs'), fm = require('front-matter')fs.readFile('./example.md', 'utf8', function(err, data){if (err) throw errvar content = fm(data)console.log(content)})解析结果
{attributes: {title: 'Just hack\'n',description: 'Nothing to see here'},body: '\nThis is some text about some stuff that happened sometime ago',frontmatter: 'title: Just hack\'n\ndescription: Nothing to see here'}front matter 虽然格式看起来不太统一 , 却是对 markdown 强有力的补充 。