rspress icon indicating copy to clipboard operation
rspress copied to clipboard

feat: support absolute paths with `<root>/` prefix in remarkFileCodeBlock

Open Copilot opened this issue 2 months ago β€’ 2 comments

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 docDirectory parameter 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 docDirectory parameter 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.

Copilot avatar Oct 17 '25 08:10 Copilot

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...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

netlify[bot] avatar Oct 17 '25 08:10 netlify[bot]

CLA assistant check
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.

CLAassistant avatar Oct 17 '25 08:10 CLAassistant

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

github-actions[bot] avatar Dec 04 '25 07:12 github-actions[bot]