Skip to content

Commit 518c35d

Browse files
committed
Initail
0 parents  commit 518c35d

File tree

10 files changed

+226
-0
lines changed

10 files changed

+226
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

bin/bookdown

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env node
2+
const Metalsmith = require('metalsmith')
3+
const each = require('lodash/collection/each')
4+
const marked = require('marked')
5+
6+
var app = bookdown(process.cwd())
7+
app.build((err) => {
8+
if (err) throw err
9+
})
10+
11+
function bookdown (cwd) {
12+
return Metalsmith(cwd)
13+
.source('.')
14+
.destination('_bookdown')
15+
.ignore('**/*.!(md)') // ignore non-md files
16+
.ignore('.*') // ignore dot directories
17+
.ignore('node_modules')
18+
.ignore('_bookdown') // ignore output
19+
.ignore('_book')
20+
.use(bookdownParse)
21+
}
22+
23+
function bookdownParse (files, ms, done) {
24+
var sources = Object.keys(files)
25+
console.log(sources)
26+
27+
// 1: find or infer the TOC.
28+
// - strip out docs/ prefix
29+
// - turn the first page in the toc to `index.html`
30+
// - turn README to index
31+
// - flatten it as `pages`
32+
var toc = {
33+
sections: {
34+
'index.html': {
35+
source: 'README.md'
36+
},
37+
'docs.html': {
38+
source: 'docs/docs.md',
39+
sections: {
40+
'rails.html': {
41+
source: 'docs/rails.md'
42+
}
43+
}
44+
},
45+
}
46+
}
47+
48+
files['toc.json'] = {
49+
contents: JSON.stringify(toc) + '\n'
50+
}
51+
52+
// 2: flatten TOC to pages.
53+
var pages = {
54+
'index.html': { source: 'README.md' },
55+
'rails.html': { source: 'docs/rails.md' }
56+
}
57+
58+
files['pages.json'] = {
59+
contents: JSON.stringify(pages) + '\n'
60+
}
61+
62+
63+
// 3: render each page
64+
each(pages, (opts, fname) => {
65+
const file = files[opts.source]
66+
file.markdown = file.contents
67+
file.contents = marked(file.contents.toString(), { gfm: true, tables: true })
68+
files[fname] = file
69+
})
70+
71+
// 4: add assets
72+
73+
// delete sources
74+
sources.forEach((fname) => {
75+
delete files[fname]
76+
})
77+
78+
done()
79+
}

index.js

Whitespace-only changes.

lib/tocify.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var marked = require('marked')
2+
var slugify = require('slugify')
3+
4+
module.exports = function tocify (md) {
5+
var tokens = marked.lexer(md)
6+
7+
var re = { sections: {} }
8+
var crumbs = [scope]
9+
var current = re
10+
var scope
11+
12+
tokens.forEach((token) => {
13+
switch (token.type) {
14+
case 'list_start':
15+
scope = current.sections = {}
16+
crumbs.push(scope)
17+
break
18+
19+
case 'text':
20+
const m = token.text.match(/^\[([^\]]*)\]\((.*)\)$/)
21+
const title = m ? m[1] : token.text
22+
const source = m ? m[2] : null
23+
const key = slugify(title).toLowerCase()
24+
25+
current = scope[key] = { title: title }
26+
if (source) current.source = source
27+
break
28+
29+
case 'list_end':
30+
crumbs.pop()
31+
scope = crumbs[crumbs.length - 1]
32+
break
33+
}
34+
})
35+
36+
return re
37+
}

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "bookdown",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha"
8+
},
9+
"bin": {
10+
"bookdown": "bin/bookdown"
11+
},
12+
"author": "",
13+
"license": "ISC",
14+
"dependencies": {
15+
"lodash": "3.10.1",
16+
"marked": "0.3.5",
17+
"metalsmith": "2.1.0",
18+
"slugify": "0.1.1"
19+
},
20+
"devDependencies": {
21+
"expect": "1.11.1",
22+
"mocha": "2.3.3"
23+
}
24+
}

test/basic_test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('my project', function () {
2+
it('works', function () {
3+
expect(2).toEqual(2)
4+
})
5+
})

test/mocha.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--require test/setup

test/setup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global.expect = require('expect')

test/tocify_test.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const tocify = require('../lib/tocify')
2+
3+
describe('tocify', function () {
4+
var toc = [
5+
'# toc',
6+
'',
7+
'* [readme](/README.md)',
8+
].join('\n')
9+
10+
var output
11+
12+
it('works', function () {
13+
output = tocify([
14+
'* [Readme](/README.md)',
15+
].join('\n'))
16+
17+
expect(output).toEqual({
18+
sections: {
19+
readme: {
20+
title: 'Readme',
21+
source: '/README.md'
22+
}
23+
}
24+
})
25+
})
26+
27+
it('handles non-links', function () {
28+
output = tocify([
29+
'* Readme',
30+
].join('\n'))
31+
32+
expect(output).toEqual({
33+
sections: {
34+
readme: {
35+
title: 'Readme'
36+
}
37+
}
38+
})
39+
})
40+
41+
it('takes care of nesting', function () {
42+
output = tocify([
43+
'* [Readme](/README.md)',
44+
'* Getting Started',
45+
' * [Install](/docs/install.md)',
46+
' * [Usage](/docs/usage.md)'
47+
].join('\n'))
48+
49+
expect(output).toEqual({
50+
sections: {
51+
readme: {
52+
title: 'Readme',
53+
source: '/README.md',
54+
},
55+
'getting-started': {
56+
title: 'Getting Started',
57+
sections: {
58+
'install': {
59+
title: 'Install',
60+
source: '/docs/install.md',
61+
},
62+
'usage': {
63+
title: 'Usage',
64+
source: '/docs/usage.md',
65+
}
66+
}
67+
}
68+
}
69+
})
70+
})
71+
72+
})

test/todo_test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
describe('to do', function () {
2+
it('parsing toc')
3+
it('flattening toc to pages')
4+
it('rendering pages')
5+
})
6+

0 commit comments

Comments
 (0)