@module-federation/rspress-plugin 0.0.0-codex-node24-dev-node20-compat-ci-20260713063237 → 0.0.0-codex-rspress-codeblock-transform-20260724100038

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 CHANGED
@@ -1,33 +1,50 @@
1
- # @examples/mf-react-component
1
+ # @module-federation/rspress-plugin
2
2
 
3
- This example demonstrates how to use Rslib to build a simple Module Federation React component.
3
+ Module Federation integration for Rspress.
4
4
 
5
- ### Command
5
+ ## Transform remote code blocks
6
6
 
7
- Build package
7
+ Enable the capability on the Rspress producer:
8
8
 
9
- ```
10
- nx build rslib-module
9
+ ```ts
10
+ pluginModuleFederation(mfConfig, {
11
+ transformCodeBlocks: true,
12
+ });
11
13
  ```
12
14
 
13
- Serve package
15
+ The producer keeps authoring regular Markdown. It does not need to add an id or
16
+ create a code-block component:
14
17
 
15
- ```
16
- nx serve rslib-module
17
- ```
18
+ ````md
19
+ ## View all commands
18
20
 
19
- Dev package
21
+ ```bash
22
+ npx mf -h
23
+ ```
24
+ ````
20
25
 
21
- 1.
26
+ The consumer creates a transformer with the browser-safe runtime entry and
27
+ passes it to the remote MDX document:
22
28
 
23
- ```
24
- nx dev rslib-module
25
- ```
29
+ ```tsx
30
+ import Cli from 'mf-doc/cli-en';
31
+ import { transformCodeBlock } from '@module-federation/rspress-plugin/runtime';
26
32
 
27
- 2.
33
+ export const replaceCliName = transformCodeBlock({
34
+ replace: [[/\bmf\b/g, 'vmok']],
35
+ filter: ({ lang }) => lang === 'bash' || lang === 'text',
36
+ });
28
37
 
38
+ export default function Page() {
39
+ return <Cli name="Vmok" cmd="vmok" transformCodeBlock={replaceCliName} />;
40
+ }
29
41
  ```
30
- nx storybook rslib-module
31
- ```
32
42
 
33
- visit http://localhost:6006
43
+ Replacement rules run in order. Without `filter`, they are considered for every
44
+ fenced code block in the remote document; blocks with no matching content keep
45
+ their original highlighting. A transformer can also return a different
46
+ language when the replacement changes the code type.
47
+
48
+ The transformation runs while rendering the remote MDX document, so the same
49
+ result is used by browser rendering and SSG. Existing HTML-based llms/Markdown
50
+ rebuilds also read the transformed output.
package/dist/index.js CHANGED
@@ -334,9 +334,64 @@ async function rebuildSearchIndexByHtml(routes, options) {
334
334
  await promises.writeFile(searchFilePath, stringifiedIndex);
335
335
  }));
336
336
  }
