Skip to content

Astro, fix .md in links

Using Obsidian to edit my Markdown files, I’ve started to link lessons using Obsidian’s own internal linking.

It’s great because with Obsidian link tracking I can move a file around, change folder or filename, and links don’t break.

Links however use a .md extension, since we’re linking markdown files here.

[3-absolute-positioning](../css-positioning/3-absolute-positioning.md)

So since I’m using Astro, links don’t work as they point to a URL ending with .md.

I found this rehype plugin for Astro: https://github.com/vernak2539/astro-rehype-relative-markdown-links

It fixes this exact problem and I’m so grateful this exists, but didn’t (at the time of writing) install on Astro 3, required Astro 2. Maybe now it’s updated.

Anyway since it’s a single file I copied the file locally and imported in my Astro config.

Only thing I noticed is, maybe because I use content collections, my content collection path was prepended to URLs now, so I hardcoded a little custom fix in the code that only works in my use case, but well, job done.

→ Download my free JavaScript Handbook!

THE VALLEY OF CODE

THE WEB DEVELOPER's MANUAL

You might be interested in those things I do:

  • Learn to code in THE VALLEY OF CODE, your your web development manual
  • Find a ton of Web Development projects to learn modern tech stacks in practice in THE VALLEY OF CODE PRO
  • I wrote 16 books for beginner software developers, DOWNLOAD THEM NOW
  • Every year I organize a hands-on cohort course coding BOOTCAMP to teach you how to build a complex, modern Web Application in practice (next edition February-March-April-May 2024)
  • Learn how to start a solopreneur business on the Internet with SOLO LAB (next edition in 2024)
  • Find me on X

Related posts that talk about astro: