-
-
Notifications
You must be signed in to change notification settings - Fork 198
feat(mdx)!: support File Code Block of ```tsx file="./filename" to write demo code in another file #2361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat(mdx)!: support File Code Block of ```tsx file="./filename" to write demo code in another file #2361
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| // TODO: do not expose remarkPluginNormalizeLink as publicAPI | ||
| export { dev, build, serve, remarkPluginNormalizeLink } from './node'; | ||
| export { | ||
| dev, | ||
| build, | ||
| serve, | ||
| remarkPluginNormalizeLink, | ||
| remarkFileCodeBlock, | ||
| } from './node'; | ||
| export * from '@rspress/shared'; | ||
| export { mergeDocConfig } from '@rspress/shared/node-utils'; | ||
| export type { RouteService } from './node/route/RouteService'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
packages/core/src/node/mdx/remarkPlugins/__snapshots__/fileCodeBlock.test.ts.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
|
||
| exports[`remarkFileCodeBlock > basic 1`] = ` | ||
| "const frontmatter = {}; | ||
| import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from "react/jsx-runtime"; | ||
| import {useMDXComponents as _provideComponents} from "@mdx-js/react"; | ||
| function _createMdxContent(props) { | ||
| const _components = { | ||
| code: "code", | ||
| pre: "pre", | ||
| span: "span", | ||
| ..._provideComponents(), | ||
| ...props.components | ||
| }; | ||
| return _jsx(_Fragment, { | ||
| children: _jsx(_Fragment, { | ||
| children: _jsx(_components.pre, { | ||
| className: "shiki css-variables", | ||
| style: { | ||
| backgroundColor: "var(--shiki-background)", | ||
| color: "var(--shiki-foreground)" | ||
| }, | ||
| tabIndex: "0", | ||
| children: _jsxs(_components.code, { | ||
| className: "language-jsx", | ||
| children: [_jsxs(_components.span, { | ||
| className: "line", | ||
| children: [_jsx(_components.span, { | ||
| style: { | ||
| color: "var(--shiki-token-keyword)" | ||
| }, | ||
| children: "export" | ||
| }), _jsx(_components.span, { | ||
| style: { | ||
| color: "var(--shiki-token-keyword)" | ||
| }, | ||
| children: " default" | ||
| }), _jsx(_components.span, { | ||
| style: { | ||
| color: "var(--shiki-foreground)" | ||
| }, | ||
| children: " () " | ||
| }), _jsx(_components.span, { | ||
| style: { | ||
| color: "var(--shiki-token-keyword)" | ||
| }, | ||
| children: "=>" | ||
| }), _jsx(_components.span, { | ||
| style: { | ||
| color: "var(--shiki-foreground)" | ||
| }, | ||
| children: " {" | ||
| })] | ||
| }), "\\n", _jsxs(_components.span, { | ||
| className: "line", | ||
| children: [_jsx(_components.span, { | ||
| style: { | ||
| color: "var(--shiki-token-keyword)" | ||
| }, | ||
| children: " return" | ||
| }), _jsx(_components.span, { | ||
| style: { | ||
| color: "var(--shiki-foreground)" | ||
| }, | ||
| children: " <" | ||
| }), _jsx(_components.span, { | ||
| style: { | ||
| color: "var(--shiki-token-string-expression)" | ||
| }, | ||
| children: "div" | ||
| }), _jsx(_components.span, { | ||
| style: { | ||
| color: "var(--shiki-foreground)" | ||
| }, | ||
| children: ">hello world</" | ||
| }), _jsx(_components.span, { | ||
| style: { | ||
| color: "var(--shiki-token-string-expression)" | ||
| }, | ||
| children: "div" | ||
| }), _jsx(_components.span, { | ||
| style: { | ||
| color: "var(--shiki-foreground)" | ||
| }, | ||
| children: ">;" | ||
| })] | ||
| }), "\\n", _jsx(_components.span, { | ||
| className: "line", | ||
| children: _jsx(_components.span, { | ||
| style: { | ||
| color: "var(--shiki-foreground)" | ||
| }, | ||
| children: "}" | ||
| }) | ||
| }), "\\n", _jsx(_components.span, { | ||
| className: "line" | ||
| })] | ||
| }) | ||
| }) | ||
| }) | ||
| }); | ||
| } | ||
| export default function MDXContent(props = {}) { | ||
| const {wrapper: MDXLayout} = { | ||
| ..._provideComponents(), | ||
| ...props.components | ||
| }; | ||
| return MDXLayout ? _jsx(MDXLayout, { | ||
| ...props, | ||
| children: _jsx(_createMdxContent, { | ||
| ...props | ||
| }) | ||
| }) : _createMdxContent(props); | ||
| } | ||
|
|
||
| MDXContent.__RSPRESS_PAGE_META = {}; | ||
|
|
||
| MDXContent.__RSPRESS_PAGE_META["index.mdx"] = {"toc":[],"title":"","headingTitle":"","frontmatter":{}}; | ||
| " | ||
| `; |
32 changes: 32 additions & 0 deletions
32
packages/core/src/node/mdx/remarkPlugins/fileCodeBlock.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { fs, vol } from 'memfs'; | ||
|
|
||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { compile } from '../processor'; | ||
|
|
||
| vi.mock('node:fs/promises', () => { | ||
| return { readFile: fs.promises.readFile }; | ||
| }); | ||
|
|
||
| describe('remarkFileCodeBlock', () => { | ||
| it('basic', async () => { | ||
| vol.fromJSON({ | ||
| '/usr/rspress-project/docs/_Demo.jsx': `export default () => { | ||
| return <div>hello world</div>; | ||
| } | ||
| `, | ||
| }); | ||
| const result = await compile({ | ||
| source: ` | ||
| \`\`\`jsx file="./_Demo.jsx" | ||
| \`\`\` | ||
| `, | ||
| checkDeadLinks: false, | ||
| docDirectory: '/usr/rspress-project/docs', | ||
| filepath: '/usr/rspress-project/docs/index.mdx', | ||
| config: null, | ||
| pluginDriver: null, | ||
| routeService: null, | ||
| }); | ||
| expect(result).toMatchSnapshot(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { readFile } from 'node:fs/promises'; | ||
| import path from 'node:path'; | ||
| import { logger } from '@rspress/shared/logger'; | ||
| import type { Root } from 'mdast'; | ||
| import type { Plugin } from 'unified'; | ||
| import { visit } from 'unist-util-visit'; | ||
|
|
||
| const ERROR_PREFIX = '[remarkFileCodeBlock]'; | ||
|
|
||
| function parseFileFromMeta(meta: string | undefined): string { | ||
| if (!meta) { | ||
| return ''; | ||
| } | ||
| const kvList = meta.split(' ').filter(Boolean) as string[]; | ||
| for (const item of kvList) { | ||
| const [k, v] = item.split('='); | ||
| if (k === 'file' && v.length > 0) { | ||
| return v.replace(/["'`]/g, ''); | ||
| } | ||
| } | ||
| return ''; | ||
| } | ||
|
|
||
| export const remarkFileCodeBlock: Plugin<[{ filepath: string }], Root> = ({ | ||
| filepath, | ||
| }) => { | ||
| return async tree => { | ||
| const promiseList: Promise<void>[] = []; | ||
| visit(tree, 'code', node => { | ||
| const { meta, value } = node; | ||
| const file = parseFileFromMeta(meta ?? ''); | ||
|
|
||
| if (!file) { | ||
| return; | ||
| } | ||
|
|
||
| if (file.startsWith('./') || file.startsWith('../')) { | ||
| const resolvedFilePath = path.join(path.dirname(filepath), file); | ||
| // we allow blank lines or spaces, which may be necessary due to formatting tools and other reasons. | ||
| if (value.trim() !== '') { | ||
| logger.error(`${ERROR_PREFIX} The content of file code block should be empty. | ||
|
|
||
| \`\`\`tsx file="./filename" | ||
| content | ||
| \`\`\` | ||
|
|
||
| this usage is not allowed, please use below: | ||
|
|
||
| \`\`\`tsx file="./filename" | ||
| \`\`\` | ||
| `); | ||
| throw new Error( | ||
| `${ERROR_PREFIX} The content of file code block should be empty.`, | ||
| ); | ||
| } | ||
|
|
||
| const promise = readFile(resolvedFilePath, 'utf-8') | ||
| .then(fileContent => { | ||
| node.value = fileContent; | ||
| }) | ||
| .catch(e => { | ||
| logger.error(`${ERROR_PREFIX} The file does not exist. | ||
| \`file="${file}"\` is resolved to ${resolvedFilePath}"`); | ||
| throw e; | ||
| }); | ||
|
|
||
| promiseList.push(promise); | ||
| return; | ||
| } | ||
|
|
||
| // TODO: support resolve.alias with rspack-resolver | ||
| throw new Error( | ||
| `${ERROR_PREFIX} The file path should use relative path "./" or "../"`, | ||
| ); | ||
| }); | ||
|
|
||
| await Promise.all(promiseList); | ||
| }; | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.