Skip to content

Commit 9ffe87b

Browse files
committed
Breaking: AST returns an object instead of an array.
BREAKING CHANGE: The documentation is no longer within Remark's AST.
1 parent d540c74 commit 9ffe87b

File tree

5 files changed

+89
-50
lines changed

5 files changed

+89
-50
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,19 @@ Example program:
4747
```js
4848
// ...
4949
const xdoc = require('xdoc-parser');
50-
// Returns a Remark.js AST with an 'xdoc' property attached to each
51-
// child whose type is 'code'. Note that only the code blocks after
52-
// a heading, 'API', as well as any code block with
53-
// a language set to 'xdoc' would be parsed.
5450
const ast = xdoc(fs.readFileSync('...'));
5551
console.log(JSON.stringify(ast, null, 2));
5652
// ...
5753
```
5854

55+
In the example above, `xdoc()` will return an object containing `markdown` and `documentation`:
56+
```ts
57+
{
58+
markdown: RemarkNode[],
59+
documentation: Partial<DocumentationNode>
60+
}
61+
```
62+
5963
You may also access the core parser class (or other classes) and simply parse the XDoc syntax if that's all you care for. For example:
6064

6165
```js

package-lock.json

Lines changed: 62 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919
"@types/chai": "^4.1.4",
2020
"@types/lodash": "^4.14.116",
2121
"@types/mocha": "^5.2.5",
22-
"@types/node": "^10.5.6",
22+
"@types/node": "^10.7.0",
2323
"antlr4": "^4.7.1",
2424
"chai": "^4.1.2",
2525
"commitizen": "^2.10.1",
2626
"conventional-changelog-eslint": "^3.0.0",
2727
"cz-adapter-eslint": "^0.1.2",
2828
"mocha": "^5.2.0",
29-
"semantic-release": "^15.9.5",
29+
"semantic-release": "^15.9.8",
3030
"shelljs": "^0.8.2",
3131
"travis-deploy-once": "^5.0.2",
32-
"ts-node": "^7.0.0",
32+
"ts-node": "^7.0.1",
3333
"typescript": "^2.9.2"
3434
},
3535
"scripts": {

src/XDocParser.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,16 @@ export default class XDocParser {
5353
}
5454

5555
parse = () => {
56-
return (new XDocCommentParser(this.source_))
57-
.parse()
58-
.filter(this.filter)
59-
.map(this.parseMarkdown)
56+
return {
57+
markdown: (new XDocCommentParser(this.source))
58+
.parse()
59+
.filter(this.filter)
60+
.map(this.parseMarkdown),
61+
documentation: this.parseXDoc(this.source)
62+
}
6063
}
6164

6265
private parseMarkdown = (token: Token): RemarkNode => {
63-
// console.log(marked.lexer(token.text, this.options.marked));
6466
let ast = remark()
6567
.data('settings', this.options.markdown.remark)
6668
.parse(token.text) as RemarkNode;
@@ -71,22 +73,24 @@ export default class XDocParser {
7173
if (node.type === 'code') {
7274
if (this.isAPI(ast.children[index - 1])) {
7375
if (!node.lang) node.lang = 'xdoc';
74-
return this.parseXDoc(node)
76+
return node;
7577
}
7678
}
7779
return node;
7880
})
7981
return ast;
8082
}
83+
8184
private isAPI = (node) => {
8285
return node && node.type === "heading" && node.children[0].value.toLowerCase() === "api";
8386
}
84-
private parseXDoc = (node) => {
85-
const documentation = (new XDocASTGenerator(node.value)).generate();
86-
node['xdoc'] = (new XDocASTVisitor(documentation, this.options.visitor)
87+
88+
private parseXDoc = (source: string) => {
89+
const XDocRegex = /@(\w+)([^{[(\n]*)?([\{\[\(][\s\S]*[\}\]\)]([\s]*(=|-)>.*)?)?([\s]*-(.)*)?/gmi;
90+
const documentation = (new XDocASTGenerator(source.match(XDocRegex).join('\n'))).generate();
91+
return (new XDocASTVisitor(documentation, this.options.visitor)
8792
.visit()
8893
);
89-
return node;
9094
}
9195

9296
private filter = (token: Token) => {

test/xdoc.core.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ import { TagNode, DocumentationNode, createDocumentationNode, createBodyNode, cr
77
const equal = chai.assert.deepEqual;
88
const source = FS.readFileSync(`${process.cwd()}/test/markdown.comment.txt`, 'utf-8');
99
const xdoc = (source: string) => (new XDocParser(source).parse());
10-
const getCode = () => xdoc(source).map(ast => {
11-
let code = ast.children.filter(node => node.type === 'code')[0];
12-
return code;
13-
})[0];
1410

1511
const documentation = (annotations: TagNode[]): Partial<DocumentationNode> => createDocumentationNode(
1612
createBodyNode(
@@ -21,14 +17,10 @@ const documentation = (annotations: TagNode[]): Partial<DocumentationNode> => cr
2117

2218
describe('XDoc Parser', () => {
2319
describe('parse markdown', () => {
24-
describe('parse xdoc code block', () => {
25-
it('shoud add an "xdoc" property to remark\'s markdown AST', () => {
26-
let code = getCode();
27-
chai.assert.exists(code['xdoc']);
28-
});
20+
describe('parse xdoc within markdown', () => {
2921

3022
it('should parse @tag id', () => equal(
31-
getCode()['xdoc'],
23+
xdoc(source).documentation,
3224
documentation([
3325
createTagNode(
3426
createTagNameNode(

0 commit comments

Comments
 (0)