Skip to content

Commit e0a6b0d

Browse files
committed
[Docs] Fix API reference
1 parent ef4ed5e commit e0a6b0d

File tree

9 files changed

+188
-1022
lines changed

9 files changed

+188
-1022
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
"@codemirror/search": "6.5.11",
8080
"@codemirror/state": "6.5.2",
8181
"@codemirror/view": "6.38.3",
82+
"@docusaurus/plugin-content-docs": "3.9.2",
83+
"@docusaurus/theme-common": "3.9.2",
8284
"@preact/signals-react": "1.3.6",
8385
"@reduxjs/toolkit": "2.6.1",
8486
"@types/xml2js": "0.4.14",

packages/docs/site/docusaurus.config.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ const config = {
2525
projectName: 'wordpress-playground', // Usually your repo name.
2626

2727
onBrokenLinks: 'throw',
28-
markdown: {
29-
hooks: {
30-
onBrokenMarkdownLinks: 'throw',
31-
},
32-
},
28+
onBrokenMarkdownLinks: 'throw',
3329

3430
// Even if you don't use internalization, you can use this field to set useful
3531
// metadata like HTML lang. For example, if your site is Chinese, you may want
@@ -71,6 +67,7 @@ const config = {
7167
},
7268
themes: ['@docusaurus/theme-live-codeblock'],
7369
plugins: [
70+
'./plugins/docusaurus-dedupe-aliases.js',
7471
getDocusaurusPluginTypedocApiConfig(),
7572
[
7673
'@docusaurus/plugin-ideal-image',
@@ -266,7 +263,7 @@ function getDocusaurusPluginTypedocApiConfig() {
266263
};
267264

268265
return [
269-
'docusaurus-plugin-typedoc-api',
266+
require.resolve('./plugins/typedoc-api-wrapper.js'),
270267
{
271268
projectRoot,
272269
packages,
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
const fs = require('fs');
2+
const moduleRequire = require('module').createRequire(__dirname);
3+
4+
const PACKAGES_TO_DEDUPE = [
5+
'@docusaurus/plugin-content-docs',
6+
'@docusaurus/theme-common',
7+
'@docusaurus/theme-search-algolia',
8+
];
9+
10+
function getExportKeys(exportsField) {
11+
if (!exportsField) {
12+
return ['.'];
13+
}
14+
15+
if (typeof exportsField === 'string') {
16+
return ['.'];
17+
}
18+
19+
if (Array.isArray(exportsField)) {
20+
return ['.'];
21+
}
22+
23+
if (typeof exportsField === 'object') {
24+
const keys = new Set(['.']);
25+
for (const key of Object.keys(exportsField)) {
26+
if (key === 'default' || key.includes('*')) {
27+
continue;
28+
}
29+
keys.add(key);
30+
}
31+
return Array.from(keys);
32+
}
33+
34+
return ['.'];
35+
}
36+
37+
function normalizeSubpath(pkgName, subpath) {
38+
if (subpath === '.' || !subpath) {
39+
return pkgName;
40+
}
41+
return `${pkgName}/${subpath.replace(/^\.\//, '')}`;
42+
}
43+
44+
function buildAliasesForPackage(pkgName) {
45+
let pkgJsonPath;
46+
try {
47+
pkgJsonPath = moduleRequire.resolve(`${pkgName}/package.json`);
48+
} catch (error) {
49+
console.warn(
50+
`docusaurus-dedupe-aliases: unable to resolve ${pkgName}`,
51+
error
52+
);
53+
return [];
54+
}
55+
56+
const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8'));
57+
const exportKeys = getExportKeys(pkgJson.exports);
58+
59+
return exportKeys.flatMap((subpath) => {
60+
const specifier = normalizeSubpath(pkgName, subpath);
61+
try {
62+
const target = moduleRequire.resolve(specifier);
63+
const aliasKey =
64+
subpath === '.' ? `${pkgName}$` : specifier.replace(/\\/g, '/');
65+
return [[aliasKey, target]];
66+
} catch (error) {
67+
console.warn(
68+
`docusaurus-dedupe-aliases: unable to resolve specifier ${specifier}`,
69+
error
70+
);
71+
return [];
72+
}
73+
});
74+
}
75+
76+
module.exports = function docusaurusDedupeAliases() {
77+
const aliasEntries = PACKAGES_TO_DEDUPE.flatMap(buildAliasesForPackage);
78+
const aliases = Object.fromEntries(aliasEntries);
79+
80+
return {
81+
name: 'docusaurus-dedupe-aliases',
82+
configureWebpack() {
83+
return {
84+
resolve: {
85+
alias: aliases,
86+
},
87+
};
88+
},
89+
};
90+
};
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const path = require('path');
2+
const typedocApiPlugin = require('docusaurus-plugin-typedoc-api');
3+
4+
const typedocPackageRoot = path.dirname(
5+
require.resolve('docusaurus-plugin-typedoc-api/package.json')
6+
);
7+
8+
const componentMap = new Map(
9+
['ApiPage', 'ApiIndex', 'ApiItem', 'ApiChangelog'].map((name) => [
10+
path.join(typedocPackageRoot, `lib/components/${name}.js`),
11+
path.join(__dirname, `../src/typedoc/${name}.tsx`),
12+
])
13+
);
14+
15+
function remapComponent(componentPath) {
16+
if (!componentPath) {
17+
return componentPath;
18+
}
19+
20+
const normalized = path.normalize(componentPath);
21+
for (const [original, replacement] of componentMap.entries()) {
22+
if (normalized === original) {
23+
return replacement;
24+
}
25+
}
26+
27+
return componentPath;
28+
}
29+
30+
function remapRoutes(routes) {
31+
if (!routes) {
32+
return routes;
33+
}
34+
35+
return routes.map(remapRoute);
36+
}
37+
38+
function remapRoute(route) {
39+
if (!route) {
40+
return route;
41+
}
42+
43+
return {
44+
...route,
45+
component: remapComponent(route.component),
46+
routes: remapRoutes(route.routes),
47+
};
48+
}
49+
50+
module.exports = function typedocApiWrapper(context, options) {
51+
const plugin = typedocApiPlugin(context, options);
52+
const originalContentLoaded = plugin.contentLoaded?.bind(plugin);
53+
54+
return {
55+
...plugin,
56+
async contentLoaded(args) {
57+
if (!originalContentLoaded) {
58+
return;
59+
}
60+
61+
const patchedActions = {
62+
...args.actions,
63+
addRoute(routeConfig) {
64+
return args.actions.addRoute(remapRoute(routeConfig));
65+
},
66+
};
67+
68+
return originalContentLoaded({
69+
...args,
70+
actions: patchedActions,
71+
});
72+
},
73+
};
74+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import type { ComponentType } from 'react';
2+
import ApiChangelog from 'docusaurus-plugin-typedoc-api/lib/components/ApiChangelog.js';
3+
4+
export default ApiChangelog as ComponentType<any>;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import type { ComponentType } from 'react';
2+
import ApiIndex from 'docusaurus-plugin-typedoc-api/lib/components/ApiIndex.js';
3+
4+
export default ApiIndex as ComponentType<any>;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import type { ComponentType } from 'react';
2+
import ApiItem from 'docusaurus-plugin-typedoc-api/lib/components/ApiItem.js';
3+
4+
export default ApiItem as ComponentType<any>;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import type { ComponentType } from 'react';
2+
import ApiPage from 'docusaurus-plugin-typedoc-api/lib/components/ApiPage.js';
3+
4+
export default ApiPage as ComponentType<any>;

0 commit comments

Comments
 (0)