@rspress/plugin-preview 0.0.0-canary-20250521

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/utils.js ADDED
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ generateId: ()=>generateId,
28
+ injectDemoBlockImport: ()=>injectDemoBlockImport,
29
+ normalizeId: ()=>normalizeId,
30
+ toValidVarName: ()=>toValidVarName
31
+ });
32
+ const toValidVarName = (str)=>{
33
+ if (/^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(str)) return str;
34
+ return str.replace(/[^0-9a-zA-Z_$]/g, '_').replace(/^([0-9])/, '_$1');
35
+ };
36
+ const generateId = (pageName, index)=>`_${toValidVarName(pageName)}_${index}`;
37
+ const normalizeId = (routePath)=>{
38
+ const result = routePath.replace(/\.(.*)?$/, '');
39
+ return toValidVarName(result);
40
+ };
41
+ const injectDemoBlockImport = (str, path)=>`
42
+ import DemoBlock from ${JSON.stringify(path)};
43
+ ${str}
44
+ `;
45
+ exports.generateId = __webpack_exports__.generateId;
46
+ exports.injectDemoBlockImport = __webpack_exports__.injectDemoBlockImport;
47
+ exports.normalizeId = __webpack_exports__.normalizeId;
48
+ exports.toValidVarName = __webpack_exports__.toValidVarName;
49
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
50
+ "generateId",
51
+ "injectDemoBlockImport",
52
+ "normalizeId",
53
+ "toValidVarName"
54
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
55
+ Object.defineProperty(exports, '__esModule', {
56
+ value: true
57
+ });
package/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ declare module '@rspress/shared' {
2
+ interface PageIndexInfo {
3
+ devPort?: number;
4
+ extraHighlightLanguages?: string[];
5
+ }
6
+ }
7
+
8
+ export * from './dist'
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@rspress/plugin-preview",
3
+ "version": "0.0.0-canary-20250521",
4
+ "description": "A plugin for rspress to preview the code block in markdown/mdx file.",
5
+ "bugs": "https://github.com/web-infra-dev/rspress/issues",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/web-infra-dev/rspress",
9
+ "directory": "packages/plugin-preview"
10
+ },
11
+ "license": "MIT",
12
+ "main": "./dist/index.js",
13
+ "types": "./index.d.ts",
14
+ "files": [
15
+ "index.d.ts",
16
+ "dist",
17
+ "static"
18
+ ],
19
+ "dependencies": {
20
+ "@rsbuild/core": "~1.3.18",
21
+ "@rsbuild/plugin-babel": "~1.0.5",
22
+ "@rsbuild/plugin-less": "~1.2.4",
23
+ "@rsbuild/plugin-react": "~1.3.1",
24
+ "@rsbuild/plugin-sass": "~1.3.1",
25
+ "@rsbuild/plugin-solid": "~1.0.5",
26
+ "lodash": "4.17.21",
27
+ "qrcode.react": "^3.2.0",
28
+ "@rspress/shared": "1.44.0",
29
+ "@rspress/theme-default": "1.44.0"
30
+ },
31
+ "devDependencies": {
32
+ "@rslib/core": "~0.6.9",
33
+ "@types/lodash": "^4.17.15",
34
+ "@types/mdast": "^3.0.15",
35
+ "@types/node": "^18.11.17",
36
+ "@types/react": "^18.3.18",
37
+ "@types/react-dom": "^18.3.5",
38
+ "mdast-util-mdx-jsx": "^2.1.4",
39
+ "mdast-util-mdxjs-esm": "^1.3.1",
40
+ "react": "^18.3.1",
41
+ "react-dom": "^18.3.1",
42
+ "react-router-dom": "^6.29.0",
43
+ "typescript": "^5.5.3",
44
+ "unified": "^10.1.2",
45
+ "unist-util-visit": "^4.1.2"
46
+ },
47
+ "peerDependencies": {
48
+ "@rspress/core": "^1.44.0",
49
+ "react": ">=17.0.0",
50
+ "react-router-dom": "^6.8.1"
51
+ },
52
+ "engines": {
53
+ "node": ">=14.17.6"
54
+ },
55
+ "publishConfig": {
56
+ "access": "public",
57
+ "provenance": true,
58
+ "registry": "https://registry.npmjs.org/"
59
+ },
60
+ "scripts": {
61
+ "build": "rslib build",
62
+ "dev": "rslib build -w",
63
+ "reset": "rimraf ./**/node_modules"
64
+ }
65
+ }
@@ -0,0 +1,6 @@
1
+ /// <reference types='@rslib/core/types' />
2
+
3
+ declare module 'virtual-meta' {
4
+ const demos: Record<string, []>;
5
+ export { demos };
6
+ }
@@ -0,0 +1,92 @@
1
+ import { NoSSR, useLang, usePageData, withBase } from '@rspress/core/runtime';
2
+ import { type MouseEvent, useCallback, useState } from 'react';
3
+ import MobileOperation from './common/mobile-operation';
4
+ import IconCode from './icons/Code';
5
+
6
+ type ContainerProps = {
7
+ children: React.ReactNode[];
8
+ isMobile: 'true' | 'false';
9
+ demoId: string;
10
+ };
11
+
12
+ const Container: React.FC<ContainerProps> = props => {
13
+ const { children, isMobile, demoId } = props;
14
+ const { page } = usePageData();
15
+ const [showCode, setShowCode] = useState(false);
16
+ const lang = useLang();
17
+ const url = `/~demo/${demoId}`;
18
+
19
+ const getPageUrl = () => {
20
+ if (page?.devPort) {
21
+ return `http://localhost:${page.devPort}/${demoId}`;
22
+ }
23
+ if (typeof window !== 'undefined') {
24
+ return `${window.location.origin}${withBase(url)}`;
25
+ }
26
+ // Do nothing in ssr
27
+ return '';
28
+ };
29
+ const toggleCode = useCallback(
30
+ (ev: MouseEvent<HTMLButtonElement>) => {
31
+ if (!showCode) {
32
+ ev.currentTarget.blur();
33
+ }
34
+ setShowCode(!showCode);
35
+ },
36
+ [showCode],
37
+ );
38
+
39
+ const [iframeKey, setIframeKey] = useState(0);
40
+ const refresh = useCallback(() => {
41
+ setIframeKey(Math.random());
42
+ }, []);
43
+
44
+ return (
45
+ <NoSSR>
46
+ <div className="rspress-preview">
47
+ {isMobile === 'true' ? (
48
+ <div className="rspress-preview-wrapper">
49
+ <div className="rspress-preview-code">{children?.[0]}</div>
50
+ <div className="rspress-preview-device">
51
+ <iframe src={getPageUrl()} key={iframeKey}></iframe>
52
+ <MobileOperation url={getPageUrl()} refresh={refresh} />
53
+ </div>
54
+ </div>
55
+ ) : (
56
+ <div>
57
+ <div className="rspress-preview-card">
58
+ <div
59
+ style={{
60
+ overflow: 'auto',
61
+ flex: 'auto',
62
+ }}
63
+ >
64
+ {children?.[1]}
65
+ </div>
66
+ <div className="rspress-preview-operations web">
67
+ <button
68
+ onClick={toggleCode}
69
+ aria-label={lang === 'zh' ? '收起代码' : 'Collapse Code'}
70
+ className={showCode ? 'button-expanded' : ''}
71
+ >
72
+ <IconCode />
73
+ </button>
74
+ </div>
75
+ </div>
76
+ <div
77
+ className={`${
78
+ showCode
79
+ ? 'rspress-preview-code-show'
80
+ : 'rspress-preview-code-hide'
81
+ }`}
82
+ >
83
+ {children?.[0]}
84
+ </div>
85
+ </div>
86
+ )}
87
+ </div>
88
+ </NoSSR>
89
+ );
90
+ };
91
+
92
+ export default Container;
@@ -0,0 +1,101 @@
1
+ import { NoSSR, useLang, usePageData, withBase } from '@rspress/core/runtime';
2
+ import { type MouseEvent, useCallback, useState } from 'react';
3
+ import IconCode from './icons/Code';
4
+ import { publishIframeUrl } from './useIframeUrlPerComp';
5
+
6
+ type ContainerProps = {
7
+ children: React.ReactNode[];
8
+ isMobile: 'true' | 'false';
9
+ demoId: string;
10
+ };
11
+
12
+ const MobileContainerFixedPerComp: React.FC<ContainerProps> = props => {
13
+ const { children, demoId } = props;
14
+ const { page } = usePageData();
15
+ const url = `/~demo/${demoId}`;
16
+
17
+ const getPageUrl = () => {
18
+ if (page?.devPort) {
19
+ return `http://localhost:${page.devPort}/${demoId}`;
20
+ }
21
+ if (typeof window !== 'undefined') {
22
+ return `${window.location.origin}${withBase(url)}`;
23
+ }
24
+ // Do nothing in ssr
25
+ return '';
26
+ };
27
+
28
+ const setIframeUrl = () => {
29
+ const url = getPageUrl();
30
+ const fixedIframe = document.querySelector('.rspress-fixed-iframe');
31
+ fixedIframe?.setAttribute('src', url);
32
+ publishIframeUrl(url);
33
+ };
34
+
35
+ return (
36
+ <div className="rspress-preview-code" onClick={setIframeUrl}>
37
+ {children}
38
+ </div>
39
+ );
40
+ };
41
+
42
+ const ContainerFixedPerComp = (props: ContainerProps) => {
43
+ const { children, isMobile } = props;
44
+ const [showCode, setShowCode] = useState(false);
45
+ const lang = useLang();
46
+
47
+ const toggleCode = useCallback(
48
+ (ev: MouseEvent<HTMLButtonElement>) => {
49
+ if (!showCode) {
50
+ ev.currentTarget.blur();
51
+ }
52
+ setShowCode(!showCode);
53
+ },
54
+ [showCode],
55
+ );
56
+
57
+ return (
58
+ <>
59
+ {isMobile === 'true' ? (
60
+ <MobileContainerFixedPerComp {...props} />
61
+ ) : (
62
+ <NoSSR>
63
+ <div className="rspress-preview">
64
+ <div>
65
+ <div className="rspress-preview-card">
66
+ <div
67
+ style={{
68
+ overflow: 'auto',
69
+ flex: 'auto',
70
+ }}
71
+ >
72
+ {children?.[1]}
73
+ </div>
74
+ <div className="rspress-preview-operations web">
75
+ <button
76
+ onClick={toggleCode}
77
+ aria-label={lang === 'zh' ? '收起代码' : 'Collapse Code'}
78
+ className={showCode ? 'button-expanded' : ''}
79
+ >
80
+ <IconCode />
81
+ </button>
82
+ </div>
83
+ </div>
84
+ <div
85
+ className={`${
86
+ showCode
87
+ ? 'rspress-preview-code-show'
88
+ : 'rspress-preview-code-hide'
89
+ }`}
90
+ >
91
+ {children?.[0]}
92
+ </div>
93
+ </div>
94
+ </div>
95
+ </NoSSR>
96
+ )}
97
+ </>
98
+ );
99
+ };
100
+
101
+ export default ContainerFixedPerComp;
@@ -0,0 +1,21 @@
1
+ :root {
2
+ --rp-demo-block-bg: #f7f8fa;
3
+ }
4
+
5
+ .dark {
6
+ --rp-demo-block-bg: #1a1a1a;
7
+ }
8
+
9
+ .rspress-demo-block {
10
+ padding-bottom: 12px;
11
+ background-color: var(--rp-demo-block-bg);
12
+ &-title {
13
+ padding: 12px 12px 8px;
14
+ color: #697b8c;
15
+ font-size: 14px;
16
+ }
17
+ &-main {
18
+ border-right: none;
19
+ border-left: none;
20
+ }
21
+ }
@@ -0,0 +1,16 @@
1
+ import './DemoBlock.scss';
2
+
3
+ interface Props {
4
+ title: string;
5
+ children?: React.ReactNode;
6
+ }
7
+
8
+ export default (props: Props) => {
9
+ const { title, children } = props;
10
+ return (
11
+ <div className="rspress-demo-block">
12
+ <div className="rspress-demo-block-title">{title}</div>
13
+ <div className="rspress-demo-block-main">{children}</div>
14
+ </div>
15
+ );
16
+ };
@@ -0,0 +1,48 @@
1
+ .rspress-fixed-device {
2
+ display: none;
3
+ position: fixed;
4
+ top: calc(var(--rp-nav-height) + var(--rp-preview-padding));
5
+ overflow: hidden;
6
+ }
7
+
8
+ .rspress-fixed-iframe {
9
+ height: var(--rp-device-height);
10
+ max-height: calc(
11
+ 100vh - var(--rp-preview-padding) * 2 - var(--rp-nav-height)
12
+ );
13
+ width: 360px;
14
+ pointer-events: auto;
15
+ border-radius: var(--rp-device-border-radius) var(--rp-device-border-radius) 0
16
+ 0;
17
+ border: var(--rp-device-border);
18
+ }
19
+
20
+ .rspress-fixed-operation {
21
+ border: var(--rp-device-border);
22
+ border-top: 0;
23
+ border-radius: 0 0 var(--rp-device-border-radius)
24
+ var(--rp-device-border-radius);
25
+ }
26
+
27
+ :root {
28
+ --rp-device-width: 360px;
29
+ --rp-device-height: 640px;
30
+ --rp-device-border-radius: 20px;
31
+ --rp-device-border: 1px solid #e5e6e8;
32
+ }
33
+
34
+ @media (min-width: 960px) {
35
+ .rspress-fixed-device {
36
+ display: inline;
37
+ left: calc(1280px - var(--rp-device-width) - var(--rp-preview-padding));
38
+ right: auto;
39
+ }
40
+ }
41
+
42
+ @media (min-width: 1280px) {
43
+ .rspress-fixed-device {
44
+ display: inline;
45
+ right: var(--rp-preview-padding);
46
+ left: auto;
47
+ }
48
+ }
@@ -0,0 +1,91 @@
1
+ import {
2
+ NoSSR,
3
+ usePageData,
4
+ useWindowSize,
5
+ withBase,
6
+ } from '@rspress/core/runtime';
7
+ import { useCallback, useEffect, useState } from 'react';
8
+ // @ts-ignore
9
+ import { normalizeId } from '../../dist/utils';
10
+ import MobileOperation from './common/mobile-operation';
11
+ import './Device.scss';
12
+
13
+ export default () => {
14
+ const { page } = usePageData();
15
+ const pageName = `${normalizeId(page.pagePath)}`;
16
+ const demoId = `_${pageName}`;
17
+ const url = `~demo/${demoId}`;
18
+ const { haveDemos } = page;
19
+
20
+ const getPageUrl = (url: string) => {
21
+ if (page?.devPort) {
22
+ return `http://localhost:${page.devPort}/${demoId}`;
23
+ }
24
+ if (typeof window !== 'undefined') {
25
+ return `${window.location.origin}${withBase(url)}`;
26
+ }
27
+ // Do nothing in ssr
28
+ return '';
29
+ };
30
+ const [asideWidth, setAsideWidth] = useState('0px');
31
+ const { width: innerWidth } = useWindowSize();
32
+ const [iframeKey, setIframeKey] = useState(0);
33
+ const refresh = useCallback(() => {
34
+ setIframeKey(Math.random());
35
+ }, []);
36
+
37
+ // get default value from root
38
+ // listen resize and re-render
39
+ useEffect(() => {
40
+ const root = document.querySelector(':root');
41
+ if (root) {
42
+ const defaultAsideWidth =
43
+ getComputedStyle(root).getPropertyValue('--rp-aside-width');
44
+ setAsideWidth(defaultAsideWidth);
45
+ }
46
+ }, []);
47
+
48
+ useEffect(() => {
49
+ const node = document.querySelector('.rspress-doc-container');
50
+ const { style } = document.documentElement;
51
+ if (haveDemos) {
52
+ if (innerWidth > 1280) {
53
+ node?.setAttribute(
54
+ 'style',
55
+ 'padding-right: calc(var(--rp-device-width) + var(--rp-preview-padding) * 2)',
56
+ );
57
+ } else if (innerWidth > 960) {
58
+ node?.setAttribute(
59
+ 'style',
60
+ `padding-right: calc(${
61
+ innerWidth - 1280
62
+ }px + var(--rp-device-width) + var(--rp-preview-padding) * 2)`,
63
+ );
64
+ } else {
65
+ node?.removeAttribute('style');
66
+ }
67
+ style.setProperty('--rp-aside-width', '0px');
68
+ } else {
69
+ node?.removeAttribute('style');
70
+ style.setProperty('--rp-aside-width', asideWidth);
71
+ }
72
+ }, [haveDemos, asideWidth, innerWidth]);
73
+
74
+ return haveDemos ? (
75
+ <div className="rspress-fixed-device">
76
+ <NoSSR>
77
+ <iframe
78
+ // refresh when load the iframe, then remove NoSSR
79
+ src={getPageUrl(url)}
80
+ className="rspress-fixed-iframe"
81
+ key={iframeKey}
82
+ ></iframe>
83
+ </NoSSR>
84
+ <MobileOperation
85
+ url={getPageUrl(url)}
86
+ className="rspress-fixed-operation"
87
+ refresh={refresh}
88
+ />
89
+ </div>
90
+ ) : null;
91
+ };
@@ -0,0 +1,106 @@
1
+ import {
2
+ NoSSR,
3
+ usePageData,
4
+ useWindowSize,
5
+ withBase,
6
+ } from '@rspress/core/runtime';
7
+ import { useCallback, useEffect, useState } from 'react';
8
+ // @ts-ignore
9
+ import { normalizeId } from '../../dist/utils';
10
+ import MobileOperation from './common/mobile-operation';
11
+ import { publishIframeUrl, useIframeUrlPerComp } from './useIframeUrlPerComp';
12
+ import './Device.scss';
13
+
14
+ export default () => {
15
+ const { page } = usePageData();
16
+ const pageName = `${normalizeId(page.pagePath)}`;
17
+ const demoId = `_${pageName}`;
18
+ const url = `~demo/${demoId}`;
19
+ const { haveDemos } = page;
20
+
21
+ const getPageUrl = (url: string) => {
22
+ if (page?.devPort) {
23
+ return `http://localhost:${page.devPort}/${demoId}`;
24
+ }
25
+ if (typeof window !== 'undefined') {
26
+ return `${window.location.origin}${withBase(url)}`;
27
+ }
28
+ // Do nothing in ssr
29
+ return '';
30
+ };
31
+
32
+ const initialUrl = getPageUrl(url);
33
+
34
+ const iframeUrl = useIframeUrlPerComp() ?? initialUrl;
35
+
36
+ const resetIframeUrl = useCallback(() => {
37
+ publishIframeUrl(initialUrl);
38
+ }, [initialUrl, url]);
39
+
40
+ useEffect(() => {
41
+ publishIframeUrl(initialUrl);
42
+ }, []);
43
+
44
+ const [asideWidth, setAsideWidth] = useState('0px');
45
+ const { width: innerWidth } = useWindowSize();
46
+ const [iframeKey, setIframeKey] = useState(0);
47
+ const refresh = useCallback(() => {
48
+ setIframeKey(Math.random());
49
+ }, []);
50
+
51
+ // get default value from root
52
+ // listen resize and re-render
53
+ useEffect(() => {
54
+ const root = document.querySelector(':root');
55
+ if (root) {
56
+ const defaultAsideWidth =
57
+ getComputedStyle(root).getPropertyValue('--rp-aside-width');
58
+ setAsideWidth(defaultAsideWidth);
59
+ }
60
+ }, []);
61
+
62
+ useEffect(() => {
63
+ const node = document.querySelector('.rspress-doc-container');
64
+ const { style } = document.documentElement;
65
+ if (haveDemos) {
66
+ if (innerWidth > 1280) {
67
+ node?.setAttribute(
68
+ 'style',
69
+ 'padding-right: calc(var(--rp-device-width) + var(--rp-preview-padding) * 2)',
70
+ );
71
+ } else if (innerWidth > 960) {
72
+ node?.setAttribute(
73
+ 'style',
74
+ `padding-right: calc(${
75
+ innerWidth - 1280
76
+ }px + var(--rp-device-width) + var(--rp-preview-padding) * 2)`,
77
+ );
78
+ } else {
79
+ node?.removeAttribute('style');
80
+ }
81
+ style.setProperty('--rp-aside-width', '0px');
82
+ } else {
83
+ node?.removeAttribute('style');
84
+ style.setProperty('--rp-aside-width', asideWidth);
85
+ }
86
+ }, [haveDemos, asideWidth, innerWidth]);
87
+
88
+ return haveDemos ? (
89
+ <div className="rspress-fixed-device">
90
+ <NoSSR>
91
+ <iframe
92
+ // refresh when load the iframe, then remove NoSSR
93
+ src={iframeUrl}
94
+ className="rspress-fixed-iframe"
95
+ key={iframeKey}
96
+ ></iframe>
97
+ </NoSSR>
98
+ <MobileOperation
99
+ url={iframeUrl}
100
+ className="rspress-fixed-operation"
101
+ refresh={refresh}
102
+ goBack={iframeUrl !== initialUrl ? resetIframeUrl : undefined}
103
+ />
104
+ </div>
105
+ ) : null;
106
+ };