@@ -9,14 +9,29 @@ import { SKIP, visit } from 'unist-util-visit';
99import { createNodeSlugger } from './slugger.mjs' ;
1010import createMetadata from '../../../metadata.mjs' ;
1111import createQueries from '../../../utils/queries/index.mjs' ;
12+ import {
13+ isLinkReference ,
14+ isMarkdownUrl ,
15+ isHeading ,
16+ isStabilityNode ,
17+ isYamlNode ,
18+ isTextWithType ,
19+ isTextWithUnixManual ,
20+ } from '../../../utils/queries/unist.mjs' ;
1221import { getRemark } from '../../../utils/remark.mjs' ;
1322
14- /**
23+ /**x
1524 * This generator generates a flattened list of metadata entries from a API doc
1625 *
1726 * @param {ParserOutput<import('mdast').Root> } input
1827 * @returns {Promise<Array<ApiDocMetadataEntry>> }
1928 */
29+ /**
30+ *
31+ * @param root0
32+ * @param root0.file
33+ * @param root0.tree
34+ */
2035export const parseApiDoc = ( { file, tree } ) => {
2136 /**
2237 * This holds references to all the Metadata entries for a given file
@@ -54,7 +69,7 @@ export const parseApiDoc = ({ file, tree }) => {
5469 const headingNodes = selectAll ( 'heading' , tree ) ;
5570
5671 // Handles Markdown link references and updates them to be plain links
57- visit ( tree , createQueries . UNIST . isLinkReference , node =>
72+ visit ( tree , isLinkReference , node =>
5873 updateLinkReference ( node , markdownDefinitions )
5974 ) ;
6075
@@ -64,9 +79,7 @@ export const parseApiDoc = ({ file, tree }) => {
6479
6580 // Handles the normalisation URLs that reference to API doc files with .md extension
6681 // to replace the .md into .html, since the API doc files get eventually compiled as HTML
67- visit ( tree , createQueries . UNIST . isMarkdownUrl , node =>
68- updateMarkdownLink ( node )
69- ) ;
82+ visit ( tree , isMarkdownUrl , node => updateMarkdownLink ( node ) ) ;
7083
7184 // If the document has no headings but it has content, we add a fake heading to the top
7285 // so that our parsing logic can work correctly, and generate content for the whole file
@@ -79,7 +92,7 @@ export const parseApiDoc = ({ file, tree }) => {
7992 // (so all elements after a Heading until the next Heading)
8093 // and then it creates and updates a Metadata entry for each API doc entry
8194 // and then generates the final content for each API doc entry and pushes it to the collection
82- visit ( tree , createQueries . UNIST . isHeading , ( headingNode , index ) => {
95+ visit ( tree , isHeading , ( headingNode , index ) => {
8396 // Creates a new Metadata entry for the current API doc file
8497 const apiEntryMetadata = createMetadata ( nodeSlugger ) ;
8598
@@ -90,8 +103,7 @@ export const parseApiDoc = ({ file, tree }) => {
90103 // This is used for ensuring that we don't include items that would
91104 // belong only to the next heading to the current Heading metadata
92105 // Note that if there is no next heading, we use the current node as the next one
93- const nextHeadingNode =
94- findAfter ( tree , index , createQueries . UNIST . isHeading ) ?? headingNode ;
106+ const nextHeadingNode = findAfter ( tree , index , isHeading ) ?? headingNode ;
95107
96108 // This is the cutover index of the subtree that we should get
97109 // of all the Nodes within the AST tree that belong to this section
@@ -109,34 +121,32 @@ export const parseApiDoc = ({ file, tree }) => {
109121
110122 // Visits all Stability Index nodes from the current subtree if there's any
111123 // and then apply the Stability Index metadata to the current metadata entry
112- visit ( subTree , createQueries . UNIST . isStabilityNode , node =>
124+ visit ( subTree , isStabilityNode , node =>
113125 addStabilityMetadata ( node , apiEntryMetadata )
114126 ) ;
115127
116128 // Visits all HTML nodes from the current subtree and if there's any that matches
117129 // our YAML metadata structure, it transforms into YAML metadata
118130 // and then apply the YAML Metadata to the current Metadata entry
119- visit ( subTree , createQueries . UNIST . isYamlNode , node => {
131+ visit ( subTree , isYamlNode , node => {
120132 // TODO: Is there always only one YAML node?
121133 apiEntryMetadata . setYamlPosition ( node . position ) ;
122134 addYAMLMetadata ( node , apiEntryMetadata ) ;
123135 } ) ;
124136
125137 // Visits all Text nodes from the current subtree and if there's any that matches
126138 // any API doc type reference and then updates the type reference to be a Markdown link
127- visit ( subTree , createQueries . UNIST . isTextWithType , ( node , _ , parent ) =>
139+ visit ( subTree , isTextWithType , ( node , _ , parent ) =>
128140 updateTypeReference ( node , parent )
129141 ) ;
130142
131143 // Visits all Unix manual references, and replaces them with links
132- visit (
133- subTree ,
134- createQueries . UNIST . isTextWithUnixManual ,
135- ( node , _ , parent ) => updateUnixManualReference ( node , parent )
144+ visit ( subTree , isTextWithUnixManual , ( node , _ , parent ) =>
145+ updateUnixManualReference ( node , parent )
136146 ) ;
137147
138148 // Removes already parsed items from the subtree so that they aren't included in the final content
139- remove ( subTree , [ createQueries . UNIST . isYamlNode ] ) ;
149+ remove ( subTree , [ isYamlNode ] ) ;
140150
141151 // Applies the AST transformations to the subtree based on the API doc entry Metadata
142152 // Note that running the transformation on the subtree isn't costly as it is a reduced tree
0 commit comments