Skip to content

fix: map relative markdown links to post URLs - #784

Open
Henglie wants to merge 1 commit into
saicaca:mainfrom
Henglie:fix/relative-post-links
Open

Henglie wants to merge 1 commit into
saicaca:mainfrom
Henglie:fix/relative-post-links

Conversation

@Henglie

@Henglie Henglie commented Nov 14, 2026

Copy link
Copy Markdown

Type of change

  • Bug fix (a non-breaking change that fixes an issue)
  • New feature (a non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other (please describe):

Checklist

  • I have read the CONTRIBUTING document.
  • I have checked to ensure that this Pull Request is not for personal changes.
  • I have performed a self-review of my own code.
  • My changes generate no new warnings.

Related Issue

Fixes #744

Changes

Links such as [B](B.md) were left as B.md, so the browser resolved them relative to the current post URL (/posts/A/B.md) instead of the target post (/posts/B/).

  • Added src/plugins/remark-post-links.mjs, which rewrites relative .md/.mdx links inside src/content/posts to the same URL Astro generates for the target entry.
  • Uses Astro's slug rules (github-slugger per path segment, /index removed), supports nested directories, query strings and fragments, and respects base.
  • Leaves external, absolute, anchor, missing and non-markdown links unchanged.
  • Registered the plugin in astro.config.mjs; added github-slugger as a direct dependency and a pnpm test script.

How To Test

  • pnpm test (4 tests, all pass).
  • pnpm build (passes; Pagefind indexed 8 pages).
  • Build-level regression check with two temporary posts:
    • without the plugin: <a href="relative-link-b.md"> in /posts/relative-link-a/index.html, resolving to /posts/relative-link-a/relative-link-b.md;
    • with the plugin: <a href="/posts/relative-link-b/">.
  • pnpm exec biome check src/plugins/remark-post-links.mjs tests/remark-post-links.test.mjs passes.

Screenshots (if applicable)

N/A.

Additional Notes

  • pnpm check and pnpm type-check still fail on main in unchanged files (src/components/Navbar.astro, src/pages/archive.astro, and the --isolatedDeclarations baseline). These failures are not introduced by this change and are not claimed as passing.
  • Not verified: clicking the generated link in a real browser; the generated HTML was inspected from a headless build instead.
  • The test uses Node's built-in test runner, so no new test framework was added.

@vercel

vercel Bot commented Nov 14, 2026

Copy link
Copy Markdown

@Henglie is attempting to deploy a commit to the zephyirdgmailcom's projects Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: cross posts links are not mapped/converted 同目录文章链接未转化

1 participant