337
+ const RUNTIME_MODULE = '@module-federation/rspress-plugin/runtime';
338
+ function hasDefaultExport(tree) {
339
+ return tree.children.some((node)=>node.data?.estree?.body?.some((statement)=>{
340
+ if ('ExportDefaultDeclaration' === statement.type) return true;
341
+ if ('ExportNamedDeclaration' !== statement.type) return false;
342
+ const specifiers = statement.specifiers;
343
+ return Array.isArray(specifiers) && specifiers.some((specifier)=>{
344
+ if ('object' != typeof specifier || null === specifier || !('exported' in specifier)) return false;
345
+ const exported = specifier.exported;
346
+ return 'object' == typeof exported && null !== exported && 'name' in exported && 'default' === exported.name;
347
+ });
348
+ }));
349
+ }
350
+ function createLayoutExportNode() {
351
+ const rawSource = JSON.stringify(RUNTIME_MODULE);
352
+ return {
353
+ type: 'mdxjsEsm',
354
+ data: {
355
+ estree: {
356
+ type: 'Program',
357
+ sourceType: 'module',
358
+ body: [
359
+ {
360
+ type: 'ExportNamedDeclaration',
361
+ declaration: null,
362
+ specifiers: [
363
+ {
364
+ type: 'ExportSpecifier',
365
+ local: {
366
+ type: 'Identifier',
367
+ name: 'default'
368
+ },
369
+ exported: {
370
+ type: 'Identifier',
371
+ name: 'default'
372
+ }
373
+ }
374
+ ],
375
+ source: {
376
+ type: 'Literal',
377
+ value: RUNTIME_MODULE,
378
+ raw: rawSource
379
+ }
380
+ }
381
+ ]
382
+ }
383
+ }
384
+ };
385
+ }
386
+ function remarkCodeBlockTransform() {
387
+ return (tree)=>{
388
+ if (hasDefaultExport(tree)) return;
389
+ tree.children.unshift(createLayoutExportNode());
390
+ };
391
+ }
337
392
  const isDev = ()=>'development' === process.env.NODE_ENV;
