Skip to content

Commit d3cb987

Browse files
committed
feat: add meta in page module
1 parent 72d0ae0 commit d3cb987

File tree

6 files changed

+39
-12
lines changed

6 files changed

+39
-12
lines changed

src/runtime/app.tsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function waitForApp(path: string): Promise<PageData> {
1919
const pagePath = cleanUrl((matched[0].route as Route).filePath);
2020
const relativePagePath = getRelativePagePath(path, pagePath, siteData.base);
2121
// API Page
22-
if (mod.api || mod.pageType === 'api') {
22+
if (mod?.meta?.api || mod?.meta?.pageType === 'api') {
2323
const subModules = await Promise.all(
2424
routes
2525
.filter(
@@ -38,16 +38,18 @@ export async function waitForApp(path: string): Promise<PageData> {
3838
siteData,
3939
pagePath,
4040
relativePagePath,
41-
pageType: 'api',
42-
subModules
41+
subModules,
42+
...omit(mod, ['default']),
43+
pageType: 'api'
4344
};
4445
} else {
4546
// Doc/Custom Page
4647
return {
4748
siteData,
4849
pagePath,
4950
relativePagePath,
50-
...omit(mod, ['default'])
51+
...omit(mod, ['default']),
52+
pageType: mod?.meta?.pageType || 'doc'
5153
} as PageData;
5254
}
5355
} else {

src/shared/types/index.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,30 @@ export type ComponentPropsWithIsland<T = unknown> = T & { __island: boolean };
146146

147147
export interface PageModule<T extends ComponentType<unknown>> {
148148
default: T;
149+
meta?: FrontMatterMeta;
149150
[key: string]: unknown;
150151
}
151152

153+
export type PageType = 'home' | 'doc' | 'api' | 'custom' | '404';
154+
155+
export interface FrontMatterMeta {
156+
title: string;
157+
description: string;
158+
api: boolean;
159+
pageType: PageType;
160+
features?: Feature[];
161+
hero?: Hero;
162+
}
163+
152164
export interface PageData {
153165
siteData: SiteData<DefaultTheme.Config>;
154166
pagePath: string;
155167
relativePagePath: string;
156168
title?: string;
169+
meta?: FrontMatterMeta;
157170
description?: string;
158-
pageType: 'home' | 'doc' | 'api' | 'custom' | '404';
171+
pageType: PageType;
159172
toc?: Header[];
160-
features?: Feature[];
161-
hero?: Hero;
162173
icon?: string;
163174
subModules?: PageModule<ComponentType<unknown>>[];
164175
}

src/theme-default/components/HomeFeatures/index.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const getGridClass = (count?: number) => {
1616
};
1717

1818
export function HomeFeature() {
19-
const { features } = usePageData();
19+
const { meta } = usePageData();
20+
const features = meta?.features;
2021
const gridClass = getGridClass(features?.length);
2122
return (
2223
<div className={styles.features}>

src/theme-default/components/HomeHero/index.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const DEFAULT_HERO = {
1212
};
1313

1414
export function HomeHero() {
15-
const { hero = DEFAULT_HERO } = usePageData();
15+
const { meta } = usePageData();
16+
const hero = meta?.hero || DEFAULT_HERO;
1617
const hasImage = hero.image !== undefined;
1718

1819
return (

src/theme-default/layout/APILayout/index.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface GroupItem {
1313

1414
export function APILayout() {
1515
const { subModules = [] } = usePageData();
16+
1617
const initialGroups = subModules.map((subModule) => {
1718
const { routePath, title } = subModule;
1819
return {
@@ -23,17 +24,21 @@ export function APILayout() {
2324
});
2425
const [groups, setGroups] = useState(initialGroups);
2526

27+
console.log(groups);
28+
2629
useEffect(() => {
2730
// Handle title hmr
2831
if (import.meta.env.DEV) {
2932
import.meta.hot?.on('md(x)-changed', ({ routePath, filePath }) => {
33+
console.log(routePath);
3034
const group = groups.find((group) => group.link === routePath);
3135
if (!group) {
3236
return;
3337
}
3438
import(/* @vite-ignore */ `${filePath}?import&t=${Date.now()}`).then(
3539
(mod) => {
3640
group.headers = mod.toc;
41+
group.text = mod.title;
3742
setGroups([...groups]);
3843
}
3944
);

src/theme-default/layout/Layout/index.tsx

+10-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ import { Helmet } from 'react-helmet-async';
88
import { APILayout } from '../APILayout';
99

1010
export const Layout: React.FC = () => {
11-
const { pageType, title: pageTitle, description, siteData } = usePageData();
12-
// Priority page title > site title
13-
const title = pageTitle || siteData?.title;
11+
const {
12+
// Inject by remark-plugin-toc
13+
title: articleTitle,
14+
meta,
15+
siteData,
16+
pageType
17+
} = usePageData();
18+
// Priority: front matter title > h1 title > site title
19+
const title = (meta?.title ?? articleTitle) || siteData?.title;
20+
const description = meta?.description || siteData.description;
1421
// Use doc layout by default
1522
const getContentLayout = () => {
1623
switch (pageType) {

0 commit comments

Comments
 (0)