Skip to content

Commit 51d4478

Browse files
committed
Refactor Markdown AST representation. Fixes #216
1 parent 3605158 commit 51d4478

File tree

92 files changed

+4762
-6449
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+4762
-6449
lines changed

lib/output/markdown.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var mdast = require('mdast'),
4+
toc = require('mdast-toc'),
45
markdownAST = require('./markdown_ast');
56

67
/**
@@ -14,7 +15,9 @@ var mdast = require('mdast'),
1415
* @return {undefined} calls callback
1516
*/
1617
module.exports = function (comments, opts, callback) {
18+
var processor = mdast().use(toc);
1719
markdownAST(comments, opts, function (err, ast) {
18-
return callback(null, mdast.stringify(ast));
20+
var processedAST = processor.run(ast);
21+
return callback(null, processor.stringify(processedAST));
1922
});
2023
};

lib/output/markdown_ast.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ function commentsToAST(comments, opts, callback) {
4545
}
4646

4747
function paramSection(comment) {
48-
return !!comment.params && u('root', [
48+
return !!comment.params && [
4949
u('strong', [u('text', 'Parameters')]),
5050
paramList(comment.params)
51-
]);
51+
];
5252
}
5353

5454
function propertySection(comment) {
55-
return !!comment.properties && u('root', [
55+
return !!comment.properties && [
5656
u('strong', [u('text', 'Properties')]),
5757
u('list', { ordered: false },
5858
comment.properties.map(function (property) {
@@ -66,46 +66,47 @@ function commentsToAST(comments, opts, callback) {
6666
])
6767
].filter(Boolean))
6868
}))
69-
]);
69+
];
7070
}
7171

7272
function examplesSection(comment) {
73-
return !!comment.examples && u('root', [
74-
u('strong', [u('text', 'Examples')]),
75-
u('root', comment.examples.map(function (example) {
73+
return !!comment.examples && [u('strong', [u('text', 'Examples')])]
74+
.concat(comment.examples.map(function (example) {
7675
return u('code', { lang: 'javascript' }, example);
77-
}))
78-
]);
76+
}));
7977
}
8078

8179
function returnsSection(comment) {
82-
return !!comment.returns && u('root', comment.returns.map(function (returns) {
80+
return !!comment.returns && comment.returns.map(function (returns) {
8381
return u('paragraph', [
8482
u('text', 'Returns '),
8583
u('strong', [u('text', formatType(returns.type))]),
8684
u('text', ' '),
8785
mdast.parse(formatInlineTags(returns.description))
8886
]);
89-
}));
87+
});
9088
}
9189

92-
return u('root', [
93-
u('heading', { depth: depth }, [u('text', comment.name)]),
94-
mdast.parse(formatInlineTags(comment.description)),
95-
paramSection(comment),
96-
propertySection(comment),
97-
examplesSection(comment),
98-
returnsSection(comment),
99-
!!comment.members.instance.length &&
100-
u('root', comment.members.instance.map(generate.bind(null, depth + 1))),
101-
!!comment.members.static.length &&
102-
u('root', comment.members.static.map(generate.bind(null, depth + 1)))
103-
].filter(Boolean));
90+
return [u('heading', { depth: depth }, [u('text', comment.name)])]
91+
.concat(mdast.parse(formatInlineTags(comment.description)).children[0])
92+
.concat(paramSection(comment))
93+
.concat(propertySection(comment))
94+
.concat(examplesSection(comment))
95+
.concat(returnsSection(comment))
96+
.concat(!!comment.members.instance.length &&
97+
comment.members.instance.reduce(function (memo, child) {
98+
return memo.concat(generate(depth + 1, child));
99+
}, []))
100+
.concat(!!comment.members.static.length &&
101+
comment.members.static.reduce(function (memo, child) {
102+
return memo.concat(generate(depth + 1, child));
103+
}, []))
104+
.filter(Boolean);
104105
}
105106

106-
return callback(null, u('root', comments.map(function (comment) {
107-
return generate(1, comment);
108-
})));
107+
return callback(null, u('root', comments.reduce(function (memo, comment) {
108+
return memo.concat(generate(1, comment));
109+
}, [])));
109110
}
110111

111112
module.exports = commentsToAST;

test/fixture/class.output.custom.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,26 @@
22

33
This is my class, a demo thing.
44

5-
65
**Properties**
76

87
- `howMany` how many things it contains
98

109

11-
1210
## getFoo
1311

1412
Get the number 42
1513

16-
1714
**Parameters**
1815

1916
- `getIt` **boolean** whether to get the number
2017

2118

22-
2319
Returns **number** forty-two
2420

2521

26-
27-
2822
## getUndefined
2923

3024
Get undefined
3125

32-
3326
Returns **undefined** does not return anything.
3427

35-
36-
37-
38-

test/fixture/class.output.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,26 @@
22

33
This is my class, a demo thing.
44

5-
65
**Properties**
76

87
- `howMany` how many things it contains
98

109

11-
1210
## getFoo
1311

1412
Get the number 42
1513

16-
1714
**Parameters**
1815

1916
- `getIt` **boolean** whether to get the number
2017

2118

22-
2319
Returns **number** forty-two
2420

2521

26-
27-
2822
## getUndefined
2923

3024
Get undefined
3125

32-
3326
Returns **undefined** does not return anything.
3427

35-
36-
37-
38-

0 commit comments

Comments
 (0)