@rspress/core 0.2.0 → 1.0.0-beta.1

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.
Files changed (38) hide show
  1. package/dist/index.js +29 -27
  2. package/dist/index.js.map +1 -1
  3. package/dist/loader.d.ts +0 -1
  4. package/dist/loader.js +14 -11
  5. package/dist/loader.js.map +1 -1
  6. package/dist/runtime/App.js +3 -4
  7. package/dist/runtime/Content.js +15 -8
  8. package/dist/runtime/hooks.d.ts +10 -0
  9. package/dist/runtime/hooks.js +6 -2
  10. package/dist/runtime/index.d.ts +1 -1
  11. package/dist/runtime/index.js +1 -1
  12. package/dist/runtime/utils.d.ts +3 -2
  13. package/dist/runtime/utils.js +6 -2
  14. package/dist/theme/bundle.css +5 -3
  15. package/dist/theme/bundle.js +45 -22
  16. package/dist/theme/bundle.js.map +1 -1
  17. package/package.json +6 -6
  18. package/src/runtime/App.tsx +4 -6
  19. package/src/runtime/Content.tsx +24 -11
  20. package/src/runtime/global.d.ts +4 -5
  21. package/src/runtime/hooks.ts +22 -2
  22. package/src/runtime/index.ts +1 -1
  23. package/src/runtime/utils.ts +9 -8
  24. package/src/theme-default/components/DocFooter/index.tsx +1 -1
  25. package/src/theme-default/components/HomeHero/index.tsx +1 -1
  26. package/src/theme-default/components/Link/index.module.scss +0 -1
  27. package/src/theme-default/components/Link/index.tsx +21 -2
  28. package/src/theme-default/components/Nav/NavMenuSingleItem.tsx +1 -1
  29. package/src/theme-default/components/Overview/index.tsx +1 -1
  30. package/src/theme-default/components/Search/logic/search.ts +1 -1
  31. package/src/theme-default/components/Sidebar/SidebarGroup.tsx +1 -1
  32. package/src/theme-default/components/Sidebar/SidebarItem.tsx +1 -1
  33. package/src/theme-default/layout/DocLayout/docComponents/index.module.scss +4 -0
  34. package/src/theme-default/layout/DocLayout/docComponents/link.tsx +14 -8
  35. package/src/theme-default/layout/DocLayout/docComponents/pre.tsx +1 -1
  36. package/src/theme-default/layout/HomeLayout/index.tsx +1 -0
  37. package/src/theme-default/logic/sideEffects.ts +7 -0
  38. package/src/theme-default/logic/usePathUtils.ts +2 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rspress/core",
3
- "version": "0.2.0",
3
+ "version": "1.0.0-beta.1",
4
4
  "description": "The Rspress Documentation Framework",
5
5
  "bugs": "https://github.com/web-infra-dev/rspress/issues",
6
6
  "repository": {
@@ -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.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"
92
+ "@rspress/shared": "1.0.0-beta.1",
93
+ "@rspress/plugin-medium-zoom": "1.0.0-beta.1",
94
+ "@rspress/plugin-last-updated": "1.0.0-beta.1",
95
+ "@rspress/plugin-auto-nav-sidebar": "1.0.0-beta.1",
96
+ "@rspress/plugin-container-syntax": "1.0.0-beta.1"
97
97
  },
98
98
  "peerDependencies": {
99
99
  "react": ">=17"
@@ -15,7 +15,6 @@ type RspressPageMeta = Record<
15
15
  {
16
16
  title: string;
17
17
  toc: Header[];
18
- frontmatter: Record<string, any>;
19
18
  }
20
19
  >;
21
20
 
@@ -44,12 +43,11 @@ export async function initPageData(routePath: string): Promise<PageData> {
44
43
  toc = [],
45
44
  // eslint-disable-next-line prefer-const
46
45
  title = '',
47
- frontmatter,
48
- } = (globalThis.__RSPRESS_PAGE_META as RspressPageMeta)?.[
49
- encodedPagePath
50
- ] || {};
46
+ } =
47
+ (mod.default.__RSPRESS_PAGE_META as RspressPageMeta)?.[encodedPagePath] ||
48
+ {};
51
49
 
