Skip to content

Commit 1bbc9a4

Browse files
committed
Replace node-fetch usages by native fetch
1 parent c74ee31 commit 1bbc9a4

File tree

4 files changed

+79
-112
lines changed

4 files changed

+79
-112
lines changed

package-lock.json

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

plugins/docusaurus-plugin-ionic-component-api/index.js

Lines changed: 34 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fetch = require('node-fetch');
2-
31
module.exports = function (context, options) {
42
return {
53
name: 'docusaurus-plugin-ionic-component-api',
@@ -18,30 +16,40 @@ module.exports = function (context, options) {
1816
* @param {*} isCurrentVersion Whether or not this is the current version of the docs
1917
*/
2018
const generateMarkdownForVersion = async (version, npmTag, isCurrentVersion) => {
21-
let COMPONENT_LINK_REGEXP;
22-
const response = await fetch(`https://unpkg.com/@ionic/docs@${npmTag}/core.json`);
23-
const { components } = await response.json();
24-
25-
const names = components.map((component) => component.tag.slice(4));
26-
// matches all relative markdown links to a component, e.g. (../button)
27-
COMPONENT_LINK_REGEXP = new RegExp(`\\(../(${names.join('|')})/?(#[^)]+)?\\)`, 'g');
28-
29-
components.forEach((comp) => {
30-
const compTag = comp.tag.slice(4);
31-
const outDir = getDirectoryPath(compTag, version, isCurrentVersion);
32-
33-
data.push({
34-
outDir,
35-
componentTag: compTag,
36-
version,
37-
props: renderProperties(comp),
38-
events: renderEvents(comp),
39-
methods: renderMethods(comp),
40-
parts: renderParts(comp),
41-
customProps: renderCustomProps(comp),
42-
slots: renderSlots(comp),
19+
try {
20+
let COMPONENT_LINK_REGEXP;
21+
const response = await fetch(`https://unpkg.com/@ionic/docs@${npmTag}/core.json`);
22+
23+
if (!response.ok) {
24+
console.error(`Failed to fetch component data for ${npmTag}: ${response.status}`);
25+
return;
26+
}
27+
28+
const { components } = await response.json();
29+
30+
const names = components.map((component) => component.tag.slice(4));
31+
// matches all relative markdown links to a component, e.g. (../button)
32+
COMPONENT_LINK_REGEXP = new RegExp(`\\(../(${names.join('|')})/?(#[^)]+)?\\)`, 'g');
33+
34+
components.forEach((comp) => {
35+
const compTag = comp.tag.slice(4);
36+
const outDir = getDirectoryPath(compTag, version, isCurrentVersion);
37+
38+
data.push({
39+
outDir,
40+
componentTag: compTag,
41+
version,
42+
props: renderProperties(comp),
43+
events: renderEvents(comp),
44+
methods: renderMethods(comp),
45+
parts: renderParts(comp),
46+
customProps: renderCustomProps(comp),
47+
slots: renderSlots(comp),
48+
});
4349
});
44-
});
50+
} catch (error) {
51+
console.error(`Error generating markdown for version ${version}:`, error.message);
52+
}
4553
};
4654

4755
for (const version of options.versions) {
@@ -234,63 +242,4 @@ function renderCustomProps({ styles: customProps }) {
234242
const mdProps = customProps.filter((prop) => prop.mode === 'md');
235243

236244
const renderTable = (props) => {
237-
if (props.length === 0) {
238-
return 'No CSS custom properties available for this component.';
239-
}
240-
241-
return `
242-
| Name | Description |
243-
| --- | --- |
244-
${props.map((prop) => `| \`${prop.name}\` | ${formatMultiline(prop.docs)} |`).join('\n')}
245-
`;
246-
};
247-
248-
if (iosProps.length > 0 || mdProps.length > 0) {
249-
// If the component has mode-specific custom props, render them in tabs for iOS and MD
250-
return `
251-
import Tabs from '@theme/Tabs';
252-
253-
import TabItem from '@theme/TabItem';
254-
255-
\`\`\`\`mdx-code-block
256-
<Tabs
257-
groupId="mode"
258-
defaultValue="ios"
259-
values={[
260-
{ value: 'ios', label: 'iOS' },
261-
{ value: 'md', label: 'MD' },
262-
]
263-
}>
264-
<TabItem value="ios">
265-
266-
${renderTable(iosProps)}
267-
268-
</TabItem>
269-
270-
<TabItem value="md">
271-
272-
${renderTable(mdProps)}
273-
274-
</TabItem>
275-
</Tabs>
276-
277-
\`\`\`\`
278-
279-
`;
280-
}
281-
// Otherwise render the custom props without the tabs for iOS and MD
282-
return renderTable(customProps);
283-
}
284-
285-
function renderSlots({ slots }) {
286-
if (slots.length === 0) {
287-
return 'No slots available for this component.';
288-
}
289-
290-
return `
291-
| Name | Description |
292-
| --- | --- |
293-
${slots.map((slot) => `| \`${slot.name}\` | ${formatMultiline(slot.docs)} |`).join('\n')}
294-
295-
`;
296-
}
245+
if

scripts/api-ja.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const fetch = require('node-fetch');
21
const fs = require('fs');
32
const path = require('path');
43
const { api: apiOverrides } = require('./data/meta-override.json');
@@ -8,16 +7,26 @@ const DEMOS_PATH = path.resolve('static/demos');
87
let COMPONENT_LINK_REGEXP;
98

109
(async function () {
11-
const response = await fetch(
12-
'https://raw.githubusercontent.com/ionic-team/ionic-docs/translation/jp/scripts/data/translated-api.json'
13-
);
14-
const { components } = await response.json();
10+
try {
11+
const response = await fetch(
12+
'https://raw.githubusercontent.com/ionic-team/ionic-docs/translation/jp/scripts/data/translated-api.json'
13+
);
1514

16-
const names = components.map((component) => component.tag.slice(4));
17-
// matches all relative markdown links to a component, e.g. (../button)
18-
COMPONENT_LINK_REGEXP = new RegExp(`\\(../(${names.join('|')})/?(#[^)]+)?\\)`, 'g');
15+
if (!response.ok) {
16+
console.error(`Failed to fetch translated API data: ${response.status}`);
17+
return;
18+
}
1919

20-
components.map(writePage);
20+
const { components } = await response.json();
21+
22+
const names = components.map((component) => component.tag.slice(4));
23+
// matches all relative markdown links to a component, e.g. (../button)
24+
COMPONENT_LINK_REGEXP = new RegExp(`\\(../(${names.join('|')})/?(#[^)]+)?\\)`, 'g');
25+
26+
components.map(writePage);
27+
} catch (error) {
28+
console.error('Error in api-ja script:', error.message);
29+
}
2130
})();
2231

2332
function writePage(page) {
@@ -36,8 +45,8 @@ function writePage(page) {
3645
// fix relative links, e.g. (../button) -> (button.md)
3746
data = data.replace(COMPONENT_LINK_REGEXP, '($1.md$2)');
3847

39-
const path = `i18n/ja/docusaurus-plugin-content-docs/current/api/${page.tag.slice(4)}.md`;
40-
fs.writeFileSync(path, data);
48+
const filePath = `i18n/ja/docusaurus-plugin-content-docs/current/api/${page.tag.slice(4)}.md`;
49+
fs.writeFileSync(filePath, data);
4150
}
4251

4352
function renderFrontmatter({ tag }) {
@@ -65,7 +74,6 @@ ${utils.getHeadTag(apiOverrides[tag])}
6574

6675
function renderReadme({ readme, encapsulation }) {
6776
const endIndex = readme.indexOf('\n');
68-
6977
const title = readme.substring(0, endIndex);
7078
const rest = readme.substring(endIndex);
7179

@@ -77,7 +85,6 @@ import EncapsulationPill from '@components/page/api/EncapsulationPill';
7785
7886
${encapsulation !== 'none' ? `<EncapsulationPill type="${encapsulation}" />` : ''}
7987
80-
8188
${addAdmonitions(rest)}
8289
`;
8390
}
@@ -128,7 +135,6 @@ function renderProperties({ props: properties }) {
128135
return '';
129136
}
130137

131-
// NOTE: replaces | with U+FF5C since MDX renders \| in tables incorrectly
132138
return `
133139
## Properties
134140
@@ -141,7 +147,7 @@ ${properties
141147
| --- | --- |
142148
| **Description** | ${prop.docs.split('\n').join('<br />')} |
143149
| **Attribute** | \`${prop.attr}\` |
144-
| **Type** | \`${prop.type.replace(/\|/g, '\uff5c')}\` |
150+
| **Type** | \`${prop.type.replace(/\|/g, '\\|')}\` |
145151
| **Default** | \`${prop.default}\` |
146152
147153
`
@@ -170,7 +176,6 @@ function renderMethods({ methods }) {
170176
return '';
171177
}
172178

173-
// NOTE: replaces | with U+FF5C since MDX renders \| in tables incorrectly
174179
return `
175180
## Methods
176181
@@ -182,7 +187,7 @@ ${methods
182187
| | |
183188
| --- | --- |
184189
| **Description** | ${method.docs.split('\n').join('<br />')} |
185-
| **Signature** | \`${method.signature.replace(/\|/g, '\uff5c')}\` |
190+
| **Signature** | \`${method.signature.replace(/\|/g, '\\|')}\` |
186191
`
187192
)
188193
.join('\n')}

scripts/bak/build-data/file-contributors.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import dotenv from 'dotenv';
22
import { outputJson } from 'fs-extra';
3-
import fetch from 'node-fetch';
4-
import { resolve } from 'path';
3+
import { resolve, dirname } from 'path';
4+
import { fileURLToPath } from 'url';
55
import url from 'url';
66

77
dotenv.config();
88

9+
// Fix for __dirname in ES modules
10+
const __filename = fileURLToPath(import.meta.url);
11+
const __dirname = dirname(__filename);
12+
913
const OUTPUT_PATH = resolve(
1014
__dirname,
1115
'../data/github-commits.json'
@@ -24,7 +28,8 @@ export default {
2428
const getAllGHCommits = async (task: any, page = 1) => {
2529
try {
2630
task.output = `Getting commits: ${page * 100 - 99} - ${page * 100}`;
27-
const request = await fetch(
31+
32+
const response = await fetch(
2833
url.format({
2934
protocol: 'https',
3035
hostname: 'api.github.com',
@@ -35,27 +40,35 @@ const getAllGHCommits = async (task: any, page = 1) => {
3540
}
3641
}), {
3742
headers: {
38-
'Authorization': process.env.GITHUB_TOKEN !== undefined ? `token ${process.env.GITHUB_TOKEN}` : ''
43+
'Authorization': process.env.GITHUB_TOKEN !== undefined ? `token ${process.env.GITHUB_TOKEN}` : '',
44+
'User-Agent': 'ionic-docs'
3945
}
4046
}
4147
);
4248

43-
let commits = await request.json().then(list => list.reduce((obj: any, commit: any) => {
49+
if (!response.ok) {
50+
console.error(`GitHub API responded with status: ${response.status}`);
51+
return {};
52+
}
53+
54+
const list = await response.json();
55+
let commits = list.reduce((obj: any, commit: any) => {
4456
obj[commit.sha] = {
4557
id: commit.author.login,
4658
avatar: commit.author.avatar_url,
4759
time: commit.commit.committer.date
4860
};
4961
return obj;
50-
}, {}));
62+
}, {});
5163

5264
// recursively get more commits if there are more to get, limit 2k
5365
if (Object.keys(commits).length === 100 && page < 20) {
5466
commits = { ...commits, ...await getAllGHCommits(task, page + 1) };
5567
}
5668

5769
return commits;
58-
} catch {
70+
} catch (error) {
71+
console.error('Error fetching commits:', error.message);
5972
return {};
6073
}
6174
};

0 commit comments

Comments
 (0)