Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 17, 2025

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.

Copy link

netlify bot commented Oct 17, 2025

Deploy Preview for rspress-v2 failed. Why did it fail? →

Name Link
🔨 Latest commit abe9c02
🔍 Latest deploy log https://app.netlify.com/projects/rspress-v2/deploys/68f1ff17cb7f430007841494

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@Copilot Copilot AI changed the title [WIP] Enhance remarkFileCodeBlock to support absolute file paths feat: support absolute paths with <root>/ prefix in remarkFileCodeBlock Oct 17, 2025
@Copilot Copilot AI requested a review from SoonIter October 17, 2025 08:36
Copilot finished work on behalf of SoonIter October 17, 2025 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants