Skip to content

Commit 801d9cb

Browse files
committed
Support typedoc 0.27 and 0.28
1 parent f60822e commit 801d9cb

File tree

5 files changed

+96
-69
lines changed

5 files changed

+96
-69
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ https://jameslan.github.io/typedoc-plugin-version-select/
6161
# Compatible TypeDoc versions
6262

6363
- 0.27.x
64+
- 0.28.x

package-lock.json

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

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typedoc-plugin-version-select",
3-
"version": "1.0.1-dev",
3+
"version": "2.0.0-dev",
44
"description": "Typedoc plugin to add a version selector next to the title",
55
"type": "module",
66
"main": "dist/index.js",
@@ -31,10 +31,11 @@
3131
"sinon": "^19.0.2",
3232
"tmp": "^0.2.3",
3333
"ts-node": "^10.9.2",
34+
"typedoc": "^0.28.0",
3435
"typescript": "^5.7.3"
3536
},
3637
"peerDependencies": {
37-
"typedoc": "^0.27.6"
38+
"typedoc": ">=0.27.0 <0.29.0"
3839
},
3940
"dependencies": {
4041
"mustache": "^4.2.0"

src/assets/version-select.css

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
#tsd-search #version-select {
1+
#version-select {
22
border: none;
33
background: none;
44
margin: 8px;
5-
font: inherit;
6-
font-weight: 700;
5+
font-size: inherit;
76
}
87

98
#tsd-search #version-select.loading {

src/index.tsx

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,41 +36,60 @@ function setupPageInjector(app: Application) {
3636
function toolbar(toolbar: (props: PageEvent<Reflection>) => JSX.Element) {
3737
return function (props: PageEvent<Reflection>) {
3838
const origin = toolbar(props);
39-
const found = findByProp(origin, 'id', 'tsd-search');
40-
found.children.push(<select id='version-select' class='title loading' />);
39+
// find the <a> element with class 'title' and inject a <select> element after it
40+
findByProp(origin, 'class', 'title', 'a', (elem, indices) => {
41+
const index = indices.pop();
42+
const children = indices.reduce((arr, idx) => arr[idx], elem.children) as JSX.Children[];
43+
children.splice(
44+
index + 1,
45+
0,
46+
<select id='version-select' class='title loading' />,
47+
);
48+
return true; // no need to continue searching
49+
});
4150
return origin;
4251
}
4352
}
4453

45-
function findByProp(element: JSX.Element, name: string, value: string): JSX.Element | undefined {
46-
function findInElement(element: JSX.Element) {
47-
if (element.props?.[name] === value) {
48-
return element;
54+
function findByProp(
55+
root: JSX.Element,
56+
name: string,
57+
value: string,
58+
tagName?: string,
59+
cb?: (elem: JSX.Element, indices: number[]) => void) {
60+
function findInElement(parent: JSX.Element, element: JSX.Element, indices: number[]) {
61+
if ((tagName == null || element.tag === tagName) && element.props?.[name] === value) {
62+
if (cb) {
63+
return cb(parent, indices.slice());
64+
}
4965
}
50-
return findInChildren(element.children);
66+
return findInChildren(element, [], element.children);
5167
}
52-
function findInChildren(children: JSX.Children[]) {
53-
for (const child of children) {
68+
function findInChildren(parent: JSX.Element, indices: number[], children: JSX.Children[]) {
69+
const newIndices = indices.slice();
70+
newIndices.push(0);
71+
for (let i = 0; i < children.length; i++) {
72+
const child = children[i];
73+
newIndices[newIndices.length - 1] = i;
5474
if (child == null) {
5575
continue;
5676
}
5777
if (Array.isArray(child)) { // child is JSX.Children[]
58-
const found = findInChildren(child);
59-
if (found != null) {
60-
return found;
61-
}
78+
if (findInChildren(parent, newIndices, child)) {
79+
return true;
80+
};
6281
continue; // didn't find in the child(JSX.Children[]), try next child
6382
}
6483
if (typeof child === 'object') { // child is JSX.Element
65-
const found = findInElement(child);
66-
if (found != null) {
67-
return found;
68-
}
84+
if (findInElement(parent, child, newIndices)) {
85+
return true;
86+
};
6987
}
7088
}
71-
return undefined; // not found
89+
return false; // not found
7290
}
73-
return findInElement(element);
91+
92+
findInChildren(root, [], root.children);
7493
}
7594

7695
function setupAssets(app: Application) {

0 commit comments

Comments
 (0)