Skip to content

Commit d9c7e99

Browse files
CopilotSoonIter
andauthored
docs: describe useSite/usePage runtime hooks (#2887)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: SoonIter <[email protected]>
1 parent 5d394d4 commit d9c7e99

File tree

6 files changed

+82
-2
lines changed

6 files changed

+82
-2
lines changed

website/docs/en/ui/hooks/_meta.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
"use-dark",
55
"router-hooks",
66
"use-i18n",
7-
"use-page-data"
7+
"use-page-data",
8+
"use-page",
9+
"use-site"
810
]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# usePage
2+
3+
`usePage` exposes metadata extracted from the current Markdown or MDX page.
4+
5+
- **Type:** `() => { page: PageDataLegacy['page'] }`
6+
7+
The `page` object contains parsed frontmatter and runtime details such as `title`, `toc`, `lang`, `version`, `routePath`, `pagePath`, `description`, `headingTitle`, `pageType`, and `lastUpdatedTime`, allowing you to build contextual UI for the active document.
8+
9+
```tsx preview
10+
import { usePage } from '@rspress/core/runtime';
11+
12+
export default function LastUpdated() {
13+
const { page } = usePage();
14+
return page.lastUpdatedTime ? (
15+
<span>Updated: {page.lastUpdatedTime}</span>
16+
) : null;
17+
}
18+
```
19+
20+
> Use `usePage` together with [`useSite`](./use-site) or `usePages` when you need both per-page metadata and global site information.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# useSite
2+
3+
`useSite` returns the normalized site configuration generated from your `rspress.config.ts`.
4+
5+
- **Type:** `() => { site: SiteData }`
6+
7+
The `site` object mirrors the normalized config, including base path, language/locales, theme configuration, search and markdown options, and multi-version settings.
8+
9+
```tsx preview
10+
import { useSite } from '@rspress/core/runtime';
11+
12+
export default function SiteTitle() {
13+
const { site } = useSite();
14+
return <span>{site.title}</span>;
15+
}
16+
```
17+
18+
> Pair with [`usePage`](./use-page) to combine global config with the current page metadata.

website/docs/zh/ui/hooks/_meta.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
"use-dark",
55
"router-hooks",
66
"use-i18n",
7-
"use-page-data"
7+
"use-page-data",
8+
"use-page",
9+
"use-site"
810
]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# usePage
2+
3+
`usePage` 提供当前 Markdown 或 MDX 页面解析得到的元信息。
4+
5+
- **类型:** `() => { page: PageDataLegacy['page'] }`
6+
7+
`page` 包含解析出的 frontmatter 以及运行时信息,例如 `title``toc``lang``version``routePath``pagePath``description``headingTitle``pageType``lastUpdatedTime` 等,方便基于当前文档构建上下文 UI。
8+
9+
```tsx preview
10+
import { usePage } from '@rspress/core/runtime';
11+
12+
export default function LastUpdated() {
13+
const { page } = usePage();
14+
return page.lastUpdatedTime ? (
15+
<span>Updated: {page.lastUpdatedTime}</span>
16+
) : null;
17+
}
18+
```
19+
20+
> 当需要同时获取页面元信息和站点全局配置时,可结合 [`useSite`](./use-site)`usePages` 使用。
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# useSite
2+
3+
`useSite` 返回根据 `rspress.config.ts` 归一化后的站点配置。
4+
5+
- **类型:** `() => { site: SiteData }`
6+
7+
`site` 包含已归一化的配置数据,包括基础路径、语言与多语言信息、主题配置、搜索与 Markdown 选项、多版本设置等。
8+
9+
```tsx preview
10+
import { useSite } from '@rspress/core/runtime';
11+
12+
export default function SiteTitle() {
13+
const { site } = useSite();
14+
return <span>{site.title}</span>;
15+
}
16+
```
17+
18+
> 可以配合 [`usePage`](./use-page) 一起使用,同时获取全局站点配置和当前页面的元信息。

0 commit comments

Comments
 (0)