-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from chrismwilliams/fix/toc-order
- Loading branch information
Showing
6 changed files
with
95 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
import type { MarkdownHeading } from "astro"; | ||
import { generateToc } from "src/utils/generateToc"; | ||
import TOCHeading from "./TOCHeading.astro"; | ||
interface Props { | ||
headings: Array<MarkdownHeading>; | ||
} | ||
const { headings } = Astro.props; | ||
const toc = generateToc(headings); | ||
--- | ||
|
||
<aside class="sticky top-20 order-2 -me-32 hidden basis-64 lg:block"> | ||
<h2 class="font-semibold">Table of Contents</h2> | ||
<ul class="mt-4 text-xs"> | ||
{toc.map((heading) => <TOCHeading heading={heading} />)} | ||
</ul> | ||
</aside> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
import type { TocItem } from "@/utils"; | ||
interface Props { | ||
heading: TocItem; | ||
} | ||
const { | ||
heading: { slug, text, depth, subheadings }, | ||
} = Astro.props; | ||
--- | ||
|
||
<li class={`${depth > 2 ? "ms-2" : ""}`}> | ||
<a | ||
class={`block line-clamp-2 hover:text-accent ${depth <= 2 ? "mt-3" : "mt-2 text-[0.6875rem]"}`} | ||
href={`#${slug}`} | ||
aria-label={`Scroll to section: ${text}`}><span class="me-0.5">#</span>{text}</a | ||
> | ||
{ | ||
!!subheadings.length && ( | ||
<ul> | ||
{subheadings.map((subheading) => ( | ||
<Astro.self heading={subheading} /> | ||
))} | ||
</ul> | ||
) | ||
} | ||
</li> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { MarkdownHeading } from "astro"; | ||
|
||
export interface TocItem extends MarkdownHeading { | ||
subheadings: Array<TocItem>; | ||
} | ||
|
||
function diveChildren(item: TocItem, depth: number): Array<TocItem> { | ||
if (depth === 1 || !item.subheadings.length) { | ||
return item.subheadings; | ||
} else { | ||
// e.g., 2 | ||
return diveChildren(item.subheadings[item.subheadings.length - 1] as TocItem, depth - 1); | ||
} | ||
} | ||
|
||
export function generateToc(headings: ReadonlyArray<MarkdownHeading>) { | ||
// this ignores/filters out h1 element(s) | ||
const bodyHeadings = [...headings.filter(({ depth }) => depth > 1)]; | ||
const toc: Array<TocItem> = []; | ||
|
||
bodyHeadings.forEach((h) => { | ||
const heading: TocItem = { ...h, subheadings: [] }; | ||
|
||
// add h2 elements into the top level | ||
if (heading.depth === 2) { | ||
toc.push(heading); | ||
} else { | ||
const lastItemInToc = toc[toc.length - 1]!; | ||
if (heading.depth < lastItemInToc.depth) { | ||
throw new Error(`Orphan heading found: ${heading.text}.`); | ||
} | ||
|
||
// higher depth | ||
// push into children, or children's children | ||
const gap = heading.depth - lastItemInToc.depth; | ||
const target = diveChildren(lastItemInToc, gap); | ||
target.push(heading); | ||
} | ||
}); | ||
return toc; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export { getFormattedDate } from "./date"; | ||
export { elementHasClass, toggleClass } from "./domElement"; | ||
export { sortMDByDate, getUniqueTags, getUniqueTagsWithCount } from "./post"; | ||
export { generateToc } from "./generateToc"; | ||
export type { TocItem } from "./generateToc"; |