feat(mdx): support absolute paths with <root>/ prefix in remarkFileCodeBlock#2665
feat(mdx): support absolute paths with <root>/ prefix in remarkFileCodeBlock#2665
<root>/ prefix in remarkFileCodeBlock#2665Conversation
✅ Deploy Preview for rspress-v2 ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
|
<root>/ prefix in remarkFileCodeBlock
…arkFileCodeBlock Co-authored-by: SoonIter <79413249+SoonIter@users.noreply.github.com>
abe9c02 to
e112231
Compare
Rsdoctor Bundle Diff Analysis📁 webPath:
📦 Download Diff Report: web Bundle Diff Generated by Rsdoctor GitHub Action |
<root>/ prefix in remarkFileCodeBlock<root>/ prefix in remarkFileCodeBlock
There was a problem hiding this comment.
Pull request overview
This PR adds support for absolute path references in the remarkFileCodeBlock plugin using a <root>/ prefix, complementing the existing relative path support (./ and ../). This enables users to reference code files using consistent absolute paths from the documentation root, avoiding complex relative paths in deeply nested MDX files.
- Added
<root>/prefix syntax for absolute paths resolved from the document root directory - Extended
remarkFileCodeBlockplugin signature to acceptdocDirectoryparameter - Added comprehensive test coverage for the new absolute path feature
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/core/src/node/mdx/remarkPlugins/fileCodeBlock.ts | Implements path resolution logic for <root>/ prefix and updates plugin signature to accept docDirectory parameter |
| packages/core/src/node/mdx/remarkPlugins/fileCodeBlock.test.ts | Adds test cases for absolute path resolution, nested paths, and error scenarios |
| packages/core/src/node/mdx/options.ts | Passes docDirectory parameter to the remarkFileCodeBlock plugin |
| packages/plugin-llms/src/normalizeMdFile.ts | Passes docDirectory from routeService.getDocsDir() to the remarkFileCodeBlock plugin |
Comments suppressed due to low confidence (1)
packages/core/src/node/mdx/remarkPlugins/fileCodeBlock.ts:79
- The error message for non-empty content blocks only shows the relative path syntax example. For consistency with the newly added
<root>/syntax support, consider updating this error message to also show the absolute path syntax option, similar to the error message at lines 103-113.
Example update:
logger.error(`${ERROR_PREFIX} ${originalMetaForErrorInfo} The content of file code block should be empty.
\`\`\`tsx file="./filename"
content
\`\`\`
this usage is not allowed, please use below:
\`\`\`tsx file="./filename"
\`\`\`
or
\`\`\`tsx file="<root>/path/to/filename"
\`\`\`
`); logger.error(`${ERROR_PREFIX} ${originalMetaForErrorInfo} The content of file code block should be empty.
\`\`\`tsx file="./filename"
content
\`\`\`
this usage is not allowed, please use below:
\`\`\`tsx file="./filename"
\`\`\`
`);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Overview
This PR enhances the
remarkFileCodeBlockplugin to support absolute paths using the<root>/prefix, in addition to the existing relative path support.Motivation
Previously, users could only reference files using relative paths (
./or../), which made it challenging to maintain consistent imports across different documentation locations. When reorganizing docs or referencing files from deeply nested MDX files, users had to use complex relative paths like../../../src/components/Button.tsx.Changes
1. Enhanced Path Resolution
The plugin now supports three path formats:
./file.tsx- Relative to current file (existing)../file.tsx- Relative to parent directory (existing)<root>/path/to/file.tsx- Absolute from document root (new)2. Implementation
Modified
fileCodeBlock.ts:docDirectoryparameter to plugin signature<root>/prefix:Updated
options.ts:docDirectoryparameter to the plugin3. Usage Example
Before:
After:
Both syntaxes continue to work, providing flexibility for different use cases.
Benefits
Testing
Added comprehensive test coverage including:
All existing tests continue to pass, ensuring no breaking changes.
Closes #ISSUE_NUMBER
Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.