52
- frontmatter = frontmatter || mod.frontmatter || {};
50
+ const frontmatter = mod.frontmatter || {};
53
51
 
54
52
  return {
55
53
  siteData,
@@ -1,12 +1,27 @@
1
- import { ReactNode, Suspense } from 'react';
1
+ import { ReactNode, Suspense, memo, ReactElement } from 'react';
2
2
  import { matchRoutes, useLocation } from 'react-router-dom';
3
+ import siteData from 'virtual-site-data';
3
4
  import { normalizeRoutePath } from './utils';
4
- import { usePageData, useViewTransition } from './hooks';
5
+ import { useViewTransition } from './hooks';
5
6
 
6
7
  const { routes } = process.env.__SSR__
7
8
  ? (require('virtual-routes-ssr') as typeof import('virtual-routes-ssr'))
8
9
  : (require('virtual-routes') as typeof import('virtual-routes'));
9
10
 
11
+ function TransitionContentImpl(props: { el: ReactElement }) {
12
+ let element = props.el;
13
+ if (siteData?.themeConfig?.enableContentAnimation) {
14
+ // eslint-disable-next-line react-hooks/rules-of-hooks
15
+ element = useViewTransition(props.el);
16
+ }
17
+ return element;
18
+ }
19
+
20
+ const TransitionContent = memo(
21
+ TransitionContentImpl,
22
+ (prevProps, nextProps) => prevProps.el === nextProps.el,
23
+ );
24
+
10
25
  export const Content = ({ fallback = <></> }: { fallback?: ReactNode }) => {
11
26
  const { pathname } = useLocation();
12
27
  const matched = matchRoutes(routes, normalizeRoutePath(pathname));
@@ -14,17 +29,15 @@ export const Content = ({ fallback = <></> }: { fallback?: ReactNode }) => {
14
29
  return <div></div>;
15
30
  }
16
31
  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
32
 
25
33
  // React 17 Suspense SSR is not supported
26
34
  if (!process.env.__IS_REACT_18__ && process.env.__SSR__) {
27
- return element;
35
+ return routesElement;
28
36
  }
29
- return <Suspense fallback={fallback}>{element}</Suspense>;
37
+
38
+ return (
39
+ <Suspense fallback={fallback}>
40
+ <TransitionContent el={routesElement} />
41
+ </Suspense>
42
+ );
30
43
  };
@@ -1,20 +1,19 @@
1
1
  declare module 'virtual-routes' {
2
- export { Route } from 'node/route/RouteService';
2
+ export { Route } from '@rspress/shared';
3
3
 
4
4
  export const routes: Route[];
5
5
  }
6
6
 
7
7
  declare module 'virtual-routes-ssr' {
8
- export { Route } from 'node/route/RouteService';
8
+ export { Route } from '@rspress/shared';
9
9
 
10
10
  export const routes: Route[];
11
11
  }
12
12
 
13
13
  declare module 'virtual-site-data' {
14
- import { SiteData } from 'shared/types';
15
- import ThemeConfig from 'shared/types/defaultTheme';
14
+ import { SiteData, DefaultThemeConfig } from '@rspress/shared';
16
15
 
17
- const data: SiteData<ThemeConfig>;
16
+ const data: SiteData<DefaultThemeConfig>;
18
17
  export default data;
19
18
  }
20
19
 
@@ -55,6 +55,16 @@ declare global {
55
55
  }
56
56
  }
57
57
 
