feat: support absolute paths with `<root>/` prefix in remarkFileCodeBlock
Overview
This PR enhances the remarkFileCodeBlock plugin 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:
- Added
docDirectoryparameter to plugin signature - Implemented path resolution logic for
<root>/prefix:if (file.startsWith('<root>/')) { resolvedFilePath = path.join(docDirectory, file.slice('<root>/'.length)); } - Updated error messages to document the new syntax
Updated options.ts:
- Passed
docDirectoryparameter to the plugin
3. Usage Example
Before:
After:
Both syntaxes continue to work, providing flexibility for different use cases.
Benefits
- Consistency: Same path works from any MDX file location
- Maintainability: Easier to reorganize documentation structure without updating all paths
- Clarity: Absolute paths are more explicit than complex relative paths
- Backward Compatible: All existing relative paths continue to work
Testing
Added comprehensive test coverage including:
- β Basic absolute path resolution
- β Nested absolute paths
- β Error handling for non-existent files
- β Error handling for non-empty content blocks
All existing tests continue to pass, ensuring no breaking changes.
Closes #ISSUE_NUMBER
Original prompt
ε’εΌΊ remarkFileCodeBlock εθ½
η°ε¨ζ―ζ file ηθ·―εΎδΈΊηΈε―Ήθ·―εΎ
η°ε¨ιθ¦ζ―ζη»ε―Ήθ·―εΎοΌζ―ζη¨ζ·εζ³δΈΊ
π¬ Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.
Deploy Preview for rspress-v2 ready!
| Name | Link |
|---|---|
| Latest commit | a65faea93917ea3a0770f57b50f91c853478c83a |
| Latest deploy log | https://app.netlify.com/projects/rspress-v2/deploys/69313e28e11a8a0008f04a46 |
| Deploy Preview | https://deploy-preview-2665--rspress-v2.netlify.app |
| Preview on mobile | Toggle QR Code...Use your smartphone camera to open QR code link. |
To edit notification comments on pull requests, go to your Netlify project configuration.
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.
:white_check_mark: SoonIter
:x: Copilot
You have signed the CLA already but the status is still pending? Let us recheck it.
Rsdoctor Bundle Diff Analysis
π web
Path: website/doc_build/web/rsdoctor-data.json
π Baseline Commit:
8da52ec1e9| PR: #2832
| Metric | Current | Baseline | Change |
|---|---|---|---|
| π Total Size | 14.5 MB | 14.5 MB | 5.8 KB (0.0%) |
| π JavaScript | 13.9 MB | 13.9 MB | 4.4 KB (0.0%) |
| π¨ CSS | 113.7 KB | 113.7 KB | 0 B (0.0%) |
| π HTML | 0 B | 0 B | N/A |
| π Other Assets | 501.3 KB | 499.9 KB | 1.5 KB (0.3%) |
π¦ Download Diff Report: web Bundle Diff
Generated by Rsdoctor GitHub Action