Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4515906

Browse files
committedNov 22, 2016
feat(config): add file property for notes
Fixes documentationjs#609
1 parent 43e01ce commit 4515906

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed
 

‎docs/CONFIG.md

+21
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,24 @@ This puts the top level API documentation for the `Map`, `LngLat`, and `LngLatBo
2727
items in the given order, and inserts a narrative item titled `Geography`
2828
after the section on maps. The `description` property of that narrative item
2929
is interpreted as Markdown.
30+
If you would like reuse your existing markdown files or just keep the content separate from the configuration you can use the `file` property. It is a filename that will be resolved against `process.cwd()`.
31+
32+
So with a `documentation.yml` file like this
33+
34+
```yml
35+
toc:
36+
- Map
37+
- name: Geography
38+
file: geo.md
39+
- LngLat
40+
- LngLatBounds
41+
```
42+
43+
and a file `geo.md`
44+
45+
```markdown
46+
These are Mapbox GL JS's ways of representing locations
47+
and areas on the sphere.
48+
```
49+
50+
it would produce the same output as the previous example.

‎lib/sort.js

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
var parseMarkdown = require('./parse_markdown');
44
var chalk = require('chalk');
5+
var path = require('path');
6+
var fs = require('fs');
57

68
/**
79
* Sort two documentation objects, given an optional order object. Returns
@@ -36,6 +38,15 @@ module.exports = function sortDocs(comments, options) {
3638
var fixed = options.toc.filter(function (val) {
3739
return typeof val === 'object' && val.name;
3840
}).map(function (val) {
41+
if (typeof val.file === 'string') {
42+
var filename = path.join(process.cwd(), val.file);
43+
try {
44+
val.description = fs.readFileSync(filename).toString();
45+
delete val.file;
46+
} catch (err) {
47+
process.stderr.write(chalk.red('Failed to read file ' + filename));
48+
}
49+
}
3950
if (typeof val.description === 'string') {
4051
val.description = parseMarkdown(val.description);
4152
}

‎test/fixture/snowflake.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# The Snowflake

‎test/lib/sort.js

+50
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,53 @@ test('sort an already-sorted stream containing a section/description', function
177177
t.deepEqual(sortTwice, [carrot, sectionMarkdown, bananas, apples]);
178178
t.end();
179179
});
180+
181+
test('sort toc with files', function (t) {
182+
var apples = { context: { sortKey: 'a' }, name: 'apples' };
183+
var carrot = { context: { sortKey: 'b' }, name: 'carrot' };
184+
var bananas = { context: { sortKey: 'c' }, name: 'bananas' };
185+
186+
var snowflake = {
187+
name: 'snowflake',
188+
file: 'test/fixture/snowflake.md'
189+
};
190+
191+
var processedSnowflake = {
192+
name: 'snowflake',
193+
kind: 'note',
194+
description: {
195+
children: [{
196+
children: [{
197+
position: {
198+
end: {column: 16, line: 1, offset: 15},
199+
indent: [],
200+
start: {column: 3, line: 1, offset: 2}
201+
},
202+
type: 'text',
203+
value: 'The Snowflake'
204+
}],
205+
depth: 1,
206+
position: {
207+
end: {column: 16, line: 1, offset: 15},
208+
indent: [],
209+
start: {column: 1, line: 1, offset: 0}
210+
},
211+
type: 'heading'
212+
}],
213+
position: {
214+
end: {column: 1, line: 2, offset: 16},
215+
start: {column: 1, line: 1, offset: 0}
216+
},
217+
type: 'root'
218+
}
219+
};
220+
t.deepEqual(sort([
221+
apples, carrot, bananas
222+
], {
223+
toc: [snowflake]
224+
}), [
225+
processedSnowflake, apples, carrot, bananas
226+
], 'with configuration');
227+
228+
t.end();
229+
});

0 commit comments

Comments
 (0)
Please sign in to comment.