vernak2539 / astro-rehype-relative-markdown-links

Rehype Plugin for Astro that allows for usage of relative links between markdown files
https://www.npmjs.com/package/astro-rehype-relative-markdown-links
12 stars 4 forks source link

bug: Links are generated for excluded content collection files #25

Closed techfg closed 5 months ago

techfg commented 6 months ago

Astro permits excluding pages within a content collection directory from having a page generated by prefixing the filename with an underscore (_exclude-me.md).

Currently, there is no check to exclude file paths that map to excluded files which results in a transformed url that is invalid because Astro has no route for the page.

For example, in the following directory structure the file _exclude-me.md should not resolve to a valid path when referenced and should not have its linked transformed.

.
├── src/
│   ├── content/
│   │   ├── docs/
│   │   |   └── test.md
│   │   |   └── _exclude-me.md
|    ├── pages/
│   │   ├── docs/
│   │   |   └── [...slug].astro

Repro: https://stackblitz.com/edit/github-pdzrjo-pk1zkm

Steps to reproduce:

  1. Open repro
  2. Click on either Exclude Me link

Expected Behavior The navigation should fail because the file started with _ and the url in the anchor tag should be what was specified in the markdown (../content/docs/_exclude-me.md) as it shouldn't have been transformed.

Actual Behavior The navigation fails (which is expected), however the url is transformed to /docs/_exclude-me & /docs/subdir/_exclude_me which are invalid routes.

Additional Information

If you go to the Debug page you'll see there are no links generated to the _exclude-me.md pages because there is no route for them in the content collection.

Proposed Solution:

In the method isValidRelativeLink, if the file name starts with _, treat it as invalid and do not transform the link.

vernak2539 commented 5 months ago

Nice one, thanks! Released in v0.13.1