Plugin Docs: Resolve relative asset paths from doc file directory#2611
Open
Plugin Docs: Resolve relative asset paths from doc file directory#2611
Conversation
Contributor
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves plugin docs asset URL rewriting by letting the parser resolve relative image/asset paths from the current doc file’s directory (instead of always from the docs root), using URL-style resolution semantics. It also updates the CLI dev server to use the same rewriting path as production by passing a root-relative assetBaseUrl.
Changes:
- Add optional
filecontext to asset rewriting soimg/...resolves relative to the markdown file’s directory. - Switch asset path rewriting to
new URL()resolution (normalizing./and../segments). - Update the CLI docs dev server (and tests/fixtures) to pass
{ assetBaseUrl: '/', file: page.file }and validate the behavior.
Reviewed changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/plugin-docs-parser/src/plugins/rehype-rewrite-asset-paths.ts | Introduces URL-based resolver + optional file context for per-page asset resolution. |
| packages/plugin-docs-parser/src/plugins/rehype-rewrite-asset-paths.test.ts | Adds coverage for per-page resolution and root-relative base behavior. |
| packages/plugin-docs-parser/src/parser.ts | Plumbs new file option through parseMarkdown to the rehype plugin. |
| packages/plugin-docs-cli/src/server/server.ts | Uses parser asset rewriting during local preview with assetBaseUrl: '/' and file: page.file. |
| packages/plugin-docs-cli/src/server/server.test.ts | Adds an integration test asserting correct per-directory image resolution in CLI output. |
| packages/plugin-docs-cli/src/fixtures/test-docs/config/database.md | Adds relative-image examples used by the new CLI server test. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does / why we need it:
Adds an optional
fileparameter toparseMarkdownso relative image srcs resolve from the doc file's directory rather than the docs root. Switches to URL-based resolution vianew URL(), which handles./,../and trailing slashes without per-caller logic.The CLI dev server now passes
{ assetBaseUrl: '/', file: page.file }so local preview exercises the same rewrite code path as production.Side effect of switching to
new URL(): srcs containing./or../segments are now normalized in the output (e.g.base/../img/y.png→base/img/y.png) instead of being passed through literally. The literal-segment behavior was technically broken anyway, so this is a quiet correctness improvement, not a regression.Which issue(s) this PR fixes:
No issue - part of the multi-page plugin docs initiative. Marketing-website consumer change to follow.
📦 Published PR as canary version:
Canary Versions✨ Test out this PR locally via:
npm install website@5.5.4-canary.2611.25323306221.0 npm install @grafana/create-plugin@7.3.1-canary.2611.25323306221.0 npm install @grafana/plugin-docs-cli@0.0.11-canary.2611.25323306221.0 npm install @grafana/plugin-docs-parser@0.0.5-canary.2611.25323306221.0 # or yarn add website@5.5.4-canary.2611.25323306221.0 yarn add @grafana/create-plugin@7.3.1-canary.2611.25323306221.0 yarn add @grafana/plugin-docs-cli@0.0.11-canary.2611.25323306221.0 yarn add @grafana/plugin-docs-parser@0.0.5-canary.2611.25323306221.0