Skip to content

Commit 04b8596

Browse files
committed
fix(toc): handles upwards discontinuity
1 parent 7eb2193 commit 04b8596

File tree

1 file changed

+16
-5
lines changed
  • apps/website/src/components/toc

1 file changed

+16
-5
lines changed

apps/website/src/components/toc/toc.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,23 @@ export const TableOfContent = component$<TableOfContentProps>((props) => {
3636
function deltaToStrg(
3737
currNode: Node,
3838
nextNode: Node,
39-
): 'same level' | 'down one level' | 'up one level' {
39+
): 'same level' | 'down one level' | 'up one level' | 'upwards discontinuous' {
4040
const delta = currNode.level - nextNode.level;
41+
if (delta > 1) {
42+
return 'upwards discontinuous';
43+
}
4144
if (delta === 1) {
4245
return 'up one level';
4346
}
47+
if (delta === 0) {
48+
return 'same level';
49+
}
4450
if (delta === -1) {
4551
return 'down one level';
4652
}
4753

48-
if (delta === 0) {
49-
return 'same level';
50-
}
5154
throw new Error(
52-
`bad headings: are not continous from: #${currNode.id} to #${nextNode.id}`,
55+
`bad headings: are downwards discontinous from: #${currNode.id} to #${nextNode.id} bc from ${currNode.level} to ${nextNode.level}`,
5356
);
5457
}
5558
function getTree(nodes: ContentHeading[]) {
@@ -64,6 +67,14 @@ function getTree(nodes: ContentHeading[]) {
6467
childrenMap.set(nextNode.level, nextNode.children);
6568
const deltaStrg = deltaToStrg(currNode, nextNode);
6669
switch (deltaStrg) {
70+
case 'upwards discontinuous': {
71+
const delta = currNode.level - nextNode.level;
72+
if (childrenMap.has(delta - 1)) {
73+
const nthParent = childrenMap.get(delta - 1);
74+
nthParent?.push(nextNode);
75+
}
76+
break;
77+
}
6778
case 'up one level': {
6879
const grandParent = childrenMap.get(currNode.level - 2);
6980
grandParent?.push(nextNode);

0 commit comments

Comments
 (0)