Skip to content

Commit 96c0c26

Browse files
committed
code review (2)
1 parent 35e6f58 commit 96c0c26

File tree

7 files changed

+58
-69
lines changed

7 files changed

+58
-69
lines changed

src/generators/legacy-json-all/index.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ export default {
5353
});
5454
});
5555

56-
await writeFile(join(output, 'all.json'), JSON.stringify(generatedValue));
56+
if (output) {
57+
await writeFile(join(output, 'all.json'), JSON.stringify(generatedValue));
58+
}
5759

5860
return generatedValue;
5961
},

src/generators/legacy-json/index.mjs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ export default {
5555
const section = processModuleNodes(node);
5656

5757
// Write it to the output file
58-
await writeFile(
59-
join(output, `${node.api}.json`),
60-
JSON.stringify(section)
61-
);
58+
if (output) {
59+
await writeFile(
60+
join(output, `${node.api}.json`),
61+
JSON.stringify(section)
62+
);
63+
}
6264
})
6365
);
6466

src/generators/legacy-json/utils/buildSection.mjs

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -71,26 +71,24 @@ function createSection(entry, head) {
7171
};
7272
}
7373

74+
/**
75+
*
76+
* @param {String} string
77+
* @returns {String}
78+
*/
79+
function transformTypeReferences(string) {
80+
// console.log(string)
81+
return string.replaceAll(/`<([^>]+)>`/g, '{$1}').replaceAll('} | {', '|');
82+
}
83+
7484
/**
7585
* Parses a list item to extract properties.
7686
* @param {import('mdast').ListItem} child - The list item node.
77-
* @param {import('../types.d.ts').HierarchizedEntry} entry - The entry containing raw content.
7887
* @returns {import('../types.d.ts').List} The parsed list.
7988
*/
80-
function parseListItem(child, entry) {
89+
function parseListItem(child) {
8190
const current = {};
8291

83-
/**
84-
* Extracts raw content from a node based on its position.
85-
* @param {import('mdast').BlockContent} node
86-
* @returns {string}
87-
*/
88-
const getRawContent = node =>
89-
entry.rawContent.slice(
90-
node.position.start.offset,
91-
node.position.end.offset
92-
);
93-
9492
/**
9593
* Extracts a pattern from text and assigns it to the current object.
9694
* @param {string} text
@@ -108,12 +106,14 @@ function parseListItem(child, entry) {
108106
};
109107

110108
// Combine and clean text from child nodes, excluding nested lists
111-
current.textRaw = child.children
112-
.filter(node => node.type !== 'list')
113-
.map(getRawContent)
114-
.join('')
115-
.replace(/\s+/g, ' ')
116-
.replace(/<!--.*?-->/gs, '');
109+
current.textRaw = transformTypeReferences(
110+
transformNodesToString(
111+
child.children.filter(node => node.type !== 'list'),
112+
true
113+
)
114+
.replace(/\s+/g, ' ')
115+
.replace(/<!--.*?-->/gs, '')
116+
);
117117

118118
let text = current.textRaw;
119119

@@ -136,9 +136,7 @@ function parseListItem(child, entry) {
136136
const optionsNode = child.children.find(child => child.type === 'list');
137137

138138
if (optionsNode) {
139-
current.options = optionsNode.children.map(child =>
140-
parseListItem(child, entry)
141-
);
139+
current.options = optionsNode.children.map(parseListItem);
142140
}
143141

144142
return current;
@@ -148,38 +146,26 @@ function parseListItem(child, entry) {
148146
* Parses stability metadata and adds it to the section.
149147
* @param {import('../types.d.ts').Section} section - The section to add stability to.
150148
* @param {Array} nodes - The AST nodes.
149+
* @param {import('../types.d.ts').HierarchizedEntry} entry - The entry to handle.
151150
*/
152-
function parseStability(section, nodes) {
153-
nodes.forEach((node, i) => {
154-
if (
155-
node.type === 'blockquote' &&
156-
node.children.length === 1 &&
157-
node.children[0].type === 'paragraph' &&
158-
nodes.slice(0, i).every(n => n.type === 'list')
159-
) {
160-
const text = transformNodesToString(node.children[0].children);
161-
const stabilityMatch = /^Stability: ([0-5])(?:\s*-\s*)?(.*)$/s.exec(text);
162-
if (stabilityMatch) {
163-
section.stability = Number(stabilityMatch[1]);
164-
section.stabilityText = stabilityMatch[2].replace(/\n/g, ' ').trim();
165-
nodes.splice(i, 1); // Remove the matched stability node to prevent further processing
166-
}
167-
}
168-
});
151+
function parseStability(section, nodes, entry) {
152+
const json = entry.stability.toJSON()[0];
153+
if (json) {
154+
section.stability = json.index;
155+
section.stabilityText = json.description;
156+
nodes.splice(0, 1);
157+
}
169158
}
170159

171160
/**
172161
* Parses a list and updates the section accordingly.
173162
* @param {import('../types.d.ts').Section} section - The section to update.
174163
* @param {Array} nodes - The AST nodes.
175-
* @param {import('../types.d.ts').HierarchizedEntry} entry - The associated entry.
176164
*/
177-
function parseList(section, nodes, entry) {
165+
function parseList(section, nodes) {
178166
const list = nodes[0]?.type === 'list' ? nodes.shift() : null;
179167

180-
const values = list
181-
? list.children.map(child => parseListItem(child, entry))
182-
: [];
168+
const values = list ? list.children.map(parseListItem) : [];
183169

184170
switch (section.type) {
185171
case 'ctor':
@@ -296,8 +282,8 @@ function handleEntry(entry, parentSection) {
296282
const [headingNode, ...nodes] = structuredClone(entry.content.children);
297283
const section = createSection(entry, headingNode);
298284

299-
parseStability(section, nodes);
300-
parseList(section, nodes, entry);
285+
parseStability(section, nodes, entry);
286+
parseList(section, nodes);
301287
addDescription(section, nodes);
302288
handleChildren(entry, section);
303289
addAdditionalMetadata(section, parentSection, headingNode);

src/metadata.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ const createMetadata = slugger => {
155155
content: section,
156156
tags,
157157
introduced_in,
158-
rawContent: apiDoc.toString(),
159158
};
160159
},
161160
};

src/queries.mjs

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

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

66
import { DOC_API_STABILITY_SECTION_REF_URL } from './constants.mjs';
77

@@ -76,23 +76,26 @@ const createQueries = () => {
7676
* @param {import('mdast').Parent} parent The parent node
7777
*/
7878
const updateTypeReference = (node, parent) => {
79-
const replacedTypes = node.value.replace(
80-
createQueries.QUERIES.normalizeTypes,
81-
transformTypeToReferenceLink
82-
);
83-
84-
// This changes the type into a link by splitting it up
85-
// into several nodes, and adding those nodes to the
86-
// parent.
79+
const replacedTypes = node.value
80+
.replace(
81+
createQueries.QUERIES.normalizeTypes,
82+
transformTypeToReferenceLink
83+
)
84+
// Remark doesn't handle leading / trailing spaces, so replace them with
85+
// HTML entities.
86+
.replace(/^\s/, '&nbsp;')
87+
.replace(/\s$/, '&nbsp;');
88+
89+
// This changes the type into a link by splitting it up into several nodes,
90+
// and adding those nodes to the parent.
8791
const {
8892
children: [newNode],
8993
} = remark.parse(replacedTypes);
94+
95+
// Find the index of the original node in the parent
9096
const index = parent.children.indexOf(node);
91-
const originalPosition = node.position;
92-
visit(newNode, node => {
93-
(node.position.start += originalPosition.start),
94-
(node.position.end += originalPosition.end);
95-
});
97+
98+
// Replace the original node with the new node(s)
9699
parent.children.splice(index, 1, ...newNode.children);
97100

98101
return [SKIP];

src/test/metadata.test.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ describe('createMetadata', () => {
7373
heading,
7474
n_api_version: undefined,
7575
introduced_in: undefined,
76-
rawContent: '',
7776
removed_in: undefined,
7877
slug: 'test-heading',
7978
source_link: 'test.com',

src/types.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ declare global {
9898
// Extra YAML section entries that are stringd and serve
9999
// to provide additional metadata about the API doc entry
100100
tags: Array<string>;
101-
// The raw file content
102-
rawContent: string;
103101
}
104102

105103
export interface ApiDocReleaseEntry {

0 commit comments

Comments
 (0)