@rspress/core 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -0
- package/dist/index.js +53 -40
- package/dist/index.js.map +1 -1
- package/dist/loader.d.ts +1 -1
- package/dist/loader.js.map +1 -1
- package/dist/runtime/App.js +8 -4
- package/dist/runtime/clientEntry.js +7 -4
- package/dist/runtime/ssrEntry.js +2 -2
- package/package.json +19 -15
- package/runtime.ts +1 -1
- package/src/runtime/App.tsx +8 -4
- package/src/runtime/clientEntry.tsx +7 -4
- package/src/runtime/ssrEntry.tsx +2 -2
- package/theme.ts +2 -2
- package/dist/runtime/Content.d.ts +0 -6
- package/dist/runtime/Content.js +0 -33
- package/dist/runtime/NoSSR.d.ts +0 -4
- package/dist/runtime/NoSSR.js +0 -17
- package/dist/runtime/hooks.d.ts +0 -39
- package/dist/runtime/hooks.js +0 -54
- package/dist/runtime/index.d.ts +0 -6
- package/dist/runtime/index.js +0 -34
- package/dist/runtime/utils.d.ts +0 -8
- package/dist/runtime/utils.js +0 -45
- package/dist/theme/bundle.css +0 -2691
- package/dist/theme/bundle.d.ts +0 -176
- package/dist/theme/bundle.js +0 -4433
- package/dist/theme/bundle.js.map +0 -1
- package/dist/theme/index.js +0 -6
- package/src/runtime/Content.tsx +0 -43
- package/src/runtime/NoSSR.tsx +0 -16
- package/src/runtime/hooks.ts +0 -110
- package/src/runtime/index.ts +0 -17
- package/src/runtime/utils.ts +0 -46
- package/src/theme-default/assets/arrow-down.svg +0 -1
- package/src/theme-default/assets/arrow-right.svg +0 -1
- package/src/theme-default/assets/copy.svg +0 -1
- package/src/theme-default/assets/down.svg +0 -1
- package/src/theme-default/assets/github.svg +0 -1
- package/src/theme-default/assets/loading.svg +0 -1
- package/src/theme-default/assets/menu.svg +0 -1
- package/src/theme-default/assets/moon.svg +0 -3
- package/src/theme-default/assets/right.svg +0 -1
- package/src/theme-default/assets/small-menu.svg +0 -1
- package/src/theme-default/assets/success.svg +0 -1
- package/src/theme-default/assets/sun.svg +0 -11
- package/src/theme-default/assets/translator.svg +0 -1
- package/src/theme-default/components/Aside/index.css +0 -22
- package/src/theme-default/components/Aside/index.tsx +0 -75
- package/src/theme-default/components/BackDrop/index.module.scss +0 -14
- package/src/theme-default/components/BackDrop/index.tsx +0 -13
- package/src/theme-default/components/Button/index.module.scss +0 -68
- package/src/theme-default/components/Button/index.tsx +0 -40
- package/src/theme-default/components/DocFooter/index.module.scss +0 -60
- package/src/theme-default/components/DocFooter/index.tsx +0 -66
- package/src/theme-default/components/EditLink/index.module.scss +0 -11
- package/src/theme-default/components/EditLink/index.tsx +0 -18
- package/src/theme-default/components/HomeFeatures/index.module.scss +0 -50
- package/src/theme-default/components/HomeFeatures/index.tsx +0 -65
- package/src/theme-default/components/HomeFooter/index.tsx +0 -15
- package/src/theme-default/components/HomeHero/index.module.scss +0 -42
- package/src/theme-default/components/HomeHero/index.tsx +0 -75
- package/src/theme-default/components/Link/index.module.scss +0 -9
- package/src/theme-default/components/Link/index.tsx +0 -90
- package/src/theme-default/components/LocalSideBar/index.scss +0 -40
- package/src/theme-default/components/LocalSideBar/index.tsx +0 -39
- package/src/theme-default/components/Nav/NavBarTitle.tsx +0 -43
- package/src/theme-default/components/Nav/NavMenuGroup.tsx +0 -156
- package/src/theme-default/components/Nav/NavMenuSingleItem.tsx +0 -40
- package/src/theme-default/components/Nav/NavTranslations.tsx +0 -16
- package/src/theme-default/components/Nav/NavVersions.tsx +0 -16
- package/src/theme-default/components/Nav/index.module.scss +0 -115
- package/src/theme-default/components/Nav/index.tsx +0 -150
- package/src/theme-default/components/Nav/menuDataHooks.tsx +0 -75
- package/src/theme-default/components/NavHambmger/index.module.scss +0 -81
- package/src/theme-default/components/NavHambmger/index.tsx +0 -39
- package/src/theme-default/components/NavScreen/NavScreenMenuGroup.tsx +0 -95
- package/src/theme-default/components/NavScreen/index.module.scss +0 -88
- package/src/theme-default/components/NavScreen/index.tsx +0 -123
- package/src/theme-default/components/Overview/index.module.scss +0 -154
- package/src/theme-default/components/Overview/index.tsx +0 -136
- package/src/theme-default/components/PackageManagerTabs/icons/Bun.tsx +0 -101
- package/src/theme-default/components/PackageManagerTabs/icons/Npm.tsx +0 -16
- package/src/theme-default/components/PackageManagerTabs/icons/Pnpm.tsx +0 -22
- package/src/theme-default/components/PackageManagerTabs/icons/Yarn.tsx +0 -18
- package/src/theme-default/components/PackageManagerTabs/index.scss +0 -5
- package/src/theme-default/components/PackageManagerTabs/index.tsx +0 -110
- package/src/theme-default/components/Search/NoSearchResult.tsx +0 -13
- package/src/theme-default/components/Search/SearchPanel.tsx +0 -340
- package/src/theme-default/components/Search/SuggestItem.tsx +0 -135
- package/src/theme-default/components/Search/assets/close.svg +0 -1
- package/src/theme-default/components/Search/assets/file.svg +0 -1
- package/src/theme-default/components/Search/assets/header.svg +0 -1
- package/src/theme-default/components/Search/assets/jump.svg +0 -1
- package/src/theme-default/components/Search/assets/loading.svg +0 -1
- package/src/theme-default/components/Search/assets/search.svg +0 -1
- package/src/theme-default/components/Search/assets/title.svg +0 -1
- package/src/theme-default/components/Search/empty.svg +0 -4
- package/src/theme-default/components/Search/index.module.scss +0 -236
- package/src/theme-default/components/Search/index.tsx +0 -39
- package/src/theme-default/components/Search/logic/Provider.ts +0 -35
- package/src/theme-default/components/Search/logic/providers/LocalProvider.ts +0 -102
- package/src/theme-default/components/Search/logic/providers/RemoteProvider.ts +0 -40
- package/src/theme-default/components/Search/logic/search.ts +0 -341
- package/src/theme-default/components/Search/logic/util.test.ts +0 -21
- package/src/theme-default/components/Search/logic/util.ts +0 -127
- package/src/theme-default/components/Sidebar/SidebarGroup.tsx +0 -180
- package/src/theme-default/components/Sidebar/SidebarItem.tsx +0 -61
- package/src/theme-default/components/Sidebar/index.module.scss +0 -142
- package/src/theme-default/components/Sidebar/index.tsx +0 -156
- package/src/theme-default/components/SocialLinks/HiddenLinks.tsx +0 -30
- package/src/theme-default/components/SocialLinks/LinkContent.tsx +0 -90
- package/src/theme-default/components/SocialLinks/ShownLinks.tsx +0 -32
- package/src/theme-default/components/SocialLinks/index.module.scss +0 -18
- package/src/theme-default/components/SocialLinks/index.tsx +0 -36
- package/src/theme-default/components/SocialLinks/presetIcons.tsx +0 -165
- package/src/theme-default/components/SwitchAppearance/index.module.scss +0 -15
- package/src/theme-default/components/SwitchAppearance/index.tsx +0 -35
- package/src/theme-default/components/Tabs/index.module.scss +0 -46
- package/src/theme-default/components/Tabs/index.tsx +0 -123
- package/src/theme-default/components/Tag/index.tsx +0 -18
- package/src/theme-default/components/Toc/index.css +0 -18
- package/src/theme-default/components/Toc/index.tsx +0 -42
- package/src/theme-default/index.ts +0 -26
- package/src/theme-default/layout/DocLayout/docComponents/code.tsx +0 -112
- package/src/theme-default/layout/DocLayout/docComponents/hr.tsx +0 -10
- package/src/theme-default/layout/DocLayout/docComponents/index.module.scss +0 -69
- package/src/theme-default/layout/DocLayout/docComponents/index.tsx +0 -33
- package/src/theme-default/layout/DocLayout/docComponents/link.tsx +0 -22
- package/src/theme-default/layout/DocLayout/docComponents/list.tsx +0 -13
- package/src/theme-default/layout/DocLayout/docComponents/paragraph.tsx +0 -19
- package/src/theme-default/layout/DocLayout/docComponents/pre.tsx +0 -37
- package/src/theme-default/layout/DocLayout/docComponents/prisim-theme.ts +0 -73
- package/src/theme-default/layout/DocLayout/docComponents/table.tsx +0 -37
- package/src/theme-default/layout/DocLayout/docComponents/title.tsx +0 -40
- package/src/theme-default/layout/DocLayout/index.module.scss +0 -138
- package/src/theme-default/layout/DocLayout/index.tsx +0 -125
- package/src/theme-default/layout/HomeLayout/index.css +0 -18
- package/src/theme-default/layout/HomeLayout/index.tsx +0 -39
- package/src/theme-default/layout/Layout/index.tsx +0 -176
- package/src/theme-default/layout/NotFountLayout/index.tsx +0 -40
- package/src/theme-default/logic/TabDataContext.ts +0 -14
- package/src/theme-default/logic/index.ts +0 -8
- package/src/theme-default/logic/sideEffects.ts +0 -174
- package/src/theme-default/logic/useAppearance.ts +0 -57
- package/src/theme-default/logic/useEditLink.ts +0 -33
- package/src/theme-default/logic/useHiddenNav.ts +0 -58
- package/src/theme-default/logic/useLocaleSiteData.ts +0 -26
- package/src/theme-default/logic/useNav.ts +0 -36
- package/src/theme-default/logic/usePathUtils.ts +0 -73
- package/src/theme-default/logic/usePrevNextPage.ts +0 -40
- package/src/theme-default/logic/useSidebarData.ts +0 -73
- package/src/theme-default/logic/utils.ts +0 -38
- package/src/theme-default/styles/base.css +0 -102
- package/src/theme-default/styles/code.css +0 -177
- package/src/theme-default/styles/index.ts +0 -7
- package/src/theme-default/styles/scrollbar.css +0 -28
- package/src/theme-default/styles/vars.css +0 -81
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
import 'nprogress/nprogress.css';
|
|
2
|
-
import '../../styles';
|
|
3
|
-
import React, { useEffect } from 'react';
|
|
4
|
-
import { Helmet } from 'react-helmet-async';
|
|
5
|
-
import Theme, { Nav } from '@theme';
|
|
6
|
-
import { DocLayout, DocLayoutProps } from '../DocLayout';
|
|
7
|
-
import { HomeLayoutProps } from '../HomeLayout';
|
|
8
|
-
import type { NavProps } from '../../components/Nav';
|
|
9
|
-
import { usePageData, Content, removeBase, withBase } from '@/runtime';
|
|
10
|
-
import { useDisableNav, useLocaleSiteData } from '@/theme-default/logic';
|
|
11
|
-
|
|
12
|
-
export enum QueryStatus {
|
|
13
|
-
Show = '1',
|
|
14
|
-
Hide = '0',
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export type LayoutProps = {
|
|
18
|
-
top?: React.ReactNode;
|
|
19
|
-
bottom?: React.ReactNode;
|
|
20
|
-
} & DocLayoutProps &
|
|
21
|
-
HomeLayoutProps &
|
|
22
|
-
NavProps;
|
|
23
|
-
|
|
24
|
-
export const Layout: React.FC<LayoutProps> = props => {
|
|
25
|
-
const {
|
|
26
|
-
top,
|
|
27
|
-
bottom,
|
|
28
|
-
beforeDocFooter,
|
|
29
|
-
beforeDoc,
|
|
30
|
-
afterDoc,
|
|
31
|
-
beforeOutline,
|
|
32
|
-
afterOutline,
|
|
33
|
-
beforeNavTitle,
|
|
34
|
-
afterNavTitle,
|
|
35
|
-
beforeNav,
|
|
36
|
-
beforeHero,
|
|
37
|
-
afterHero,
|
|
38
|
-
beforeFeatures,
|
|
39
|
-
afterFeatures,
|
|
40
|
-
} = props;
|
|
41
|
-
const docProps: DocLayoutProps = {
|
|
42
|
-
beforeDocFooter,
|
|
43
|
-
beforeDoc,
|
|
44
|
-
afterDoc,
|
|
45
|
-
beforeOutline,
|
|
46
|
-
afterOutline,
|
|
47
|
-
};
|
|
48
|
-
const homeProps: HomeLayoutProps = {
|
|
49
|
-
beforeHero,
|
|
50
|
-
afterHero,
|
|
51
|
-
beforeFeatures,
|
|
52
|
-
afterFeatures,
|
|
53
|
-
};
|
|
54
|
-
const { siteData, page } = usePageData();
|
|
55
|
-
const {
|
|
56
|
-
pageType,
|
|
57
|
-
lang: currentLang,
|
|
58
|
-
// Inject by remark-plugin-toc
|
|
59
|
-
title: articleTitle,
|
|
60
|
-
frontmatter,
|
|
61
|
-
} = page;
|
|
62
|
-
const localesData = useLocaleSiteData();
|
|
63
|
-
const defaultLang = siteData.lang || '';
|
|
64
|
-
const [hideNavbar, setHideNavbar] = useDisableNav();
|
|
65
|
-
// Always show sidebar by default
|
|
66
|
-
// Priority: front matter title > h1 title
|
|
67
|
-
let title = (frontmatter?.title as string) ?? articleTitle;
|
|
68
|
-
const mainTitle = siteData.title || localesData.title;
|
|
69
|
-
const localeLanguages = Object.values(siteData.themeConfig.locales || {});
|
|
70
|
-
const langs = localeLanguages.map(item => item.lang) || [];
|
|
71
|
-
|
|
72
|
-
if (title && pageType === 'doc') {
|
|
73
|
-
// append main title as a suffix
|
|
74
|
-
title = `${title} - ${mainTitle}`;
|
|
75
|
-
} else {
|
|
76
|
-
title = mainTitle;
|
|
77
|
-
}
|
|
78
|
-
const description =
|
|
79
|
-
(frontmatter?.description as string) ||
|
|
80
|
-
siteData.description ||
|
|
81
|
-
localesData.description;
|
|
82
|
-
// Use doc layout by default
|
|
83
|
-
const getContentLayout = () => {
|
|
84
|
-
switch (pageType) {
|
|
85
|
-
case 'home':
|
|
86
|
-
return <Theme.HomeLayout {...homeProps} />;
|
|
87
|
-
case 'doc':
|
|
88
|
-
return <DocLayout {...docProps} />;
|
|
89
|
-
case '404':
|
|
90
|
-
return <Theme.NotFoundLayout />;
|
|
91
|
-
// The custom pageType will have navbar while the blank pageType will not.
|
|
92
|
-
case 'custom':
|
|
93
|
-
case 'blank':
|
|
94
|
-
return <Content />;
|
|
95
|
-
default:
|
|
96
|
-
return <DocLayout {...docProps} />;
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
useEffect(() => {
|
|
101
|
-
if (!defaultLang || process.env.TEST === '1') {
|
|
102
|
-
// Check the window.navigator.language to determine the default language
|
|
103
|
-
// If the default language is not the same as the current language, redirect to the default language
|
|
104
|
-
// The default language will not have a lang prefix in the URL
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
// Normalize current url, to ensure that the home url is always with a trailing slash
|
|
108
|
-
const { pathname } = window.location;
|
|
109
|
-
const cleanPathname = removeBase(pathname);
|
|
110
|
-
// Check if the user is visiting the site for the first time
|
|
111
|
-
const FIRST_VISIT_KEY = 'rspress-visited';
|
|
112
|
-
const visited = localStorage.getItem(FIRST_VISIT_KEY);
|
|
113
|
-
if (visited) {
|
|
114
|
-
return;
|
|
115
|
-
} else {
|
|
116
|
-
localStorage.setItem(FIRST_VISIT_KEY, '1');
|
|
117
|
-
}
|
|
118
|
-
const targetLang = window.navigator.language.split('-')[0];
|
|
119
|
-
if (!langs.includes(targetLang)) {
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
if (targetLang !== currentLang) {
|
|
123
|
-
if (targetLang === defaultLang) {
|
|
124
|
-
// Redirect to the default language
|
|
125
|
-
window.location.replace(pathname.replace(`/${currentLang}`, ''));
|
|
126
|
-
} else if (currentLang === defaultLang) {
|
|
127
|
-
// Redirect to the current language
|
|
128
|
-
window.location.replace(withBase(`/${targetLang}${cleanPathname}`));
|
|
129
|
-
} else {
|
|
130
|
-
// Redirect to the current language
|
|
131
|
-
window.location.replace(
|
|
132
|
-
pathname.replace(`/${currentLang}`, `/${targetLang}`),
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}, []);
|
|
137
|
-
|
|
138
|
-
// Control the display of the navbar, sidebar and aside
|
|
139
|
-
useEffect(() => {
|
|
140
|
-
const query = new URLSearchParams(location.search);
|
|
141
|
-
const navbar = query.get('navbar');
|
|
142
|
-
const sidebar = query.get('sidebar');
|
|
143
|
-
const aside = query.get('outline');
|
|
144
|
-
if (navbar === QueryStatus.Hide) {
|
|
145
|
-
setHideNavbar(true);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (sidebar === QueryStatus.Hide) {
|
|
149
|
-
document.documentElement.style.setProperty('--rp-sidebar-width', '0');
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (aside === QueryStatus.Hide) {
|
|
153
|
-
document.documentElement.style.setProperty('--rp-aside-width', '0');
|
|
154
|
-
}
|
|
155
|
-
}, []);
|
|
156
|
-
return (
|
|
157
|
-
<div>
|
|
158
|
-
<Helmet>
|
|
159
|
-
{title ? <title>{title}</title> : null}
|
|
160
|
-
{description ? <meta name="description" content={description} /> : null}
|
|
161
|
-
</Helmet>
|
|
162
|
-
{top}
|
|
163
|
-
|
|
164
|
-
{pageType !== 'blank' && !hideNavbar && (
|
|
165
|
-
<Nav
|
|
166
|
-
beforeNavTitle={beforeNavTitle}
|
|
167
|
-
afterNavTitle={afterNavTitle}
|
|
168
|
-
beforeNav={beforeNav}
|
|
169
|
-
/>
|
|
170
|
-
)}
|
|
171
|
-
|
|
172
|
-
<section>{getContentLayout()}</section>
|
|
173
|
-
{bottom}
|
|
174
|
-
</div>
|
|
175
|
-
);
|
|
176
|
-
};
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { usePageData } from '@/runtime';
|
|
2
|
-
|
|
3
|
-
export function NotFoundLayout() {
|
|
4
|
-
const { siteData } = usePageData();
|
|
5
|
-
const defaultLang = siteData.lang;
|
|
6
|
-
// Consider the existing sites include the defaultLang in some links, such as '/zh/guide/quick-start'
|
|
7
|
-
// We need to redirect them to '/guide/quick-start'
|
|
8
|
-
// In the meanwhile, we will not show the 404 page for the user experience
|
|
9
|
-
if (
|
|
10
|
-
defaultLang &&
|
|
11
|
-
typeof window !== 'undefined' &&
|
|
12
|
-
location.pathname.includes(`/${defaultLang}/`)
|
|
13
|
-
) {
|
|
14
|
-
const redirectUrl = location.pathname.replace(`/${defaultLang}/`, '/');
|
|
15
|
-
window.location.replace(redirectUrl);
|
|
16
|
-
return <></>;
|
|
17
|
-
} else {
|
|
18
|
-
// The 404 page content
|
|
19
|
-
return (
|
|
20
|
-
<div className="m-auto mt-50 p-16 sm:p-8 sm:pt-24 sm:pb-40 text-center flex-center flex-col">
|
|
21
|
-
<p className="text-6xl font-semibold">404</p>
|
|
22
|
-
<h1 className="leading-5 pt-3 text-xl font-bold">PAGE NOT FOUND</h1>
|
|
23
|
-
<div
|
|
24
|
-
style={{ height: '1px' }}
|
|
25
|
-
className="mt-6 mx-auto mb-4.5 w-16 bg-gray-light-1"
|
|
26
|
-
/>
|
|
27
|
-
|
|
28
|
-
<div className="pt-5">
|
|
29
|
-
<a
|
|
30
|
-
className="py-2 px-4 rounded-2xl inline-block border-solid border-brand text-brand font-medium hover:border-brand-dark hover:text-brand-dark transition-colors duration-300"
|
|
31
|
-
href={siteData.base}
|
|
32
|
-
aria-label="go to home"
|
|
33
|
-
>
|
|
34
|
-
Take me home
|
|
35
|
-
</a>
|
|
36
|
-
</div>
|
|
37
|
-
</div>
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { createContext } from 'react';
|
|
2
|
-
|
|
3
|
-
export interface TabData {
|
|
4
|
-
[key: string]: number | undefined;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export interface ITabDataContext {
|
|
8
|
-
tabData: TabData;
|
|
9
|
-
setTabData: (data: TabData) => void;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const TabDataContext = createContext<ITabDataContext>(
|
|
13
|
-
{} as ITabDataContext,
|
|
14
|
-
);
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export { usePrevNextPage } from './usePrevNextPage';
|
|
2
|
-
export { useEditLink } from './useEditLink';
|
|
3
|
-
export { useSidebarData } from './useSidebarData';
|
|
4
|
-
export { useHiddenNav, useDisableNav } from './useHiddenNav';
|
|
5
|
-
export { useLocaleSiteData } from './useLocaleSiteData';
|
|
6
|
-
export { setup, bindingAsideScroll, scrollToTarget } from './sideEffects';
|
|
7
|
-
export { usePathUtils } from './usePathUtils';
|
|
8
|
-
export * from './utils';
|
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
import { throttle } from 'lodash-es';
|
|
2
|
-
import { inBrowser } from '@rspress/shared';
|
|
3
|
-
|
|
4
|
-
export const DEFAULT_NAV_HEIGHT = 72;
|
|
5
|
-
|
|
6
|
-
export function scrollToTarget(
|
|
7
|
-
target: HTMLElement,
|
|
8
|
-
isSmooth: boolean,
|
|
9
|
-
fallbackHeight = DEFAULT_NAV_HEIGHT,
|
|
10
|
-
) {
|
|
11
|
-
const targetPadding = parseInt(
|
|
12
|
-
window.getComputedStyle(target).paddingTop,
|
|
13
|
-
10,
|
|
14
|
-
);
|
|
15
|
-
|
|
16
|
-
const targetTop =
|
|
17
|
-
window.scrollY +
|
|
18
|
-
target.getBoundingClientRect().top -
|
|
19
|
-
fallbackHeight -
|
|
20
|
-
targetPadding;
|
|
21
|
-
// Only scroll smoothly in page header anchor
|
|
22
|
-
window.scrollTo({
|
|
23
|
-
left: 0,
|
|
24
|
-
top: targetTop,
|
|
25
|
-
...(isSmooth ? { behavior: 'smooth' } : {}),
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// Control the scroll behavior of the browser when user clicks on a link
|
|
30
|
-
function bindingWindowScroll() {
|
|
31
|
-
// Initial scroll position
|
|
32
|
-
function scrollTo(el: HTMLElement, hash: string, isSmooth = false) {
|
|
33
|
-
let target: HTMLElement | null = null;
|
|
34
|
-
try {
|
|
35
|
-
target = el.classList.contains('header-anchor')
|
|
36
|
-
? el
|
|
37
|
-
: document.getElementById(decodeURIComponent(hash.slice(1)));
|
|
38
|
-
} catch (e) {
|
|
39
|
-
console.warn(e);
|
|
40
|
-
}
|
|
41
|
-
if (target) {
|
|
42
|
-
scrollToTarget(target, isSmooth);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
window.addEventListener(
|
|
47
|
-
'click',
|
|
48
|
-
e => {
|
|
49
|
-
// Only handle a tag click
|
|
50
|
-
const link = (e.target as Element).closest('a');
|
|
51
|
-
if (link) {
|
|
52
|
-
const { origin, hash, target, pathname, search } = link;
|
|
53
|
-
const currentUrl = window.location;
|
|
54
|
-
// only intercept inbound links
|
|
55
|
-
if (hash && target !== '_blank' && origin === currentUrl.origin) {
|
|
56
|
-
// scroll between hash anchors in the same page
|
|
57
|
-
if (
|
|
58
|
-
pathname === currentUrl.pathname &&
|
|
59
|
-
search === currentUrl.search &&
|
|
60
|
-
hash &&
|
|
61
|
-
link.classList.contains('header-anchor')
|
|
62
|
-
) {
|
|
63
|
-
e.preventDefault();
|
|
64
|
-
history.pushState(null, '', hash);
|
|
65
|
-
// use smooth scroll when clicking on header anchor links
|
|
66
|
-
scrollTo(link, hash, true);
|
|
67
|
-
// still emit the event so we can listen to it in themes
|
|
68
|
-
window.dispatchEvent(new Event('hashchange'));
|
|
69
|
-
} else {
|
|
70
|
-
window.addEventListener('RspressReloadContent', () => {
|
|
71
|
-
if (location.hash.length > 1) {
|
|
72
|
-
const ele = document.getElementById(location.hash.slice(1));
|
|
73
|
-
scrollToTarget(ele, false);
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
},
|
|
80
|
-
{ capture: true },
|
|
81
|
-
);
|
|
82
|
-
window.addEventListener('hashchange', e => {
|
|
83
|
-
e.preventDefault();
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Binding the scroll event to the aside element
|
|
88
|
-
export function bindingAsideScroll() {
|
|
89
|
-
function isBottom() {
|
|
90
|
-
return (
|
|
91
|
-
document.documentElement.scrollTop + window.innerHeight >=
|
|
92
|
-
document.documentElement.scrollHeight
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
const aside = document.getElementById('aside-container');
|
|
96
|
-
const links = Array.from(
|
|
97
|
-
document.querySelectorAll<HTMLAnchorElement>('.rspress-doc .header-anchor'),
|
|
98
|
-
).filter(item => item.parentElement?.tagName !== 'H1');
|
|
99
|
-
|
|
100
|
-
if (!aside || !links.length) {
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
let prevActiveLink: null | HTMLAnchorElement = null;
|
|
105
|
-
const headers = Array.from(aside?.getElementsByTagName('a') || []).map(item =>
|
|
106
|
-
decodeURIComponent(item.hash),
|
|
107
|
-
);
|
|
108
|
-
if (!headers.length) {
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
// Util function to set dom ref after determining the active link
|
|
112
|
-
const activate = (links: HTMLAnchorElement[], index: number) => {
|
|
113
|
-
if (links[index]) {
|
|
114
|
-
const id = links[index].getAttribute('href');
|
|
115
|
-
const currentLink = aside?.querySelector(
|
|
116
|
-
`a[href="#${id?.slice(1)}"] > span`,
|
|
117
|
-
);
|
|
118
|
-
if (currentLink) {
|
|
119
|
-
if (prevActiveLink) {
|
|
120
|
-
prevActiveLink.classList.remove('aside-active');
|
|
121
|
-
}
|
|
122
|
-
prevActiveLink = currentLink as HTMLAnchorElement;
|
|
123
|
-
prevActiveLink.classList.add('aside-active');
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
const setActiveLink = () => {
|
|
128
|
-
if (isBottom()) {
|
|
129
|
-
activate(links, links.length - 1);
|
|
130
|
-
} else {
|
|
131
|
-
// Compute current index
|
|
132
|
-
for (let i = 0; i < links.length; i++) {
|
|
133
|
-
const currentAnchor = links[i];
|
|
134
|
-
const nextAnchor = links[i + 1];
|
|
135
|
-
const scrollTop = Math.ceil(window.scrollY);
|
|
136
|
-
const currentAnchorTop =
|
|
137
|
-
currentAnchor.parentElement.offsetTop - DEFAULT_NAV_HEIGHT;
|
|
138
|
-
if ((i === 0 && scrollTop < currentAnchorTop) || scrollTop === 0) {
|
|
139
|
-
activate(links, 0);
|
|
140
|
-
break;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
if (!nextAnchor) {
|
|
144
|
-
activate(links, i);
|
|
145
|
-
break;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const nextAnchorTop =
|
|
149
|
-
nextAnchor.parentElement.offsetTop - DEFAULT_NAV_HEIGHT;
|
|
150
|
-
|
|
151
|
-
if (scrollTop >= currentAnchorTop && scrollTop < nextAnchorTop) {
|
|
152
|
-
activate(links, i);
|
|
153
|
-
break;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
const throttledSetLink = throttle(setActiveLink, 200);
|
|
159
|
-
|
|
160
|
-
window.addEventListener('scroll', throttledSetLink);
|
|
161
|
-
|
|
162
|
-
// eslint-disable-next-line consistent-return
|
|
163
|
-
return () => {
|
|
164
|
-
window.removeEventListener('scroll', throttledSetLink);
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
export function setup() {
|
|
169
|
-
if (!inBrowser()) {
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
bindingWindowScroll();
|
|
174
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import siteData from 'virtual-site-data';
|
|
2
|
-
import { APPEARANCE_KEY } from '@rspress/shared';
|
|
3
|
-
|
|
4
|
-
declare global {
|
|
5
|
-
interface Window {
|
|
6
|
-
MODERN_THEME?: string;
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
let classList: DOMTokenList | undefined;
|
|
11
|
-
// Determine if the theme mode of the user's operating system is dark
|
|
12
|
-
let userPreference: string;
|
|
13
|
-
let query: MediaQueryList;
|
|
14
|
-
|
|
15
|
-
const setClass = (dark: boolean): void => {
|
|
16
|
-
classList?.[dark ? 'add' : 'remove']('dark');
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const updateAppearance = (): void => {
|
|
20
|
-
const disableDarkMode = siteData.themeConfig.darkMode === false;
|
|
21
|
-
if (disableDarkMode) {
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
// We set the MODERN_THEME as a global variable to determine whether the theme is dark or light.
|
|
25
|
-
if (window.MODERN_THEME) {
|
|
26
|
-
setClass(window.MODERN_THEME === 'dark');
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
const userPreference = localStorage.getItem(APPEARANCE_KEY) || 'auto';
|
|
30
|
-
query = window.matchMedia('(prefers-color-scheme: dark)');
|
|
31
|
-
setClass(
|
|
32
|
-
userPreference === 'auto' ? query.matches : userPreference === 'dark',
|
|
33
|
-
);
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
if (typeof window !== 'undefined' && typeof localStorage !== 'undefined') {
|
|
37
|
-
// When user preference is auto,the modern theme will change with the system user's operating system theme.
|
|
38
|
-
// eslint-disable-next-line prefer-destructuring
|
|
39
|
-
classList = document.documentElement.classList;
|
|
40
|
-
updateAppearance();
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export const isDarkMode = () => classList?.contains('dark');
|
|
44
|
-
|
|
45
|
-
export const getToggle = () => {
|
|
46
|
-
if (typeof window !== 'undefined') {
|
|
47
|
-
window.addEventListener('storage', updateAppearance);
|
|
48
|
-
}
|
|
49
|
-
return () => {
|
|
50
|
-
const isDark = isDarkMode();
|
|
51
|
-
if (typeof window !== 'undefined' && typeof localStorage !== 'undefined') {
|
|
52
|
-
setClass(!isDark);
|
|
53
|
-
userPreference = isDark ? 'light' : 'dark';
|
|
54
|
-
localStorage.setItem(APPEARANCE_KEY, userPreference);
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
};
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { useLocaleSiteData } from './useLocaleSiteData';
|
|
2
|
-
import { usePageData } from '@/runtime';
|
|
3
|
-
|
|
4
|
-
interface EditLink {
|
|
5
|
-
docRepoBaseUrl?: string;
|
|
6
|
-
text?: string;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function useEditLink() {
|
|
10
|
-
const { siteData, page } = usePageData();
|
|
11
|
-
const locales = useLocaleSiteData();
|
|
12
|
-
|
|
13
|
-
const editLink: EditLink =
|
|
14
|
-
locales.editLink ?? siteData.themeConfig?.editLink ?? {};
|
|
15
|
-
|
|
16
|
-
if (!editLink.docRepoBaseUrl || !editLink.text) {
|
|
17
|
-
return null;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
let { docRepoBaseUrl } = editLink;
|
|
21
|
-
|
|
22
|
-
if (!docRepoBaseUrl.endsWith('/')) {
|
|
23
|
-
docRepoBaseUrl += '/';
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const relativePagePath = page._relativePath.replace(/\\/g, '/');
|
|
27
|
-
const link = `${docRepoBaseUrl}${relativePagePath}`;
|
|
28
|
-
|
|
29
|
-
return {
|
|
30
|
-
text: editLink.text,
|
|
31
|
-
link,
|
|
32
|
-
};
|
|
33
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { throttle } from 'lodash-es';
|
|
2
|
-
import { useEffect, useRef, useState } from 'react';
|
|
3
|
-
import { useLocation, usePageData } from '@/runtime';
|
|
4
|
-
|
|
5
|
-
// The two hooks has the similar name, but they are quite different.
|
|
6
|
-
// `useDisableNav` is used to determine whether the navigation bar is disabled. It depends on both the frontmatter and the themeConfig.
|
|
7
|
-
// `useHiddenNav` is used to determine whether the navigation bar is hidden. It depends on the themeConfig and the scroll event.
|
|
8
|
-
|
|
9
|
-
export function useDisableNav() {
|
|
10
|
-
const {
|
|
11
|
-
siteData: { themeConfig },
|
|
12
|
-
page: { frontmatter = {} },
|
|
13
|
-
} = usePageData();
|
|
14
|
-
const initialState =
|
|
15
|
-
!(frontmatter?.navbar ?? true) || themeConfig?.hideNavbar === 'always';
|
|
16
|
-
const [disableNav, setDisableNav] = useState<boolean>(initialState);
|
|
17
|
-
// Priority: frontmatter.navbar > themeConfig.hideNavbar
|
|
18
|
-
return [disableNav, setDisableNav] as const;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function useHiddenNav() {
|
|
22
|
-
const {
|
|
23
|
-
siteData: { themeConfig },
|
|
24
|
-
} = usePageData();
|
|
25
|
-
const hiddenBehaivor = themeConfig.hideNavbar ?? 'never';
|
|
26
|
-
const [hiddenNav, setHiddenNav] = useState(false);
|
|
27
|
-
const { pathname } = useLocation();
|
|
28
|
-
const lastScrollTop = useRef(0);
|
|
29
|
-
|
|
30
|
-
if (hiddenBehaivor === 'never') {
|
|
31
|
-
return false;
|
|
32
|
-
} else if (hiddenBehaivor === 'always') {
|
|
33
|
-
return true;
|
|
34
|
-
} else {
|
|
35
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
36
|
-
useEffect(() => {
|
|
37
|
-
setHiddenNav(false);
|
|
38
|
-
const onScrollListen = throttle(() => {
|
|
39
|
-
const { scrollTop } = document.documentElement;
|
|
40
|
-
if (scrollTop === lastScrollTop.current) {
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
const shouldHidden =
|
|
44
|
-
lastScrollTop.current > 0 && scrollTop - lastScrollTop.current > 0;
|
|
45
|
-
setHiddenNav(shouldHidden);
|
|
46
|
-
lastScrollTop.current = scrollTop <= 0 ? 0 : scrollTop;
|
|
47
|
-
}, 200);
|
|
48
|
-
|
|
49
|
-
window.addEventListener('mousewheel', onScrollListen);
|
|
50
|
-
|
|
51
|
-
return () => {
|
|
52
|
-
window.removeEventListener('mousewheel', onScrollListen);
|
|
53
|
-
};
|
|
54
|
-
}, [pathname]);
|
|
55
|
-
|
|
56
|
-
return hiddenNav;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { NormalizedLocales } from '@rspress/shared';
|
|
2
|
-
import { usePageData } from '@/runtime';
|
|
3
|
-
|
|
4
|
-
export function useLocaleSiteData(): NormalizedLocales {
|
|
5
|
-
const pageData = usePageData();
|
|
6
|
-
const {
|
|
7
|
-
page: { lang },
|
|
8
|
-
} = pageData;
|
|
9
|
-
const themeConfig = pageData?.siteData?.themeConfig ?? {};
|
|
10
|
-
const defaultLang = pageData.siteData.lang ?? '';
|
|
11
|
-
const locales = themeConfig?.locales;
|
|
12
|
-
if (!locales || locales.length === 0) {
|
|
13
|
-
return {
|
|
14
|
-
nav: themeConfig.nav,
|
|
15
|
-
sidebar: themeConfig.sidebar,
|
|
16
|
-
prevPageText: themeConfig.prevPageText,
|
|
17
|
-
nextPageText: themeConfig.nextPageText,
|
|
18
|
-
} as NormalizedLocales;
|
|
19
|
-
}
|
|
20
|
-
const localeInfo = locales.find(locale => locale.lang === lang)!;
|
|
21
|
-
|
|
22
|
-
return {
|
|
23
|
-
...localeInfo,
|
|
24
|
-
langRoutePrefix: lang === defaultLang ? '/' : lang,
|
|
25
|
-
} as NormalizedLocales;
|
|
26
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { useState } from 'react';
|
|
2
|
-
|
|
3
|
-
export function useNav() {
|
|
4
|
-
const [isScreenOpen, setIsScreenOpen] = useState(false);
|
|
5
|
-
function openScreen() {
|
|
6
|
-
setIsScreenOpen(true);
|
|
7
|
-
window.addEventListener('resize', closeScreenOnTabletWindow);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function closeScreen() {
|
|
11
|
-
setIsScreenOpen(false);
|
|
12
|
-
window.removeEventListener('resize', closeScreenOnTabletWindow);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function toggleScreen() {
|
|
16
|
-
if (isScreenOpen) {
|
|
17
|
-
closeScreen();
|
|
18
|
-
} else {
|
|
19
|
-
openScreen();
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Close screen when the user resizes the window wider than tablet size.
|
|
25
|
-
*/
|
|
26
|
-
function closeScreenOnTabletWindow() {
|
|
27
|
-
window.outerWidth >= 768 && closeScreen();
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return {
|
|
31
|
-
isScreenOpen,
|
|
32
|
-
openScreen,
|
|
33
|
-
closeScreen,
|
|
34
|
-
toggleScreen,
|
|
35
|
-
};
|
|
36
|
-
}
|