@rspress/core 0.0.7 → 0.0.9

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.
@@ -0,0 +1,89 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import siteData from "virtual-site-data";
3
+ import { matchRoutes, useLocation } from "react-router-dom";
4
+ import { HelmetProvider } from "react-helmet-async";
5
+ import { useContext, useLayoutEffect } from "react";
6
+ import { cleanUrl, isProduction } from "@rspress/shared";
7
+ import { isEqualPath, normalizeRoutePath } from "./utils";
8
+ import { DataContext } from "./hooks";
9
+ import "virtual-global-styles";
10
+ const { default: Theme } = require("@theme");
11
+ export async function initPageData(routePath) {
12
+ const { routes } = process.env.__SSR__ ? require("virtual-routes-ssr") : require("virtual-routes");
13
+ const matched = matchRoutes(routes, routePath);
14
+ if (matched) {
15
+ const matchedRoute = matched[0].route;
16
+ const mod = await matchedRoute.preload();
17
+ const pagePath = cleanUrl(matched[0].route.filePath);
18
+ const extractPageInfo = siteData.pages.find((page) => {
19
+ const normalize = (p) => (
20
+ // compat the path that has no / suffix
21
+ p.replace(/\/$/, "")
22
+ );
23
+ return isEqualPath(normalize(page.routePath), normalize(routePath));
24
+ });
25
+ const encodedPagePath = encodeURIComponent(pagePath);
26
+ let {
27
+ // eslint-disable-next-line prefer-const
28
+ toc = [],
29
+ // eslint-disable-next-line prefer-const
30
+ title = "",
31
+ frontmatter
32
+ } = globalThis.__RSPRESS_PAGE_META?.[encodedPagePath] || {};
33
+ frontmatter = frontmatter || mod.frontmatter || {};
34
+ return {
35
+ siteData,
36
+ page: {
37
+ pagePath,
38
+ ...extractPageInfo,
39
+ pageType: frontmatter?.pageType || "doc",
40
+ title,
41
+ frontmatter,
42
+ // Trade off:
43
+ // 1. the `extractPageInfo` includes complete toc even if import doc fragments, because we use `flattenMdxContent` function to make all doc fragments' toc included.However, it is only computed once when build
44
+ // 2. the mod.toc is not complete toc, but it is computed every time through loader when doc changed
45
+ // We choose the better solutions for different environments:
46
+ // In production, we use the extractPageInfo.toc to ensure the toc is complete and accurate.
47
+ // In development, we use the mod.toc to ensure the toc is up to date to ensure DX.However, we cannot ensure the complete toc info when including doc fragments.
48
+ toc: isProduction() ? extractPageInfo?.toc : toc
49
+ }
50
+ };
51
+ } else {
52
+ return {
53
+ siteData,
54
+ page: {
55
+ pagePath: "",
56
+ pageType: "404",
57
+ routePath: "/404",
58
+ lang: siteData.lang || "",
59
+ frontmatter: {},
60
+ title: "404",
61
+ toc: [],
62
+ _filepath: "",
63
+ _relativePath: ""
64
+ }
65
+ };
66
+ }
67
+ }
68
+ export function App({ helmetContext }) {
69
+ const { setData: setPageData } = useContext(DataContext);
70
+ const { pathname } = useLocation();
71
+ useLayoutEffect(() => {
72
+ async function refetchData() {
73
+ try {
74
+ const pageData = await initPageData(normalizeRoutePath(pathname));
75
+ setPageData(pageData);
76
+ } catch (e) {
77
+ console.log(e);
78
+ }
79
+ }
80
+ refetchData();
81
+ }, [
82
+ pathname,
83
+ setPageData
84
+ ]);
85
+ return /* @__PURE__ */ _jsx(HelmetProvider, {
86
+ context: helmetContext,
87
+ children: /* @__PURE__ */ _jsx(Theme.Layout, {})
88
+ });
89
+ }
@@ -0,0 +1,20 @@
1
+ import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { Suspense } from "react";
3
+ import { matchRoutes, useLocation } from "react-router-dom";
4
+ import { normalizeRoutePath } from "./utils";
5
+ const { routes } = process.env.__SSR__ ? require("virtual-routes-ssr") : require("virtual-routes");
6
+ export const Content = ({ fallback = /* @__PURE__ */ _jsx(_Fragment, {}) }) => {
7
+ const { pathname } = useLocation();
8
+ const matched = matchRoutes(routes, normalizeRoutePath(pathname));
9
+ if (!matched) {
10
+ return /* @__PURE__ */ _jsx("div", {});
11
+ }
12
+ const routesElement = matched[0].route.element;
13
+ if (!process.env.__IS_REACT_18__ && process.env.__SSR__) {
14
+ return routesElement;
15
+ }
16
+ return /* @__PURE__ */ _jsx(Suspense, {
17
+ fallback,
18
+ children: routesElement
19
+ });
20
+ };
@@ -0,0 +1,16 @@
1
+ import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { useEffect, useState } from "react";
3
+ export function NoSSR(props) {
4
+ const { children } = props;
5
+ const [isMounted, setIsMounted] = useState(false);
6
+ useEffect(() => {
7
+ setIsMounted(true);
8
+ }, []);
9
+ if (!isMounted) {
10
+ return null;
11
+ } else {
12
+ return /* @__PURE__ */ _jsx(_Fragment, {
13
+ children
14
+ });
15
+ }
16
+ }
@@ -0,0 +1,52 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useState } from "react";
3
+ import { BrowserRouter } from "react-router-dom";
4
+ import { isProduction } from "@rspress/shared";
5
+ import { App, initPageData } from "./App";
6
+ import { DataContext, ThemeContext } from "./hooks";
7
+ import { normalizeRoutePath } from "./utils";
8
+ const { default: Theme } = require("@theme");
9
+ export async function renderInBrowser() {
10
+ const container = document.getElementById("root");
11
+ const enhancedApp = async () => {
12
+ const initialPageData = await initPageData(normalizeRoutePath(window.location.pathname));
13
+ return function RootApp2() {
14
+ const [data, setData] = useState(initialPageData);
15
+ const [theme, setTheme] = useState("light");
16
+ return /* @__PURE__ */ _jsx(ThemeContext.Provider, {
17
+ value: {
18
+ theme,
19
+ setTheme
20
+ },
21
+ children: /* @__PURE__ */ _jsx(DataContext.Provider, {
22
+ value: {
23
+ data,
24
+ setData
25
+ },
26
+ children: /* @__PURE__ */ _jsx(BrowserRouter, {
27
+ children: /* @__PURE__ */ _jsx(App, {})
28
+ })
29
+ })
30
+ });
31
+ };
32
+ };
33
+ const RootApp = await enhancedApp();
34
+ if (process.env.__IS_REACT_18__) {
35
+ const { createRoot, hydrateRoot } = require("react-dom/client");
36
+ if (isProduction()) {
37
+ hydrateRoot(container, /* @__PURE__ */ _jsx(RootApp, {}));
38
+ } else {
39
+ createRoot(container).render(/* @__PURE__ */ _jsx(RootApp, {}));
40
+ }
41
+ } else {
42
+ const ReactDOM = require("react-dom");
43
+ if (isProduction()) {
44
+ ReactDOM.hydrate(/* @__PURE__ */ _jsx(RootApp, {}), container);
45
+ } else {
46
+ ReactDOM.render(/* @__PURE__ */ _jsx(RootApp, {}), container);
47
+ }
48
+ }
49
+ }
50
+ renderInBrowser().then(() => {
51
+ Theme.setup();
52
+ });
@@ -0,0 +1,20 @@
1
+ import { createContext, useContext } from "react";
2
+ import i18nTextData from "virtual-i18n-text";
3
+ export const DataContext = createContext({});
4
+ export const ThemeContext = createContext({});
5
+ export function usePageData() {
6
+ const ctx = useContext(DataContext);
7
+ return ctx.data;
8
+ }
9
+ export function useLang() {
10
+ const ctx = useContext(DataContext);
11
+ return ctx.data.page.lang || "";
12
+ }
13
+ export function useDark() {
14
+ const ctx = useContext(ThemeContext);
15
+ return ctx.theme === "dark";
16
+ }
17
+ export function useI18n() {
18
+ const lang = useLang();
19
+ return (key) => i18nTextData[key][lang];
20
+ }
@@ -0,0 +1,6 @@
1
+ export * from "./hooks";
2
+ export * from "./Content";
3
+ export { normalizeHref, withBase, removeBase, addLeadingSlash, removeTrailingSlash, normalizeSlash, isProduction, normalizeRoutePath, isEqualPath } from "./utils";
4
+ export { useLocation, useNavigate } from "react-router-dom";
5
+ export { Helmet } from "react-helmet-async";
6
+ export { NoSSR } from "./NoSSR";
@@ -0,0 +1,30 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { renderToString } from "react-dom/server";
3
+ import { StaticRouter } from "react-router-dom/server";
4
+ import { App, initPageData } from "./App";
5
+ import { DataContext, ThemeContext } from "./hooks";
6
+ const DEFAULT_THEME = "light";
7
+ export async function render(pagePath, helmetContext) {
8
+ const initialPageData = await initPageData(pagePath);
9
+ const appHtml = renderToString(/* @__PURE__ */ _jsx(ThemeContext.Provider, {
10
+ value: {
11
+ theme: DEFAULT_THEME
12
+ },
13
+ children: /* @__PURE__ */ _jsx(DataContext.Provider, {
14
+ value: {
15
+ data: initialPageData
16
+ },
17
+ children: /* @__PURE__ */ _jsx(StaticRouter, {
18
+ location: pagePath,
19
+ children: /* @__PURE__ */ _jsx(App, {
20
+ helmetContext
21
+ })
22
+ })
23
+ })
24
+ }));
25
+ return {
26
+ appHtml,
27
+ pageData: initialPageData
28
+ };
29
+ }
30
+ export { routes } from "virtual-routes-ssr";
@@ -0,0 +1,15 @@
1
+ import siteData from "virtual-site-data";
2
+ import { addLeadingSlash, removeTrailingSlash, normalizeSlash, isProduction, normalizeHref, withBase as rawWithBase, removeBase as rawRemoveBase } from "@rspress/shared";
3
+ export function normalizeRoutePath(routePath) {
4
+ return decodeURIComponent(routePath).replace(/\.html$/, "").replace(/\/index$/, "/");
5
+ }
6
+ export function withBase(url = "/") {
7
+ return rawWithBase(url, siteData.base);
8
+ }
9
+ export function removeBase(url) {
10
+ return rawRemoveBase(url, siteData.base);
11
+ }
12
+ export function isEqualPath(a, b) {
13
+ return withBase(normalizeHref(a)) === withBase(normalizeHref(b));
14
+ }
15
+ export { addLeadingSlash, removeTrailingSlash, normalizeSlash, isProduction, normalizeHref };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rspress/core",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "The Rspress Documentation Framework",
5
5
  "bugs": "https://github.com/web-infra-dev/rspress/issues",
