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: When `contentPath` is the same as the `collectionPath`, some transformed paths are incorrect #18

Closed techfg closed 6 months ago

techfg commented 6 months ago

In a situation where the contentPath is the same as the collectionPath, transformed paths in a few situations do not generate correctly. While it seems a bit odd to have these be the same, it's actually the way Starlight works and Astro's docs are based on Starlight. Starlight hoists src/content/docs to the root of the site so instead of an expected path of /docs/subdir/page you get /subdir/page. In this scenario, the collectionPath becomes the same as the contentPath from a routing perspective.

[!NOTE] To utilize astro-rehype-relative-markdown-links with Starlight, the plugin configuration must contain contentPath: 'src/content/docs' for paths to resolve correctly. See Additional Information below for more details/thoughts.

Regarding the issue that this all presents:

  1. References to the root index.md transform to empty string (''). For a page in the docs folder (the effective root) referencing the "root" index page:

    Markdown: [home page](./index.md)
    Transformed Url: `''`
  2. Pages with custom slug have their collection paths "double-up".

    • For a page docs/subdir/mypage.md with a custom slug of subdir/my-page, the following markdown in a page in docs (root) folder:
      Markdown: [My Page](./subdir/mypage.md)
      Transformed Url: `subdir/subdir/my-page)
    • For a page docs/myslugpage.md with a custom slug of my-slug-page, the following markdown in a page in docs (root) folder:
      Markdown: [My Slug Page](./myslugpage.md)
      Transformed Url: `myslugpagemd/my-slug-page`

Repro:

With Proposed Fixes from PR #21:

Steps to reproduce:

  1. Open repro
    • NOTE: The site opens at /index because that is the default behavior of Astro for index documents in the site/collection root. Setting the slug to undefined is required for the route to be / for root index pages. The code in the repro will handle undefined but its required to set the slug to empty string ('') on the src/content/docs/index.md file (slug: '').
  2. Inspect Root Index in dev console under Issue #18 (there is no hyperlink generated)

Expected Result: Link generated is /index and navigation successful

Actual Result: Linked generated is '' and no href in anchor tag (<a>Root Index</a>)

  1. Go back to site root
  2. Click on Root Custom Slug under Issue #18

Expected Result: Link generated is /custom-slug and navigation successful

Actual Result: Link generated is /customslugmd/custom-slug and 404

  1. Go back to site root
  2. Click on Guides Custom Slug under Issue #18

Expected Result: Link generated is /guides/example-custom-slug and navigation successful

Actual Result: Link generated is /guides/guides/example-custom-slug and 404

Additional Information

The requirement to have contentPath configured on the plugin to match the docs collection that Starlight uses (or anything else out there that uses a subfolder of content as the effective site root), makes it such that any other content collections are going to have issues with this plugin since there is only one contentPath currently from the plugins perspective. This is something that should likely get added to the README and possible consideration for future feature to offer a filter option so that the plugin could be added multiple times or an object/array that contains mappings. Something to consider but not for today :)

techfg commented 6 months ago

FYI - Edited OP to adjust some details that were incorrect/lacking clarity. Also updated repro link with one that eliminates Starlight and just uses stock Astro functionality for simplicity. Also added a Stackblitz to the repro that contains the proposed fixes from #21.

Starlight based repro from original post still available here.

vernak2539 commented 6 months ago

Released in v0.10.0. Thanks!