Skip to content

Commit e6d04b3

Browse files
committed
Update api-ja.js
1 parent 159ead8 commit e6d04b3

File tree

1 file changed

+72
-31
lines changed

1 file changed

+72
-31
lines changed

scripts/api-ja.js

Lines changed: 72 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const fetch = globalThis.fetch;
2-
32
const fs = require('fs');
43
const path = require('path');
54
const { api: apiOverrides } = require('./data/meta-override.json');
@@ -9,36 +8,66 @@ const DEMOS_PATH = path.resolve('static/demos');
98
let COMPONENT_LINK_REGEXP;
109

1110
(async function () {
12-
const response = await fetch(
13-
'https://raw.githubusercontent.com/ionic-team/ionic-docs/translation/jp/scripts/data/translated-api.json',
14-
);
15-
const { components } = await response.json();
16-
17-
const names = components.map((component) => component.tag.slice(4));
18-
// matches all relative markdown links to a component, e.g. (../button)
19-
COMPONENT_LINK_REGEXP = new RegExp(`\\(../(${names.join('|')})/?(#[^)]+)?\\)`, 'g');
20-
21-
components.map(writePage);
11+
try {
12+
const response = await fetch(
13+
'https://raw.githubusercontent.com/ionic-team/ionic-docs/translation/jp/scripts/data/translated-api.json',
14+
);
15+
16+
if (!response.ok) {
17+
console.error(`Failed to fetch translated API data: ${response.status} ${response.statusText}`);
18+
return;
19+
}
20+
21+
const data = await response.json();
22+
23+
if (!data || !data.components) {
24+
console.error('Invalid API data structure - missing components');
25+
return;
26+
}
27+
28+
const { components } = data;
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.map(writePage);
35+
} catch (error) {
36+
console.error('Error fetching or processing translated API data:', error);
37+
// Don't fail the build, just skip Japanese API generation
38+
console.log('Skipping Japanese API generation due to error');
39+
}
2240
})();
2341

2442
function writePage(page) {
25-
let data = [
26-
renderFrontmatter(page),
27-
renderReadme(page),
28-
renderUsage(page),
29-
renderProperties(page),
30-
renderEvents(page),
31-
renderMethods(page),
32-
renderParts(page),
33-
renderCustomProps(page),
34-
renderSlots(page),
35-
].join('');
36-
37-
// fix relative links, e.g. (../button) -> (button.md)
38-
data = data.replace(COMPONENT_LINK_REGEXP, '($1.md$2)');
39-
40-
const path = `i18n/ja/docusaurus-plugin-content-docs/current/api/${page.tag.slice(4)}.md`;
41-
fs.writeFileSync(path, data);
43+
try {
44+
let data = [
45+
renderFrontmatter(page),
46+
renderReadme(page),
47+
renderUsage(page),
48+
renderProperties(page),
49+
renderEvents(page),
50+
renderMethods(page),
51+
renderParts(page),
52+
renderCustomProps(page),
53+
renderSlots(page),
54+
].join('');
55+
56+
// fix relative links, e.g. (../button) -> (button.md)
57+
data = data.replace(COMPONENT_LINK_REGEXP, '($1.md$2)');
58+
59+
const filePath = `i18n/ja/docusaurus-plugin-content-docs/current/api/${page.tag.slice(4)}.md`;
60+
61+
// Ensure directory exists
62+
const dir = path.dirname(filePath);
63+
if (!fs.existsSync(dir)) {
64+
fs.mkdirSync(dir, { recursive: true });
65+
}
66+
67+
fs.writeFileSync(filePath, data);
68+
} catch (error) {
69+
console.error(`Error writing page for ${page.tag}:`, error);
70+
}
4271
}
4372

4473
function renderFrontmatter({ tag }) {
@@ -64,9 +93,21 @@ ${utils.getHeadTag(apiOverrides[tag])}
6493
`;
6594
}
6695

67-
function renderReadme({ readme, encapsulation }) {
96+
function renderReadme(page) {
97+
// Add null/undefined check
98+
if (!page || !page.readme) {
99+
console.warn(`Missing readme for component: ${page?.tag || 'unknown'}`);
100+
return '';
101+
}
102+
103+
const readme = page.readme;
68104
const endIndex = readme.indexOf('\n');
69-
105+
106+
// Add additional safety check
107+
if (endIndex === -1) {
108+
return readme; // Return the whole readme if no newline found
109+
}
110+
70111
const title = readme.substring(0, endIndex);
71112
const rest = readme.substring(endIndex);
72113

@@ -76,7 +117,7 @@ function renderReadme({ readme, encapsulation }) {
76117
return `
77118
import EncapsulationPill from '@components/page/api/EncapsulationPill';
78119
79-
${encapsulation !== 'none' ? `<EncapsulationPill type="${encapsulation}" />` : ''}
120+
${page.encapsulation !== 'none' ? `<EncapsulationPill type="${page.encapsulation}" />` : ''}
80121
81122
82123
${addAdmonitions(rest)}

0 commit comments

Comments
 (0)