-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
168 additions
and
84 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
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
21 changes: 21 additions & 0 deletions
21
src/application/dto/article/utils/generateTableOfContents/generateTableOfContents.ts
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 { CollectionEntry } from "astro:content"; | ||
import { slugify } from "@modules/core/utils/slugify"; | ||
|
||
type TableOfContentsReturnType = CollectionEntry<"articles">["data"]["tableOfContents"]; | ||
|
||
const HEADINGS_REGEX = /<h([1-6])>(.*?)<\/h\1>/g; | ||
|
||
export function generateTableOfContents(html: string): TableOfContentsReturnType { | ||
const items: TableOfContentsReturnType = []; | ||
const matches = html.matchAll(HEADINGS_REGEX); | ||
|
||
for (const match of matches) { | ||
const level = Number.parseInt(match[1]); | ||
const heading = match[2]; | ||
const id = slugify(heading); | ||
|
||
items.push({ id, heading, level }); | ||
} | ||
|
||
return items; | ||
} |
1 change: 1 addition & 0 deletions
1
src/application/dto/article/utils/generateTableOfContents/index.ts
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 @@ | ||
export * from "./generateTableOfContents"; |
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
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
21 changes: 21 additions & 0 deletions
21
src/ui/modules/article/components/tableOfContents/TableOfContents.astro
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 { CollectionEntry } from "astro:content"; | ||
import "./table-of-contents.css"; | ||
interface TableOfContentsProps { | ||
tableOfContents: CollectionEntry<"articles">["data"]["tableOfContents"]; | ||
} | ||
const { tableOfContents } = Astro.props as TableOfContentsProps; | ||
--- | ||
<aside class="article__table-of-contents__wrapper" role="complementary"> | ||
<nav class="article__table-of-contents__nav" aria-label="Table of contents"> | ||
<ul class="article__table-of-contents__list flex column-nowrap"> | ||
{tableOfContents.map(({ id, heading, level}) => ( | ||
<li class="article__table-of-contents__item --underline-on-hover --is-clickable" style={`--inline-level: ${level};`}> | ||
<a class="article__table-of-contents__item__link" href={`#${id}`}>{heading}</a> | ||
</li> | ||
))} | ||
</ul> | ||
</nav> | ||
</aside> |
25 changes: 25 additions & 0 deletions
25
src/ui/modules/article/components/tableOfContents/table-of-contents.css
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,25 @@ | ||
.article__table-of-contents__wrapper { | ||
position: sticky; | ||
top: calc(var(--header-height) + 2rem); | ||
} | ||
|
||
.article__table-of-contents__nav { | ||
position: absolute; | ||
right: 1rem; | ||
} | ||
.article__table-of-contents__list { | ||
gap: 0.25rem 0; | ||
} | ||
.article__table-of-contents__item { | ||
margin-inline-start: calc(0.25rem * var(--inline-level)); | ||
width: fit-content; | ||
|
||
&:hover { | ||
color: var(--primary-main); | ||
} | ||
} | ||
|
||
.article__table-of-contents__item__link { | ||
position: relative; | ||
z-index: 1; | ||
} |
Oops, something went wrong.