Skip to content

Commit 491c621

Browse files
committed
refactor: change generateToc
1 parent 8adcaf1 commit 491c621

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

src/components/WrapperPost.astro

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,9 @@ import DraftContentNotice from './DraftContentNotice.astro'
88
import FallbackContentNotice from './FallbackContentNotice.astro'
99
import Toc from './Toc.astro'
1010
11-
const { entry } = Astro.props
11+
const { entry, hasToc, headins } = Astro.props
1212
const { data: frontmatter } = entry
1313
14-
const {
15-
remarkPluginFrontmatter: { hasToc },
16-
headings,
17-
} = await entry.render()
18-
19-
const toc = generateToc(headings ?? [], 1, 4)
20-
2114
let tweetUrl = ''
2215
let elkUrl = ''
2316
@@ -71,7 +64,7 @@ if (config.social?.mastodon) {
7164
{
7265
hasToc && (
7366
<section>
74-
<Toc headings={toc} />
67+
<Toc headings={headins} />
7568
</section>
7669
)
7770
}

src/routes/common.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type Props = {
1010
const { route } = Astro.props
1111
1212
const { Content, headings } = await route.entry.render()
13-
const routeData = generateRouteData({ props: { ...route, headings }, url: Astro.url })
13+
const routeData = await generateRouteData({ props: { ...route, headings }, url: Astro.url })
1414
---
1515

1616
<Page {...routeData}><Content frontmatter={route.entry.data} /></Page>

src/utils/route-data.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Route } from './routing'
33
import config from 'virtual:vitesse/user-config'
44
import { formatPath } from './format-path'
55
import { getNavBar, type NavBarEntry } from './navigation'
6+
import { generateToc, type TocHeading } from './toc'
67

78
export interface PageProps extends Route {
89

@@ -16,6 +17,8 @@ export interface VitesseRouteData extends Route {
1617
isFullWidthLayout?: boolean
1718
/** Site navigation sidebar entries for this page. */
1819
navBar: NavBarEntry[]
20+
hasToc: boolean
21+
headins: TocHeading[]
1922
}
2023

2124
/** Get the site title for a given language. */
@@ -31,15 +34,22 @@ export function getSiteTitleHref(locale: string | undefined): string {
3134
return formatPath(locale || '/')
3235
}
3336

34-
export function generateRouteData({
37+
export async function generateRouteData({
3538
props,
3639
url,
3740
}: {
3841
props: PageProps
3942
url: URL
40-
}): VitesseRouteData {
43+
}): Promise<VitesseRouteData> {
4144
const { entry, locale, lang } = props
4245
const navBar = getNavBar(url.pathname, locale)
46+
let tocHeading: TocHeading[] = []
47+
48+
const { remarkPluginFrontmatter: { hasToc = false }, headings } = await entry.render()
49+
50+
if (hasToc) {
51+
tocHeading = generateToc(headings, 1, 4)
52+
}
4353

4454
const siteTitle = getSiteTitle(lang)
4555
return {
@@ -48,5 +58,7 @@ export function generateRouteData({
4858
navBar,
4959
siteTitleHref: getSiteTitleHref(locale),
5060
isFullWidthLayout: entry?.data.layoutFullWidth,
61+
hasToc,
62+
headins: tocHeading,
5163
}
5264
}

0 commit comments

Comments
 (0)