338
393
  function plugin_pluginModuleFederation(mfConfig, rspressOptions) {
339
- const { autoShared = true, rebuildSearchIndex = true, rebuildLlms = true } = rspressOptions || {};
394
+ const { autoShared = true, rebuildSearchIndex = true, rebuildLlms = true, transformCodeBlocks = false } = rspressOptions || {};
340
395
  if (autoShared) mfConfig.shared = {
341
396
  react: {
342
397
  singleton: true,
@@ -375,6 +430,11 @@ function plugin_pluginModuleFederation(mfConfig, rspressOptions) {
375
430
  let routes = [];
376
431
  return {
377
432
  name: 'plugin-module-federation',
433
+ markdown: transformCodeBlocks ? {
434
+ remarkPlugins: [
435
+ remarkCodeBlockTransform
436
+ ]
437
+ } : void 0,
378
438
  async config (config) {
379
439
  if (!isDev() && false !== config.ssg) {
380
440
  enableSSG = true;
package/dist/plugin.d.ts CHANGED
@@ -1,9 +1,16 @@
1
1
  import type { moduleFederationPlugin } from '@module-federation/sdk';
2
2
  import type { RspressPlugin } from '@rspress/core';
3
- type RspressPluginOptions = {
3
+ export type RspressPluginOptions = {
4
4
  autoShared?: boolean;
5
5
  rebuildSearchIndex?: boolean;
6
6
  rebuildLlms?: boolean;
7
+ /**
8
+ * Allow exposed MDX documents to transform fenced code blocks through a
9
+ * `transformCodeBlock` prop.
10
+ *
11
+ * @default false
12
+ */
13
+ transformCodeBlocks?: boolean;
7
14
  };
8
15
  export declare function pluginModuleFederation(mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions, rspressOptions?: RspressPluginOptions): RspressPlugin;
9
16
  export { createModuleFederationConfig } from '@module-federation/sdk';
@@ -0,0 +1,26 @@
1
+ type EstreeNode = {
2
+ type: string;
3
+ [key: string]: unknown;
4
+ };
5
+ type EstreeProgram = {
6
+ type: 'Program';
7
+ sourceType: 'module';
8
+ body: EstreeNode[];
9
+ };
10
+ type MdxNode = {
11
+ type: string;
12
+ data?: {
13
+ estree?: EstreeProgram;
14
+ };
15
+ };
16
+ type MdxRoot = {
17
+ children: MdxNode[];
18
+ };
19
+ /**
20
+ * Install the plugin runtime as the implicit MDX layout. MDX renders the
21
+ * document body as the layout's child, so the layout can provide code-block
22
+ * components before the body is evaluated.
23
+ */
24
+ export declare function remarkCodeBlockTransform(): (tree: MdxRoot) => void;
25
+ export declare const codeBlockTransformRuntimeModule = "@module-federation/rspress-plugin/runtime";
26
+ export {};
@@ -0,0 +1,48 @@
1
+ import { type ReactNode } from 'react';
2
+ export type CodeBlockInfo = {
3
+ code: string;
4
+ lang: string;
5
+ title?: string;
6
+ };
7
+ export type CodeBlockTransformResult = string | {
8
+ code: string;
9
+ lang?: string;
10
+ } | null | undefined;
11
+ export type CodeBlockTransformer = (block: CodeBlockInfo) => CodeBlockTransformResult;
12
+ export type CodeBlockReplacement = readonly [from: string | RegExp, to: string];
13
+ export type TransformCodeBlockOptions = {
14
+ /**
15
+ * Ordered replacements applied to every fenced code block.
16
+ */
17
+ replace: readonly CodeBlockReplacement[];
18
+ /**
19
+ * Limit replacements to selected code blocks.
20
+ */
21
+ filter?: (block: CodeBlockInfo) => boolean;
22
+ /**
23
+ * Optionally update the language after replacements have been applied.
24
+ */
25
+ lang?: string | ((block: CodeBlockInfo, original: CodeBlockInfo) => string | undefined);
26
+ };
27
+ /**
28
+ * Create a code-block transformer from replacement rules.
29
+ *
30
+ * @example
31
+ * ```tsx
32
+ * const replaceCliName = transformCodeBlock({
33
+ * replace: [[/\bmf\b/g, 'vmok']],
34
+ * });
35
+ *
36
+ * <RemoteDoc transformCodeBlock={replaceCliName} />
37
+ * ```
38
+ */
39
+ export declare function transformCodeBlock({ replace, filter, lang, }: TransformCodeBlockOptions): CodeBlockTransformer;
40
+ type CodeBlockTransformLayoutProps = {
41
+ children?: ReactNode;
42
+ transformCodeBlock?: CodeBlockTransformer | null;
43
+ };
44
+ /**
45
+ * Internal MDX layout injected by the Rspress plugin.
46
+ */
47
+ export default function CodeBlockTransformLayout({ children, transformCodeBlock: directTransformer, }: CodeBlockTransformLayoutProps): string | number | boolean | import("react/jsx-runtime").JSX.Element | Iterable<ReactNode> | null | undefined;
48
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,117 @@
1
+ import { MDXProvider, useMDXComponents } from "@mdx-js/react";
2
+ import react, { Children, cloneElement, createContext, isValidElement, useContext, useMemo } from "react";
3
+ function transformCodeBlock({ replace, filter, lang }) {
4
+ return (original)=>{
5
+ if (filter && !filter(original)) return;
6
+ let code = original.code;
7
+ for (const [from, to] of replace)code = 'string' == typeof from ? code.split(from).join(to) : code.replace(from, to);
8
+ const transformed = {
9
+ ...original,
10
+ code
11
+ };
12
+ const nextLang = 'function' == typeof lang ? lang(transformed, original) : lang;
13
+ const resolvedLang = nextLang ?? original.lang;
14
+ if (code === original.code && resolvedLang === original.lang) return;
15
+ return {
16
+ code,
17
+ lang: resolvedLang
18
+ };
19
+ };
20
+ }
21
+ const CodeBlockTransformContext = /*#__PURE__*/ createContext(null);
22
+ function getTextContent(node) {
23
+ if ('string' == typeof node || 'number' == typeof node) return String(node);
24
+ if (!node) return '';
25
+ if (Array.isArray(node)) return node.map(getTextContent).join('');
26
+ if (/*#__PURE__*/ isValidElement(node)) return getTextContent(node.props.children);
27
+ let text = '';
28
+ Children.forEach(node, (child)=>{
29
+ text += getTextContent(child);
30
+ });
31
+ return text;
32
+ }
33
+ function isRspressCodeBlock(className, lang) {
34
+ return 'string' == typeof className && className.split(/\s+/).includes('shiki') && 'string' == typeof lang;
35
+ }
36
+ function replaceHighlightedText(node, originalCode, transformedCode) {
37
+ let prefixLength = 0;
38
+ while(prefixLength < originalCode.length && prefixLength < transformedCode.length && originalCode[prefixLength] === transformedCode[prefixLength])prefixLength++;
39
+ let suffixLength = 0;
40
+ while(suffixLength < originalCode.length - prefixLength && suffixLength < transformedCode.length - prefixLength && originalCode[originalCode.length - suffixLength - 1] === transformedCode[transformedCode.length - suffixLength - 1])suffixLength++;
41
+ const oldSuffixStart = originalCode.length - suffixLength;
42
+ const replacement = transformedCode.slice(prefixLength, transformedCode.length - suffixLength);
43
+ let offset = 0;
44
+ let replacementInserted = false;
45
+ const visit = (current)=>{
46
+ if ('string' == typeof current || 'number' == typeof current) {
47
+ const text = String(current);
48
+ const start = offset;
49
+ const end = start + text.length;
50
+ offset = end;
51
+ if (end <= prefixLength || start >= oldSuffixStart) return text;
52
+ let next = '';
53
+ if (start < prefixLength) next += text.slice(0, prefixLength - start);
54
+ if (!replacementInserted) {
55
+ next += replacement;
56
+ replacementInserted = true;
57
+ }
58
+ if (end > oldSuffixStart) next += text.slice(oldSuffixStart - start);
59
+ return next;
60
+ }
61
+ if (Array.isArray(current)) return Children.map(current, visit);
62
+ if (/*#__PURE__*/ isValidElement(current)) return /*#__PURE__*/ cloneElement(current, void 0, visit(current.props.children));
63
+ return current;
64
+ };
65
+ const result = visit(node);
66
+ return getTextContent(result) === transformedCode ? result : transformedCode;
67
+ }
68
+ function TransformedCodeBlock({ OriginalPre, originalProps, originalCode, code, lang }) {
69
+ const { children, ...preProps } = originalProps;
70
+ const codeElement = /*#__PURE__*/ isValidElement(children) ? /*#__PURE__*/ cloneElement(children, void 0, replaceHighlightedText(children.props.children, originalCode, code)) : /*#__PURE__*/ react.createElement('code', void 0, code);
71
+ return /*#__PURE__*/ react.createElement(OriginalPre, {
72
+ ...preProps,
73
+ lang: lang
74
+ }, codeElement);
75
+ }
76
+ function CodeBlockTransformLayout({ children, transformCodeBlock: directTransformer }) {
77
+ const inherited = useContext(CodeBlockTransformContext);
78
+ const mdxComponents = useMDXComponents();
79
+ if (inherited && void 0 === directTransformer) return children;
80
+ const transformer = null === directTransformer ? void 0 : directTransformer ?? inherited?.transformer;
81
+ const OriginalPre = inherited?.pre ?? mdxComponents.pre ?? 'pre';
82
+ const components = useMemo(()=>({
83
+ pre: (props)=>{
84
+ const { children: codeChildren, lang, title, className } = props;
85
+ if (!transformer || !isRspressCodeBlock(className, lang)) return /*#__PURE__*/ react.createElement(OriginalPre, props);
86
+ const original = {
87
+ code: getTextContent(codeChildren),
88
+ lang: lang,
89
+ title: 'string' == typeof title ? title : void 0
90
+ };
91
+ const result = transformer(original);
92
+ if (null == result) return /*#__PURE__*/ react.createElement(OriginalPre, props);
93
+ const code = 'string' == typeof result ? result : result.code;
94
+ const transformedLang = 'string' == typeof result ? original.lang : result.lang ?? original.lang;
95
+ return /*#__PURE__*/ react.createElement(TransformedCodeBlock, {
96
+ OriginalPre: OriginalPre,
97
+ originalProps: props,
98
+ originalCode: original.code,
99
+ code: code,
100
+ lang: transformedLang
101
+ });
102
+ }
103
+ }), [
104
+ OriginalPre,
105
+ transformer
106
+ ]);
107
+ return /*#__PURE__*/ react.createElement(CodeBlockTransformContext.Provider, {
108
+ value: {
109
+ transformer,
110
+ pre: OriginalPre
111
+ }
112
+ }, /*#__PURE__*/ react.createElement(MDXProvider, {
113
+ components: components
114
+ }, children));
115
+ }
116
+ export default CodeBlockTransformLayout;
117
+ export { transformCodeBlock };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/rspress-plugin",
3
- "version": "0.0.0-codex-node24-dev-node20-compat-ci-20260713063237",
3
+ "version": "0.0.0-codex-rspress-codeblock-transform-20260724100038",
4
4
  "type": "module",
5
5
  "description": "Module Federation plugin for Rspress",
6
6
  "keywords": [
@@ -25,35 +25,47 @@
25
25
  ".": {
26
26
  "types": "./dist/plugin.d.ts",
27
27
  "import": "./dist/index.js"
28
+ },
29
+ "./runtime": {
30
+ "types": "./dist/runtime/index.d.ts",
31
+ "import": "./dist/runtime.js"
28
32
  }
29
33
  },
30
34
  "module": "./dist/index.js",
31
35
  "types": "./dist/plugin.d.ts",
32
36
  "devDependencies": {
37
+ "@mdx-js/mdx": "^3.1.1",
33
38
  "@rslib/core": "^0.23.2",
34
39
  "@rspress/core": "2.0.14",
40
+ "@rstest/core": "^0.10.6",
35
41
  "@types/html-to-text": "^9.0.4",
36
42
  "@types/lodash-es": "^4.17.12",
37
43
  "@types/react": "^18.3.11",
38
- "typescript": "6.0.3"
44
+ "@types/react-dom": "^18.3.1",
45
+ "react": "^19.2.6",
46
+ "react-dom": "^19.2.6",
47
+ "typescript": "7.0.2"
39
48
  },
40
49
  "dependencies": {
50
+ "@mdx-js/react": "^3.1.1",
41
51
  "cheerio": "1.0.0-rc.12",
42
52
  "html-to-text": "9.0.5",
43
53
  "lodash-es": "4.18.1",
44
54
  "@rspress/shared": "2.0.14",
45
- "@module-federation/sdk": "0.0.0-codex-node24-dev-node20-compat-ci-20260713063237",
46
- "@module-federation/enhanced": "0.0.0-codex-node24-dev-node20-compat-ci-20260713063237",
47
- "@module-federation/rsbuild-plugin": "0.0.0-codex-node24-dev-node20-compat-ci-20260713063237",
48
- "@module-federation/error-codes": "0.0.0-codex-node24-dev-node20-compat-ci-20260713063237"
55
+ "@module-federation/sdk": "0.0.0-codex-rspress-codeblock-transform-20260724100038",
56
+ "@module-federation/enhanced": "0.0.0-codex-rspress-codeblock-transform-20260724100038",
57
+ "@module-federation/rsbuild-plugin": "0.0.0-codex-rspress-codeblock-transform-20260724100038",
58
+ "@module-federation/error-codes": "0.0.0-codex-rspress-codeblock-transform-20260724100038"
49
59
  },
50
60
  "peerDependencies": {
51
- "@rspress/core": "2.0.14"
61
+ "@rspress/core": "2.0.14",
62
+ "react": ">=18.0.0"
52
63
  },
53
64
  "scripts": {
54
65
  "build": "rslib build",
55
66
  "build:watch": "rslib build --watch",
56
67
  "dev": "pnpm run build:watch",
68
+ "test": "rstest",
57
69
  "pre-release": "pnpm exec turbo run build --filter=@module-federation/webpack-bundler-runtime"
58
70
  }
59
71
  }