Skip to content

Commit

Permalink
Improve weird header handling
Browse files Browse the repository at this point in the history
Resolves #2515
  • Loading branch information
Gerrit0 committed Mar 6, 2024
1 parent 9124254 commit edd6b6b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Bug Fixes

- Fixed an issue introduced with 0.25.10 which causes the page index to initially render empty, #2514.
- "On This Page" section is now smarter when handling page headings which do not follow the normal `h1>h2>h3` process, #2515.

## v0.25.10 (2024-03-03)

Expand Down
27 changes: 15 additions & 12 deletions src/lib/output/themes/default/partials/navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,16 @@ export function pageSidebar(context: DefaultThemeRenderContext, props: PageEvent
export function pageNavigation(context: DefaultThemeRenderContext, props: PageEvent<Reflection>) {
const levels: JSX.Element[][] = [[]];

function finalizeLevel() {
function finalizeLevel(finishedHandlingHeadings: boolean) {
const level = levels.pop()!;
if (levels[levels.length - 1].length === 0 && finishedHandlingHeadings) {
levels[levels.length - 1] = level;
return;
}

const built = (
<ul>
{levels.pop()!.map((l) => (
{level.map((l) => (
<li>{l}</li>
))}
</ul>
Expand All @@ -136,9 +142,9 @@ export function pageNavigation(context: DefaultThemeRenderContext, props: PageEv
for (const heading of props.pageHeadings) {
const inferredLevel = heading.level ? heading.level + 1 : 1;
while (inferredLevel < levels.length) {
finalizeLevel();
finalizeLevel(false);
}
if (inferredLevel > levels.length) {
while (inferredLevel > levels.length) {
// Lower level than before
levels.push([]);
}
Expand All @@ -152,13 +158,16 @@ export function pageNavigation(context: DefaultThemeRenderContext, props: PageEv
}

while (levels.length > 1) {
finalizeLevel();
finalizeLevel(true);
}

if (!levels[0].length) {
return <></>;
}

levels.unshift([]);
finalizeLevel(true);

return (
<details open={true} class="tsd-index-accordion tsd-page-navigation">
<summary class="tsd-accordion-summary">
Expand All @@ -167,13 +176,7 @@ export function pageNavigation(context: DefaultThemeRenderContext, props: PageEv
On This Page
</h3>
</summary>
<div class="tsd-accordion-details">
<ul>
{levels[0].map((l) => (
<li>{l}</li>
))}
</ul>
</div>
<div class="tsd-accordion-details">{levels[0]}</div>
</details>
);
}

0 comments on commit edd6b6b

Please sign in to comment.