Skip to content

Commit 9c1c9ac

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

File tree

5 files changed

+67
-62
lines changed

5 files changed

+67
-62
lines changed

npm-shrinkwrap.json

Lines changed: 10 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"unified": "^11.0.5",
7878
"unist-builder": "^4.0.0",
7979
"unist-util-find-after": "^5.0.0",
80+
"unist-util-is": "^6.0.1",
8081
"unist-util-position": "^5.0.0",
8182
"unist-util-remove": "^4.0.0",
8283
"unist-util-select": "^5.1.0",

src/generators/metadata/utils/parse.mjs

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { u as createTree } from 'unist-builder';
44
import { findAfter } from 'unist-util-find-after';
5+
import { is } from 'unist-util-is';
56
import { remove } from 'unist-util-remove';
67
import { selectAll } from 'unist-util-select';
78
import { SKIP, visit } from 'unist-util-visit';
@@ -55,21 +56,29 @@ export const parseApiDoc = ({ file, tree }, typeMap) => {
5556
// Get all Markdown Heading entries from the tree
5657
const headingNodes = selectAll('heading', tree);
5758

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+
});
6277

6378
// Removes all the original definitions from the tree as they are not needed
6479
// anymore, since all link references got updated to be plain links
6580
remove(tree, markdownDefinitions);
6681

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-
7382
// If the document has no headings but it has content, we add a fake heading to the top
7483
// so that our parsing logic can work correctly, and generate content for the whole file
7584
if (headingNodes.length === 0 && tree.children.length > 0) {
@@ -113,34 +122,44 @@ export const parseApiDoc = ({ file, tree }, typeMap) => {
113122
// `index + 1` is used to skip the current Heading Node
114123
const subTree = createTree('root', tree.children.slice(index, stop));
115124

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;
129161
});
130162

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-
144163
// Removes already parsed items from the subtree so that they aren't included in the final content
145164
remove(subTree, [createQueries.UNIST.isYamlNode]);
146165

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: 0 additions & 11 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,
@@ -35,8 +34,6 @@ const createQueries = typeMap => {
3534
const yamlContent = extractYamlContent(node);
3635

3736
apiEntryMetadata.updateProperties(parseYAMLIntoMetadata(yamlContent));
38-
39-
return [SKIP];
4037
};
4138

4239
/**
@@ -64,8 +61,6 @@ const createQueries = typeMap => {
6461
createQueries.QUERIES.markdownUrl,
6562
(_, filename, hash = '') => `${filename}.html${hash}`
6663
);
67-
68-
return [SKIP];
6964
};
7065

7166
/**
@@ -96,8 +91,6 @@ const createQueries = typeMap => {
9691

9792
// Replace the original node with the new node(s)
9893
parent.children.splice(index, 1, ...newNode.children);
99-
100-
return [SKIP];
10194
};
10295

10396
/**
@@ -113,8 +106,6 @@ const createQueries = typeMap => {
113106

114107
node.type = 'link';
115108
node.url = definition.url;
116-
117-
return [SKIP];
118109
};
119110

120111
/**
@@ -154,8 +145,6 @@ const createQueries = typeMap => {
154145
// Adds the Stability Index metadata to the current Metadata entry
155146
apiEntryMetadata?.addStability(stabilityIndexNode);
156147
}
157-
158-
return [SKIP];
159148
};
160149

161150
/**

0 commit comments

Comments
 (0)