6
6
  "repository": {
@@ -91,11 +91,11 @@
91
91
  "unist-util-visit": "^4.1.1",
92
92
  "unist-util-visit-children": "^2.0.1",
93
93
  "yaml-front-matter": "^4.1.1",
94
- "@rspress/shared": "0.0.7",
95
- "@rspress/plugin-medium-zoom": "0.0.7",
96
- "@rspress/plugin-last-updated": "0.0.7",
97
- "@rspress/plugin-auto-nav-sidebar": "0.0.7",
98
- "@rspress/remark-container": "0.0.6"
94
+ "@rspress/shared": "0.0.9",
95
+ "@rspress/plugin-medium-zoom": "0.0.9",
96
+ "@rspress/plugin-last-updated": "0.0.9",
97
+ "@rspress/plugin-auto-nav-sidebar": "0.0.9",
98
+ "@rspress/remark-container": "0.0.8"
99
99
  },
100
100
  "peerDependencies": {
101
101
  "react": ">=17"
package/runtime.ts CHANGED
@@ -1 +1 @@
1
- export * from './src/runtime';
1
+ export * from './dist/runtime';
@@ -2,12 +2,14 @@ import siteData from 'virtual-site-data';
2
2
  import { matchRoutes, useLocation } from 'react-router-dom';
3
3
  import { HelmetProvider } from 'react-helmet-async';
4
4
  import { useContext, useLayoutEffect } from 'react';
5
- import Theme from '@theme';
6
5
  import { Header, PageData, cleanUrl, isProduction } from '@rspress/shared';
7
6
  import { isEqualPath, normalizeRoutePath } from './utils';
8
7
  import { DataContext } from './hooks';
9
8
  import 'virtual-global-styles';
10
9
 
10
+ // eslint-disable-next-line import/no-commonjs
11
+ const { default: Theme } = require('@theme');
12
+
11
13
  type RspressPageMeta = Record<
12
14
  string,
13
15
  {
@@ -1,4 +1,3 @@
1
- import Theme from '@theme';
2
1
  import { useState } from 'react';
3
2
  import { BrowserRouter } from 'react-router-dom';
4
3
  import { isProduction } from '@rspress/shared';
@@ -6,6 +5,9 @@ import { App, initPageData } from './App';
6
5
  import { DataContext, ThemeContext } from './hooks';
7
6
  import { normalizeRoutePath } from './utils';
8
7
 
8
+ // eslint-disable-next-line import/no-commonjs
9
+ const { default: Theme } = require('@theme');
10
+
9
11
  export async function renderInBrowser() {
10
12
  const container = document.getElementById('root')!;
11
13
  const enhancedApp = async () => {