@rspress/core 0.1.1 → 0.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rspress/core",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "The Rspress Documentation Framework",
5
5
  "bugs": "https://github.com/web-infra-dev/rspress/issues",
6
6
  "repository": {
@@ -42,7 +42,7 @@
42
42
  "@mdx-js/react": "2.2.1",
43
43
  "@modern-js/builder": "2.35.1",
44
44
  "@modern-js/builder-rspack-provider": "2.35.1",
45
- "@rspress/mdx-rs": "0.3.1",
45
+ "@rspress/mdx-rs": "0.3.2",
46
46
  "@modern-js/utils": "2.35.1",
47
47
  "@types/compression": "^1.7.2",
48
48
  "@types/polka": "^0.5.4",
@@ -89,11 +89,11 @@
89
89
  "unist-util-visit-children": "^2.0.1",
90
90
  "yaml-front-matter": "^4.1.1",
91
91
  "rspack-plugin-virtual-module": "0.1.12",
92
- "@rspress/shared": "0.1.1",
93
- "@rspress/plugin-medium-zoom": "0.1.1",
94
- "@rspress/plugin-last-updated": "0.1.1",
95
- "@rspress/plugin-auto-nav-sidebar": "0.1.1",
96
- "@rspress/plugin-container-syntax": "0.1.1"
92
+ "@rspress/shared": "0.2.0",
93
+ "@rspress/plugin-medium-zoom": "0.2.0",
94
+ "@rspress/plugin-last-updated": "0.2.0",
95
+ "@rspress/plugin-auto-nav-sidebar": "0.2.0",
96
+ "@rspress/plugin-container-syntax": "0.2.0"
97
97
  },
98
98
  "peerDependencies": {
99
99
  "react": ">=17"
@@ -80,6 +80,7 @@ export async function initPageData(routePath: string): Promise<PageData> {
80
80
  frontmatter: {},
81
81
  title: '404',
82
82
  toc: [],
83
+ version: '',
83
84
  _filepath: '',
84
85
  _relativePath: '',
85
86
  },
@@ -1,6 +1,7 @@
1
1
  import { ReactNode, Suspense } from 'react';
2
2
  import { matchRoutes, useLocation } from 'react-router-dom';
3
3
  import { normalizeRoutePath } from './utils';
4
+ import { usePageData, useViewTransition } from './hooks';
4
5
 
5
6
  const { routes } = process.env.__SSR__
6
7
  ? (require('virtual-routes-ssr') as typeof import('virtual-routes-ssr'))
@@ -13,9 +14,17 @@ export const Content = ({ fallback = <></> }: { fallback?: ReactNode }) => {
13
14
  return <div></div>;
14
15
  }
15
16
  const routesElement = matched[0].route.element;
17
+ /**
18
+ * useLayoutEffect to flush elements animation
19
+ */
20
+ let element = routesElement;
21
+ if (usePageData().siteData.themeConfig.enableContentAnimation) {
22
+ element = useViewTransition(routesElement);
23
+ }
24
+
16
25
  // React 17 Suspense SSR is not supported
17
26
  if (!process.env.__IS_REACT_18__ && process.env.__SSR__) {
18
- return routesElement;
27
+ return element;
19
28
  }
20
- return <Suspense fallback={fallback}>{routesElement}</Suspense>;
29
+ return <Suspense fallback={fallback}>{element}</Suspense>;
21
30
  };
@@ -1,6 +1,7 @@
1
- import { createContext, useContext } from 'react';
1
+ import { createContext, useContext, useLayoutEffect, useState } from 'react';
2
2
  import { PageData } from '@rspress/shared';
3
3
  import i18nTextData from 'virtual-i18n-text';
4
+ import { flushSync } from 'react-dom';
4
5
 
5
6
  // Type shim for window.__EDEN_PAGE_DATA__
