• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import * as common from '../common/index.mjs';
2import * as fixtures from '../common/fixtures.mjs';
3
4import assert from 'assert';
5import fs from 'fs';
6import { createRequire } from 'module';
7
8import * as json from '../../tools/doc/json.mjs';
9
10const require = createRequire(new URL('../../tools/doc/', import.meta.url));
11const unified = require('unified');
12const markdown = require('remark-parse');
13
14function toJSON(input, filename, cb) {
15  function nullCompiler() {
16    this.Compiler = (tree) => tree;
17  }
18
19  unified()
20    .use(markdown)
21    .use(json.jsonAPI, { filename })
22    .use(nullCompiler)
23    .process(input, cb);
24}
25
26// Outputs valid json with the expected fields when given simple markdown
27// Test data is a list of objects with two properties.
28// The file property is the file path.
29// The json property is some json which will be generated by the doctool.
30const testData = [
31  {
32    file: fixtures.path('sample_document.md'),
33    json: {
34      type: 'module',
35      source: 'foo',
36      modules: [{
37        textRaw: 'Sample Markdown',
38        name: 'sample_markdown',
39        modules: [{
40          textRaw: 'Seussian Rhymes',
41          name: 'seussian_rhymes',
42          desc: '<ol>\n<li>fish</li>\n<li>fish</li>\n</ol>\n' +
43                  '<ul>\n<li>Red fish</li>\n<li>Blue fish</li>\n</ul>',
44          type: 'module',
45          displayName: 'Seussian Rhymes'
46        }],
47        type: 'module',
48        displayName: 'Sample Markdown'
49      }]
50    }
51  },
52  {
53    file: fixtures.path('order_of_end_tags_5873.md'),
54    json: {
55      type: 'module',
56      source: 'foo',
57      modules: [{
58        textRaw: 'Title',
59        name: 'title',
60        modules: [{
61          textRaw: 'Subsection',
62          name: 'subsection',
63          classMethods: [{
64            textRaw: 'Static method: Buffer.from(array)',
65            type: 'classMethod',
66            name: 'from',
67            signatures: [
68              {
69                params: [{
70                  textRaw: '`array` {Array}',
71                  name: 'array',
72                  type: 'Array'
73                }]
74              },
75            ]
76          }],
77          type: 'module',
78          displayName: 'Subsection'
79        }],
80        type: 'module',
81        displayName: 'Title'
82      }]
83    }
84  },
85  {
86    file: fixtures.path('doc_with_yaml.md'),
87    json: {
88      type: 'module',
89      source: 'foo',
90      modules: [
91        {
92          textRaw: 'Sample Markdown with YAML info',
93          name: 'sample_markdown_with_yaml_info',
94          modules: [
95            {
96              textRaw: 'Foobar',
97              name: 'foobar',
98              meta: {
99                added: ['v1.0.0'],
100                changes: []
101              },
102              desc: '<p>Describe <code>Foobar</code> in more detail ' +
103                'here.</p>',
104              type: 'module',
105              displayName: 'Foobar'
106            },
107            {
108              textRaw: 'Foobar II',
109              name: 'foobar_ii',
110              meta: {
111                added: ['v5.3.0', 'v4.2.0'],
112                changes: [
113                  { 'version': 'v4.2.0',
114                    'pr-url': 'https://github.com/nodejs/node/pull/3276',
115                    'description': 'The `error` parameter can now be ' +
116                      'an arrow function.' },
117                ]
118              },
119              desc: '<p>Describe <code>Foobar II</code> in more detail ' +
120                'here. fg(1)</p>',
121              type: 'module',
122              displayName: 'Foobar II'
123            },
124            {
125              textRaw: 'Deprecated thingy',
126              name: 'deprecated_thingy',
127              meta: {
128                added: ['v1.0.0'],
129                deprecated: ['v2.0.0'],
130                changes: []
131              },
132              desc: '<p>Describe <code>Deprecated thingy</code> in more ' +
133                'detail here. fg(1p)</p>',
134              type: 'module',
135              displayName: 'Deprecated thingy'
136            },
137            {
138              textRaw: 'Something',
139              name: 'something',
140              desc: '<!-- This is not a metadata comment -->\n<p>' +
141                'Describe <code>Something</code> in more detail here.</p>',
142              type: 'module',
143              displayName: 'Something'
144            },
145          ],
146          type: 'module',
147          displayName: 'Sample Markdown with YAML info'
148        },
149      ]
150    }
151  },
152  {
153    file: fixtures.path('doc_with_backticks_in_headings.md'),
154    json: {
155      type: 'module',
156      source: 'foo',
157      modules: [
158        {
159          textRaw: 'Fhqwhgads',
160          name: 'fhqwhgads',
161          properties: [
162            {
163              name: 'fullName',
164              textRaw: '`Fqhqwhgads.fullName`'
165            },
166          ],
167          classMethods: [
168            {
169              name: 'again',
170              signatures: [
171                {
172                  params: []
173                },
174              ],
175              textRaw: 'Static method: `Fhqwhgads.again()`',
176              type: 'classMethod'
177            },
178          ],
179          classes: [
180            {
181              textRaw: 'Class: `ComeOn`',
182              type: 'class',
183              name: 'ComeOn'
184            },
185          ],
186          ctors: [
187            {
188              name: 'Fhqwhgads',
189              signatures: [
190                {
191                  params: []
192                },
193              ],
194              textRaw: 'Constructor: `new Fhqwhgads()`',
195              type: 'ctor'
196            },
197          ],
198          methods: [
199            {
200              textRaw: '`everybody.to(limit)`',
201              type: 'method',
202              name: 'to',
203              signatures: [{ params: [] }]
204            },
205          ],
206          events: [
207            {
208              textRaw: "Event: `'FHQWHfest'`",
209              type: 'event',
210              name: 'FHQWHfest',
211              params: []
212            },
213          ],
214          type: 'module',
215          displayName: 'Fhqwhgads'
216        },
217      ]
218    }
219  },
220];
221
222testData.forEach((item) => {
223  fs.readFile(item.file, 'utf8', common.mustSucceed((input) => {
224    toJSON(input, 'foo', common.mustSucceed((output) => {
225      assert.deepStrictEqual(output.json, item.json);
226    }));
227  }));
228});
229