Skip to content

Commit 942fad0

Browse files
committed
Merged feature/frontmatter into master
2 parents a256019 + b1fc03f commit 942fad0

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/files.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var fs = require('fs');
33
var path = require('path');
44
var async = require('async');
55
var color = require('bash-color');
6+
var fm = require('front-matter');
67

78
// Use a loop to read all files
89
function ReadFile(filePath, filesJson, sortedBy) {
@@ -60,10 +61,25 @@ function ReadFile(filePath, filesJson, sortedBy) {
6061
var obj = path.parse(newpath);
6162

6263
if (obj.ext === '.md') {
63-
filesJson[obj.name] = newpath + ")\n";
64+
filesJson[getFrontMatterTitle(newpath)] = newpath + ")\n";
6465
}
6566
}
6667
}
68+
69+
function getFrontMatterTitle(newpath)
70+
{
71+
// default to use filename
72+
var title = path.parse(newpath).name;
73+
var content = fs.readFileSync(newpath,'utf8');
74+
75+
if (!fm.test(content)) return title; // skip if no front matter
76+
77+
var frontMatter = fm(content);
78+
if (typeof frontMatter.attributes.title === "undefined") return title; // skip if no 'title' attributes
79+
// todo: set 'title' via config
80+
81+
return frontMatter.attributes.title;
82+
}
6783
}
6884

6985
module.exports = ReadFile;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"bash-color": "0.0.3",
3535
"cheerio": "^0.20.0",
3636
"commander": "^2.9.0",
37+
"front-matter": "^2.1.1",
3738
"fs-extra": "^0.26.2",
3839
"iconv-lite": "^0.4.13",
3940
"lodash": "^3.10.1",

0 commit comments

Comments
 (0)