Skip to content

Commit acbeb2f

Browse files
committed
redirect to test-runners instead.
1 parent 55a206e commit acbeb2f

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/theme/NavbarItem/index.js

+50
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,42 @@ import React from 'react';
1919
import NavbarItem from '@theme-original/NavbarItem';
2020
import { useLocation } from '@docusaurus/router';
2121

22+
const missingSites = calculateMissingSites(require('../../../nodejs/sidebars').docs, {
23+
'python': require('../../../python/sidebars').docs,
24+
'dotnet': require('../../../dotnet/sidebars').docs,
25+
'java': require('../../../java/sidebars').docs,
26+
});
27+
28+
function flatSidebar(sidebar, result = []) {
29+
for (const item of sidebar) {
30+
if (item.type === 'category') {
31+
flatSidebar(item.items, result);
32+
} else {
33+
result.push(item.id);
34+
}
35+
}
36+
return result;
37+
}
38+
39+
/**
40+
* @param {object} baseSidebar
41+
* @param {object} sidebars
42+
* @returns {string[]}
43+
*/
44+
function calculateMissingSites(baseSidebar, sidebars) {
45+
const missing = [];
46+
const baseItems = flatSidebar(baseSidebar);
47+
for (const [language, sidebar] of Object.entries(sidebars)) {
48+
const items = flatSidebar(sidebar);
49+
const missingInLanguage = [];
50+
for (const item of baseItems) {
51+
if (!items.includes(item))
52+
missing.push(`/${language}/docs/${item}`);
53+
}
54+
}
55+
return missing;
56+
}
57+
2258
export default function NavbarItemWrapper(props) {
2359
const location = useLocation();
2460
const languagePrefix = props['data-language-prefix'];
@@ -27,6 +63,8 @@ export default function NavbarItemWrapper(props) {
2763
// Rewrite the new link
2864
const newPathname = location.pathname.replace(/^(\/(java|dotnet|python))?\/(.*)/, '$3');
2965
propsOverrides.href = "pathname://" + languagePrefix + newPathname + location.hash;
66+
if (isMissingInLanguagePort(location.pathname, languagePrefix) && newPathname.startsWith('docs/test-'))
67+
propsOverrides.href = "pathname://" + languagePrefix + 'docs/test-runners';
3068
propsOverrides.autoAddBaseUrl = false
3169
propsOverrides.target = '_self';
3270

@@ -54,3 +92,15 @@ export default function NavbarItemWrapper(props) {
5492
</>
5593
);
5694
}
95+
96+
/**
97+
* @param {string} location
98+
* @param {string} linkPathPrefix
99+
* @returns {boolean}
100+
*/
101+
function isMissingInLanguagePort(location, linkPathPrefix) {
102+
const languagePort = linkPathPrefix.replace(/\//g, '');
103+
if (!languagePort)
104+
return false;
105+
return missingSites.includes('/' + languagePort + location.replace(/^(\/(java|dotnet|python))?(\/.*)/, '$3').replace(/(.*)\.html$/, '$1'));
106+
}

0 commit comments

Comments
 (0)