Skip to content

Commit 0399542

Browse files
authored
Merge pull request #16 from garyng/master
Feature: Add ability to get title from file's frontmatter
2 parents 584903f + 53d4f8f commit 0399542

File tree

6 files changed

+56
-9
lines changed

6 files changed

+56
-9
lines changed

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,33 @@ or, For example:
5656
$ book sm -r ../sailsjs-docs-gitbook/en -i 0home -u 'myApp' -c 'concepts, reference, userguides' -n "Sails.js 官方文档(中英合辑)"
5757
```
5858

59-
**Note**`-s` or `--sortedBy` can not be given `-`, commander.js will parse it an option. But you can set it in `book.json` as follow.
59+
To see the command line options:
60+
61+
```
62+
$ book sm --help
63+
64+
Usage: summary|sm [options]
65+
66+
Generate a `SUMMARY.md` from a folder
67+
68+
Options:
69+
70+
-h, --help output usage information
71+
-r, --root [string] root folder, default is `.`
72+
-n, --bookname [string] book name, default is `Your Book Name`.
73+
-c, --catalog [list] catalog folders included book files, default is `all`.
74+
-i, --ignores [list] ignore folders that be excluded, default is `[]`.
75+
-u, --unchanged [list] unchanged catalog like `request.js`, default is `[]`.
76+
-o, --outputfile [string] output file, defaut is `./SUMMARY.md`
77+
-s, --sortedBy [string] sorted by sortedBy, for example: `num-`, defaut is sorted by characters
78+
-d, --disableTitleFormatting don't convert filename/folder name to start case (for example: `JavaScript` to `Java Script`), default is `false`
79+
80+
```
81+
82+
**Notes**
83+
* The article title is taken from `title` property in the articles front-matter. If this property is not available, the articles filename will be used as title for the summary.
84+
* The option `-s` or `--sortedBy` can not be given `-` as argument, because commander.js will parse it an option. But you can set it in `book.json` as follows.
85+
6086

6187
2> Create a `book.json` in the book`s root folder
6288

@@ -70,7 +96,8 @@ for example:
7096
"catalog": "all", // or [chapter1,chapter2, ...]
7197
"ignores": [], //Default: '.*', '_book'...
7298
"unchanged": [], // for example: ['myApp'] -> `myApp` not `My App`
73-
"sortedBy": "-"
99+
"sortedBy": "-",
100+
"disableTitleFormatting": true // default: false
74101
}
75102
```
76103

bin/summary.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ program
2727
.option("-u, --unchanged [list]", "unchanged catalog like `request.js`, default is `[]`.")
2828
.option("-o, --outputfile [string]", "output file, defaut is `./SUMMARY.md`")
2929
.option("-s, --sortedBy [string]", "sorted by sortedBy, for example: `num-`, defaut is sorted by characters")
30+
.option("-d, --disableTitleFormatting", "don't convert filename/folder name to start case (for example: `JavaScript` to `Java Script`), default is `false`")
3031
.action(function(options) {
3132
// generate `SUMMARY.md`
32-
// Fixme
33+
// Fixme
3334
// if (options.length >= 1) {
3435
// console.log(color.red('\nError! The sub commands "%s" has been deprecated, please read the follow messages:'), cmd);
3536
// program.help();

lib/bookJson.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ function BookConfig(root) {
1919
book.unchanged = config.unchanged || [];
2020
book.bookname = config.bookname || 'Your Book Name';
2121
book.sortedBy = config.sortedBy || '';
22-
22+
book.disableTitleFormatting = config.disableTitleFormatting || false;
23+
2324
return book;
2425
}
2526

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;

lib/summary/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ var root,
2020
catalog,
2121
ignores,
2222
unchanged,
23-
sortedBy;
23+
sortedBy,
24+
disableTitleFormatting;
2425

2526
// Get options
2627
function init(options) {
@@ -34,6 +35,7 @@ function init(options) {
3435
unchanged = options.unchanged || bookConfig.unchanged;
3536
bookname = options.bookname || bookConfig.bookname;
3637
sortedBy = options.sortedBy || bookConfig.sortedBy;
38+
disableTitleFormatting = options.disableTitleFormatting || bookConfig.disableTitleFormatting;
3739
}
3840

3941
// Get summary
@@ -166,9 +168,8 @@ function prettyCatalogName(fileName) {
166168
fileName = fileName.match(folderNameReg)[1];
167169
}
168170
}
169-
170171
// Don`t format the files like `req.options.*` using dot, unchanged or Chinese string.
171-
if (_.size(fileName.split(".")) > 1 || _.includes(unchanged, fileName) || isChinese(fileName)) {
172+
if (_.size(fileName.split(".")) > 1 || _.includes(unchanged, fileName) || isChinese(fileName) || disableTitleFormatting) {
172173
return fileName;
173174
}
174175
return _.startCase(fileName);
@@ -191,7 +192,7 @@ function isSkiped(key, skip) {
191192
// Write to file encoded with utf-8
192193
function writeFile(fileName, data) {
193194
fs.writeFile(fileName, data, 'utf-8', function() {
194-
console.log(color.green("Finished, generate a SUMMARY.md successfully."));
195+
console.log(color.green("Finished, generated '"+fileName+"' successfully."));
195196
})
196197
}
197198

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)