Skip to content

Commit 342aeb1

Browse files
committed
Update API
1 parent 0061547 commit 342aeb1

File tree

8 files changed

+103
-103
lines changed

8 files changed

+103
-103
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# bookdown
1+
# bookdown-core
22

33
How it works:
44

@@ -18,5 +18,5 @@ docs/
1818

1919
## API
2020

21-
* `bookdown-core/compile` - Metalsmith middleware.
22-
* `bookdown-core` - Metalsmith generator.
21+
* `bookdown-core` - Metalsmith middleware.
22+
* `bookdown-core/ms` - Metalsmith instance generator.

compile.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/compile.js

Lines changed: 0 additions & 84 deletions
This file was deleted.

lib/index.js

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,84 @@
1-
'use strict'
1+
const ware = require('ware')
2+
const each = require('lodash/collection/each')
3+
const marked = require('marked')
24

3-
const Metalsmith = require('metalsmith')
4-
const compile = require('./compile')
5+
const tocify = require('./tocify')
6+
const indexify = require('./indexify')
7+
8+
module.exports = function (options) {
9+
var compile = ware()
10+
.use(buildIndex)
11+
.use(renderMarkdown)
12+
13+
return compile.run.bind(compile)
14+
}
515

616
/**
7-
* Returns a metalsmith object.
17+
* Builds toc.json and index.json.
18+
*
19+
* Each toc item has:
20+
*
21+
* - `sections` (key/value of sections)
22+
* - `source` (path to source)
23+
* - `title` (title)
24+
* - `url` (URL, if applicable)
25+
*
26+
* Each index item has:
27+
*
28+
* - `source`
29+
* - `title`
830
*/
931

10-
function bookdown (cwd, options) {
11-
return Metalsmith(cwd)
12-
.source('.')
13-
.destination('_bookdown')
14-
.ignore('!{*.md,docs}') // only include /docs/ and /*.md
15-
.ignore('_bookdown') // ignore output
16-
.use(compile(options))
32+
function buildIndex (files, ms, done) {
33+
if (!files['docs/README.md']) {
34+
var err = new Error('No table of contents found')
35+
return done(err)
36+
}
37+
38+
var toc = tocify(files['docs/README.md'].contents.toString())
39+
var index = indexify(toc)
40+
41+
files['toc.json'] = { contents: JSON.stringify(toc, null, 2) + '\n' }
42+
files['index.json'] = { contents: JSON.stringify(index, null, 2) + '\n' }
43+
44+
done()
1745
}
1846

19-
module.exports = bookdown
47+
/**
48+
* Converts .md to .html.
49+
*
50+
* At the end of this, you get a site with `.html` files (bare, no layout)
51+
* and a `toc.json` and `index.json`.
52+
*
53+
* Each html also has:
54+
*
55+
* - `title` (title according to TOC)
56+
* - `source` (path of source)
57+
* - `markdown` (raw Markdown source)
58+
* - `html` (rendered HTML)
59+
*/
60+
61+
function renderMarkdown (files, ms, done) {
62+
var sources = Object.keys(files)
63+
var pages = JSON.parse(files['index.json'].contents)
64+
65+
// 3: render each page
66+
each(pages, (opts, fname) => {
67+
const file = files[opts.source]
68+
file.markdown = file.contents
69+
file.html = marked(file.markdown.toString(), { gfm: true, tables: true })
70+
file.title = opts.title
71+
file.source = opts.source
72+
file.contents = file.html
73+
files[fname] = file
74+
})
75+
76+
// delete sources
77+
sources.forEach((fname) => {
78+
if (fname !== 'toc.json' && fname !== 'index.json') {
79+
delete files[fname]
80+
}
81+
})
82+
83+
done()
84+
}

lib/ms.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict'
2+
3+
const Metalsmith = require('metalsmith')
4+
const compile = require('./compile')
5+
6+
/**
7+
* Returns a metalsmith object.
8+
*/
9+
10+
function bookdown (cwd, options) {
11+
return Metalsmith(cwd)
12+
.source('.')
13+
.destination('_bookdown')
14+
.ignore('!{*.md,docs}') // only include /docs/ and /*.md
15+
.ignore('_bookdown') // ignore output
16+
.use(compile(options))
17+
}
18+
19+
module.exports = bookdown

ms.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib/ms')

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"scripts": {
77
"test": "mocha"
88
},
9-
"author": "",
10-
"license": "ISC",
9+
"author": "Rico Sta. Cruz <rico@ricostacruz.com>",
10+
"license": "MIT",
1111
"dependencies": {
1212
"lodash": "3.10.1",
1313
"marked": "0.3.5",

test/compile_test.js renamed to test/index_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const compile = require('../lib/compile')()
1+
const compile = require('../index')()
22

33
describe('compile', function () {
44
beforeEach(function (done) {

0 commit comments

Comments
 (0)