Skip to content

Commit 03dfbb8

Browse files
committed
fix(perf): consolidate visit() calls in metadata gen
Signed-off-by: flakey5 <[email protected]>
1 parent d980054 commit 03dfbb8

File tree

4 files changed

+92
-56
lines changed

4 files changed

+92
-56
lines changed

src/generators/json-simple/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export default {
5050
});
5151

5252
// This simply grabs all the different files and stringifies them
53-
const stringifiedContent = JSON.stringify(mappedInput);
53+
const stringifiedContent = JSON.stringify(mappedInput, null, 2);
5454

5555
if (options.output) {
5656
// Writes all the API docs stringified content into one file

src/generators/metadata/utils/parse.mjs

Lines changed: 88 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,39 @@ export const parseApiDoc = ({ file, tree }, typeMap) => {
5555
// Get all Markdown Heading entries from the tree
5656
const headingNodes = selectAll('heading', tree);
5757

58+
// visit(tree, node => {
59+
// let skip = false;
60+
61+
// // Update Markdown link references to be plain links
62+
// if (is(node, createQueries.UNIST.isLinkReference)) {
63+
// skip = true;
64+
// updateLinkReference(node, markdownDefinitions);
65+
// }
66+
67+
// // Normalizes URLs that reference API doc files with .md extensions to
68+
// // be .html instead, since the files will eventually get compiled as HTML
69+
// if (is(node, createQueries.UNIST.isMarkdownUrl)) {
70+
// skip = true;
71+
// updateMarkdownLink(node);
72+
// }
73+
74+
// return skip ? SKIP : undefined;
75+
// });
76+
5877
// Handles Markdown link references and updates them to be plain links
59-
visit(tree, createQueries.UNIST.isLinkReference, node =>
60-
updateLinkReference(node, markdownDefinitions)
61-
);
78+
visit(tree, createQueries.UNIST.isLinkReference, node => {
79+
updateLinkReference(node, markdownDefinitions);
80+
return [SKIP];
81+
});
6282

6383
// Removes all the original definitions from the tree as they are not needed
6484
// anymore, since all link references got updated to be plain links
6585
remove(tree, markdownDefinitions);
6686

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-
);
87+
visit(tree, createQueries.UNIST.isMarkdownUrl, node => {
88+
updateMarkdownLink(node);
89+
return [SKIP];
90+
});
7291

7392
// If the document has no headings but it has content, we add a fake heading to the top
7493
// so that our parsing logic can work correctly, and generate content for the whole file
@@ -113,33 +132,73 @@ export const parseApiDoc = ({ file, tree }, typeMap) => {
113132
// `index + 1` is used to skip the current Heading Node
114133
const subTree = createTree('root', tree.children.slice(index, stop));
115134

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);
135+
visit(subTree, (node, index, parent) => {
136+
let skip = false;
137+
138+
// Applies stability index metadata if present
139+
if (createQueries.UNIST.isStabilityNode(node)) {
140+
skip = true;
141+
142+
addStabilityMetadata(
143+
node,
144+
ignoreStability ? undefined : apiEntryMetadata
145+
);
146+
}
147+
148+
// Transform any YAML metadata and then apply it to the current
149+
// metadata entry
150+
if (createQueries.UNIST.isYamlNode(node)) {
151+
// TODO: Is there always only one YAML node?
152+
apiEntryMetadata.setYamlPosition(node.position);
153+
addYAMLMetadata(node, apiEntryMetadata);
154+
}
155+
156+
let valueUpdated = false;
157+
158+
if (createQueries.UNIST.isTextWithType(node)) {
159+
skip = true;
160+
161+
valueUpdated = true;
162+
node.value = updateTypeReference(node);
163+
}
164+
165+
if (createQueries.UNIST.isTextWithUnixManual(node)) {
166+
skip = true;
167+
168+
valueUpdated = true;
169+
node.value = updateUnixManualReference(node);
170+
}
171+
172+
if (valueUpdated) {
173+
// This changes the type into a link by splitting it up into several nodes,
174+
// and adding those nodes to the parent.
175+
const {
176+
children: [newNode],
177+
} = remarkProcessor.parse(node.value);
178+
179+
// Replace the original node with the new node(s)
180+
parent.children.splice(index, 1, ...newNode.children);
181+
}
182+
183+
return skip ? SKIP : undefined;
129184
});
130185

131186
// Visits all Text nodes from the current subtree and if there's any that matches
132187
// 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-
);
188+
// visit(subTree, createQueries.UNIST.isTextWithType, (node, _, parent) => {
189+
// updateTypeReference(node, parent);
190+
// return SKIP;
191+
// });
136192