6
7
  declare global {
@@ -32,6 +33,11 @@ export function useLang(): string {
32
33
  return ctx.data.page.lang || '';
33
34
  }
34
35
 
36
+ export function useVersion(): string {
37
+ const ctx = useContext(DataContext);
38
+ return ctx.data.page.version || '';
39
+ }
40
+
35
41
  export function useDark() {
36
42
  const ctx = useContext(ThemeContext);
37
43
  return ctx.theme === 'dark';
@@ -42,3 +48,43 @@ export function useI18n<T = Record<string, Record<string, string>>>() {
42
48
 
43
49
  return (key: keyof T) => i18nTextData[key][lang];
44
50
  }
51
+
52
+ declare global {
53
+ interface Document {
54
+ startViewTransition: (callback: () => void) => void;
55
+ }
56
+ }
57
+
58
+ export function useViewTransition(dom) {
59
+ /**
60
+ * use a pesudo element to hold the actual JSX element so we can schedule the
61
+ * update later in sync
62
+ */
63
+ const [element, setElement] = useState(dom);
64
+
65
+ useLayoutEffect(() => {
66
+ if (document.startViewTransition) {
67
+ /**
68
+ * the browser will take a screenshot here
69
+ */
70
+ document.startViewTransition(() => {
71
+ /**
72
+ * react will batch all the updates in callback and flush it sync
73
+ */
74
+ flushSync(() => {
75
+ setElement(dom);
76
+ });
77
+ /**
78
+ * react flushed the dom to browser
79
+ * and the browser will start the animation
80
+ */
81
+ });
82
+ } else {
83
+ setElement(dom);
84
+ }
85
+ }, [dom]);
86
+ /**
87
+ * take this element to the actual VDOM tree
88
+ */
89
+ return element;
90
+ }
@@ -9,7 +9,7 @@ import { SwitchAppearance } from '../SwitchAppearance';
9
9
  import { NavMenuGroup, NavMenuGroupItem } from './NavMenuGroup';
10
10
  import { NavMenuSingleItem } from './NavMenuSingleItem';
11
11
  import styles from './index.module.scss';
12
- import { ThemeContext, usePageData, withBase } from '@/runtime';
12
+ import { ThemeContext, usePageData, useVersion, withBase } from '@/runtime';
13
13
 
14
14
  export interface NavProps {
15
15
  beforeNav?: React.ReactNode;
@@ -70,21 +70,35 @@ export function Nav(props: NavProps) {
70
70
  const { pathname } = useLocation();
71
71
  const { theme } = useContext(ThemeContext);
72
72
  const localeData = useLocaleSiteData();
73
+ const currentVersion = useVersion();
73
74
  const [isMobile, setIsMobile] = useState(false);
74
75
  const localeLanguages = Object.values(siteData.themeConfig.locales || {});
75
76
  const hasMultiLanguage = localeLanguages.length > 1;
76
77
  const socialLinks = siteData.themeConfig.socialLinks || [];
77
78
  const hasSocialLinks = socialLinks.length > 0;
78
- const defaultLang = siteData.lang || 'zh';
79
+ const defaultLang = siteData.lang || '';
80
+ const defaultVersion = siteData.multiVersion.default || '';
79
81
  const { lang } = page;
80
- const langs = localeLanguages.map(item => item.lang || 'zh') || [];
82
+ const langs = localeLanguages.map(item => item.lang || '') || [];
81
83
  const [logo, setLogo] = useState(getLogoUrl(rawLogo, theme));
82
84
 
83
85
  const translationMenuData = hasMultiLanguage
84
86
  ? {
85
87
  items: localeLanguages.map(item => ({
86
88
  text: item?.label,
87
- link: replaceLang(pathname, item.lang, defaultLang, langs, base),
89
+ link: replaceLang(
90
+ pathname,
91
+ {
92
+ current: lang,
93
+ target: item.lang,
94
+ default: defaultLang,
95
+ },
96
+ {
97
+ current: currentVersion,
98
+ default: defaultVersion,
99
+ },
100
+ base,
101
+ ),
88
102
  })),
89
103
  activeValue: localeLanguages.find(item => lang === item.lang)?.label,
90
104
  }
@@ -173,7 +187,7 @@ export function Nav(props: NavProps) {
173
187
  };
174
188
  return (
175
189
  <header
176
- className={`top-0 left-0 md:fixed w-full`}
190
+ className="top-0 left-0 md:fixed w-full"
177
191
  style={{
178
192
  zIndex: 'var(--rp-z-index-nav)',
179
193
  background: 'var(--rp-c-bg)',
@@ -22,3 +22,5 @@ export default {
22
22
  export { getCustomMDXComponent } from './layout/DocLayout/docComponents';
23
23
 
24
24
  export * from './logic';
25
+
26
+ export { Layout, NotFoundLayout, HomeLayout };
@@ -1,30 +1,15 @@
1
1
  import { ComponentProps } from 'react';
2
- import { isExternalUrl, normalizeHref, normalizeSlash } from '@rspress/shared';
3
2
  import styles from './index.module.scss';
4
- import { withBase, useLang, usePageData, removeBase } from '@/runtime';
3
+ import { usePathUtils } from '@/theme-default/logic';
5
4
 
6
5
  export const A = (props: ComponentProps<'a'>) => {
7
- let { href = '' } = props;
8
- const lang = useLang();
9
- const pageData = usePageData();
10
- const defaultLang = pageData.siteData.lang;
11
-
12
- if (defaultLang && !isExternalUrl(href) && !href.startsWith('#')) {
13
- href = normalizeSlash(href);
14
- const startWithLang = removeBase(href).startsWith(`/${lang}`);
15
- // Add lang prefix if not default lang
16
- if (lang !== defaultLang && !startWithLang) {
17
- href = `/${lang}${href}`;
18
- }
19
-
20
- if (lang === defaultLang && startWithLang) {
21
- href = removeBase(href).replace(`/${lang}`, '');
22
- }
23
-
24
- href = normalizeHref(withBase(href || ''));
25
- }
26
-
6
+ const { href = '' } = props;
7
+ const { normalizeLinkHref } = usePathUtils();
27
8
  return (
28
- <a {...props} className={`${styles.link} ${props.className}`} href={href} />
9
+ <a
10
+ {...props}
11
+ className={`${styles.link} ${props.className}`}
12
+ href={normalizeLinkHref(href)}
13
+ />
29
14
  );
30
15
  };
@@ -14,6 +14,52 @@
14
14
  display: none;
15
15
  }
16
16
 
17
+ :global(.rspress-doc) {
18
+ min-height: 60vh;
19
+ view-transition-name: flip;
20
+ }
21
+
22
+ :global(*) {
23
+ view-transition-name: none;
24
+ }
25
+
26
+
27
+ @keyframes fade-out {
28
+ from {
29
+ opacity: 1;
30
+ }
31
+ to {
32
+ opacity: 0;
33
+ transform: translateY(20px);
34
+ }
35
+ }
36
+
37
+ @keyframes fade-in {
38
+ from {
39
+ opacity: 0;
40
+ transform: translateY(20px);
41
+ }
42
+ to {
43
+ opacity: 1;
44
+ }
45
+ }
46
+
47
+ ::view-transition-old(flip),
48
+ ::view-transition-new(flip) {
49
+ animation-time: 250ms;
50
+ }
51
+
52
+ ::view-transition-old(flip) {
53
+ animation-name: fade-out;
54
+ transition-timing-function: cubic-bezier(0, 0, 0.58, 1);
55
+ }
56
+
57
+ ::view-transition-new(flip) {
58
+ animation-name: fade-in;
59
+ animation-delay: 180ms;
60
+ transition-timing-function: cubic-bezier(0.42, 0, 1, 1);
61
+ }
62
+
17
63
  @media (max-width: 960px) {
18
64
  .docLayout {
19
65
  position: relative;
@@ -3,4 +3,5 @@ export { useEditLink } from './useEditLink';
3
3
  export { useSidebarData } from './useSidebarData';
4
4
  export { useLocaleSiteData } from './useLocaleSiteData';
5
5
  export { setup, bindingAsideScroll, scrollToTarget } from './sideEffects';
6
+ export { usePathUtils } from './usePathUtils';
6
7
  export * from './utils';
@@ -0,0 +1,72 @@
1
+ import { isExternalUrl, normalizeHref } from '@rspress/shared';
2
+ import {
3
+ removeBase,
4
+ useLang,
5
+ usePageData,
6
+ useVersion,
7
+ withBase,
8
+ } from '@/runtime';
9
+
10
+ export function usePathUtils() {
11
+ const currentLang = useLang();
12
+ const currentVersion = useVersion();
13
+ const pageData = usePageData();
14
+ const defaultLang = pageData.siteData.lang;
15
+ const defaultVersion = pageData.siteData.multiVersion.default;
16
+
17
+ const normalizeLinkHref = (rawHref: string) => {
18
+ let href = rawHref;
19
+ if (
20
+ (defaultLang || defaultVersion) &&
21
+ !isExternalUrl(href) &&
22
+ !href.startsWith('#')
23
+ ) {
24
+ href = removeBase(href);
25
+ const linkParts = href.split('/').filter(Boolean);
26
+ let versionPart = '';
27
+ let langPart = '';
28
+ let purePathPart = '';
29
+
30
+ // When add the version prefix, the situation is as follows:
31
+ // - current version is not default version
32
+ // - current link does not start with currrent version
33
+ if (defaultVersion) {
34
+ if (currentVersion !== defaultVersion) {
35
+ versionPart = currentVersion;
36
+
37
+ if (linkParts[0] === currentVersion) {
38
+ linkParts.shift();
39
+ }
40
+ } else if (linkParts[0] === defaultVersion) {
41
+ linkParts.shift();
42
+ }
43
+ }
44
+
45
+ if (defaultLang) {
46
+ if (currentLang !== defaultLang) {
47
+ langPart = currentLang;
48
+
49
+ if (linkParts[0] === currentLang) {
50
+ linkParts.shift();
51
+ }
52
+ } else if (linkParts[0] === defaultLang) {
53
+ linkParts.shift();
54
+ }
55
+ }
56
+
57
+ purePathPart = linkParts.join('/');
58
+
59
+ return normalizeHref(
60
+ withBase(
61
+ [versionPart, langPart, purePathPart].filter(Boolean).join('/'),
62
+ ),
63
+ );
64
+ } else {
65
+ return href;
66
+ }
67
+ };
68
+
69
+ return {
70
+ normalizeLinkHref,
71
+ };
72
+ }