Skip to content

Commit 6259df1

Browse files
committed
redirects for original URLs starting with #reference/...
1 parent f0a43a6 commit 6259df1

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

apify-docs-theme/src/theme/DocSidebarItem/Link/index.jsx

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ export default function DocSidebarItemLink({
3131
props.target = '_self';
3232
}
3333

34+
if (item.customProps) {
35+
for (const key of Object.keys(item.customProps)) {
36+
props[`data-${key}`] = item.customProps[key];
37+
}
38+
}
39+
3440
return (
3541
<li
3642
className={clsx(

apify-docs-theme/static/js/custom.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,14 @@ function redirectOpenApiDocs() {
8484
}
8585

8686
if (hash.startsWith('#/reference/')) {
87-
const id = hash.substring('/#reference/'.length);
88-
console.log('redirect', { id, hash });
87+
const sidebarItems = document.querySelectorAll('[data-altids]');
88+
89+
for (const item of sidebarItems) {
90+
const ids = item.getAttribute('data-altids').split(',');
91+
if (ids.find((variant) => variant === hash)) {
92+
item.click();
93+
}
94+
}
8995
}
9096

9197
if (hash.startsWith('#tag/')) {
@@ -109,7 +115,7 @@ document.addEventListener('scroll', () => {
109115
});
110116

111117
window.addEventListener('load', () => {
112-
redirectOpenApiDocs();
118+
setTimeout(() => redirectOpenApiDocs(), 500);
113119

114120
// we need to wait a bit more, since the event fires too soon, and a lot of hydration is done after it
115121
setTimeout(() => scrollSidebarItemIntoView(), 1000);

docusaurus.config.js

+30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const { join } = require('node:path');
22

3+
const clsx = require('clsx');
4+
35
const { config } = require('./apify-docs-theme');
46
const { collectSlugs } = require('./tools/utils/collectSlugs');
57
const { externalLinkProcessor } = require('./tools/utils/externalLink');
@@ -190,6 +192,34 @@ module.exports = {
190192
groupPathsBy: 'tag',
191193
sidebarCollapsed: false,
192194
sidebarCollapsible: false,
195+
sidebarGenerators: {
196+
createDocItem: (item, context) => {
197+
const legacyUrls = item.api['x-legacy-doc-urls'] ?? [];
198+
const altIds = legacyUrls.map((url) => {
199+
const { hash } = new URL(url);
200+
return hash;
201+
});
202+
const sidebarLabel = item.frontMatter.sidebar_label;
203+
const { title } = item;
204+
const id = item.type === 'schema' ? `schemas/${item.id}` : item.id;
205+
const className = item.type === 'api'
206+
? clsx({
207+
'menu__list-item--deprecated': item.api.deprecated,
208+
'api-method': !!item.api.method,
209+
}, item.api.method)
210+
: clsx({
211+
'menu__list-item--deprecated': item.schema.deprecated,
212+
}, 'schema');
213+
214+
return {
215+
type: 'doc',
216+
id: context.basePath === '' ? `${id}` : `${context.basePath}/${id}`,
217+
label: sidebarLabel ?? title ?? id,
218+
customProps: { altIds },
219+
className,
220+
};
221+
},
222+
},
193223
},
194224
},
195225
},

0 commit comments

Comments
 (0)