@rspress/plugin-preview 2.0.0-beta.2 → 2.0.0-beta.21
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/README.md +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +45 -59
- package/index.d.ts +1 -2
- package/package.json +14 -15
- package/static/global-components/Container.tsx +1 -1
- package/static/global-components/Device.tsx +1 -6
- package/static/global-components/common/mobile-operation.tsx +1 -1
- package/static/global-styles/entry.css +22 -5
package/README.md
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
@@ -1,16 +1,15 @@
|
|
1
|
-
import
|
2
|
-
import
|
3
|
-
import
|
4
|
-
import
|
5
|
-
import
|
6
|
-
import
|
7
|
-
import
|
8
|
-
import
|
9
|
-
import
|
10
|
-
|
11
|
-
const
|
12
|
-
const
|
13
|
-
const virtualDir = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(process.cwd(), 'node_modules', __WEBPACK_EXTERNAL_MODULE__rspress_shared_baa012d0__.RSPRESS_TEMP_DIR, 'virtual-demo');
|
1
|
+
import node_net from "node:net";
|
2
|
+
import node_path, { dirname, join, resolve as external_node_path_resolve } from "node:path";
|
3
|
+
import { createRsbuild, mergeRsbuildConfig } from "@rsbuild/core";
|
4
|
+
import { pluginBabel } from "@rsbuild/plugin-babel";
|
5
|
+
import { pluginReact } from "@rsbuild/plugin-react";
|
6
|
+
import { pluginSolid } from "@rsbuild/plugin-solid";
|
7
|
+
import { cloneDeep, isEqual } from "lodash";
|
8
|
+
import { RSPRESS_TEMP_DIR, normalizePosixPath, removeTrailingSlash } from "rspress/core";
|
9
|
+
import node_fs, { writeFileSync } from "node:fs";
|
10
|
+
const staticPath = node_path.join(__dirname, '..', 'static');
|
11
|
+
const demoBlockComponentPath = node_path.join(staticPath, 'global-components', 'DemoBlock.tsx');
|
12
|
+
const virtualDir = node_path.join(process.cwd(), 'node_modules', RSPRESS_TEMP_DIR, 'virtual-demo');
|
14
13
|
const toValidVarName = (str)=>{
|
15
14
|
if (/^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(str)) return str;
|
16
15
|
return str.replace(/[^0-9a-zA-Z_$]/g, '_').replace(/^([0-9])/, '_$1');
|
@@ -33,11 +32,11 @@ const getLangFileExt = (lang)=>{
|
|
33
32
|
};
|
34
33
|
function generateEntry(demos, framework, position, customEntry) {
|
35
34
|
const sourceEntry = {};
|
36
|
-
const entryCssPath =
|
35
|
+
const entryCssPath = join(staticPath, 'global-styles', 'entry.css');
|
37
36
|
if ('follow' === position) Object.values(demos).forEach((routes)=>{
|
38
37
|
routes.forEach((route)=>{
|
39
38
|
const { id, path: demoPath } = route;
|
40
|
-
const entry =
|
39
|
+
const entry = join(virtualDir, `${id}.entry.tsx`);
|
41
40
|
const solidEntry = `
|
42
41
|
import { render } from 'solid-js/web';
|
43
42
|
import ${JSON.stringify(entryCssPath)};
|
@@ -55,7 +54,7 @@ function generateEntry(demos, framework, position, customEntry) {
|
|
55
54
|
entryCssPath,
|
56
55
|
demoPath
|
57
56
|
}) : 'react' === framework ? reactEntry : solidEntry;
|
58
|
-
|
57
|
+
writeFileSync(entry, entryContent);
|
59
58
|
sourceEntry[id] = entry;
|
60
59
|
});
|
61
60
|
});
|
@@ -92,8 +91,8 @@ function generateEntry(demos, framework, position, customEntry) {
|
|
92
91
|
`;
|
93
92
|
const renderContent = 'solid' === framework ? solidContent : reactContent;
|
94
93
|
const id = `_${toValidVarName(key)}`;
|
95
|
-
const entry =
|
96
|
-
|
94
|
+
const entry = join(virtualDir, `${id}.entry.tsx`);
|
95
|
+
writeFileSync(entry, renderContent);
|
97
96
|
sourceEntry[id] = entry;
|
98
97
|
});
|
99
98
|
return sourceEntry;
|
@@ -304,26 +303,25 @@ const getExternalDemoContent = (tempVar)=>({
|
|
304
303
|
const remarkPlugin_demos = {};
|
305
304
|
const remarkCodeToDemo = function({ getRouteMeta, previewMode, defaultRenderMode, position, previewLanguages, previewCodeTransform }) {
|
306
305
|
const routeMeta = getRouteMeta();
|
307
|
-
|
306
|
+
node_fs.mkdirSync(virtualDir, {
|
308
307
|
recursive: true
|
309
308
|
});
|
310
309
|
const data = this.data();
|
311
310
|
return (tree, vfile)=>{
|
312
311
|
const demoMdx = [];
|
313
|
-
const route = routeMeta.find((meta)=>
|
312
|
+
const route = routeMeta.find((meta)=>normalizePosixPath(meta.absolutePath) === normalizePosixPath(vfile.path || vfile.history[0]));
|
314
313
|
if (!route) return;
|
315
314
|
const { pageName } = route;
|
316
315
|
remarkPlugin_demos[pageName] = [];
|
317
316
|
let title = pageName;
|
318
317
|
let index = 1;
|
319
|
-
let externalDemoIndex = 0;
|
320
318
|
function constructDemoNode(demoId, demoPath, currentNode, isMobileMode, externalDemoIndex) {
|
321
319
|
if (isMobileMode) {
|
322
320
|
const relativePathReg = new RegExp(/^\.\.?\/.*$/);
|
323
321
|
remarkPlugin_demos[pageName].push({
|
324
322
|
title,
|
325
323
|
id: demoId,
|
326
|
-
path: relativePathReg.test(demoPath) ? (
|
324
|
+
path: relativePathReg.test(demoPath) ? external_node_path_resolve(vfile.dirname || dirname(vfile.path), demoPath) : demoPath
|
327
325
|
});
|
328
326
|
} else demoMdx.push(getASTNodeImport(`Demo${demoId}`, demoPath));
|
329
327
|
const tempVar = `externalDemoContent${externalDemoIndex}`;
|
@@ -365,18 +363,6 @@ const remarkCodeToDemo = function({ getRouteMeta, previewMode, defaultRenderMode
|
|
365
363
|
title = firstChild && 'value' in firstChild && firstChild.value || title;
|
366
364
|
}
|
367
365
|
});
|
368
|
-
lib_visit(tree, 'mdxJsxFlowElement', (node)=>{
|
369
|
-
if ('code' === node.name) {
|
370
|
-
const src = (0, __WEBPACK_EXTERNAL_MODULE__rspress_shared_node_utils_78947ce6__.getNodeAttribute)(node, 'src');
|
371
|
-
if ('string' != typeof src) return;
|
372
|
-
const currentMode = (0, __WEBPACK_EXTERNAL_MODULE__rspress_shared_node_utils_78947ce6__.getNodeAttribute)(node, 'previewMode') ?? previewMode;
|
373
|
-
const isMobileAttr = (0, __WEBPACK_EXTERNAL_MODULE__rspress_shared_node_utils_78947ce6__.getNodeAttribute)(node, 'isMobile');
|
374
|
-
let isMobileMode = false;
|
375
|
-
isMobileMode = void 0 === isMobileAttr ? 'iframe' === currentMode : null === isMobileAttr ? true : 'object' == typeof isMobileAttr ? 'false' !== isMobileAttr.value : 'false' !== isMobileAttr;
|
376
|
-
const id = generateId(pageName, index++);
|
377
|
-
constructDemoNode(id, src, node, isMobileMode, externalDemoIndex++);
|
378
|
-
}
|
379
|
-
});
|
380
366
|
lib_visit(tree, 'code', (node)=>{
|
381
367
|
if ('hasVisited' in node) return;
|
382
368
|
if (node.lang && previewLanguages.includes(node.lang)) {
|
@@ -391,13 +377,13 @@ const remarkCodeToDemo = function({ getRouteMeta, previewMode, defaultRenderMode
|
|
391
377
|
});
|
392
378
|
const isMobileMode = node.meta?.includes('mobile') || node.meta?.includes('iframe') || !node.meta?.includes('web') && !node.meta?.includes('internal') && 'iframe' === previewMode;
|
393
379
|
const id = generateId(pageName, index++);
|
394
|
-
const virtualModulePath =
|
380
|
+
const virtualModulePath = join(virtualDir, `${id}.${getLangFileExt(node.lang)}`);
|
395
381
|
constructDemoNode(id, virtualModulePath, node, isMobileMode);
|
396
|
-
if (
|
397
|
-
const content =
|
382
|
+
if (node_fs.existsSync(virtualModulePath)) {
|
383
|
+
const content = node_fs.readFileSync(virtualModulePath, 'utf-8');
|
398
384
|
if (content === value) return;
|
399
385
|
}
|
400
|
-
|
386
|
+
node_fs.writeFileSync(virtualModulePath, value);
|
401
387
|
}
|
402
388
|
});
|
403
389
|
tree.children.unshift(...demoMdx);
|
@@ -414,7 +400,7 @@ function pluginPreview(options) {
|
|
414
400
|
const previewMode = options?.previewMode ?? (isMobile ? 'iframe' : 'internal');
|
415
401
|
const { devPort = 7890, framework = 'react', position = iframePosition, builderConfig = {}, customEntry } = iframeOptions;
|
416
402
|
const globalUIComponents = 'fixed' === position ? [
|
417
|
-
|
403
|
+
join(staticPath, 'global-components', 'Device.tsx')
|
418
404
|
] : [];
|
419
405
|
const getRouteMeta = ()=>src_routeMeta;
|
420
406
|
let lastDemos;
|
@@ -433,7 +419,7 @@ function pluginPreview(options) {
|
|
433
419
|
async beforeBuild (_, isProd) {
|
434
420
|
if (!isProd) try {
|
435
421
|
await new Promise((resolve, reject)=>{
|
436
|
-
const server =
|
422
|
+
const server = node_net.createServer();
|
437
423
|
server.unref();
|
438
424
|
server.on('error', reject);
|
439
425
|
server.listen({
|
@@ -449,26 +435,27 @@ function pluginPreview(options) {
|
|
449
435
|
}
|
450
436
|
},
|
451
437
|
async afterBuild (config, isProd) {
|
452
|
-
if (
|
453
|
-
lastDemos =
|
438
|
+
if (isEqual(remarkPlugin_demos, lastDemos)) return;
|
439
|
+
lastDemos = cloneDeep(remarkPlugin_demos);
|
454
440
|
await devServer?.server?.close();
|
455
441
|
devServer = void 0;
|
456
442
|
const sourceEntry = generateEntry(remarkPlugin_demos, framework, position, customEntry);
|
457
|
-
const outDir =
|
443
|
+
const outDir = join(config.outDir ?? 'doc_build', '~demo');
|
458
444
|
if (0 === Object.keys(sourceEntry).length) return;
|
459
445
|
const { html, source, output, performance } = clientConfig ?? {};
|
460
|
-
const rsbuildConfig =
|
461
|
-
dev: {
|
462
|
-
progressBar: false
|
463
|
-
},
|
446
|
+
const rsbuildConfig = mergeRsbuildConfig({
|
464
447
|
server: {
|
465
448
|
port: devPort,
|
466
449
|
printUrls: ()=>void 0,
|
467
450
|
strictPort: true
|
468
451
|
},
|
452
|
+
dev: {
|
453
|
+
lazyCompilation: false
|
454
|
+
},
|
469
455
|
performance: {
|
470
456
|
...performance,
|
471
|
-
printFileSize: false
|
457
|
+
printFileSize: false,
|
458
|
+
buildCache: false
|
472
459
|
},
|
473
460
|
html,
|
474
461
|
source: {
|
@@ -477,15 +464,15 @@ function pluginPreview(options) {
|
|
477
464
|
},
|
478
465
|
output: {
|
479
466
|
...output,
|
480
|
-
assetPrefix: output?.assetPrefix ? `${
|
467
|
+
assetPrefix: output?.assetPrefix ? `${removeTrailingSlash(output.assetPrefix)}/~demo` : '/~demo',
|
481
468
|
distPath: {
|
482
469
|
root: outDir
|
483
470
|
},
|
484
471
|
copy: void 0
|
485
|
-
}
|
486
|
-
plugins: config?.builderPlugins
|
472
|
+
}
|
487
473
|
}, builderConfig);
|
488
|
-
const rsbuildInstance = await
|
474
|
+
const rsbuildInstance = await createRsbuild({
|
475
|
+
callerName: 'rspress',
|
489
476
|
rsbuildConfig
|
490
477
|
});
|
491
478
|
const { pluginSass } = await import("@rsbuild/plugin-sass");
|
@@ -495,13 +482,13 @@ function pluginPreview(options) {
|
|
495
482
|
pluginLess()
|
496
483
|
]);
|
497
484
|
if ('solid' === framework) rsbuildInstance.addPlugins([
|
498
|
-
|
485
|
+
pluginBabel({
|
499
486
|
include: /\.(?:jsx|tsx)$/
|
500
487
|
}),
|
501
|
-
|
488
|
+
pluginSolid()
|
502
489
|
]);
|
503
490
|
if ('react' === framework) rsbuildInstance.addPlugins([
|
504
|
-
|
491
|
+
pluginReact()
|
505
492
|
]);
|
506
493
|
if (isProd) rsbuildInstance.build();
|
507
494
|
else devServer = await rsbuildInstance.startDevServer();
|
@@ -509,7 +496,7 @@ function pluginPreview(options) {
|
|
509
496
|
builderConfig: {
|
510
497
|
source: {
|
511
498
|
include: [
|
512
|
-
|
499
|
+
join(__dirname, '..')
|
513
500
|
]
|
514
501
|
},
|
515
502
|
tools: {
|
@@ -540,7 +527,6 @@ function pluginPreview(options) {
|
|
540
527
|
},
|
541
528
|
extendPageData (pageData, isProd) {
|
542
529
|
if (!isProd) pageData.devPort = port;
|
543
|
-
pageData.extraHighlightLanguages = previewLanguages;
|
544
530
|
},
|
545
531
|
markdown: {
|
546
532
|
remarkPlugins: [
|
@@ -557,11 +543,11 @@ function pluginPreview(options) {
|
|
557
543
|
]
|
558
544
|
],
|
559
545
|
globalComponents: [
|
560
|
-
|
546
|
+
join(staticPath, 'global-components', 'Container.tsx')
|
561
547
|
]
|
562
548
|
},
|
563
549
|
globalUIComponents,
|
564
|
-
globalStyles:
|
550
|
+
globalStyles: join(staticPath, 'global-styles', `${previewMode}.css`)
|
565
551
|
};
|
566
552
|
}
|
567
553
|
export { pluginPreview };
|
package/index.d.ts
CHANGED
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@rspress/plugin-preview",
|
3
|
-
"version": "2.0.0-beta.
|
3
|
+
"version": "2.0.0-beta.21",
|
4
4
|
"description": "A plugin for rspress to preview the code block in markdown/mdx file.",
|
5
5
|
"bugs": "https://github.com/web-infra-dev/rspress/issues",
|
6
6
|
"repository": {
|
@@ -23,38 +23,37 @@
|
|
23
23
|
"static"
|
24
24
|
],
|
25
25
|
"dependencies": {
|
26
|
-
"@rsbuild/core": "1.
|
26
|
+
"@rsbuild/core": "~1.4.6",
|
27
27
|
"@rsbuild/plugin-babel": "~1.0.5",
|
28
|
-
"@rsbuild/plugin-less": "~1.2.
|
29
|
-
"@rsbuild/plugin-react": "~1.
|
30
|
-
"@rsbuild/plugin-sass": "~1.3.
|
28
|
+
"@rsbuild/plugin-less": "~1.2.5",
|
29
|
+
"@rsbuild/plugin-react": "~1.3.4",
|
30
|
+
"@rsbuild/plugin-sass": "~1.3.3",
|
31
31
|
"@rsbuild/plugin-solid": "~1.0.5",
|
32
32
|
"lodash": "4.17.21",
|
33
33
|
"qrcode.react": "^4.2.0",
|
34
|
-
"@rspress/
|
35
|
-
"@rspress/theme-default": "2.0.0-beta.2"
|
34
|
+
"@rspress/theme-default": "2.0.0-beta.21"
|
36
35
|
},
|
37
36
|
"devDependencies": {
|
38
|
-
"@rslib/core": "0.
|
39
|
-
"@types/lodash": "^4.17.
|
37
|
+
"@rslib/core": "0.10.5",
|
38
|
+
"@types/lodash": "^4.17.20",
|
40
39
|
"@types/mdast": "^4.0.4",
|
41
|
-
"@types/node": "^
|
42
|
-
"@types/react": "^
|
43
|
-
"@types/react-dom": "^
|
40
|
+
"@types/node": "^22.8.1",
|
41
|
+
"@types/react": "^19.1.8",
|
42
|
+
"@types/react-dom": "^19.1.6",
|
44
43
|
"mdast-util-mdx-jsx": "^3.2.0",
|
45
44
|
"mdast-util-mdxjs-esm": "^2.0.1",
|
46
45
|
"react": "^19.1.0",
|
47
46
|
"react-dom": "^19.1.0",
|
48
47
|
"react-router-dom": "^6.29.0",
|
49
|
-
"rsbuild-plugin-publint": "^0.3.
|
48
|
+
"rsbuild-plugin-publint": "^0.3.2",
|
50
49
|
"typescript": "^5.8.2",
|
51
50
|
"unified": "^11.0.5",
|
52
51
|
"unist-util-visit": "^5.0.0"
|
53
52
|
},
|
54
53
|
"peerDependencies": {
|
55
|
-
"@rspress/core": "^2.0.0-beta.2",
|
56
54
|
"react": ">=18.0.0",
|
57
|
-
"react-router-dom": "^6.8.1"
|
55
|
+
"react-router-dom": "^6.8.1",
|
56
|
+
"rspress": "^2.0.0-beta.21"
|
58
57
|
},
|
59
58
|
"engines": {
|
60
59
|
"node": ">=18.0.0"
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { NoSSR, useLang, usePageData, withBase } from '@rspress/core/runtime';
|
2
1
|
import { type MouseEvent, useCallback, useState } from 'react';
|
2
|
+
import { NoSSR, useLang, usePageData, withBase } from 'rspress/runtime';
|
3
3
|
import MobileOperation from './common/mobile-operation';
|
4
4
|
import IconCode from './icons/Code';
|
5
5
|
|
@@ -1,10 +1,5 @@
|
|
1
|
-
import {
|
2
|
-
NoSSR,
|
3
|
-
usePageData,
|
4
|
-
useWindowSize,
|
5
|
-
withBase,
|
6
|
-
} from '@rspress/core/runtime';
|
7
1
|
import { useCallback, useEffect, useState } from 'react';
|
2
|
+
import { NoSSR, usePageData, useWindowSize, withBase } from 'rspress/runtime';
|
8
3
|
// @ts-ignore
|
9
4
|
import { normalizeId } from '../../dist/utils';
|
10
5
|
import MobileOperation from './common/mobile-operation';
|
@@ -1,4 +1,3 @@
|
|
1
|
-
import { useLang } from '@rspress/core/runtime';
|
2
1
|
import { QRCodeSVG } from 'qrcode.react';
|
3
2
|
import {
|
4
3
|
type MouseEventHandler,
|
@@ -7,6 +6,7 @@ import {
|
|
7
6
|
useRef,
|
8
7
|
useState,
|
9
8
|
} from 'react';
|
9
|
+
import { useLang } from 'rspress/runtime';
|
10
10
|
import IconLaunch from '../icons/Launch';
|
11
11
|
import IconQrcode from '../icons/Qrcode';
|
12
12
|
import IconRefresh from '../icons/Refresh';
|
@@ -1,12 +1,29 @@
|
|
1
|
-
@import url(@rspress/theme-default/bundle.css);
|
2
|
-
|
3
1
|
:root {
|
4
|
-
--rp-iframe-bg:
|
2
|
+
--rp-iframe-bg: #f6f8fa;
|
3
|
+
--rp-iframe-nav-bg: #ffffff;
|
5
4
|
}
|
6
5
|
|
6
|
+
/* TODO: support dark mode */
|
7
|
+
/* .dark {
|
8
|
+
--rp-iframe-bg: #242424;
|
9
|
+
--rp-iframe-nav-bg: #191d24;
|
10
|
+
} */
|
11
|
+
|
7
12
|
body {
|
8
|
-
background-color: var(--rp-iframe-bg);
|
13
|
+
background-color: var(--rp-iframe-bg) !important;
|
14
|
+
margin: 0px; /* copied from preflight.css */
|
15
|
+
}
|
16
|
+
|
17
|
+
/* #region copied from preflight.css */
|
18
|
+
*,
|
19
|
+
::before,
|
20
|
+
::after {
|
21
|
+
box-sizing: border-box; /* 1 */
|
22
|
+
border-width: 0; /* 2 */
|
23
|
+
border-style: solid; /* 2 */
|
24
|
+
border-color: theme('borderColor.DEFAULT', currentColor); /* 2 */
|
9
25
|
}
|
26
|
+
/* #endregion copied from preflight.css */
|
10
27
|
|
11
28
|
.preview-nav {
|
12
29
|
position: relative;
|
@@ -14,7 +31,7 @@ body {
|
|
14
31
|
align-items: center;
|
15
32
|
justify-content: center;
|
16
33
|
height: 56px;
|
17
|
-
background-color: var(--rp-
|
34
|
+
background-color: var(--rp-iframe-nav-bg) !important;
|
18
35
|
font-weight: 600;
|
19
36
|
font-size: 17px;
|
20
37
|
}
|