|
1 |
| -import { |
2 |
| - DataContext, |
3 |
| - isEqualPath, |
4 |
| - pathnameToRouteService, |
5 |
| - useLocation, |
6 |
| -} from '@rspress/runtime'; |
7 |
| -import { |
8 |
| - type BaseRuntimePageInfo, |
9 |
| - type FrontMatterMeta, |
10 |
| - type Header, |
11 |
| - MDX_OR_MD_REGEXP, |
12 |
| - type PageData, |
13 |
| - cleanUrl, |
14 |
| -} from '@rspress/shared'; |
| 1 | +import { DataContext, useLocation } from '@rspress/runtime'; |
15 | 2 | import { Layout } from '@theme';
|
16 | 3 | import React, { useContext, useLayoutEffect } from 'react';
|
17 | 4 | import { HelmetProvider } from 'react-helmet-async';
|
18 | 5 | import globalComponents from 'virtual-global-components';
|
19 |
| -import siteData from 'virtual-site-data'; |
20 | 6 | import 'virtual-global-styles';
|
| 7 | +import { initPageData } from './initPageData'; |
21 | 8 |
|
22 |
| -export enum QueryStatus { |
| 9 | +enum QueryStatus { |
23 | 10 | Show = '1',
|
24 | 11 | Hide = '0',
|
25 | 12 | }
|
26 | 13 |
|
27 |
| -type PageMeta = { |
28 |
| - title: string; |
29 |
| - headingTitle: string; |
30 |
| - toc: Header[]; |
31 |
| - frontmatter: FrontMatterMeta; |
32 |
| -}; |
33 |
| - |
34 |
| -export async function initPageData(routePath: string): Promise<PageData> { |
35 |
| - const matchedRoute = pathnameToRouteService(routePath); |
36 |
| - if (matchedRoute) { |
37 |
| - // Preload route component |
38 |
| - const mod = await matchedRoute.preload(); |
39 |
| - const pagePath = cleanUrl(matchedRoute.filePath); |
40 |
| - const normalize = (p: string) => |
41 |
| - // compat the path that has no / suffix and ignore case |
42 |
| - p |
43 |
| - .replace(/\/$/, '') |
44 |
| - .toLowerCase(); |
45 |
| - const extractPageInfo: BaseRuntimePageInfo = siteData.pages.find(page => |
46 |
| - isEqualPath(normalize(page.routePath), normalize(matchedRoute.path)), |
47 |
| - )!; |
48 |
| - |
49 |
| - // FIXME: when sidebar item is configured as link string, the sidebar text won't updated when page title changed |
50 |
| - // Reason: The sidebar item text depends on pageData, which is not updated when page title changed, because the pageData is computed once when build |
51 |
| - const encodedPagePath = encodeURIComponent(pagePath); |
52 |
| - const meta: PageMeta = |
53 |
| - ( |
54 |
| - mod.default as unknown as { |
55 |
| - __RSPRESS_PAGE_META: Record<string, PageMeta>; |
56 |
| - } |
57 |
| - ).__RSPRESS_PAGE_META?.[encodedPagePath] || ({} as PageMeta); |
58 |
| - // mdx loader will generate __RSPRESS_PAGE_META, |
59 |
| - // if the filePath don't match it, we can get meta from j(t)sx if we customize it |
60 |
| - const { |
61 |
| - toc = [], |
62 |
| - title = '', |
63 |
| - frontmatter = {}, |
64 |
| - ...rest |
65 |
| - } = MDX_OR_MD_REGEXP.test(matchedRoute.filePath) |
66 |
| - ? meta |
67 |
| - : (mod as unknown as PageMeta); |
68 |
| - return { |
69 |
| - siteData, |
70 |
| - page: { |
71 |
| - ...rest, |
72 |
| - pagePath, |
73 |
| - ...extractPageInfo, |
74 |
| - pageType: frontmatter?.pageType || 'doc', |
75 |
| - title, |
76 |
| - frontmatter, |
77 |
| - toc, |
78 |
| - }, |
79 |
| - } satisfies PageData; |
80 |
| - } |
81 |
| - |
82 |
| - let lang = siteData.lang || ''; |
83 |
| - let version = siteData.multiVersion?.default || ''; |
84 |
| - |
85 |
| - if (siteData.lang && typeof window !== 'undefined') { |
86 |
| - const path = location.pathname |
87 |
| - .replace(siteData.base, '') |
88 |
| - .split('/') |
89 |
| - .slice(0, 2); |
90 |
| - |
91 |
| - if (siteData.locales.length) { |
92 |
| - const result = siteData.locales.find(({ lang }) => path.includes(lang)); |
93 |
| - |
94 |
| - if (result) { |
95 |
| - lang = result.lang; |
96 |
| - } |
97 |
| - } |
98 |
| - |
99 |
| - if (siteData.multiVersion.versions) { |
100 |
| - const result = siteData.multiVersion.versions.find(version => |
101 |
| - path.includes(version), |
102 |
| - ); |
103 |
| - |
104 |
| - if (result) { |
105 |
| - version = result; |
106 |
| - } |
107 |
| - } |
108 |
| - } |
109 |
| - |
110 |
| - // 404 Page |
111 |
| - return { |
112 |
| - siteData, |
113 |
| - page: { |
114 |
| - pagePath: '', |
115 |
| - pageType: '404', |
116 |
| - routePath: '/404', |
117 |
| - lang, |
118 |
| - frontmatter: {}, |
119 |
| - title: '404', |
120 |
| - toc: [], |
121 |
| - version, |
122 |
| - _filepath: '', |
123 |
| - _relativePath: '', |
124 |
| - }, |
125 |
| - }; |
126 |
| -} |
127 |
| - |
128 | 14 | export function App({ helmetContext }: { helmetContext?: object }) {
|
129 | 15 | const { setData: setPageData, data } = useContext(DataContext);
|
130 | 16 | const frontmatter = data.page.frontmatter || {};
|
|
0 commit comments