58
+ /**
59
+ * There is a pitfall.
60
+ * I was working on the navigation between pages with hash. eg. from `guide/start` -> `config/repress#nav`
61
+ * I need a time to dispatch an event so that the sideEffect.ts would know that
62
+ * the dom is attached to the browser. Otherwise the scroll position and the
63
+ * animation would be incorrect. You can search for `RspressReloadContent` in this codebase
64
+ * to findout the logic that are consuming the event.
65
+ * The reason I didn't write it here is that I hope the logic of handling scroll and position
66
+ * could be in one place so that people wouldn't be confused.
67
+ */
58
68
  export function useViewTransition(dom) {
59
69
  /**
60
70
  * use a pesudo element to hold the actual JSX element so we can schedule the
@@ -63,7 +73,7 @@ export function useViewTransition(dom) {
63
73
  const [element, setElement] = useState(dom);
64
74
 
65
75
  useLayoutEffect(() => {
66
- if (document.startViewTransition) {
76
+ if (document.startViewTransition && element !== dom) {
67
77
  /**
68
78
  * the browser will take a screenshot here
69
79
  */
@@ -78,9 +88,19 @@ export function useViewTransition(dom) {
78
88
  * react flushed the dom to browser
79
89
  * and the browser will start the animation
80
90
  */
91
+ /**
92
+ * dispatchEvent for several logic
93
+ */
94
+ window.dispatchEvent(new Event('RspressReloadContent'));
81
95
  });
82
96
  } else {
83
- setElement(dom);
97
+ flushSync(() => {
98
+ setElement(dom);
99
+ });
100
+ /**
101
+ * dispatchEvent for several logic
102
+ */
103
+ window.dispatchEvent(new Event('RspressReloadContent'));
84
104
  }
85
105
  }, [dom]);