137193
// 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-
);
194+
// visit(
195+
// subTree,
196+
// createQueries.UNIST.isTextWithUnixManual,
197+
// (node, _, parent) => {
198+
// updateUnixManualReference(node, parent);
199+
// return SKIP;
200+
// }
201+
// );
143202

144203
// Removes already parsed items from the subtree so that they aren't included in the final content
145204
remove(subTree, [createQueries.UNIST.isYamlNode]);

src/utils/highlighter.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default function rehypeShikiji() {
5353
// We want the contents of the <pre> element, hence we attempt to get the first child
5454
const preElement = node.children[0];
5555

56-
// If thereÄs nothing inside the <pre> element... What are we doing here?
56+
// If there's nothing inside the <pre> element... What are we doing here?
5757
if (!preElement || !preElement.properties) {
5858
return;
5959
}

src/utils/queries/index.mjs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
import { u as createTree } from 'unist-builder';
4-
import { SKIP } from 'unist-util-visit';
54

65
import {
76
DOC_API_STABILITY_SECTION_REF_URL,
@@ -14,7 +13,6 @@ import {
1413
transformTypeToReferenceLink,
1514
transformUnixManualToLink,
1615
} from '../parser/index.mjs';
17-
import { getRemark } from '../remark.mjs';
1816
import { transformNodesToString } from '../unist.mjs';
1917

2018
/**
@@ -23,7 +21,6 @@ import { transformNodesToString } from '../unist.mjs';
2321
* @param {Record<string, string>} typeMap The mapping to types to links
2422
*/
2523
const createQueries = typeMap => {
26-
const remark = getRemark();
2724
/**
2825
* Sanitizes the YAML source by returning the inner YAML content
2926
* and then parsing it into an API Metadata object and updating the current Metadata
@@ -35,8 +32,6 @@ const createQueries = typeMap => {
3532
const yamlContent = extractYamlContent(node);
3633

3734
apiEntryMetadata.updateProperties(parseYAMLIntoMetadata(yamlContent));
38-
39-
return [SKIP];
4035
};
4136

4237
/**
@@ -64,8 +59,6 @@ const createQueries = typeMap => {
6459
createQueries.QUERIES.markdownUrl,
6560
(_, filename, hash = '') => `${filename}.html${hash}`
6661
);
67-
68-
return [SKIP];
6962
};
7063

7164
/**
@@ -77,27 +70,15 @@ const createQueries = typeMap => {
7770
* @param {Function} transformer The function to transform the reference
7871
*
7972
*/
80-
const updateReferences = (query, transformer, node, parent) => {
73+
const updateReferences = (query, transformer, node) => {
8174
const replacedTypes = node.value
8275
.replace(query, transformer)
8376
// Remark doesn't handle leading / trailing spaces, so replace them with
8477
// HTML entities.
8578
.replace(/^\s/, '&nbsp;')
8679
.replace(/\s$/, '&nbsp;');
8780

88-
// This changes the type into a link by splitting it up into several nodes,
89-
// and adding those nodes to the parent.
90-
const {
91-
children: [newNode],
92-
} = remark.parse(replacedTypes);
93-
94-
// Find the index of the original node in the parent
95-
const index = parent.children.indexOf(node);
96-
97-
// Replace the original node with the new node(s)
98-
parent.children.splice(index, 1, ...newNode.children);
99-
100-
return [SKIP];
81+
return replacedTypes;
10182
};
10283

10384
/**
@@ -113,8 +94,6 @@ const createQueries = typeMap => {
11394

11495
node.type = 'link';
11596
node.url = definition.url;
116-
117-
return [SKIP];
11897
};
11998

12099
/**
@@ -154,8 +133,6 @@ const createQueries = typeMap => {
154133
// Adds the Stability Index metadata to the current Metadata entry
155134
apiEntryMetadata?.addStability(stabilityIndexNode);
156135
}
157-
158-
return [SKIP];
159136
};
160137

161138
/**

0 commit comments

Comments
 (0)