Skip to content

Commit

Permalink
feat: add indentations for "on this page" nav (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
brookslybrand authored Mar 26, 2024
1 parent 7d534e4 commit 00af5da
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 8 additions & 2 deletions app/components/doc-route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ function LargeOnThisPage({ doc }: { doc: SerializeFrom<Doc> }) {
<nav className="mb-3 flex items-center font-semibold">On this page</nav>
<ul className="md-toc flex flex-col flex-wrap gap-3 leading-[1.125]">
{doc.headings.map((heading, i) => (
<li key={i}>
<li
key={i}
className={heading.headingLevel === "h2" ? "ml-0" : "ml-4"}
>
<Link
to={`#${heading.slug}`}
dangerouslySetInnerHTML={{ __html: heading.html || "" }}
Expand Down Expand Up @@ -166,7 +169,10 @@ function SmallOnThisPage({ doc }: { doc: SerializeFrom<Doc> }) {
</summary>
<ul className="pl-9">
{doc.headings.map((heading, i) => (
<li key={i}>
<li
key={i}
className={heading.headingLevel === "h2" ? "ml-0" : "ml-4"}
>
<Link
to={`#${heading.slug}`}
dangerouslySetInnerHTML={{ __html: heading.html || "" }}
Expand Down
7 changes: 6 additions & 1 deletion app/modules/gh-docs/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export interface MenuDoc {

export interface Doc extends Omit<MenuDoc, "hasContent"> {
html: string;
headings: { html: string | null; slug: string | undefined }[];
headings: {
headingLevel: string;
html: string | null;
slug: string | undefined;
}[];
}

declare global {
Expand Down Expand Up @@ -109,6 +113,7 @@ function createTableOfContentsFromHeadings(html: string) {
let $headings = $(html)("h2,h3");

let headings = $headings.toArray().map((heading) => ({
headingLevel: heading.name,
html: $(heading)("a").remove().end().children().html(),
slug: heading.attributes.find((attr) => attr.name === "id")?.value,
}));
Expand Down

0 comments on commit 00af5da

Please sign in to comment.