|
| 1 | +import { strictEqual, deepStrictEqual } from 'node:assert'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | + |
| 4 | +import { u } from 'unist-builder'; |
| 5 | +import { VFile } from 'vfile'; |
| 6 | + |
| 7 | +import { parseApiDoc } from '../utils/parse.mjs'; |
| 8 | + |
| 9 | +describe('generators/metadata/utils/parse', () => { |
| 10 | + it('parses heading, stability, YAML and converts markdown links', () => { |
| 11 | + const tree = u('root', [ |
| 12 | + u('heading', { depth: 1 }, [u('text', 'My API')]), |
| 13 | + u('blockquote', [u('paragraph', [u('text', 'Stability: 2 - stable')])]), |
| 14 | + u('html', '<!-- YAML\nsource_link: https://example.com\n-->'), |
| 15 | + u('paragraph', [ |
| 16 | + u('text', 'See '), |
| 17 | + u('link', { url: 'other.md#foo' }, [u('text', 'other')]), |
| 18 | + ]), |
| 19 | + ]); |
| 20 | + |
| 21 | + const file = new VFile({ path: 'doc/api/my-api.md' }); |
| 22 | + |
| 23 | + const results = parseApiDoc({ file, tree }, {}); |
| 24 | + |
| 25 | + strictEqual(results.length, 1); |
| 26 | + const [entry] = results; |
| 27 | + |
| 28 | + strictEqual(entry.source_link, 'https://example.com'); |
| 29 | + strictEqual(entry.stability.children.length, 1); |
| 30 | + strictEqual(entry.stability.children[0].data.index, '2'); |
| 31 | + |
| 32 | + // Find a paragraph child that contains a link and assert transformed URL |
| 33 | + const paragraph = entry.content.children.find(n => n.type === 'paragraph'); |
| 34 | + const link = paragraph.children.find(c => c.type === 'link'); |
| 35 | + strictEqual(link.url, 'other.html#foo'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('inserts a fake heading when none exist', () => { |
| 39 | + const tree = u('root', [u('paragraph', [u('text', 'No heading content')])]); |
| 40 | + const file = new VFile({ path: 'doc/api/noheading.md' }); |
| 41 | + |
| 42 | + const results = parseApiDoc({ file, tree }, {}); |
| 43 | + |
| 44 | + strictEqual(results.length, 1); |
| 45 | + const [entry] = results; |
| 46 | + |
| 47 | + // Fake heading has empty text |
| 48 | + deepStrictEqual(entry.heading.data.text, ''); |
| 49 | + }); |
| 50 | + |
| 51 | + it('converts link references using definitions and removes definitions', () => { |
| 52 | + const heading = u('heading', { depth: 1 }, [u('text', 'Ref API')]); |
| 53 | + |
| 54 | + const linkRef = u('linkReference', { identifier: 'def1' }, [ |
| 55 | + u('text', 'ref'), |
| 56 | + ]); |
| 57 | + |
| 58 | + const definition = u( |
| 59 | + 'definition', |
| 60 | + { identifier: 'def1', url: 'https://def.example/' }, |
| 61 | + [] |
| 62 | + ); |
| 63 | + |
| 64 | + const tree = u('root', [ |
| 65 | + heading, |
| 66 | + u('paragraph', [u('text', 'See '), linkRef]), |
| 67 | + definition, |
| 68 | + ]); |
| 69 | + |
| 70 | + const file = new VFile({ path: 'doc/api/ref-api.md' }); |
| 71 | + |
| 72 | + const results = parseApiDoc({ file, tree }, {}); |
| 73 | + |
| 74 | + strictEqual(results.length, 1); |
| 75 | + const [entry] = results; |
| 76 | + |
| 77 | + const paragraph = entry.content.children.find(n => n.type === 'paragraph'); |
| 78 | + const link = paragraph.children.find(c => c.type === 'link'); |
| 79 | + strictEqual(link.url, 'https://def.example/'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('converts type references to links using provided typeMap', () => { |
| 83 | + const tree = u('root', [ |
| 84 | + u('heading', { depth: 1 }, [u('text', 'Types API')]), |
| 85 | + u('paragraph', [u('text', 'Type is {Foo}')]), |
| 86 | + ]); |
| 87 | + |
| 88 | + const file = new VFile({ path: 'doc/api/types.md' }); |
| 89 | + |
| 90 | + const results = parseApiDoc({ file, tree }, { Foo: 'foo.html' }); |
| 91 | + |
| 92 | + strictEqual(results.length, 1); |
| 93 | + const [entry] = results; |
| 94 | + |
| 95 | + const paragraph = entry.content.children.find(n => n.type === 'paragraph'); |
| 96 | + const link = paragraph.children.find(c => c.type === 'link'); |
| 97 | + strictEqual(link.url, 'foo.html'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('converts unix manual references to man7 links', () => { |
| 101 | + const tree = u('root', [ |
| 102 | + u('heading', { depth: 1 }, [u('text', 'Man API')]), |
| 103 | + u('paragraph', [u('text', 'Run ls(1) for help')]), |
| 104 | + ]); |
| 105 | + |
| 106 | + const file = new VFile({ path: 'doc/api/man.md' }); |
| 107 | + |
| 108 | + const results = parseApiDoc({ file, tree }, {}); |
| 109 | + |
| 110 | + strictEqual(results.length, 1); |
| 111 | + const [entry] = results; |
| 112 | + |
| 113 | + const paragraph = entry.content.children.find(n => n.type === 'paragraph'); |
| 114 | + const link = paragraph.children.find(c => c.type === 'link'); |
| 115 | + // should point to man7 man page for ls in section 1 |
| 116 | + strictEqual(link.url.includes('man-pages/man1/ls.1.html'), true); |
| 117 | + }); |
| 118 | +}); |
0 commit comments