Skip to content

Commit 9412855

Browse files
committed
Output metadata into docpress.json
1 parent 358ae2b commit 9412855

File tree

5 files changed

+60
-21
lines changed

5 files changed

+60
-21
lines changed

lib/index.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const indexify = require('./indexify')
99
const fixHtml = require('./fix_html')
1010
const syntaxHighlight = require('./syntax_highlight')
1111

12+
const assign = Object.assign
13+
1214
module.exports = function (options) {
1315
var app = ware()
1416
.use(buildIndex)
@@ -21,8 +23,17 @@ module.exports = function (options) {
2123
}
2224

2325
/**
24-
* Builds toc.json and index.json.
26+
* Builds docpress.json (`files['docpress.json']`). It is as JSON file, which
27+
* has the following metadata:
28+
*
29+
* - `index` — pages index.
30+
* - `toc` — table of contents (as a tree).
31+
* — `sources` — a mapping of source to destination filenames.
2532
*
33+
* These datas can be obtained via `files['docpress.json'].index` (ie, as
34+
* Metalsmith file metadata) or by parsing docpress.json.
35+
*
36+
* ### Table of contents
2637
* Each toc item has:
2738
*
2839
* - `sections` (key/value of sections)
@@ -32,12 +43,18 @@ module.exports = function (options) {
3243
* - `url` (URL, if applicable)
3344
* - `headings` (array of `{ title, depth, id }`)
3445
*
46+
* ### Index
3547
* Each index item has:
3648
*
37-
* - `source`
38-
* - `title`
39-
* - `slug`
40-
* - `headings`
49+
* - `source` — path to source
50+
* - `title` — the page title according to TOC
51+
* - `slug` — slug for the page
52+
* - `headings` — an array of headings
53+
*
54+
* ### Files
55+
* It will modify files with new metadata:
56+
*
57+
* - `newName` — the build output filename
4158
*/
4259

4360
function buildIndex (files, ms, done) {
@@ -64,9 +81,13 @@ function buildIndex (files, ms, done) {
6481

6582
verifyIndex(indexes.index, files)
6683

67-
files['toc.json'] = { contents: JSON.stringify(toc, null, 2) + '\n' }
68-
files['index.json'] = { contents: JSON.stringify(indexes.index, null, 2) + '\n' }
69-
files['sources.json'] = { contents: JSON.stringify(indexes.sources, null, 2) + '\n' }
84+
var data = {
85+
toc: toc,
86+
index: indexes.index,
87+
sources: indexes.sources
88+
}
89+
90+
files['docpress.json'] = assign({}, data, { contents: JSON.stringify(data, null, 2) + '\n' })
7091

7192
done()
7293
}
@@ -86,8 +107,8 @@ function buildIndex (files, ms, done) {
86107
*/
87108

88109
function renderMarkdown (files, ms, done) {
89-
var pages = JSON.parse(files['index.json'].contents)
90-
var sources = JSON.parse(files['sources.json'].contents)
110+
var pages = files['docpress.json'].index
111+
var sources = files['docpress.json'].sources
91112

92113
// render each page
93114
each(pages, (page, fname) => {

test/fixture_test.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ describe('fixture', function () {
1717

1818
it('outputs the right files', function () {
1919
expect(fx.exists('_docpress/index.html')).toEqual(true)
20-
expect(fx.exists('_docpress/index.json')).toEqual(true)
21-
expect(fx.exists('_docpress/toc.json')).toEqual(true)
20+
expect(fx.exists('_docpress/docpress.json')).toEqual(true)
2221
expect(fx.exists('_docpress/testing.html')).toEqual(true)
2322
expect(fx.exists('_docpress/cleanup.html')).toEqual(true)
2423
})
@@ -39,23 +38,42 @@ describe('fixture', function () {
3938
expect(fx.exists('_docpress/README.md')).toEqual(false)
4039
})
4140

42-
describe('toc.json', function () {
41+
describe('docpress.json', function () {
4342
before(function () {
44-
data = fx.read('_docpress/toc.json')
43+
data = fx.read('_docpress/docpress.json')
4544
data = JSON.parse(data)
4645
})
4746

47+
it('has index', function () {
48+
expect(data.index).toExist()
49+
})
50+
51+
it('has toc', function () {
52+
expect(data.toc).toExist()
53+
})
54+
55+
it('has sources', function () {
56+
expect(data.sources).toExist()
57+
})
58+
})
59+
60+
describe('docpress.json/toc', function () {
61+
before(function () {
62+
data = fx.read('_docpress/docpress.json')
63+
data = JSON.parse(data).toc
64+
})
65+
4866
it('renders proper json', function () { })
4967

5068
it('has headings', function () {
5169
expect(data.sections[0].headings.length).toBeGreaterThan(2)
5270
})
5371
})
5472

55-
describe('index.json', function () {
73+
describe('docpress.json/index', function () {
5674
before(function () {
57-
data = fx.read('_docpress/index.json')
58-
data = JSON.parse(data)
75+
data = fx.read('_docpress/docpress.json')
76+
data = JSON.parse(data).index
5977
})
6078

6179
it('renders proper json', function () { })

test/index/image_refs_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('index/image refs:', function () {
3434
})
3535

3636
it('adds to sources', function () {
37-
expect(JSON.parse(this.files['sources.json'].contents)).toEqual({
37+
expect(JSON.parse(this.files['docpress.json'].contents).sources).toEqual({
3838
'README.md': 'index.html',
3939
'docs/images/screenshot.png': 'images/screenshot.png'
4040
})

test/index/index_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ describe('index:', function () {
4444

4545
describe('toc.json:', function () {
4646
beforeEach(function () {
47-
this.tocFile = this.files['toc.json']
48-
this.toc = JSON.parse(this.tocFile.contents)
47+
this.tocFile = this.files['docpress.json']
48+
this.toc = JSON.parse(this.tocFile.contents).toc
4949
})
5050

5151
it('renders.json', function () {

test/index/unused_md_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('index/unused md files:', function () {
2929

3030
it('removes unprocessed .md files', function () {
3131
expect(Object.keys(this.files)).toEqual([
32-
'toc.json', 'index.json', 'sources.json', 'index.html'
32+
'docpress.json', 'index.html'
3333
])
3434
})
3535
})

0 commit comments

Comments
 (0)