Make the TypeDoc theme resilient against nested child lists (#589)

It's not clear why TypeDoc generates lists of lists for JSX node
children, but it does and we need to handle it.
This commit is contained in:
Natalie Weizenbaum 2021-10-11 22:10:12 +00:00 committed by GitHub
parent 67ec2dbdbb
commit e9fc66b955
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -47,13 +47,13 @@ class SassSiteRenderContext extends DefaultThemeRenderContext {
const childrenByCategories = context._groupByCategory(props.model);
if (childrenByCategories.size === 0) return navigation;
const secondary = navigation.children[navigation.children.length - 1];
const secondary = context._getNthChild(navigation, 1);
if (!secondary) return navigation;
const firstLI = secondary.children[0].children[0];
const firstLI = context._getNthChild(context._getNthChild(secondary, 0), 0);
const ul = firstLI.props["class"].startsWith("current ")
? firstLI.children[1]
: secondary.children[0];
? context._getNthChild(firstLI, 1)
: context._getNthChild(secondary, 0);
ul.children = Array.from(childrenByCategories).map(([title, children]) =>
JSX.createElement(JSX.Fragment, null,
@ -68,6 +68,26 @@ class SassSiteRenderContext extends DefaultThemeRenderContext {
return navigation;
}, this);
// Returns the `n`-th child of a JSX node. For some reason, JSX nodes created
// by TypeDoc can contain nested arrays, so this traverses them.
_getNthChild = (node, n) => {
let i = 0;
function traverse(array) {
for (const element of array) {
if (element instanceof Array) {
const result = traverse(element);
if (result != undefined) return result;
} else {
if (i === n) return element;
i++;
}
}
}
return traverse(node.children);
};
// Returns a map from category titles to the set of members of those
// categories.
_groupByCategory = (model) => {