Skip to content

Commit 53ce6fe

Browse files
committed
chore: updated metadata parsing
1 parent f25fb22 commit 53ce6fe

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

src/generators/json-simple/index.mjs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ export default {
3838
// Deep clones the content nodes to avoid affecting upstream nodes
3939
const content = JSON.parse(JSON.stringify(node.content));
4040

41-
// Removes all the Stability Index nodes, since they shouldn't be included in the final JSON
42-
// and are already represented in the metadata (metadata.stability.toJSON)
43-
remove(content, [createQueries.UNIST.isStabilityNode]);
41+
// Removes numerous nodes from the content that should not be on the "body"
42+
// of the JSON version of the API docs as they are already represented in the metadata
43+
remove(content, [
44+
createQueries.UNIST.isStabilityNode,
45+
createQueries.UNIST.isHeading,
46+
]);
4447

4548
// For the JSON generate we want to transform the whole content into JSON
4649
content.toJSON = () => remarkProcessor.stringify(content);

src/metadata.mjs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,19 @@ const createMetadata = slugger => {
6969
* @param {HeadingMetadataParent} heading The new heading metadata
7070
*/
7171
setHeading: heading => {
72-
internalMetadata.heading = heading;
72+
// We clone the heading to ensure that we don't accidentally override it
73+
// with later mutations below on the `.create` method
74+
internalMetadata.heading = { ...heading };
7375
},
7476
/**
7577
* Set the Stability Index of a given Metadata
7678
*
7779
* @param {StabilityIndexParent} stability The stability index node to be added
7880
*/
7981
addStability: stability => {
80-
internalMetadata.stability.children.push(stability);
82+
// We clone the stability to ensure that we don't accidentally override it
83+
// with later mutations below on the `.create` method
84+
internalMetadata.stability.children.push({ ...stability });
8185
},
8286
/**
8387
* Set the Metadata (from YAML if exists) properties to the current Metadata entry
@@ -132,7 +136,7 @@ const createMetadata = slugger => {
132136
// a certain navigation section to a page ad the exact point of the page (scroll)
133137
// This is useful for classes, globals and other type of YAML entries, as they reside
134138
// within a module (page) and we want to link to them directly
135-
const slugHash = `#${slugger.slug(internalMetadata.heading.text)}`;
139+
const sectionSlug = slugger.slug(internalMetadata.heading.data.text);
136140

137141
const {
138142
source_link: sourceLink,
@@ -141,6 +145,9 @@ const createMetadata = slugger => {
141145
tags = [],
142146
} = internalMetadata.properties;
143147

148+
// Also add the slug to the heading data as it is used to build the heading
149+
internalMetadata.heading.data.slug = sectionSlug;
150+
144151
// Defines the toJSON method for the Heading AST node to be converted as JSON
145152
internalMetadata.heading.toJSON = () => internalMetadata.heading.data;
146153

@@ -153,7 +160,7 @@ const createMetadata = slugger => {
153160
// The API file basename (without the extension)
154161
api: apiDoc.stem,
155162
// The path/slug of the API section
156-
slug: `${apiDoc.stem}.html${slugHash}`,
163+
slug: sectionSlug,
157164
// The source link of said API section
158165
sourceLink: sourceLink,
159166
// The latest updates to an API section

src/parser.mjs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ const createParser = () => {
6464
// Get all Markdown Footnote definitions from the tree
6565
const markdownDefinitions = selectAll('definition', apiDocTree);
6666

67+
// Get all Markdown Heading entries from the tree
68+
const headingNodes = selectAll('heading', apiDocTree);
69+
6770
// Handles Markdown link references and updates them to be plain links
6871
visit(apiDocTree, createQueries.UNIST.isLinkReference, node =>
6972
updateLinkReference(node, markdownDefinitions)
@@ -79,6 +82,12 @@ const createParser = () => {
7982
updateMarkdownLink(node)
8083
);
8184

85+
// If the document has no headings but it has content, we add a fake heading to the top
86+
// so that our parsing logic can work correctly, and generate content for the whole file
87+
if (headingNodes.length === 0 && apiDocTree.children.length > 0) {
88+
apiDocTree.children.unshift(createTree('heading', { depth: 1 }, []));
89+
}
90+
8291
// Handles iterating the tree and creating subtrees for each API doc entry
8392
// where an API doc entry is defined by a Heading Node
8493
// (so all elements after a Heading until the next Heading)
@@ -113,7 +122,7 @@ const createParser = () => {
113122
// `index + 1` is used to skip the current Heading Node
114123
const subTree = createTree(
115124
'root',
116-
apiDocTree.children.slice(index + 1, stop)
125+
apiDocTree.children.slice(index, stop)
117126
);
118127

119128
// Visits all Stability Index nodes from the current subtree if there's any

src/types.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Root } from 'mdast';
1+
import type { Heading, Root } from 'mdast';
22
import type { SemVer } from 'semver';
33
import type { Parent, Node, Data } from 'unist';
44

@@ -20,7 +20,7 @@ declare global {
2020
}
2121

2222
export interface StabilityIndexParent extends Parent {
23-
children: Array<NodeWithData<Node, StabilityIndexMetadataEntry>>;
23+
children: Array<NodeWithData<Root, StabilityIndexMetadataEntry>>;
2424
}
2525

2626
export interface HeadingMetadataEntry {
@@ -35,10 +35,11 @@ declare global {
3535
text: string;
3636
name: string;
3737
depth: number;
38+
slug: string;
3839
}
3940

4041
export interface HeadingMetadataParent
41-
extends NodeWithData<Parent, HeadingMetadataEntry> {}
42+
extends NodeWithData<Heading, HeadingMetadataEntry> {}
4243

4344
export interface ApiDocMetadataChange {
4445
// The Node.js version or versions where said change was introduced simultaneously

0 commit comments

Comments
 (0)