Skip to content

Plugin Docs: Resolve relative asset paths from doc file directory#2611

Open
sunker wants to merge 3 commits intomainfrom
sunker/parser-per-page-assets
Open

Plugin Docs: Resolve relative asset paths from doc file directory#2611
sunker wants to merge 3 commits intomainfrom
sunker/parser-per-page-assets

Conversation

@sunker
Copy link
Copy Markdown
Contributor

@sunker sunker commented May 4, 2026

What this PR does / why we need it:

Adds an optional file parameter to parseMarkdown so relative image srcs resolve from the doc file's directory rather than the docs root. Switches to URL-based resolution via new 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.pngbase/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

@sunker sunker added the patch Increment the patch version when merged label May 4, 2026
@sunker sunker changed the title Plugin Docs Parser: resolve relative asset paths from doc file directory Plugin Docs: resolve relative asset paths from doc file directory May 4, 2026
@sunker sunker changed the title Plugin Docs: resolve relative asset paths from doc file directory Plugin Docs: Resolve relative asset paths from doc file directory May 4, 2026
@grafana-plugins-platform-bot grafana-plugins-platform-bot Bot moved this from 📬 Triage to 🔬 In review in Grafana Catalog Team May 4, 2026
@sunker sunker marked this pull request as ready for review May 4, 2026 13:42
@sunker sunker requested a review from a team as a code owner May 4, 2026 13:42
@sunker sunker added the release Create a release when this pr is merged label May 4, 2026
@sunker sunker self-assigned this May 4, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 4, 2026

Hello! 👋 This repository uses Auto for releasing packages using PR labels.

✨ This PR can be merged and will trigger a new patch release.
NOTE: When merging a PR with the release label please avoid merging another PR. For further information see here.

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 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 file context to asset rewriting so img/... 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.

Comment thread packages/plugin-docs-parser/src/plugins/rehype-rewrite-asset-paths.ts Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

patch Increment the patch version when merged release Create a release when this pr is merged

Projects

Status: 🔬 In review

Development

Successfully merging this pull request may close these issues.

2 participants