|
2 | 2 |
|
3 | 3 | import { u as createTree } from 'unist-builder'; |
4 | 4 | import { findAfter } from 'unist-util-find-after'; |
| 5 | +import { is } from 'unist-util-is'; |
5 | 6 | import { remove } from 'unist-util-remove'; |
6 | 7 | import { selectAll } from 'unist-util-select'; |
7 | 8 | import { SKIP, visit } from 'unist-util-visit'; |
@@ -55,21 +56,29 @@ export const parseApiDoc = ({ file, tree }, typeMap) => { |
55 | 56 | // Get all Markdown Heading entries from the tree |
56 | 57 | const headingNodes = selectAll('heading', tree); |
57 | 58 |
|
58 | | - // Handles Markdown link references and updates them to be plain links |
59 | | - visit(tree, createQueries.UNIST.isLinkReference, node => |
60 | | - updateLinkReference(node, markdownDefinitions) |
61 | | - ); |
| 59 | + visit(tree, node => { |
| 60 | + let skip = false; |
| 61 | + |
| 62 | + // Update Markdown link references to be plain links |
| 63 | + if (is(node, createQueries.UNIST.isLinkReference)) { |
| 64 | + skip = true; |
| 65 | + updateLinkReference(node, markdownDefinitions); |
| 66 | + } |
| 67 | + |
| 68 | + // Normalizes URLs that reference API doc files with .md extensions to |
| 69 | + // be .html instead, since the files will eventually get compiled as HTML |
| 70 | + if (is(node, createQueries.UNIST.isMarkdownUrl)) { |
| 71 | + skip = true; |
| 72 | + updateMarkdownLink(node); |
| 73 | + } |
| 74 | + |
| 75 | + return skip ? SKIP : undefined; |
| 76 | + }); |
62 | 77 |
|
63 | 78 | // Removes all the original definitions from the tree as they are not needed |
64 | 79 | // anymore, since all link references got updated to be plain links |
65 | 80 | remove(tree, markdownDefinitions); |
66 | 81 |
|
67 | | - // Handles the normalisation URLs that reference to API doc files with .md extension |
68 | | - // to replace the .md into .html, since the API doc files get eventually compiled as HTML |
69 | | - visit(tree, createQueries.UNIST.isMarkdownUrl, node => |
70 | | - updateMarkdownLink(node) |
71 | | - ); |
72 | | - |
73 | 82 | // If the document has no headings but it has content, we add a fake heading to the top |
74 | 83 | // so that our parsing logic can work correctly, and generate content for the whole file |
75 | 84 | if (headingNodes.length === 0 && tree.children.length > 0) { |
@@ -113,34 +122,44 @@ export const parseApiDoc = ({ file, tree }, typeMap) => { |
113 | 122 | // `index + 1` is used to skip the current Heading Node |
114 | 123 | const subTree = createTree('root', tree.children.slice(index, stop)); |
115 | 124 |
|
116 | | - // Visits all Stability Index nodes from the current subtree if there's any |
117 | | - // and then apply the Stability Index metadata to the current metadata entry |
118 | | - visit(subTree, createQueries.UNIST.isStabilityNode, node => |
119 | | - addStabilityMetadata(node, ignoreStability ? undefined : apiEntryMetadata) |
120 | | - ); |
121 | | - |
122 | | - // Visits all HTML nodes from the current subtree and if there's any that matches |
123 | | - // our YAML metadata structure, it transforms into YAML metadata |
124 | | - // and then apply the YAML Metadata to the current Metadata entry |
125 | | - visit(subTree, createQueries.UNIST.isYamlNode, node => { |
126 | | - // TODO: Is there always only one YAML node? |
127 | | - apiEntryMetadata.setYamlPosition(node.position); |
128 | | - addYAMLMetadata(node, apiEntryMetadata); |
| 125 | + visit(subTree, (node, _, parent) => { |
| 126 | + let skip = false; |
| 127 | + |
| 128 | + // Applies stability index metadata if present |
| 129 | + if (is(node, createQueries.UNIST.isStabilityNode)) { |
| 130 | + skip = true; |
| 131 | + |
| 132 | + addStabilityMetadata( |
| 133 | + node, |
| 134 | + ignoreStability ? undefined : apiEntryMetadata |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + // Transform any YAML metadata and then apply it to the current |
| 139 | + // metadata entry |
| 140 | + if (is(node, createQueries.UNIST.isYamlNode)) { |
| 141 | + skip = true; |
| 142 | + |
| 143 | + // TODO: Is there always only one YAML node? |
| 144 | + apiEntryMetadata.setYamlPosition(node.position); |
| 145 | + addYAMLMetadata(node, apiEntryMetadata); |
| 146 | + } |
| 147 | + |
| 148 | + // Update any API doc type reference to be a Markdown link |
| 149 | + if (is(node, createQueries.UNIST.isTextWithType)) { |
| 150 | + skip = true; |
| 151 | + updateTypeReference(node, parent); |
| 152 | + } |
| 153 | + |
| 154 | + // Update all Unix manual references to be links |
| 155 | + if (is(node, createQueries.UNIST.isTextWithUnixManual)) { |
| 156 | + skip = true; |
| 157 | + updateUnixManualReference(node, parent); |
| 158 | + } |
| 159 | + |
| 160 | + return skip ? SKIP : undefined; |
129 | 161 | }); |
130 | 162 |
|
131 | | - // Visits all Text nodes from the current subtree and if there's any that matches |
132 | | - // any API doc type reference and then updates the type reference to be a Markdown link |
133 | | - visit(subTree, createQueries.UNIST.isTextWithType, (node, _, parent) => |
134 | | - updateTypeReference(node, parent) |
135 | | - ); |
136 | | - |
137 | | - // Visits all Unix manual references, and replaces them with links |
138 | | - visit( |
139 | | - subTree, |
140 | | - createQueries.UNIST.isTextWithUnixManual, |
141 | | - (node, _, parent) => updateUnixManualReference(node, parent) |
142 | | - ); |
143 | | - |
144 | 163 | // Removes already parsed items from the subtree so that they aren't included in the final content |
145 | 164 | remove(subTree, [createQueries.UNIST.isYamlNode]); |
146 | 165 |
|
|
0 commit comments