@rspress/plugin-preview 0.0.0-nightly-20231009160300 → 0.0.0-nightly-20231011160512
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/index.d.ts +6 -0
- package/dist/index.js +33 -11
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
- package/static/global-components/Container.scss +3 -3
- package/static/global-components/Container.tsx +77 -5
- package/static/global-components/common/mobile-operation.tsx +11 -2
- package/static/global-components/icons/Codesandbox.tsx +22 -0
package/dist/index.d.ts
CHANGED
@@ -12,6 +12,12 @@ type Options = {
|
|
12
12
|
* @default 'follow'
|
13
13
|
*/
|
14
14
|
iframePosition: 'fixed' | 'follow';
|
15
|
+
/**
|
16
|
+
* whether the demo can open in codesandbox
|
17
|
+
* @default false
|
18
|
+
* @experimental
|
19
|
+
*/
|
20
|
+
enableCodesandbox: boolean;
|
15
21
|
};
|
16
22
|
declare let routeMeta: RouteMeta[];
|
17
23
|
declare const demoRoutes: {
|
package/dist/index.js
CHANGED
@@ -266,7 +266,7 @@ __export(src_exports, {
|
|
266
266
|
routeMeta: () => routeMeta
|
267
267
|
});
|
268
268
|
module.exports = __toCommonJS(src_exports);
|
269
|
-
var import_path3 =
|
269
|
+
var import_path3 = require("path");
|
270
270
|
var import_rspack_plugin_virtual_module = require("rspack-plugin-virtual-module");
|
271
271
|
var import_shared2 = require("@rspress/shared");
|
272
272
|
|
@@ -287,9 +287,9 @@ var toValidVarName = (str) => {
|
|
287
287
|
var generateId = (pageName, index) => {
|
288
288
|
return `_${toValidVarName(pageName)}_${index}`;
|
289
289
|
};
|
290
|
-
var injectDemoBlockImport = (str,
|
290
|
+
var injectDemoBlockImport = (str, path2) => {
|
291
291
|
return `
|
292
|
-
import DemoBlock from '${
|
292
|
+
import DemoBlock from '${path2}';
|
293
293
|
${str}
|
294
294
|
`;
|
295
295
|
};
|
@@ -310,7 +310,10 @@ var demoBlockComponentPath = import_path.default.join(
|
|
310
310
|
);
|
311
311
|
|
312
312
|
// src/codeToDemo.ts
|
313
|
-
var
|
313
|
+
var json = JSON.parse(
|
314
|
+
import_fs_extra.default.readFileSync((0, import_path2.resolve)(process.cwd(), "./package.json"), "utf8")
|
315
|
+
);
|
316
|
+
var remarkCodeToDemo = ({ isMobile, getRouteMeta, iframePosition, enableCodesandbox }) => {
|
314
317
|
const routeMeta2 = getRouteMeta();
|
315
318
|
return (tree, vfile) => {
|
316
319
|
const demos = [];
|
@@ -352,6 +355,21 @@ var remarkCodeToDemo = ({ isMobile, getRouteMeta, iframePosition }) => {
|
|
352
355
|
type: "mdxJsxAttribute",
|
353
356
|
name: "url",
|
354
357
|
value: demoRoute
|
358
|
+
},
|
359
|
+
{
|
360
|
+
type: "mdxJsxAttribute",
|
361
|
+
name: "content",
|
362
|
+
value: currentNode.value
|
363
|
+
},
|
364
|
+
{
|
365
|
+
type: "mdxJsxAttribute",
|
366
|
+
name: "packageName",
|
367
|
+
value: json.name
|
368
|
+
},
|
369
|
+
{
|
370
|
+
type: "mdxJsxAttribute",
|
371
|
+
name: "enableCodesandbox",
|
372
|
+
value: enableCodesandbox
|
355
373
|
}
|
356
374
|
],
|
357
375
|
children: [
|
@@ -490,8 +508,9 @@ var demoMeta = {};
|
|
490
508
|
function pluginPreview(options) {
|
491
509
|
const isMobile = options?.isMobile ?? false;
|
492
510
|
const iframePosition = options?.iframePosition ?? "follow";
|
511
|
+
const enableCodesandbox = options?.enableCodesandbox ?? false;
|
493
512
|
const demoRuntimeModule = new import_rspack_plugin_virtual_module.RspackVirtualModulePlugin({});
|
494
|
-
const globalUIComponents = iframePosition === "fixed" ? [import_path3.
|
513
|
+
const globalUIComponents = iframePosition === "fixed" ? [(0, import_path3.join)(staticPath, "global-components", "Device.tsx")] : [];
|
495
514
|
const getRouteMeta = () => routeMeta;
|
496
515
|
return {
|
497
516
|
name: "@rspress/plugin-preview",
|
@@ -523,10 +542,10 @@ import Demo from ${JSON.stringify(demoComponentPath)}
|
|
523
542
|
const { visit: visit2 } = await Promise.resolve().then(() => (init_unist_util_visit(), unist_util_visit_exports));
|
524
543
|
const { default: fs2 } = await import("@modern-js/utils/fs-extra");
|
525
544
|
const { default: remarkGFM } = await import("remark-gfm");
|
526
|
-
let title = import_path3.
|
545
|
+
let title = (0, import_path3.parse)(filepath).name;
|
527
546
|
try {
|
528
547
|
const processor = createProcessor({
|
529
|
-
format: import_path3.
|
548
|
+
format: (0, import_path3.extname)(filepath).slice(1),
|
530
549
|
remarkPlugins: [remarkGFM]
|
531
550
|
});
|
532
551
|
const source = await fs2.readFile(filepath, "utf-8");
|
@@ -621,21 +640,24 @@ import Demo from ${JSON.stringify(demoComponentPath)}
|
|
621
640
|
plugins: [demoRuntimeModule]
|
622
641
|
},
|
623
642
|
bundlerChain(chain) {
|
624
|
-
chain.module.rule("Raw").resourceQuery(/raw/).type("asset/source").end().rule("MDX").oneOf("MDXMeta").before("MDXCompile").resourceQuery(/meta/).use("mdx-meta-loader").loader(import_path3.
|
643
|
+
chain.module.rule("Raw").resourceQuery(/raw/).type("asset/source").end().rule("MDX").oneOf("MDXMeta").before("MDXCompile").resourceQuery(/meta/).use("mdx-meta-loader").loader((0, import_path3.join)(__dirname, "../mdx-meta-loader.cjs")).end();
|
625
644
|
chain.resolve.extensions.prepend(".md").prepend(".mdx");
|
626
645
|
}
|
627
646
|
}
|
628
647
|
},
|
629
648
|
markdown: {
|
630
649
|
remarkPlugins: [
|
631
|
-
[
|
650
|
+
[
|
651
|
+
remarkCodeToDemo,
|
652
|
+
{ isMobile, getRouteMeta, iframePosition, enableCodesandbox }
|
653
|
+
]
|
632
654
|
],
|
633
655
|
globalComponents: [
|
634
|
-
import_path3.
|
656
|
+
(0, import_path3.join)(staticPath, "global-components", "Container.tsx")
|
635
657
|
]
|
636
658
|
},
|
637
659
|
globalUIComponents,
|
638
|
-
globalStyles: import_path3.
|
660
|
+
globalStyles: (0, import_path3.join)(
|
639
661
|
staticPath,
|
640
662
|
"global-styles",
|
641
663
|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;AAAA,IACA,UAAU;AAAA,MACR,eAAe;AAAA,QACb,CAAC,kBAAkB,EAAE,UAAU,cAAc,eAAe,CAAC;AAAA,MAC/D;AAAA,MACA,kBAAkB;AAAA,QAChB,qBAAK,KAAK,YAAY,qBAAqB,eAAe;AAAA,MAC5D;AAAA,IACF;AAAA,IACA;AAAA,IACA,cAAc,qBAAK;AAAA,MACjB;AAAA,MACA;AAAA,MACA,GAAG,WAAW,WAAW;AAAA,IAC3B;AAAA,IACA,eAAe;AACb,aAAO;AAAA,IACT;AAAA,EACF;AACF;","names":["index","is","visit","path","routeMeta","externalDemoIndex","fs","pageName"],"sources":["../../../node_modules/.pnpm/unist-util-is@5.2.1/node_modules/unist-util-is/lib/index.js","../../../node_modules/.pnpm/unist-util-is@5.2.1/node_modules/unist-util-is/index.js","../../../node_modules/.pnpm/unist-util-visit-parents@5.1.3/node_modules/unist-util-visit-parents/lib/color.js","../../../node_modules/.pnpm/unist-util-visit-parents@5.1.3/node_modules/unist-util-visit-parents/lib/index.js","../../../node_modules/.pnpm/unist-util-visit-parents@5.1.3/node_modules/unist-util-visit-parents/index.js","../../../node_modules/.pnpm/unist-util-visit@4.1.1/node_modules/unist-util-visit/index.js","../src/index.ts","../src/codeToDemo.ts","../src/utils.ts","../src/constant.ts"],"sourcesContent":["/**\n * @typedef {import('unist').Node} Node\n * @typedef {import('unist').Parent} Parent\n */\n\n/**\n * @typedef {Record<string, unknown>} Props\n * @typedef {null | undefined | string | Props | TestFunctionAnything | Array<string | Props | TestFunctionAnything>} Test\n * Check for an arbitrary node, unaware of TypeScript inferral.\n *\n * @callback TestFunctionAnything\n * Check if a node passes a test, unaware of TypeScript inferral.\n * @param {unknown} this\n * The given context.\n * @param {Node} node\n * A node.\n * @param {number | null | undefined} [index]\n * The node’s position in its parent.\n * @param {Parent | null | undefined} [parent]\n * The node’s parent.\n * @returns {boolean | void}\n * Whether this node passes the test.\n */\n\n/**\n * @template {Node} Kind\n * Node type.\n * @typedef {Kind['type'] | Partial<Kind> | TestFunctionPredicate<Kind> | Array<Kind['type'] | Partial<Kind> | TestFunctionPredicate<Kind>>} PredicateTest\n * Check for a node that can be inferred by TypeScript.\n */\n\n/**\n * Check if a node passes a certain test.\n *\n * @template {Node} Kind\n * Node type.\n * @callback TestFunctionPredicate\n * Complex test function for a node that can be inferred by TypeScript.\n * @param {Node} node\n * A node.\n * @param {number | null | undefined} [index]\n * The node’s position in its parent.\n * @param {Parent | null | undefined} [parent]\n * The node’s parent.\n * @returns {node is Kind}\n * Whether this node passes the test.\n */\n\n/**\n * @callback AssertAnything\n * Check that an arbitrary value is a node, unaware of TypeScript inferral.\n * @param {unknown} [node]\n * Anything (typically a node).\n * @param {number | null | undefined} [index]\n * The node’s position in its parent.\n * @param {Parent | null | undefined} [parent]\n * The node’s parent.\n * @returns {boolean}\n * Whether this is a node and passes a test.\n */\n\n/**\n * Check if a node is a node and passes a certain node test.\n *\n * @template {Node} Kind\n * Node type.\n * @callback AssertPredicate\n * Check that an arbitrary value is a specific node, aware of TypeScript.\n * @param {unknown} [node]\n * Anything (typically a node).\n * @param {number | null | undefined} [index]\n * The node’s position in its parent.\n * @param {Parent | null | undefined} [parent]\n * The node’s parent.\n * @returns {node is Kind}\n * Whether this is a node and passes a test.\n */\n\n/**\n * Check if `node` is a `Node` and whether it passes the given test.\n *\n * @param node\n * Thing to check, typically `Node`.\n * @param test\n * A check for a specific node.\n * @param index\n * The node’s position in its parent.\n * @param parent\n * The node’s parent.\n * @returns\n * Whether `node` is a node and passes a test.\n */\nexport const is =\n /**\n * @type {(\n * (() => false) &\n * (<Kind extends Node = Node>(node: unknown, test: PredicateTest<Kind>, index: number, parent: Parent, context?: unknown) => node is Kind) &\n * (<Kind extends Node = Node>(node: unknown, test: PredicateTest<Kind>, index?: null | undefined, parent?: null | undefined, context?: unknown) => node is Kind) &\n * ((node: unknown, test: Test, index: number, parent: Parent, context?: unknown) => boolean) &\n * ((node: unknown, test?: Test, index?: null | undefined, parent?: null | undefined, context?: unknown) => boolean)\n * )}\n */\n (\n /**\n * @param {unknown} [node]\n * @param {Test} [test]\n * @param {number | null | undefined} [index]\n * @param {Parent | null | undefined} [parent]\n * @param {unknown} [context]\n * @returns {boolean}\n */\n // eslint-disable-next-line max-params\n function is(node, test, index, parent, context) {\n const check = convert(test)\n\n if (\n index !== undefined &&\n index !== null &&\n (typeof index !== 'number' ||\n index < 0 ||\n index === Number.POSITIVE_INFINITY)\n ) {\n throw new Error('Expected positive finite index')\n }\n\n if (\n parent !== undefined &&\n parent !== null &&\n (!is(parent) || !parent.children)\n ) {\n throw new Error('Expected parent node')\n }\n\n if (\n (parent === undefined || parent === null) !==\n (index === undefined || index === null)\n ) {\n throw new Error('Expected both parent and index')\n }\n\n // @ts-expect-error Looks like a node.\n return node && node.type && typeof node.type === 'string'\n ? Boolean(check.call(context, node, index, parent))\n : false\n }\n )\n\n/**\n * Generate an assertion from a test.\n *\n * Useful if you’re going to test many nodes, for example when creating a\n * utility where something else passes a compatible test.\n *\n * The created function is a bit faster because it expects valid input only:\n * a `node`, `index`, and `parent`.\n *\n * @param test\n * * when nullish, checks if `node` is a `Node`.\n * * when `string`, works like passing `(node) => node.type === test`.\n * * when `function` checks if function passed the node is true.\n * * when `object`, checks that all keys in test are in node, and that they have (strictly) equal values.\n * * when `array`, checks if any one of the subtests pass.\n * @returns\n * An assertion.\n */\nexport const convert =\n /**\n * @type {(\n * (<Kind extends Node>(test: PredicateTest<Kind>) => AssertPredicate<Kind>) &\n * ((test?: Test) => AssertAnything)\n * )}\n */\n (\n /**\n * @param {Test} [test]\n * @returns {AssertAnything}\n */\n function (test) {\n if (test === undefined || test === null) {\n return ok\n }\n\n if (typeof test === 'string') {\n return typeFactory(test)\n }\n\n if (typeof test === 'object') {\n return Array.isArray(test) ? anyFactory(test) : propsFactory(test)\n }\n\n if (typeof test === 'function') {\n return castFactory(test)\n }\n\n throw new Error('Expected function, string, or object as test')\n }\n )\n\n/**\n * @param {Array<string | Props | TestFunctionAnything>} tests\n * @returns {AssertAnything}\n */\nfunction anyFactory(tests) {\n /** @type {Array<AssertAnything>} */\n const checks = []\n let index = -1\n\n while (++index < tests.length) {\n checks[index] = convert(tests[index])\n }\n\n return castFactory(any)\n\n /**\n * @this {unknown}\n * @param {Array<unknown>} parameters\n * @returns {boolean}\n */\n function any(...parameters) {\n let index = -1\n\n while (++index < checks.length) {\n if (checks[index].call(this, ...parameters)) return true\n }\n\n return false\n }\n}\n\n/**\n * Turn an object into a test for a node with a certain fields.\n *\n * @param {Props} check\n * @returns {AssertAnything}\n */\nfunction propsFactory(check) {\n return castFactory(all)\n\n /**\n * @param {Node} node\n * @returns {boolean}\n */\n function all(node) {\n /** @type {string} */\n let key\n\n for (key in check) {\n // @ts-expect-error: hush, it sure works as an index.\n if (node[key] !== check[key]) return false\n }\n\n return true\n }\n}\n\n/**\n * Turn a string into a test for a node with a certain type.\n *\n * @param {string} check\n * @returns {AssertAnything}\n */\nfunction typeFactory(check) {\n return castFactory(type)\n\n /**\n * @param {Node} node\n */\n function type(node) {\n return node && node.type === check\n }\n}\n\n/**\n * Turn a custom test into a test for a node that passes that test.\n *\n * @param {TestFunctionAnything} check\n * @returns {AssertAnything}\n */\nfunction castFactory(check) {\n return assertion\n\n /**\n * @this {unknown}\n * @param {unknown} node\n * @param {Array<unknown>} parameters\n * @returns {boolean}\n */\n function assertion(node, ...parameters) {\n return Boolean(\n node &&\n typeof node === 'object' &&\n 'type' in node &&\n // @ts-expect-error: fine.\n Boolean(check.call(this, node, ...parameters))\n )\n }\n}\n\nfunction ok() {\n return true\n}\n","/**\n * @typedef {import('./lib/index.js').Test} Test\n * @typedef {import('./lib/index.js').TestFunctionAnything} TestFunctionAnything\n * @typedef {import('./lib/index.js').AssertAnything} AssertAnything\n */\n\n/**\n * @template {import('unist').Node} Kind\n * @typedef {import('./lib/index.js').PredicateTest<Kind>} PredicateTest\n */\n\n/**\n * @template {import('unist').Node} Kind\n * @typedef {import('./lib/index.js').TestFunctionPredicate<Kind>} TestFunctionPredicate\n */\n\n/**\n * @template {import('unist').Node} Kind\n * @typedef {import('./lib/index.js').AssertPredicate<Kind>} AssertPredicate\n */\n\nexport {is, convert} from './lib/index.js'\n","/**\n * @param {string} d\n * @returns {string}\n */\nexport function color(d) {\n return '\\u001B[33m' + d + '\\u001B[39m'\n}\n","/**\n * @typedef {import('unist').Node} Node\n * @typedef {import('unist').Parent} Parent\n * @typedef {import('unist-util-is').Test} Test\n */\n\n/**\n * @typedef {boolean | 'skip'} Action\n * Union of the action types.\n *\n * @typedef {number} Index\n * Move to the sibling at `index` next (after node itself is completely\n * traversed).\n *\n * Useful if mutating the tree, such as removing the node the visitor is\n * currently on, or any of its previous siblings.\n * Results less than 0 or greater than or equal to `children.length` stop\n * traversing the parent.\n *\n * @typedef {[(Action | null | undefined | void)?, (Index | null | undefined)?]} ActionTuple\n * List with one or two values, the first an action, the second an index.\n *\n * @typedef {Action | ActionTuple | Index | null | undefined | void} VisitorResult\n * Any value that can be returned from a visitor.\n */\n\n/**\n * @template {Node} [Visited=Node]\n * Visited node type.\n * @template {Parent} [Ancestor=Parent]\n * Ancestor type.\n * @callback Visitor\n * Handle a node (matching `test`, if given).\n *\n * Visitors are free to transform `node`.\n * They can also transform the parent of node (the last of `ancestors`).\n *\n * Replacing `node` itself, if `SKIP` is not returned, still causes its\n * descendants to be walked (which is a bug).\n *\n * When adding or removing previous siblings of `node` (or next siblings, in\n * case of reverse), the `Visitor` should return a new `Index` to specify the\n * sibling to traverse after `node` is traversed.\n * Adding or removing next siblings of `node` (or previous siblings, in case\n * of reverse) is handled as expected without needing to return a new `Index`.\n *\n * Removing the children property of an ancestor still results in them being\n * traversed.\n * @param {Visited} node\n * Found node.\n * @param {Array<Ancestor>} ancestors\n * Ancestors of `node`.\n * @returns {VisitorResult}\n * What to do next.\n *\n * An `Index` is treated as a tuple of `[CONTINUE, Index]`.\n * An `Action` is treated as a tuple of `[Action]`.\n *\n * Passing a tuple back only makes sense if the `Action` is `SKIP`.\n * When the `Action` is `EXIT`, that action can be returned.\n * When the `Action` is `CONTINUE`, `Index` can be returned.\n */\n\n/**\n * @template {Node} [Tree=Node]\n * Tree type.\n * @template {Test} [Check=string]\n * Test type.\n * @typedef {Visitor<import('./complex-types.js').Matches<import('./complex-types.js').InclusiveDescendant<Tree>, Check>, Extract<import('./complex-types.js').InclusiveDescendant<Tree>, Parent>>} BuildVisitor\n * Build a typed `Visitor` function from a tree and a test.\n *\n * It will infer which values are passed as `node` and which as `parents`.\n */\n\nimport {convert} from 'unist-util-is'\nimport {color} from './color.js'\n\n/**\n * Continue traversing as normal.\n */\nexport const CONTINUE = true\n\n/**\n * Stop traversing immediately.\n */\nexport const EXIT = false\n\n/**\n * Do not traverse this node’s children.\n */\nexport const SKIP = 'skip'\n\n/**\n * Visit nodes, with ancestral information.\n *\n * This algorithm performs *depth-first* *tree traversal* in *preorder*\n * (**NLR**) or if `reverse` is given, in *reverse preorder* (**NRL**).\n *\n * You can choose for which nodes `visitor` is called by passing a `test`.\n * For complex tests, you should test yourself in `visitor`, as it will be\n * faster and will have improved type information.\n *\n * Walking the tree is an intensive task.\n * Make use of the return values of the visitor when possible.\n * Instead of walking a tree multiple times, walk it once, use `unist-util-is`\n * to check if a node matches, and then perform different operations.\n *\n * You can change the tree.\n * See `Visitor` for more info.\n *\n * @param tree\n * Tree to traverse.\n * @param test\n * `unist-util-is`-compatible test\n * @param visitor\n * Handle each node.\n * @param reverse\n * Traverse in reverse preorder (NRL) instead of the default preorder (NLR).\n * @returns\n * Nothing.\n */\nexport const visitParents =\n /**\n * @type {(\n * (<Tree extends Node, Check extends Test>(tree: Tree, test: Check, visitor: BuildVisitor<Tree, Check>, reverse?: boolean | null | undefined) => void) &\n * (<Tree extends Node>(tree: Tree, visitor: BuildVisitor<Tree>, reverse?: boolean | null | undefined) => void)\n * )}\n */\n (\n /**\n * @param {Node} tree\n * @param {Test} test\n * @param {Visitor<Node>} visitor\n * @param {boolean | null | undefined} [reverse]\n * @returns {void}\n */\n function (tree, test, visitor, reverse) {\n if (typeof test === 'function' && typeof visitor !== 'function') {\n reverse = visitor\n // @ts-expect-error no visitor given, so `visitor` is test.\n visitor = test\n test = null\n }\n\n const is = convert(test)\n const step = reverse ? -1 : 1\n\n factory(tree, undefined, [])()\n\n /**\n * @param {Node} node\n * @param {number | undefined} index\n * @param {Array<Parent>} parents\n */\n function factory(node, index, parents) {\n /** @type {Record<string, unknown>} */\n // @ts-expect-error: hush\n const value = node && typeof node === 'object' ? node : {}\n\n if (typeof value.type === 'string') {\n const name =\n // `hast`\n typeof value.tagName === 'string'\n ? value.tagName\n : // `xast`\n typeof value.name === 'string'\n ? value.name\n : undefined\n\n Object.defineProperty(visit, 'name', {\n value:\n 'node (' + color(node.type + (name ? '<' + name + '>' : '')) + ')'\n })\n }\n\n return visit\n\n function visit() {\n /** @type {ActionTuple} */\n let result = []\n /** @type {ActionTuple} */\n let subresult\n /** @type {number} */\n let offset\n /** @type {Array<Parent>} */\n let grandparents\n\n if (!test || is(node, index, parents[parents.length - 1] || null)) {\n result = toResult(visitor(node, parents))\n\n if (result[0] === EXIT) {\n return result\n }\n }\n\n // @ts-expect-error looks like a parent.\n if (node.children && result[0] !== SKIP) {\n // @ts-expect-error looks like a parent.\n offset = (reverse ? node.children.length : -1) + step\n // @ts-expect-error looks like a parent.\n grandparents = parents.concat(node)\n\n // @ts-expect-error looks like a parent.\n while (offset > -1 && offset < node.children.length) {\n // @ts-expect-error looks like a parent.\n subresult = factory(node.children[offset], offset, grandparents)()\n\n if (subresult[0] === EXIT) {\n return subresult\n }\n\n offset =\n typeof subresult[1] === 'number' ? subresult[1] : offset + step\n }\n }\n\n return result\n }\n }\n }\n )\n\n/**\n * Turn a return value into a clean result.\n *\n * @param {VisitorResult} value\n * Valid return values from visitors.\n * @returns {ActionTuple}\n * Clean result.\n */\nfunction toResult(value) {\n if (Array.isArray(value)) {\n return value\n }\n\n if (typeof value === 'number') {\n return [CONTINUE, value]\n }\n\n return [value]\n}\n","// Note: types exported from `index.d.ts`\nexport {CONTINUE, EXIT, SKIP, visitParents} from './lib/index.js'\n","/**\n * @typedef {import('unist').Node} Node\n * @typedef {import('unist').Parent} Parent\n * @typedef {import('unist-util-is').Test} Test\n * @typedef {import('unist-util-visit-parents').VisitorResult} VisitorResult\n * @typedef {import('./complex-types.js').Visitor} Visitor\n */\n\nimport {visitParents} from 'unist-util-visit-parents'\n\n/**\n * Visit children of tree which pass test.\n *\n * @param tree\n * Tree to walk\n * @param [test]\n * `unist-util-is`-compatible test\n * @param visitor\n * Function called for nodes that pass `test`.\n * @param reverse\n * Traverse in reverse preorder (NRL) instead of preorder (NLR) (default).\n */\nexport const visit =\n /**\n * @type {(\n * (<Tree extends Node, Check extends Test>(tree: Tree, test: Check, visitor: import('./complex-types.js').BuildVisitor<Tree, Check>, reverse?: boolean) => void) &\n * (<Tree extends Node>(tree: Tree, visitor: import('./complex-types.js').BuildVisitor<Tree>, reverse?: boolean) => void)\n * )}\n */\n (\n /**\n * @param {Node} tree\n * @param {Test} test\n * @param {import('./complex-types.js').Visitor} visitor\n * @param {boolean} [reverse]\n */\n function (tree, test, visitor, reverse) {\n if (typeof test === 'function' && typeof visitor !== 'function') {\n reverse = visitor\n visitor = test\n test = null\n }\n\n visitParents(tree, test, overload, reverse)\n\n /**\n * @param {Node} node\n * @param {Array<Parent>} parents\n */\n function overload(node, parents) {\n const parent = parents[parents.length - 1]\n return visitor(\n node,\n parent ? parent.children.indexOf(node) : null,\n parent\n )\n }\n }\n )\n\nexport {CONTINUE, EXIT, SKIP} from 'unist-util-visit-parents'\n","import path, { join } from 'path';\nimport { RspackVirtualModulePlugin } from 'rspack-plugin-virtual-module';\nimport {\n type RspressPlugin,\n type RouteMeta,\n RSPRESS_TEMP_DIR,\n normalizePosixPath,\n} from '@rspress/shared';\nimport { remarkCodeToDemo } from './codeToDemo';\nimport { generateId, injectDemoBlockImport } from './utils';\nimport {\n demoBlockComponentPath,\n demoComponentPath,\n staticPath,\n} from './constant';\n\nexport type Options = {\n /**\n * preview in mobile mode or not\n * when isMobile is true, 1. aside will hide. 2. default preview component by iframe\n * @default false\n */\n isMobile: boolean;\n /**\n * position of the iframe\n * @default 'follow'\n */\n iframePosition: 'fixed' | 'follow';\n};\n\ninterface Heading {\n type: string;\n depth?: number;\n children?: ChildNode[];\n}\n\ninterface ChildNode {\n type: 'link' | 'text' | 'inlineCode';\n value: string;\n children?: ChildNode[];\n}\n\n// eslint-disable-next-line import/no-mutable-exports\nexport let routeMeta: RouteMeta[];\nexport const demoRoutes: { path: string }[] = [];\nexport const demoMeta: Record<\n string,\n { id: string; virtualModulePath: string; title: string }[]\n> = {};\n\n/**\n * The plugin is used to preview component.\n */\nexport function pluginPreview(options?: Options): RspressPlugin {\n const isMobile = options?.isMobile ?? false;\n const iframePosition = options?.iframePosition ?? 'follow';\n const demoRuntimeModule = new RspackVirtualModulePlugin({});\n const globalUIComponents =\n iframePosition === 'fixed'\n ? [path.join(staticPath, 'global-components', 'Device.tsx')]\n : [];\n const getRouteMeta = () => routeMeta;\n return {\n name: '@rspress/plugin-preview',\n addPages(_config, _isProd) {\n return [\n {\n routePath: '/~demo/:id',\n content: `---\npageType: \"blank\"\n---\n\nimport Demo from ${JSON.stringify(demoComponentPath)}\n\n<Demo iframePosition=\"${iframePosition}\"/>\n `,\n },\n ];\n },\n async routeGenerated(routes: RouteMeta[]) {\n // init routeMeta\n routeMeta = routes;\n\n const files = routes.map(route => route.absolutePath);\n // Write the demo code ahead of time\n // Fix: rspack build error because demo file is not exist, probably the demo file was written in rspack build process?\n await Promise.all(\n files.map(async (filepath, _index) => {\n const isMdxFile = /\\.mdx?$/.test(filepath);\n if (!isMdxFile) {\n return;\n }\n const { createProcessor } = await import('@mdx-js/mdx');\n const { visit } = await import('unist-util-visit');\n const { default: fs } = await import('@modern-js/utils/fs-extra');\n const { default: remarkGFM } = await import('remark-gfm');\n let title = path.parse(filepath).name;\n try {\n const processor = createProcessor({\n format: path.extname(filepath).slice(1) as 'mdx' | 'md',\n remarkPlugins: [remarkGFM],\n });\n const source = await fs.readFile(filepath, 'utf-8');\n const ast = processor.parse(source);\n let index = 1;\n const { pageName } = routeMeta.find(\n meta =>\n normalizePosixPath(meta.absolutePath) ===\n normalizePosixPath(filepath),\n )!;\n\n const registerDemo = (\n demoId: string,\n demoPath: string,\n isMobileMode: boolean,\n ) => {\n if (isMobileMode) {\n // only add demoMeta in mobile mode\n demoMeta[filepath] = demoMeta[filepath] ?? [];\n const isExist = demoMeta[filepath].find(\n item => item.id === demoId,\n );\n if (!isExist) {\n demoMeta[filepath].push({\n id: demoId,\n title,\n virtualModulePath: demoPath,\n });\n }\n }\n };\n visit(ast, 'heading', (node: Heading) => {\n if (node.depth === 1) {\n if (node.children) {\n title = node.children[0].value;\n }\n }\n });\n\n visit(ast, 'mdxJsxFlowElement', (node: any) => {\n if (node.name === 'code') {\n const src = node.attributes.find(\n (attr: { name: string; value: string }) =>\n attr.name === 'src',\n )?.value;\n const isMobileMode =\n node.attributes.find(\n (attr: { name: string; value: boolean }) =>\n attr.name === 'isMobile',\n )?.value ?? isMobile;\n if (!src) {\n return;\n }\n const id = generateId(pageName, index++);\n registerDemo(id, src, isMobileMode);\n }\n });\n\n visit(ast, 'code', (node: any) => {\n if (node.lang === 'jsx' || node.lang === 'tsx') {\n const { value } = node;\n const isPure = node?.meta?.includes('pure');\n\n // do not anything for pure mode\n if (isPure) {\n return;\n }\n\n // every code block can change their preview mode by meta\n const isMobileMode =\n node?.meta?.includes('mobile') ||\n (!node?.meta?.includes('web') && isMobile);\n\n const { pageName } = routeMeta.find(\n meta =>\n normalizePosixPath(meta.absolutePath) ===\n normalizePosixPath(filepath),\n )!;\n const id = generateId(pageName, index++);\n\n const demoDir = join(\n process.cwd(),\n 'node_modules',\n RSPRESS_TEMP_DIR,\n `virtual-demo`,\n );\n\n const virtualModulePath = join(demoDir, `${id}.tsx`);\n registerDemo(id, virtualModulePath, isMobileMode);\n\n fs.ensureDirSync(join(demoDir));\n fs.writeFileSync(\n virtualModulePath,\n injectDemoBlockImport(value, demoBlockComponentPath),\n );\n }\n });\n } catch (e) {\n console.error(e);\n throw e;\n }\n }),\n );\n /**\n * expect the meta of each demo file is like this:\n * {\n * id,\n * component,\n * }\n */\n const virtualMeta = `\n ${files\n .map((filepath, index) => {\n return `import Route${index} from '${filepath}?meta';`;\n })\n .join('\\n')}\n export const demos = [${files\n .map((_, index) => `Route${index}`)\n .flat()\n .join(',')}];\n `;\n demoRuntimeModule.writeModule('virtual-meta', virtualMeta);\n },\n builderConfig: {\n tools: {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment, @typescript-eslint/prefer-ts-expect-error\n // @ts-ignore\n rspack: {\n plugins: [demoRuntimeModule],\n },\n bundlerChain(chain) {\n chain.module\n .rule('Raw')\n .resourceQuery(/raw/)\n .type('asset/source')\n .end()\n .rule('MDX')\n .oneOf('MDXMeta')\n .before('MDXCompile')\n .resourceQuery(/meta/)\n .use('mdx-meta-loader')\n .loader(path.join(__dirname, '../mdx-meta-loader.cjs'))\n .end();\n\n chain.resolve.extensions.prepend('.md').prepend('.mdx');\n },\n },\n },\n markdown: {\n remarkPlugins: [\n [remarkCodeToDemo, { isMobile, getRouteMeta, iframePosition }],\n ],\n globalComponents: [\n path.join(staticPath, 'global-components', 'Container.tsx'),\n ],\n },\n globalUIComponents,\n globalStyles: path.join(\n staticPath,\n 'global-styles',\n `${isMobile ? 'mobile' : 'web'}.css`,\n ),\n addSSGRoutes() {\n return demoRoutes;\n },\n };\n}\n","import { join } from 'path';\nimport { visit } from 'unist-util-visit';\nimport fs from '@modern-js/utils/fs-extra';\nimport {\n RSPRESS_TEMP_DIR,\n normalizePosixPath,\n type RouteMeta,\n} from '@rspress/shared';\nimport type { Plugin } from 'unified';\nimport type { Root } from 'mdast';\nimport type { MdxjsEsm } from 'mdast-util-mdxjs-esm';\nimport { injectDemoBlockImport, generateId } from './utils';\nimport { demoBlockComponentPath } from './constant';\nimport { demoRoutes } from '.';\n\n/**\n * remark plugin to transform code to demo\n */\nexport const remarkCodeToDemo: Plugin<\n [\n {\n isMobile: boolean;\n getRouteMeta: () => RouteMeta[];\n iframePosition: 'fixed' | 'follow';\n },\n ],\n Root\n> = ({ isMobile, getRouteMeta, iframePosition }) => {\n const routeMeta = getRouteMeta();\n\n return (tree, vfile) => {\n const demos: MdxjsEsm[] = [];\n const route = routeMeta.find(\n meta =>\n normalizePosixPath(meta.absolutePath) ===\n normalizePosixPath(vfile.path || vfile.history[0]),\n );\n if (!route) {\n return;\n }\n const { pageName } = route;\n let index = 1;\n let externalDemoIndex = 0;\n\n function constructDemoNode(\n demoId: string,\n demoPath: string,\n currentNode: any,\n isMobileMode: boolean,\n // Only for external demo\n externalDemoIndex?: number,\n ) {\n const demoRoute = `/~demo/${demoId}`;\n if (isMobileMode) {\n // only add demoRoutes in mobile mode\n demoRoutes.push({\n path: demoRoute,\n });\n } else {\n demos.push(getASTNodeImport(`Demo${demoId}`, demoPath));\n }\n\n // get external demo content\n const tempVar = `externalDemoContent${externalDemoIndex}`;\n if (externalDemoIndex !== undefined) {\n demos.push(getASTNodeImport(tempVar, `${demoPath}?raw`));\n }\n\n if (isMobileMode && iframePosition === 'fixed') {\n // Only show the code block\n externalDemoIndex !== undefined &&\n Object.assign(currentNode, getExternalDemoContent(tempVar));\n } else {\n // Use container to show the code and view\n Object.assign(currentNode, {\n type: 'mdxJsxFlowElement',\n name: 'Container',\n attributes: [\n {\n type: 'mdxJsxAttribute',\n name: 'isMobile',\n value: isMobileMode,\n },\n {\n type: 'mdxJsxAttribute',\n name: 'url',\n value: demoRoute,\n },\n ],\n children: [\n externalDemoIndex === undefined\n ? {\n ...currentNode,\n hasVisited: true,\n }\n : getExternalDemoContent(tempVar),\n isMobileMode\n ? {\n type: 'mdxJsxFlowElement',\n name: null,\n }\n : {\n type: 'mdxJsxFlowElement',\n name: `Demo${demoId}`,\n },\n ],\n });\n }\n }\n\n // 1. External demo , use <code src=\"xxx\" /> to declare demo\n tree.children.forEach((node: any) => {\n if (node.type === 'mdxJsxFlowElement' && node.name === 'code') {\n const src = node.attributes.find(\n (attr: { name: string; value: string }) => attr.name === 'src',\n )?.value;\n const isMobileMode =\n node.attributes.find(\n (attr: { name: string; value: boolean }) =>\n attr.name === 'isMobile',\n )?.value ?? isMobile;\n if (!src) {\n return;\n }\n const id = generateId(pageName, index++);\n constructDemoNode(id, src, node, isMobileMode, externalDemoIndex++);\n }\n });\n\n // 2. Internal demo, use ```j/tsx to declare demo\n visit(tree, 'code', node => {\n // hasVisited is a custom property\n if ('hasVisited' in node) {\n return;\n }\n\n if (node.lang === 'jsx' || node.lang === 'tsx') {\n const value = injectDemoBlockImport(node.value, demoBlockComponentPath);\n\n const isPure = node?.meta?.includes('pure');\n\n // do nothing for pure mode\n if (isPure) {\n return;\n }\n\n // every code block can change their preview mode by meta\n const isMobileMode =\n node?.meta?.includes('mobile') ||\n (!node?.meta?.includes('web') && isMobile);\n\n const demoDir = join(\n process.cwd(),\n 'node_modules',\n RSPRESS_TEMP_DIR,\n `virtual-demo`,\n );\n const id = generateId(pageName, index++);\n const virtualModulePath = join(demoDir, `${id}.tsx`);\n fs.ensureDirSync(join(demoDir));\n // Only when the content of the file changes, the file will be written\n // Avoid to trigger the hmr indefinitely\n if (fs.existsSync(virtualModulePath)) {\n const content = fs.readFileSync(virtualModulePath, 'utf-8');\n if (content !== value) {\n fs.writeFileSync(virtualModulePath, value);\n }\n }\n constructDemoNode(id, virtualModulePath, node, isMobileMode);\n }\n });\n\n tree.children.unshift(...demos);\n };\n};\n\nconst getASTNodeImport = (name: string, from: string) =>\n ({\n type: 'mdxjsEsm',\n value: `import ${name} from ${JSON.stringify(from)}`,\n data: {\n estree: {\n type: 'Program',\n sourceType: 'module',\n body: [\n {\n type: 'ImportDeclaration',\n specifiers: [\n {\n type: 'ImportDefaultSpecifier',\n local: { type: 'Identifier', name },\n },\n ],\n source: {\n type: 'Literal',\n value: from,\n raw: `${JSON.stringify(from)}`,\n },\n },\n ],\n },\n },\n } as MdxjsEsm);\n\nconst getExternalDemoContent = (tempVar: string) => ({\n type: 'mdxJsxFlowElement',\n name: 'pre',\n children: [\n {\n type: 'mdxJsxFlowElement',\n name: 'code',\n attributes: [\n {\n type: 'mdxJsxAttribute',\n name: 'className',\n value: 'language-tsx',\n },\n {\n type: 'mdxJsxAttribute',\n name: 'children',\n value: {\n type: 'mdxJsxExpressionAttribute',\n value: tempVar,\n data: {\n estree: {\n type: 'Program',\n body: [\n {\n type: 'ExpressionStatement',\n expression: {\n type: 'Identifier',\n name: tempVar,\n },\n },\n ],\n },\n },\n },\n },\n ],\n },\n ],\n});\n","/**\n * Converts a string to a valid variable name. If the string is already a valid variable name, returns the original string.\n * @param str - The string to convert.\n * @returns The converted string.\n */\nexport const toValidVarName = (str: string): string => {\n // Check if the string is a valid variable name\n if (/^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(str)) {\n return str; // If it is a valid variable name, return the original string\n } else {\n // If it is not a valid variable name, convert it to a valid variable name\n return str.replace(/[^0-9a-zA-Z_$]/g, '_').replace(/^([0-9])/, '_$1');\n }\n};\n\nexport const generateId = (pageName: string, index: number) => {\n return `_${toValidVarName(pageName)}_${index}`;\n};\n\n/**\n * remove .html extension and validate\n * @param routePath id from pathname\n * @returns normalized id\n */\nexport const normalizeId = (routePath: string) => {\n const result = routePath.replace(/\\.(.*)?$/, '');\n return toValidVarName(result);\n};\n\nexport const injectDemoBlockImport = (str: string, path: string): string => {\n return `\n import DemoBlock from '${path}';\n ${str}\n `;\n};\n","import path from 'path';\n\nexport const staticPath = path.join(__dirname, '..', 'static');\n\nexport const demoComponentPath = path.join(\n __dirname,\n '..',\n 'dist',\n 'virtual-demo.js',\n);\n\nexport const demoBlockComponentPath = path.join(\n staticPath,\n 'global-components',\n 'DemoBlock.tsx',\n);\n"]}
|
1
|
+
{"version":3,"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0MA,SAAS,WAAW,OAAO;AAEzB,QAA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OAAO,YAAY,EACnB,cAAc,MAAM,EACpB,IAAI,iBAAiB,EACrB,WAAO,mBAAK,WAAW,wBAAwB,CAAC,EAChD,IAAI;AAEP,gBAAM,QAAQ,WAAW,QAAQ,KAAK,EAAE,QAAQ,MAAM;AAAA,QACxD;AAAA,MACF;AAAA,IACF;AAAA,IACA,UAAU;AAAA,MACR,eAAe;AAAA,QACb;AAAA,UACE;AAAA,UACA,EAAE,UAAU,cAAc,gBAAgB,kBAAkB;AAAA,QAC9D;AAAA,MACF;AAAA,MACA,kBAAkB;AAAA,YAChB,mBAAK,YAAY,qBAAqB,eAAe;AAAA,MACvD;AAAA,IACF;AAAA,IACA;AAAA,IACA,kBAAc;AAAA,MACZ;AAAA,MACA;AAAA,MACA,GAAG,WAAW,WAAW;AAAA,IAC3B;AAAA,IACA,eAAe;AACb,aAAO;AAAA,IACT;AAAA,EACF;AACF;","names":["index","is","visit","path","routeMeta","externalDemoIndex","fs","pageName"],"sources":["../../../node_modules/.pnpm/unist-util-is@5.2.1/node_modules/unist-util-is/lib/index.js","../../../node_modules/.pnpm/unist-util-is@5.2.1/node_modules/unist-util-is/index.js","../../../node_modules/.pnpm/unist-util-visit-parents@5.1.3/node_modules/unist-util-visit-parents/lib/color.js","../../../node_modules/.pnpm/unist-util-visit-parents@5.1.3/node_modules/unist-util-visit-parents/lib/index.js","../../../node_modules/.pnpm/unist-util-visit-parents@5.1.3/node_modules/unist-util-visit-parents/index.js","../../../node_modules/.pnpm/unist-util-visit@4.1.1/node_modules/unist-util-visit/index.js","../src/index.ts","../src/codeToDemo.ts","../src/utils.ts","../src/constant.ts"],"sourcesContent":["/**\n * @typedef {import('unist').Node} Node\n * @typedef {import('unist').Parent} Parent\n */\n\n/**\n * @typedef {Record<string, unknown>} Props\n * @typedef {null | undefined | string | Props | TestFunctionAnything | Array<string | Props | TestFunctionAnything>} Test\n * Check for an arbitrary node, unaware of TypeScript inferral.\n *\n * @callback TestFunctionAnything\n * Check if a node passes a test, unaware of TypeScript inferral.\n * @param {unknown} this\n * The given context.\n * @param {Node} node\n * A node.\n * @param {number | null | undefined} [index]\n * The node’s position in its parent.\n * @param {Parent | null | undefined} [parent]\n * The node’s parent.\n * @returns {boolean | void}\n * Whether this node passes the test.\n */\n\n/**\n * @template {Node} Kind\n * Node type.\n * @typedef {Kind['type'] | Partial<Kind> | TestFunctionPredicate<Kind> | Array<Kind['type'] | Partial<Kind> | TestFunctionPredicate<Kind>>} PredicateTest\n * Check for a node that can be inferred by TypeScript.\n */\n\n/**\n * Check if a node passes a certain test.\n *\n * @template {Node} Kind\n * Node type.\n * @callback TestFunctionPredicate\n * Complex test function for a node that can be inferred by TypeScript.\n * @param {Node} node\n * A node.\n * @param {number | null | undefined} [index]\n * The node’s position in its parent.\n * @param {Parent | null | undefined} [parent]\n * The node’s parent.\n * @returns {node is Kind}\n * Whether this node passes the test.\n */\n\n/**\n * @callback AssertAnything\n * Check that an arbitrary value is a node, unaware of TypeScript inferral.\n * @param {unknown} [node]\n * Anything (typically a node).\n * @param {number | null | undefined} [index]\n * The node’s position in its parent.\n * @param {Parent | null | undefined} [parent]\n * The node’s parent.\n * @returns {boolean}\n * Whether this is a node and passes a test.\n */\n\n/**\n * Check if a node is a node and passes a certain node test.\n *\n * @template {Node} Kind\n * Node type.\n * @callback AssertPredicate\n * Check that an arbitrary value is a specific node, aware of TypeScript.\n * @param {unknown} [node]\n * Anything (typically a node).\n * @param {number | null | undefined} [index]\n * The node’s position in its parent.\n * @param {Parent | null | undefined} [parent]\n * The node’s parent.\n * @returns {node is Kind}\n * Whether this is a node and passes a test.\n */\n\n/**\n * Check if `node` is a `Node` and whether it passes the given test.\n *\n * @param node\n * Thing to check, typically `Node`.\n * @param test\n * A check for a specific node.\n * @param index\n * The node’s position in its parent.\n * @param parent\n * The node’s parent.\n * @returns\n * Whether `node` is a node and passes a test.\n */\nexport const is =\n /**\n * @type {(\n * (() => false) &\n * (<Kind extends Node = Node>(node: unknown, test: PredicateTest<Kind>, index: number, parent: Parent, context?: unknown) => node is Kind) &\n * (<Kind extends Node = Node>(node: unknown, test: PredicateTest<Kind>, index?: null | undefined, parent?: null | undefined, context?: unknown) => node is Kind) &\n * ((node: unknown, test: Test, index: number, parent: Parent, context?: unknown) => boolean) &\n * ((node: unknown, test?: Test, index?: null | undefined, parent?: null | undefined, context?: unknown) => boolean)\n * )}\n */\n (\n /**\n * @param {unknown} [node]\n * @param {Test} [test]\n * @param {number | null | undefined} [index]\n * @param {Parent | null | undefined} [parent]\n * @param {unknown} [context]\n * @returns {boolean}\n */\n // eslint-disable-next-line max-params\n function is(node, test, index, parent, context) {\n const check = convert(test)\n\n if (\n index !== undefined &&\n index !== null &&\n (typeof index !== 'number' ||\n index < 0 ||\n index === Number.POSITIVE_INFINITY)\n ) {\n throw new Error('Expected positive finite index')\n }\n\n if (\n parent !== undefined &&\n parent !== null &&\n (!is(parent) || !parent.children)\n ) {\n throw new Error('Expected parent node')\n }\n\n if (\n (parent === undefined || parent === null) !==\n (index === undefined || index === null)\n ) {\n throw new Error('Expected both parent and index')\n }\n\n // @ts-expect-error Looks like a node.\n return node && node.type && typeof node.type === 'string'\n ? Boolean(check.call(context, node, index, parent))\n : false\n }\n )\n\n/**\n * Generate an assertion from a test.\n *\n * Useful if you’re going to test many nodes, for example when creating a\n * utility where something else passes a compatible test.\n *\n * The created function is a bit faster because it expects valid input only:\n * a `node`, `index`, and `parent`.\n *\n * @param test\n * * when nullish, checks if `node` is a `Node`.\n * * when `string`, works like passing `(node) => node.type === test`.\n * * when `function` checks if function passed the node is true.\n * * when `object`, checks that all keys in test are in node, and that they have (strictly) equal values.\n * * when `array`, checks if any one of the subtests pass.\n * @returns\n * An assertion.\n */\nexport const convert =\n /**\n * @type {(\n * (<Kind extends Node>(test: PredicateTest<Kind>) => AssertPredicate<Kind>) &\n * ((test?: Test) => AssertAnything)\n * )}\n */\n (\n /**\n * @param {Test} [test]\n * @returns {AssertAnything}\n */\n function (test) {\n if (test === undefined || test === null) {\n return ok\n }\n\n if (typeof test === 'string') {\n return typeFactory(test)\n }\n\n if (typeof test === 'object') {\n return Array.isArray(test) ? anyFactory(test) : propsFactory(test)\n }\n\n if (typeof test === 'function') {\n return castFactory(test)\n }\n\n throw new Error('Expected function, string, or object as test')\n }\n )\n\n/**\n * @param {Array<string | Props | TestFunctionAnything>} tests\n * @returns {AssertAnything}\n */\nfunction anyFactory(tests) {\n /** @type {Array<AssertAnything>} */\n const checks = []\n let index = -1\n\n while (++index < tests.length) {\n checks[index] = convert(tests[index])\n }\n\n return castFactory(any)\n\n /**\n * @this {unknown}\n * @param {Array<unknown>} parameters\n * @returns {boolean}\n */\n function any(...parameters) {\n let index = -1\n\n while (++index < checks.length) {\n if (checks[index].call(this, ...parameters)) return true\n }\n\n return false\n }\n}\n\n/**\n * Turn an object into a test for a node with a certain fields.\n *\n * @param {Props} check\n * @returns {AssertAnything}\n */\nfunction propsFactory(check) {\n return castFactory(all)\n\n /**\n * @param {Node} node\n * @returns {boolean}\n */\n function all(node) {\n /** @type {string} */\n let key\n\n for (key in check) {\n // @ts-expect-error: hush, it sure works as an index.\n if (node[key] !== check[key]) return false\n }\n\n return true\n }\n}\n\n/**\n * Turn a string into a test for a node with a certain type.\n *\n * @param {string} check\n * @returns {AssertAnything}\n */\nfunction typeFactory(check) {\n return castFactory(type)\n\n /**\n * @param {Node} node\n */\n function type(node) {\n return node && node.type === check\n }\n}\n\n/**\n * Turn a custom test into a test for a node that passes that test.\n *\n * @param {TestFunctionAnything} check\n * @returns {AssertAnything}\n */\nfunction castFactory(check) {\n return assertion\n\n /**\n * @this {unknown}\n * @param {unknown} node\n * @param {Array<unknown>} parameters\n * @returns {boolean}\n */\n function assertion(node, ...parameters) {\n return Boolean(\n node &&\n typeof node === 'object' &&\n 'type' in node &&\n // @ts-expect-error: fine.\n Boolean(check.call(this, node, ...parameters))\n )\n }\n}\n\nfunction ok() {\n return true\n}\n","/**\n * @typedef {import('./lib/index.js').Test} Test\n * @typedef {import('./lib/index.js').TestFunctionAnything} TestFunctionAnything\n * @typedef {import('./lib/index.js').AssertAnything} AssertAnything\n */\n\n/**\n * @template {import('unist').Node} Kind\n * @typedef {import('./lib/index.js').PredicateTest<Kind>} PredicateTest\n */\n\n/**\n * @template {import('unist').Node} Kind\n * @typedef {import('./lib/index.js').TestFunctionPredicate<Kind>} TestFunctionPredicate\n */\n\n/**\n * @template {import('unist').Node} Kind\n * @typedef {import('./lib/index.js').AssertPredicate<Kind>} AssertPredicate\n */\n\nexport {is, convert} from './lib/index.js'\n","/**\n * @param {string} d\n * @returns {string}\n */\nexport function color(d) {\n return '\\u001B[33m' + d + '\\u001B[39m'\n}\n","/**\n * @typedef {import('unist').Node} Node\n * @typedef {import('unist').Parent} Parent\n * @typedef {import('unist-util-is').Test} Test\n */\n\n/**\n * @typedef {boolean | 'skip'} Action\n * Union of the action types.\n *\n * @typedef {number} Index\n * Move to the sibling at `index` next (after node itself is completely\n * traversed).\n *\n * Useful if mutating the tree, such as removing the node the visitor is\n * currently on, or any of its previous siblings.\n * Results less than 0 or greater than or equal to `children.length` stop\n * traversing the parent.\n *\n * @typedef {[(Action | null | undefined | void)?, (Index | null | undefined)?]} ActionTuple\n * List with one or two values, the first an action, the second an index.\n *\n * @typedef {Action | ActionTuple | Index | null | undefined | void} VisitorResult\n * Any value that can be returned from a visitor.\n */\n\n/**\n * @template {Node} [Visited=Node]\n * Visited node type.\n * @template {Parent} [Ancestor=Parent]\n * Ancestor type.\n * @callback Visitor\n * Handle a node (matching `test`, if given).\n *\n * Visitors are free to transform `node`.\n * They can also transform the parent of node (the last of `ancestors`).\n *\n * Replacing `node` itself, if `SKIP` is not returned, still causes its\n * descendants to be walked (which is a bug).\n *\n * When adding or removing previous siblings of `node` (or next siblings, in\n * case of reverse), the `Visitor` should return a new `Index` to specify the\n * sibling to traverse after `node` is traversed.\n * Adding or removing next siblings of `node` (or previous siblings, in case\n * of reverse) is handled as expected without needing to return a new `Index`.\n *\n * Removing the children property of an ancestor still results in them being\n * traversed.\n * @param {Visited} node\n * Found node.\n * @param {Array<Ancestor>} ancestors\n * Ancestors of `node`.\n * @returns {VisitorResult}\n * What to do next.\n *\n * An `Index` is treated as a tuple of `[CONTINUE, Index]`.\n * An `Action` is treated as a tuple of `[Action]`.\n *\n * Passing a tuple back only makes sense if the `Action` is `SKIP`.\n * When the `Action` is `EXIT`, that action can be returned.\n * When the `Action` is `CONTINUE`, `Index` can be returned.\n */\n\n/**\n * @template {Node} [Tree=Node]\n * Tree type.\n * @template {Test} [Check=string]\n * Test type.\n * @typedef {Visitor<import('./complex-types.js').Matches<import('./complex-types.js').InclusiveDescendant<Tree>, Check>, Extract<import('./complex-types.js').InclusiveDescendant<Tree>, Parent>>} BuildVisitor\n * Build a typed `Visitor` function from a tree and a test.\n *\n * It will infer which values are passed as `node` and which as `parents`.\n */\n\nimport {convert} from 'unist-util-is'\nimport {color} from './color.js'\n\n/**\n * Continue traversing as normal.\n */\nexport const CONTINUE = true\n\n/**\n * Stop traversing immediately.\n */\nexport const EXIT = false\n\n/**\n * Do not traverse this node’s children.\n */\nexport const SKIP = 'skip'\n\n/**\n * Visit nodes, with ancestral information.\n *\n * This algorithm performs *depth-first* *tree traversal* in *preorder*\n * (**NLR**) or if `reverse` is given, in *reverse preorder* (**NRL**).\n *\n * You can choose for which nodes `visitor` is called by passing a `test`.\n * For complex tests, you should test yourself in `visitor`, as it will be\n * faster and will have improved type information.\n *\n * Walking the tree is an intensive task.\n * Make use of the return values of the visitor when possible.\n * Instead of walking a tree multiple times, walk it once, use `unist-util-is`\n * to check if a node matches, and then perform different operations.\n *\n * You can change the tree.\n * See `Visitor` for more info.\n *\n * @param tree\n * Tree to traverse.\n * @param test\n * `unist-util-is`-compatible test\n * @param visitor\n * Handle each node.\n * @param reverse\n * Traverse in reverse preorder (NRL) instead of the default preorder (NLR).\n * @returns\n * Nothing.\n */\nexport const visitParents =\n /**\n * @type {(\n * (<Tree extends Node, Check extends Test>(tree: Tree, test: Check, visitor: BuildVisitor<Tree, Check>, reverse?: boolean | null | undefined) => void) &\n * (<Tree extends Node>(tree: Tree, visitor: BuildVisitor<Tree>, reverse?: boolean | null | undefined) => void)\n * )}\n */\n (\n /**\n * @param {Node} tree\n * @param {Test} test\n * @param {Visitor<Node>} visitor\n * @param {boolean | null | undefined} [reverse]\n * @returns {void}\n */\n function (tree, test, visitor, reverse) {\n if (typeof test === 'function' && typeof visitor !== 'function') {\n reverse = visitor\n // @ts-expect-error no visitor given, so `visitor` is test.\n visitor = test\n test = null\n }\n\n const is = convert(test)\n const step = reverse ? -1 : 1\n\n factory(tree, undefined, [])()\n\n /**\n * @param {Node} node\n * @param {number | undefined} index\n * @param {Array<Parent>} parents\n */\n function factory(node, index, parents) {\n /** @type {Record<string, unknown>} */\n // @ts-expect-error: hush\n const value = node && typeof node === 'object' ? node : {}\n\n if (typeof value.type === 'string') {\n const name =\n // `hast`\n typeof value.tagName === 'string'\n ? value.tagName\n : // `xast`\n typeof value.name === 'string'\n ? value.name\n : undefined\n\n Object.defineProperty(visit, 'name', {\n value:\n 'node (' + color(node.type + (name ? '<' + name + '>' : '')) + ')'\n })\n }\n\n return visit\n\n function visit() {\n /** @type {ActionTuple} */\n let result = []\n /** @type {ActionTuple} */\n let subresult\n /** @type {number} */\n let offset\n /** @type {Array<Parent>} */\n let grandparents\n\n if (!test || is(node, index, parents[parents.length - 1] || null)) {\n result = toResult(visitor(node, parents))\n\n if (result[0] === EXIT) {\n return result\n }\n }\n\n // @ts-expect-error looks like a parent.\n if (node.children && result[0] !== SKIP) {\n // @ts-expect-error looks like a parent.\n offset = (reverse ? node.children.length : -1) + step\n // @ts-expect-error looks like a parent.\n grandparents = parents.concat(node)\n\n // @ts-expect-error looks like a parent.\n while (offset > -1 && offset < node.children.length) {\n // @ts-expect-error looks like a parent.\n subresult = factory(node.children[offset], offset, grandparents)()\n\n if (subresult[0] === EXIT) {\n return subresult\n }\n\n offset =\n typeof subresult[1] === 'number' ? subresult[1] : offset + step\n }\n }\n\n return result\n }\n }\n }\n )\n\n/**\n * Turn a return value into a clean result.\n *\n * @param {VisitorResult} value\n * Valid return values from visitors.\n * @returns {ActionTuple}\n * Clean result.\n */\nfunction toResult(value) {\n if (Array.isArray(value)) {\n return value\n }\n\n if (typeof value === 'number') {\n return [CONTINUE, value]\n }\n\n return [value]\n}\n","// Note: types exported from `index.d.ts`\nexport {CONTINUE, EXIT, SKIP, visitParents} from './lib/index.js'\n","/**\n * @typedef {import('unist').Node} Node\n * @typedef {import('unist').Parent} Parent\n * @typedef {import('unist-util-is').Test} Test\n * @typedef {import('unist-util-visit-parents').VisitorResult} VisitorResult\n * @typedef {import('./complex-types.js').Visitor} Visitor\n */\n\nimport {visitParents} from 'unist-util-visit-parents'\n\n/**\n * Visit children of tree which pass test.\n *\n * @param tree\n * Tree to walk\n * @param [test]\n * `unist-util-is`-compatible test\n * @param visitor\n * Function called for nodes that pass `test`.\n * @param reverse\n * Traverse in reverse preorder (NRL) instead of preorder (NLR) (default).\n */\nexport const visit =\n /**\n * @type {(\n * (<Tree extends Node, Check extends Test>(tree: Tree, test: Check, visitor: import('./complex-types.js').BuildVisitor<Tree, Check>, reverse?: boolean) => void) &\n * (<Tree extends Node>(tree: Tree, visitor: import('./complex-types.js').BuildVisitor<Tree>, reverse?: boolean) => void)\n * )}\n */\n (\n /**\n * @param {Node} tree\n * @param {Test} test\n * @param {import('./complex-types.js').Visitor} visitor\n * @param {boolean} [reverse]\n */\n function (tree, test, visitor, reverse) {\n if (typeof test === 'function' && typeof visitor !== 'function') {\n reverse = visitor\n visitor = test\n test = null\n }\n\n visitParents(tree, test, overload, reverse)\n\n /**\n * @param {Node} node\n * @param {Array<Parent>} parents\n */\n function overload(node, parents) {\n const parent = parents[parents.length - 1]\n return visitor(\n node,\n parent ? parent.children.indexOf(node) : null,\n parent\n )\n }\n }\n )\n\nexport {CONTINUE, EXIT, SKIP} from 'unist-util-visit-parents'\n","import { join, parse, extname } from 'path';\nimport { RspackVirtualModulePlugin } from 'rspack-plugin-virtual-module';\nimport {\n type RspressPlugin,\n type RouteMeta,\n RSPRESS_TEMP_DIR,\n normalizePosixPath,\n} from '@rspress/shared';\nimport { remarkCodeToDemo } from './codeToDemo';\nimport { generateId, injectDemoBlockImport } from './utils';\nimport {\n demoBlockComponentPath,\n demoComponentPath,\n staticPath,\n} from './constant';\n\nexport type Options = {\n /**\n * preview in mobile mode or not\n * when isMobile is true, 1. aside will hide. 2. default preview component by iframe\n * @default false\n */\n isMobile: boolean;\n /**\n * position of the iframe\n * @default 'follow'\n */\n iframePosition: 'fixed' | 'follow';\n /**\n * whether the demo can open in codesandbox\n * @default false\n * @experimental\n */\n enableCodesandbox: boolean;\n};\n\ninterface Heading {\n type: string;\n depth?: number;\n children?: ChildNode[];\n}\n\ninterface ChildNode {\n type: 'link' | 'text' | 'inlineCode';\n value: string;\n children?: ChildNode[];\n}\n\n// eslint-disable-next-line import/no-mutable-exports\nexport let routeMeta: RouteMeta[];\nexport const demoRoutes: { path: string }[] = [];\nexport const demoMeta: Record<\n string,\n { id: string; virtualModulePath: string; title: string }[]\n> = {};\n\n/**\n * The plugin is used to preview component.\n */\nexport function pluginPreview(options?: Options): RspressPlugin {\n const isMobile = options?.isMobile ?? false;\n const iframePosition = options?.iframePosition ?? 'follow';\n const enableCodesandbox = options?.enableCodesandbox ?? false;\n const demoRuntimeModule = new RspackVirtualModulePlugin({});\n const globalUIComponents =\n iframePosition === 'fixed'\n ? [join(staticPath, 'global-components', 'Device.tsx')]\n : [];\n const getRouteMeta = () => routeMeta;\n return {\n name: '@rspress/plugin-preview',\n addPages(_config, _isProd) {\n return [\n {\n routePath: '/~demo/:id',\n content: `---\npageType: \"blank\"\n---\n\nimport Demo from ${JSON.stringify(demoComponentPath)}\n\n<Demo iframePosition=\"${iframePosition}\"/>\n `,\n },\n ];\n },\n async routeGenerated(routes: RouteMeta[]) {\n // init routeMeta\n routeMeta = routes;\n\n const files = routes.map(route => route.absolutePath);\n // Write the demo code ahead of time\n // Fix: rspack build error because demo file is not exist, probably the demo file was written in rspack build process?\n await Promise.all(\n files.map(async (filepath, _index) => {\n const isMdxFile = /\\.mdx?$/.test(filepath);\n if (!isMdxFile) {\n return;\n }\n const { createProcessor } = await import('@mdx-js/mdx');\n const { visit } = await import('unist-util-visit');\n const { default: fs } = await import('@modern-js/utils/fs-extra');\n const { default: remarkGFM } = await import('remark-gfm');\n let title = parse(filepath).name;\n try {\n const processor = createProcessor({\n format: extname(filepath).slice(1) as 'mdx' | 'md',\n remarkPlugins: [remarkGFM],\n });\n const source = await fs.readFile(filepath, 'utf-8');\n const ast = processor.parse(source);\n let index = 1;\n const { pageName } = routeMeta.find(\n meta =>\n normalizePosixPath(meta.absolutePath) ===\n normalizePosixPath(filepath),\n )!;\n\n const registerDemo = (\n demoId: string,\n demoPath: string,\n isMobileMode: boolean,\n ) => {\n if (isMobileMode) {\n // only add demoMeta in mobile mode\n demoMeta[filepath] = demoMeta[filepath] ?? [];\n const isExist = demoMeta[filepath].find(\n item => item.id === demoId,\n );\n if (!isExist) {\n demoMeta[filepath].push({\n id: demoId,\n title,\n virtualModulePath: demoPath,\n });\n }\n }\n };\n visit(ast, 'heading', (node: Heading) => {\n if (node.depth === 1) {\n if (node.children) {\n title = node.children[0].value;\n }\n }\n });\n\n visit(ast, 'mdxJsxFlowElement', (node: any) => {\n if (node.name === 'code') {\n const src = node.attributes.find(\n (attr: { name: string; value: string }) =>\n attr.name === 'src',\n )?.value;\n const isMobileMode =\n node.attributes.find(\n (attr: { name: string; value: boolean }) =>\n attr.name === 'isMobile',\n )?.value ?? isMobile;\n if (!src) {\n return;\n }\n const id = generateId(pageName, index++);\n registerDemo(id, src, isMobileMode);\n }\n });\n\n visit(ast, 'code', (node: any) => {\n if (node.lang === 'jsx' || node.lang === 'tsx') {\n const { value } = node;\n const isPure = node?.meta?.includes('pure');\n\n // do not anything for pure mode\n if (isPure) {\n return;\n }\n\n // every code block can change their preview mode by meta\n const isMobileMode =\n node?.meta?.includes('mobile') ||\n (!node?.meta?.includes('web') && isMobile);\n\n const { pageName } = routeMeta.find(\n meta =>\n normalizePosixPath(meta.absolutePath) ===\n normalizePosixPath(filepath),\n )!;\n const id = generateId(pageName, index++);\n\n const demoDir = join(\n process.cwd(),\n 'node_modules',\n RSPRESS_TEMP_DIR,\n `virtual-demo`,\n );\n\n const virtualModulePath = join(demoDir, `${id}.tsx`);\n registerDemo(id, virtualModulePath, isMobileMode);\n\n fs.ensureDirSync(join(demoDir));\n fs.writeFileSync(\n virtualModulePath,\n injectDemoBlockImport(value, demoBlockComponentPath),\n );\n }\n });\n } catch (e) {\n console.error(e);\n throw e;\n }\n }),\n );\n /**\n * expect the meta of each demo file is like this:\n * {\n * id,\n * component,\n * }\n */\n const virtualMeta = `\n ${files\n .map((filepath, index) => {\n return `import Route${index} from '${filepath}?meta';`;\n })\n .join('\\n')}\n export const demos = [${files\n .map((_, index) => `Route${index}`)\n .flat()\n .join(',')}];\n `;\n demoRuntimeModule.writeModule('virtual-meta', virtualMeta);\n },\n builderConfig: {\n tools: {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment, @typescript-eslint/prefer-ts-expect-error\n // @ts-ignore\n rspack: {\n plugins: [demoRuntimeModule],\n },\n bundlerChain(chain) {\n chain.module\n .rule('Raw')\n .resourceQuery(/raw/)\n .type('asset/source')\n .end()\n .rule('MDX')\n .oneOf('MDXMeta')\n .before('MDXCompile')\n .resourceQuery(/meta/)\n .use('mdx-meta-loader')\n .loader(join(__dirname, '../mdx-meta-loader.cjs'))\n .end();\n\n chain.resolve.extensions.prepend('.md').prepend('.mdx');\n },\n },\n },\n markdown: {\n remarkPlugins: [\n [\n remarkCodeToDemo,\n { isMobile, getRouteMeta, iframePosition, enableCodesandbox },\n ],\n ],\n globalComponents: [\n join(staticPath, 'global-components', 'Container.tsx'),\n ],\n },\n globalUIComponents,\n globalStyles: join(\n staticPath,\n 'global-styles',\n `${isMobile ? 'mobile' : 'web'}.css`,\n ),\n addSSGRoutes() {\n return demoRoutes;\n },\n };\n}\n","import { join, resolve } from 'path';\nimport { visit } from 'unist-util-visit';\nimport fs from '@modern-js/utils/fs-extra';\nimport {\n RSPRESS_TEMP_DIR,\n normalizePosixPath,\n type RouteMeta,\n} from '@rspress/shared';\nimport type { Plugin } from 'unified';\nimport type { Root } from 'mdast';\nimport type { MdxjsEsm } from 'mdast-util-mdxjs-esm';\nimport { injectDemoBlockImport, generateId } from './utils';\nimport { demoBlockComponentPath } from './constant';\nimport { demoRoutes } from '.';\n\n// FIXME: remove it\nconst json = JSON.parse(\n fs.readFileSync(resolve(process.cwd(), './package.json'), 'utf8'),\n);\n\n/**\n * remark plugin to transform code to demo\n */\nexport const remarkCodeToDemo: Plugin<\n [\n {\n isMobile: boolean;\n getRouteMeta: () => RouteMeta[];\n iframePosition: 'fixed' | 'follow';\n enableCodesandbox: boolean;\n },\n ],\n Root\n> = ({ isMobile, getRouteMeta, iframePosition, enableCodesandbox }) => {\n const routeMeta = getRouteMeta();\n\n return (tree, vfile) => {\n const demos: MdxjsEsm[] = [];\n const route = routeMeta.find(\n meta =>\n normalizePosixPath(meta.absolutePath) ===\n normalizePosixPath(vfile.path || vfile.history[0]),\n );\n if (!route) {\n return;\n }\n const { pageName } = route;\n let index = 1;\n let externalDemoIndex = 0;\n\n function constructDemoNode(\n demoId: string,\n demoPath: string,\n currentNode: any,\n isMobileMode: boolean,\n // Only for external demo\n externalDemoIndex?: number,\n ) {\n const demoRoute = `/~demo/${demoId}`;\n if (isMobileMode) {\n // only add demoRoutes in mobile mode\n demoRoutes.push({\n path: demoRoute,\n });\n } else {\n demos.push(getASTNodeImport(`Demo${demoId}`, demoPath));\n }\n\n // get external demo content\n const tempVar = `externalDemoContent${externalDemoIndex}`;\n if (externalDemoIndex !== undefined) {\n demos.push(getASTNodeImport(tempVar, `${demoPath}?raw`));\n }\n\n if (isMobileMode && iframePosition === 'fixed') {\n // Only show the code block\n externalDemoIndex !== undefined &&\n Object.assign(currentNode, getExternalDemoContent(tempVar));\n } else {\n // Use container to show the code and view\n Object.assign(currentNode, {\n type: 'mdxJsxFlowElement',\n name: 'Container',\n attributes: [\n {\n type: 'mdxJsxAttribute',\n name: 'isMobile',\n value: isMobileMode,\n },\n {\n type: 'mdxJsxAttribute',\n name: 'url',\n value: demoRoute,\n },\n {\n type: 'mdxJsxAttribute',\n name: 'content',\n value: currentNode.value,\n },\n {\n type: 'mdxJsxAttribute',\n name: 'packageName',\n value: json.name,\n },\n {\n type: 'mdxJsxAttribute',\n name: 'enableCodesandbox',\n value: enableCodesandbox,\n },\n ],\n children: [\n externalDemoIndex === undefined\n ? {\n ...currentNode,\n hasVisited: true,\n }\n : getExternalDemoContent(tempVar),\n isMobileMode\n ? {\n type: 'mdxJsxFlowElement',\n name: null,\n }\n : {\n type: 'mdxJsxFlowElement',\n name: `Demo${demoId}`,\n },\n ],\n });\n }\n }\n\n // 1. External demo , use <code src=\"xxx\" /> to declare demo\n tree.children.forEach((node: any) => {\n if (node.type === 'mdxJsxFlowElement' && node.name === 'code') {\n const src = node.attributes.find(\n (attr: { name: string; value: string }) => attr.name === 'src',\n )?.value;\n const isMobileMode =\n node.attributes.find(\n (attr: { name: string; value: boolean }) =>\n attr.name === 'isMobile',\n )?.value ?? isMobile;\n if (!src) {\n return;\n }\n const id = generateId(pageName, index++);\n constructDemoNode(id, src, node, isMobileMode, externalDemoIndex++);\n }\n });\n\n // 2. Internal demo, use ```j/tsx to declare demo\n visit(tree, 'code', node => {\n // hasVisited is a custom property\n if ('hasVisited' in node) {\n return;\n }\n\n if (node.lang === 'jsx' || node.lang === 'tsx') {\n const value = injectDemoBlockImport(node.value, demoBlockComponentPath);\n\n const isPure = node?.meta?.includes('pure');\n\n // do nothing for pure mode\n if (isPure) {\n return;\n }\n\n // every code block can change their preview mode by meta\n const isMobileMode =\n node?.meta?.includes('mobile') ||\n (!node?.meta?.includes('web') && isMobile);\n\n const demoDir = join(\n process.cwd(),\n 'node_modules',\n RSPRESS_TEMP_DIR,\n `virtual-demo`,\n );\n const id = generateId(pageName, index++);\n const virtualModulePath = join(demoDir, `${id}.tsx`);\n fs.ensureDirSync(join(demoDir));\n // Only when the content of the file changes, the file will be written\n // Avoid to trigger the hmr indefinitely\n if (fs.existsSync(virtualModulePath)) {\n const content = fs.readFileSync(virtualModulePath, 'utf-8');\n if (content !== value) {\n fs.writeFileSync(virtualModulePath, value);\n }\n }\n constructDemoNode(id, virtualModulePath, node, isMobileMode);\n }\n });\n\n tree.children.unshift(...demos);\n };\n};\n\nconst getASTNodeImport = (name: string, from: string) =>\n ({\n type: 'mdxjsEsm',\n value: `import ${name} from ${JSON.stringify(from)}`,\n data: {\n estree: {\n type: 'Program',\n sourceType: 'module',\n body: [\n {\n type: 'ImportDeclaration',\n specifiers: [\n {\n type: 'ImportDefaultSpecifier',\n local: { type: 'Identifier', name },\n },\n ],\n source: {\n type: 'Literal',\n value: from,\n raw: `${JSON.stringify(from)}`,\n },\n },\n ],\n },\n },\n } as MdxjsEsm);\n\nconst getExternalDemoContent = (tempVar: string) => ({\n type: 'mdxJsxFlowElement',\n name: 'pre',\n children: [\n {\n type: 'mdxJsxFlowElement',\n name: 'code',\n attributes: [\n {\n type: 'mdxJsxAttribute',\n name: 'className',\n value: 'language-tsx',\n },\n {\n type: 'mdxJsxAttribute',\n name: 'children',\n value: {\n type: 'mdxJsxExpressionAttribute',\n value: tempVar,\n data: {\n estree: {\n type: 'Program',\n body: [\n {\n type: 'ExpressionStatement',\n expression: {\n type: 'Identifier',\n name: tempVar,\n },\n },\n ],\n },\n },\n },\n },\n ],\n },\n ],\n});\n","/**\n * Converts a string to a valid variable name. If the string is already a valid variable name, returns the original string.\n * @param str - The string to convert.\n * @returns The converted string.\n */\nexport const toValidVarName = (str: string): string => {\n // Check if the string is a valid variable name\n if (/^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(str)) {\n return str; // If it is a valid variable name, return the original string\n } else {\n // If it is not a valid variable name, convert it to a valid variable name\n return str.replace(/[^0-9a-zA-Z_$]/g, '_').replace(/^([0-9])/, '_$1');\n }\n};\n\nexport const generateId = (pageName: string, index: number) => {\n return `_${toValidVarName(pageName)}_${index}`;\n};\n\n/**\n * remove .html extension and validate\n * @param routePath id from pathname\n * @returns normalized id\n */\nexport const normalizeId = (routePath: string) => {\n const result = routePath.replace(/\\.(.*)?$/, '');\n return toValidVarName(result);\n};\n\nexport const injectDemoBlockImport = (str: string, path: string): string => {\n return `\n import DemoBlock from '${path}';\n ${str}\n `;\n};\n","import path from 'path';\n\nexport const staticPath = path.join(__dirname, '..', 'static');\n\nexport const demoComponentPath = path.join(\n __dirname,\n '..',\n 'dist',\n 'virtual-demo.js',\n);\n\nexport const demoBlockComponentPath = path.join(\n staticPath,\n 'global-components',\n 'DemoBlock.tsx',\n);\n"]}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@rspress/plugin-preview",
|
3
|
-
"version": "0.0.0-nightly-
|
3
|
+
"version": "0.0.0-nightly-20231011160512",
|
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": {
|
@@ -25,7 +25,8 @@
|
|
25
25
|
"qrcode.react": "^3.1.0",
|
26
26
|
"remark-gfm": "3.0.1",
|
27
27
|
"rspack-plugin-virtual-module": "0.1.12",
|
28
|
-
"
|
28
|
+
"codesandbox": "2.2.3",
|
29
|
+
"@rspress/shared": "0.0.0-nightly-20231011160512"
|
29
30
|
},
|
30
31
|
"devDependencies": {
|
31
32
|
"@types/mdast": "^3.0.10",
|
@@ -44,7 +45,7 @@
|
|
44
45
|
"peerDependencies": {
|
45
46
|
"react": ">=17",
|
46
47
|
"react-router-dom": "^6.8.1",
|
47
|
-
"@rspress/core": "0.0.0-nightly-
|
48
|
+
"@rspress/core": "0.0.0-nightly-20231011160512"
|
48
49
|
},
|
49
50
|
"files": [
|
50
51
|
"dist",
|
@@ -42,9 +42,8 @@
|
|
42
42
|
&.web {
|
43
43
|
display: flex;
|
44
44
|
justify-content: center;
|
45
|
-
|
46
|
-
|
47
|
-
right: 16px;
|
45
|
+
align-items: center;
|
46
|
+
flex: none;
|
48
47
|
}
|
49
48
|
}
|
50
49
|
|
@@ -58,6 +57,7 @@
|
|
58
57
|
position: relative;
|
59
58
|
border: 1px solid #e6e6e6;
|
60
59
|
border-radius: 8px;
|
60
|
+
display: flex;
|
61
61
|
}
|
62
62
|
|
63
63
|
&-qrcode {
|
@@ -1,17 +1,23 @@
|
|
1
1
|
import { useCallback, useState } from 'react';
|
2
2
|
import { withBase, useLang, NoSSR } from '@rspress/core/runtime';
|
3
|
+
import { getParameters } from 'codesandbox/lib/api/define';
|
3
4
|
import MobileOperation from './common/mobile-operation';
|
4
5
|
import IconCode from './icons/Code';
|
6
|
+
import IconCodesandbox from './icons/Codesandbox';
|
5
7
|
import './Container.scss';
|
6
8
|
|
7
9
|
type ContainerProps = {
|
8
10
|
children: React.ReactNode[];
|
9
11
|
isMobile: 'true' | 'false';
|
12
|
+
enableCodesandbox: 'true' | 'false';
|
10
13
|
url: string;
|
14
|
+
content: string;
|
15
|
+
packageName: string;
|
11
16
|
};
|
12
17
|
|
13
18
|
const Container: React.FC<ContainerProps> = props => {
|
14
|
-
const { children, isMobile, url } =
|
19
|
+
const { children, isMobile, url, content, packageName, enableCodesandbox } =
|
20
|
+
props;
|
15
21
|
const [showCode, setShowCode] = useState(false);
|
16
22
|
const lang = useLang();
|
17
23
|
|
@@ -34,6 +40,54 @@ const Container: React.FC<ContainerProps> = props => {
|
|
34
40
|
setIframeKey(Math.random());
|
35
41
|
}, []);
|
36
42
|
|
43
|
+
const gotoCodeSandBox = () => {
|
44
|
+
const sandBoxConfig = {
|
45
|
+
files: {
|
46
|
+
'package.json': {
|
47
|
+
isBinary: false,
|
48
|
+
content: JSON.stringify({
|
49
|
+
dependencies: {
|
50
|
+
react: '18',
|
51
|
+
'react-dom': '18',
|
52
|
+
[packageName]: 'latest',
|
53
|
+
},
|
54
|
+
}),
|
55
|
+
},
|
56
|
+
[`demo.tsx`]: {
|
57
|
+
isBinary: false,
|
58
|
+
content,
|
59
|
+
},
|
60
|
+
[`index.tsx`]: {
|
61
|
+
isBinary: false,
|
62
|
+
content: [
|
63
|
+
`import React from 'react'`,
|
64
|
+
`import ReactDOM from 'react-dom'`,
|
65
|
+
`import Demo from './demo'`,
|
66
|
+
`ReactDOM.render(<Demo />, document.getElementById('root'))`,
|
67
|
+
].join('\n'),
|
68
|
+
},
|
69
|
+
},
|
70
|
+
};
|
71
|
+
|
72
|
+
// to specific demo file
|
73
|
+
const query = `file=/demo.tsx`;
|
74
|
+
const form = document.createElement('form');
|
75
|
+
form.action = `https://codesandbox.io/api/v1/sandboxes/define?query=${encodeURIComponent(
|
76
|
+
query,
|
77
|
+
)}`;
|
78
|
+
form.target = '_blank';
|
79
|
+
form.method = 'POST';
|
80
|
+
form.style.display = 'none';
|
81
|
+
const field = document.createElement('input');
|
82
|
+
field.name = 'parameters';
|
83
|
+
field.type = 'hidden';
|
84
|
+
field.setAttribute('value', getParameters(sandBoxConfig));
|
85
|
+
form.appendChild(field);
|
86
|
+
document.body.appendChild(form);
|
87
|
+
form.submit();
|
88
|
+
document.body.removeChild(form);
|
89
|
+
};
|
90
|
+
|
37
91
|
return (
|
38
92
|
<NoSSR>
|
39
93
|
<div className="rspress-preview">
|
@@ -42,7 +96,13 @@ const Container: React.FC<ContainerProps> = props => {
|
|
42
96
|
<div className="rspress-preview-code">{children?.[0]}</div>
|
43
97
|
<div className="rspress-preview-device">
|
44
98
|
<iframe src={getPageUrl()} key={iframeKey}></iframe>
|
45
|
-
<MobileOperation
|
99
|
+
<MobileOperation
|
100
|
+
url={url}
|
101
|
+
refresh={refresh}
|
102
|
+
gotoCodeSandBox={
|
103
|
+
enableCodesandbox === 'true' ? gotoCodeSandBox : undefined
|
104
|
+
}
|
105
|
+
/>
|
46
106
|
</div>
|
47
107
|
</div>
|
48
108
|
) : (
|
@@ -51,16 +111,28 @@ const Container: React.FC<ContainerProps> = props => {
|
|
51
111
|
<div
|
52
112
|
style={{
|
53
113
|
overflow: 'auto',
|
54
|
-
|
114
|
+
flex: 'auto',
|
55
115
|
}}
|
56
116
|
>
|
57
117
|
{children?.[1]}
|
58
118
|
</div>
|
59
119
|
<div className="rspress-preview-operations web">
|
120
|
+
{enableCodesandbox === 'true' && (
|
121
|
+
<button
|
122
|
+
onClick={gotoCodeSandBox}
|
123
|
+
aria-label={
|
124
|
+
lang === 'zh'
|
125
|
+
? '在 Codesandbox 打开'
|
126
|
+
: 'Open in Codesandbox'
|
127
|
+
}
|
128
|
+
>
|
129
|
+
<IconCodesandbox />
|
130
|
+
</button>
|
131
|
+
)}
|
60
132
|
<button
|
61
133
|
onClick={toggleCode}
|
62
|
-
aria-label={lang === 'zh' ? '收起代码' : ''}
|
63
|
-
className={showCode ? 'button-expanded' : '
|
134
|
+
aria-label={lang === 'zh' ? '收起代码' : 'Collapse Code'}
|
135
|
+
className={showCode ? 'button-expanded' : ''}
|
64
136
|
>
|
65
137
|
<IconCode />
|
66
138
|
</button>
|
@@ -4,15 +4,18 @@ import { withBase, useLang } from '@rspress/core/runtime';
|
|
4
4
|
import IconLaunch from '../icons/Launch';
|
5
5
|
import IconQrcode from '../icons/Qrcode';
|
6
6
|
import IconRefresh from '../icons/Refresh';
|
7
|
+
import IconCodesandbox from '../icons/Codesandbox';
|
7
8
|
|
8
9
|
const locales = {
|
9
10
|
zh: {
|
10
11
|
refresh: '刷新页面',
|
11
12
|
open: '在新页面打开',
|
13
|
+
codesandbox: '在 Codesandbox 打开',
|
12
14
|
},
|
13
15
|
en: {
|
14
|
-
refresh: '
|
16
|
+
refresh: 'Refresh',
|
15
17
|
open: 'Open in new page',
|
18
|
+
codesandbox: 'Open in Codesandbox',
|
16
19
|
},
|
17
20
|
};
|
18
21
|
|
@@ -20,9 +23,10 @@ export default (props: {
|
|
20
23
|
url: string;
|
21
24
|
className?: string;
|
22
25
|
refresh: () => void;
|
26
|
+
gotoCodeSandBox?: () => void;
|
23
27
|
}) => {
|
24
28
|
const [showQRCode, setShowQRCode] = useState(false);
|
25
|
-
const { url, className = '', refresh } = props;
|
29
|
+
const { url, className = '', refresh, gotoCodeSandBox } = props;
|
26
30
|
const lang = useLang();
|
27
31
|
const triggerRef = useRef(null);
|
28
32
|
const t = lang === 'zh' ? locales.zh : locales.en;
|
@@ -85,6 +89,11 @@ export default (props: {
|
|
85
89
|
|
86
90
|
return (
|
87
91
|
<div className={`rspress-preview-operations mobile ${className}`}>
|
92
|
+
{gotoCodeSandBox && (
|
93
|
+
<button onClick={gotoCodeSandBox} aria-label={t.codesandbox}>
|
94
|
+
<IconCodesandbox />
|
95
|
+
</button>
|
96
|
+
)}
|
88
97
|
<button onClick={refresh} aria-label={t.refresh}>
|
89
98
|
<IconRefresh />
|
90
99
|
</button>
|
@@ -0,0 +1,22 @@
|
|
1
|
+
const Codesandbox = ({ color = 'currentColor', ...props }) => (
|
2
|
+
<svg
|
3
|
+
width="1em"
|
4
|
+
height="1em"
|
5
|
+
viewBox="0 0 48 48"
|
6
|
+
fill="none"
|
7
|
+
stroke={color}
|
8
|
+
strokeWidth="4"
|
9
|
+
xmlns="http://www.w3.org/2000/svg"
|
10
|
+
{...props}
|
11
|
+
>
|
12
|
+
<path
|
13
|
+
d="M25.0016 1.5998L42.9016 11.8998C43.5016 12.2998 43.9016 12.8998 43.9016 13.5998V34.2998C43.9016 34.9998 43.5016 35.6998 42.9016 35.9998L25.0016 46.3998C24.4016 46.7998 23.6016 46.7998 23.0016 46.3998L5.10156 36.0998C4.50156 35.6998 4.10156 35.0998 4.10156 34.3998V13.6998C4.10156 12.9998 4.50156 12.2998 5.10156 11.9998L23.0016 1.5998C23.6016 1.1998 24.4016 1.1998 25.0016 1.5998ZM38.5016 13.9998L30.6016 9.49981L24.0016 13.9998L17.5016 9.9998L10.2016 14.2998L24.0016 22.9998L38.5016 13.9998ZM22.0016 40.3998V26.2998L8.00156 17.3998V25.2998L16.0016 30.7998V36.9998L22.0016 40.3998ZM26.0016 40.3998L32.0016 36.8998V31.6998L40.0016 26.1998V17.2998L26.0016 26.1998V40.3998Z"
|
14
|
+
fill="currentColor"
|
15
|
+
stroke="none"
|
16
|
+
stroke-width="none"
|
17
|
+
stroke-linecap="butt"
|
18
|
+
></path>
|
19
|
+
</svg>
|
20
|
+
);
|
21
|
+
|
22
|
+
export default Codesandbox;
|