Skip to content

feat(mdx): support absolute paths with <root>/ prefix in remarkFileCodeBlock#2665

Merged
SoonIter merged 4 commits intomainfrom
copilot/enhance-remark-file-codeblock
Dec 4, 2025
Merged

feat(mdx): support absolute paths with <root>/ prefix in remarkFileCodeBlock#2665
SoonIter merged 4 commits intomainfrom
copilot/enhance-remark-file-codeblock

Conversation

Copy link
Copy Markdown
Contributor

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:

```tsx file="../../../src/components/Button.tsx"
```

After:

```tsx file="<root>/src/components/Button.tsx"
```

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.

@netlify
Copy link
Copy Markdown

netlify Bot commented Oct 17, 2025

Deploy Preview for rspress-v2 ready!

Name Link
🔨 Latest commit a65faea
🔍 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.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Oct 17, 2025

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.

✅ SoonIter
❌ Copilot
You have signed the CLA already but the status is still pending? Let us recheck it.

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 AI requested a review from SoonIter October 17, 2025 08:36
Copilot AI and others added 2 commits December 4, 2025 15:08
…arkFileCodeBlock

Co-authored-by: SoonIter <79413249+SoonIter@users.noreply.github.com>
@SoonIter SoonIter force-pushed the copilot/enhance-remark-file-codeblock branch from abe9c02 to e112231 Compare December 4, 2025 07:10
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Dec 4, 2025

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

@SoonIter SoonIter changed the title feat: support absolute paths with <root>/ prefix in remarkFileCodeBlock feat(mdx): support absolute paths with <root>/ prefix in remarkFileCodeBlock Dec 4, 2025
@SoonIter SoonIter requested a review from Timeless0911 December 4, 2025 07:31
@SoonIter SoonIter marked this pull request as ready for review December 4, 2025 07:32
Copilot AI review requested due to automatic review settings December 4, 2025 07:32
Timeless0911
Timeless0911 previously approved these changes Dec 4, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 remarkFileCodeBlock plugin signature to accept docDirectory parameter
  • 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.

Comment thread packages/core/src/node/mdx/remarkPlugins/fileCodeBlock.ts
Comment thread packages/core/src/node/mdx/remarkPlugins/fileCodeBlock.test.ts
@SoonIter SoonIter merged commit 69926c8 into main Dec 4, 2025
8 of 9 checks passed
@SoonIter SoonIter deleted the copilot/enhance-remark-file-codeblock branch December 4, 2025 09:32
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.

5 participants