86
106
  /**
@@ -1,7 +1,7 @@
1
1
  export * from './hooks';
2
2
  export * from './Content';
3
3
  export {
4
- normalizeHref,
4
+ normalizeHrefInRuntime,
5
5
  withBase,
6
6
  removeBase,
7
7
  addLeadingSlash,
@@ -24,13 +24,14 @@ export function removeBase(url: string): string {
24
24
  }
25
25
 
26
26
  export function isEqualPath(a: string, b: string) {
27
- return withBase(normalizeHref(a)) === withBase(normalizeHref(b));
27
+ return (
28
+ withBase(normalizeHrefInRuntime(a)) === withBase(normalizeHrefInRuntime(b))
29
+ );
28
30
  }
29
31
 
30
- export {
31
- addLeadingSlash,
32
- removeTrailingSlash,
33
- normalizeSlash,
34
- isProduction,
35
- normalizeHref,
36
- };
32
+ export function normalizeHrefInRuntime(a: string) {
33
+ const cleanUrls = Boolean(siteData?.route?.cleanUrls);
34
+ return normalizeHref(a, cleanUrls);
35
+ }
36
+
37
+ export { addLeadingSlash, removeTrailingSlash, normalizeSlash, isProduction };
@@ -2,7 +2,7 @@ import { useLocaleSiteData, usePrevNextPage } from '../../logic';
2
2
  import EditLink from '../EditLink';
3
3
  import { Link } from '../Link';
4
4
  import styles from './index.module.scss';
5
- import { normalizeHref, usePageData } from '@/runtime';
5
+ import { normalizeHrefInRuntime as normalizeHref, usePageData } from '@/runtime';
6
6
 
7
7
  export function DocFooter() {
8
8
  const { prevPage, nextPage } = usePrevNextPage();
@@ -1,7 +1,7 @@
1
1
  import { FrontMatterMeta } from '@rspress/shared';
2
2
  import { Button } from '../Button';
3
3
  import styles from './index.module.scss';
4
- import { normalizeHref } from '@/runtime';
4
+ import { normalizeHrefInRuntime as normalizeHref } from '@/runtime';
5
5
 
6
6
  const DEFAULT_HERO = {
7
7
  name: 'modern',
@@ -5,6 +5,5 @@
5
5
 
6
6
  .link {
7
7
  display: block;
8
- font-size: 14px;
9
8
  font-weight: 500;
10
9
  }
@@ -1,9 +1,14 @@
1
1
  import React from 'react';
2
- import { matchRoutes, useNavigate } from 'react-router-dom';
2
+ import { matchRoutes, useLocation, useNavigate } from 'react-router-dom';
3
3
  import nprogress from 'nprogress';
4
4
  import { routes } from 'virtual-routes';
5
5
  import styles from './index.module.scss';
6
- import { normalizeHref, normalizeRoutePath, withBase } from '@/runtime';
6
+ import {
7
+ normalizeHrefInRuntime as normalizeHref,
8
+ normalizeRoutePath,
9
+ withBase,
10
+ } from '@/runtime';
11
+ import { scrollToTarget } from '@/theme-default/logic';
7
12
 
8
13
  export interface LinkProps {
9
14
  href?: string;
@@ -23,10 +28,23 @@ export function Link(props: LinkProps) {
23
28
  const rel = isExternal ? 'noopener noreferrer' : undefined;
24
29
  const withBaseUrl = isExternal ? href : withBase(normalizeHref(href));
25
30
  const navigate = useNavigate();
31
+ const { pathname } = useLocation();
32
+ const inCurrentPage = withBaseUrl.startsWith(pathname);
26
33
  const handleNavigate = async (
27
34
  e: React.MouseEvent<HTMLAnchorElement, MouseEvent>,
28
35
  ) => {
29
36
  e.preventDefault();
37
+ // handle hash link in current page
38
+ const hash = withBaseUrl.split('#')[1];
39
+ if (!isExternal && inCurrentPage && hash) {
40
+ const el = document.getElementById(hash);
41
+ if (el) {
42
+ scrollToTarget(el, true);
43
+ }
44
+ return;
45
+ }
46
+
47
+ // handle normal link
30
48
  if (!process.env.__SSR__) {
31
49
  const matchedRoutes = matchRoutes(
32
50
  routes,
@@ -44,6 +62,7 @@ export function Link(props: LinkProps) {
44
62
  navigate(withBaseUrl, { replace: false });
45
63
  }
46
64
  };
65
+
47
66
  if (!isExternal) {
48
67
  return (
49
68
  <a
@@ -6,7 +6,7 @@ import {
6
6
  import { Link } from '../Link';
7
7
  import { Tag } from '../Tag';
8
8
  import styles from './index.module.scss';
9
- import { normalizeHref } from '@/runtime';
9
+ import { normalizeHrefInRuntime as normalizeHref } from '@/runtime';
10
10
 
11
11
  interface Props {
12
12
  pathname: string;
@@ -3,7 +3,7 @@ import { Header, NormalizedSidebarGroup, SidebarItem } from '@rspress/shared';
3
3
  import { useSidebarData } from '../../logic';
4
4
  import { Link } from '../Link';
5
5
  import styles from './index.module.scss';
6
- import { usePageData, normalizeHref, withBase, isEqualPath } from '@/runtime';
6
+ import { usePageData, normalizeHrefInRuntime as normalizeHref, withBase, isEqualPath } from '@/runtime';
7
7
 
8
8
  interface GroupItem {
9
9
  text?: string;
@@ -13,7 +13,7 @@ import {
13
13
  } from './util';
14
14
  import { LocalProvider } from './providers/LocalProvider';
15
15
  import { RemoteProvider } from './providers/RemoteProvider';
16
- import { normalizeHref } from '@/runtime';
16
+ import { normalizeHrefInRuntime as normalizeHref } from '@/runtime';
17
17
 
18
18
  const THRESHOLD_CONTENT_LENGTH = 100;
19
19
 
@@ -6,7 +6,7 @@ import { Tag } from '../Tag';
6
6
  import styles from './index.module.scss';
7
7
  import { SidebarItem } from './SidebarItem';
8
8
  import { highlightTitleStyle, matchCache, type SidebarItemProps } from '.';
9
- import { normalizeHref, withBase } from '@/runtime';
9
+ import { normalizeHrefInRuntime as normalizeHref, withBase } from '@/runtime';
10
10
 
11
11
  export function SidebarGroup(props: SidebarItemProps) {
12
12
  const { item, depth = 0, activeMatcher, id, setSidebarData } = props;
@@ -4,7 +4,7 @@ import { Tag } from '../Tag';
4
4
  import styles from './index.module.scss';
5
5
  import { SidebarGroup } from './SidebarGroup';
6
6
  import { SidebarItemProps, highlightTitleStyle } from '.';
7
- import { normalizeHref } from '@/runtime';
7
+ import { normalizeHrefInRuntime as normalizeHref } from '@/runtime';
8
8
 
9
9
  export function SidebarItem(props: SidebarItemProps) {
10
10
  const { item, depth = 0, activeMatcher, id, setSidebarData } = props;
@@ -63,3 +63,7 @@
63
63
  :global(.rspress-doc details:hover) {
64
64
  background-color: var(--rp-c-bg-mute);
65
65
  }
66
+
67
+ .inline-link {
68
+ display: inline;
69
+ }
@@ -1,15 +1,21 @@
1
1
  import { ComponentProps } from 'react';
2
2
  import styles from './index.module.scss';
3
3
  import { usePathUtils } from '@/theme-default/logic';
4
+ import { Link } from '@/theme-default/components/Link';
4
5
 
5
6
  export const A = (props: ComponentProps<'a'>) => {
6
- const { href = '' } = props;
7
+ const { href = '', className = '' } = props;
7
8
  const { normalizeLinkHref } = usePathUtils();
8
- return (
9
- <a
10
- {...props}
11
- className={`${styles.link} ${props.className}`}
12
- href={normalizeLinkHref(href)}
13
- />
14
- );
9
+
10
+ if (className.includes('header-anchor')) {
11
+ return <a {...props} className={`${styles.link} ${className}`} />;
12
+ } else {
13
+ return (
14
+ <Link
15
+ {...props}
16
+ className={`${className} ${styles.link} ${styles['inline-link']}`}
17
+ href={normalizeLinkHref(href)}
18
+ />
19
+ );
20
+ }
15
21
  };
@@ -31,7 +31,7 @@ export function Pre({
31
31
  return (
32
32
  <div className={className || DEFAULT_LANGUAGE_CLASS}>
33
33
  {codeTitle && <div className="rspress-code-title">{codeTitle}</div>}
34
- <div className="rspress-code-content">{children}</div>
34
+ <div className="rspress-code-content rspress-scrollbar">{children}</div>
35
35
  </div>
36
36
  );
37
37
  }
@@ -16,6 +16,7 @@ export function HomeLayout(props: HomeLayoutProps) {
16
16
  const {
17
17
  page: { frontmatter },
18
18
  } = usePageData();
19
+
19
20
  return (
20
21
  <div
21
22
  className="relative"
@@ -63,6 +63,13 @@ function bindingWindowScroll() {
63
63
  scrollTo(link, hash, true);
64
64
  // still emit the event so we can listen to it in themes
65
65
  window.dispatchEvent(new Event('hashchange'));
66
+ } else {
67
+ window.addEventListener('RspressReloadContent', () => {
68
+ if (location.hash.length > 1) {
69
+ const ele = document.getElementById(location.hash.slice(1));
70
+ scrollToTarget(ele, false);
71
+ }
72
+ });
66
73
  }
67
74
  }
68
75
  }
@@ -1,8 +1,9 @@
1
- import { isExternalUrl, normalizeHref } from '@rspress/shared';
1
+ import { isExternalUrl } from '@rspress/shared';
2
2
  import {
3
3
  removeBase,
4
4
  useLang,
5
5
  usePageData,
6
+ normalizeHrefInRuntime as normalizeHref,
6
7
  useVersion,
7
8
  withBase,
8
9
  } from '@/runtime';