Skip to content

Fix absolutePathToLang() issue#3298

Merged
delucis merged 3 commits intowithastro:mainfrom
HiDeoo:hd-fix-absolute-path-to-lang-spaces
Jul 15, 2025
Merged

Fix absolutePathToLang() issue#3298
delucis merged 3 commits intowithastro:mainfrom
HiDeoo:hd-fix-absolute-path-to-lang-spaces

Conversation

@HiDeoo
Copy link
Copy Markdown
Member

@HiDeoo HiDeoo commented Jul 14, 2025

Description

This PR fixes an issue with the absolutePathToLang() helper function not handling absolute paths with spaces correctly.

  • absolutePathToLang() is meant to be called with the path property of a virtual file (altho not mandatory), which is always a POSIX-style path (using / as a separator) no matter the operating system.
  • It calls getCollectionPath() that relies on an URL pathname which will also be POSIX-style but also encoded, e.g. spaces are replaced with %20.
  • A virtual file's path property does not encodes special characters like spaces.

This can lead to issues when the path contains spaces which can be solved in different approaches:

  • Using fileURLToPath() in getCollectionPath() to convert the URL to a file path, altho depending on the OS, different separators will be used and would require an additional step to convert it again to a POSIX-style path.
  • Relying on pathToFileURL() on the input path of absolutePathToLang() to convert it to a URL-encoded path also using POSIX-style separators and using its pathname.

I went with the second approach which replaces an existing step instead of adding a new one.

On top of the added test, I also manually tested on Windows using a path with spaces:

Before After
before after

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Jul 14, 2025

🦋 Changeset detected

Latest commit: 5df5ec1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@astrojs/starlight Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify
Copy link
Copy Markdown

netlify bot commented Jul 14, 2025

Deploy Preview for astro-starlight ready!

Name Link
🔨 Latest commit 5df5ec1
🔍 Latest deploy log https://app.netlify.com/projects/astro-starlight/deploys/68766071329d390008188620
😎 Deploy Preview https://deploy-preview-3298--astro-starlight.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 100 (no change from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 100 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions bot added the 🌟 core Changes to Starlight’s main package label Jul 14, 2025
Copy link
Copy Markdown
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @HiDeoo, this looks good.

One question: absolutePathToLang() is a public API. Would we expect this change to impact Starlight plugin authors at all? I notice we have the test file changing the direct use of URL#pathname, but I guess that was a slightly unusual usage and not something we’d expect real-world users to be doing?

@HiDeoo
Copy link
Copy Markdown
Member Author

HiDeoo commented Jul 15, 2025

Following your comment, I pushed 2 changes that I thought would be useful:

  • I mentioned in the changeset that this function is part of the plugin API.
  • I added a comment on why we are specifically using fileURLToPath() in our tests.

To answer your question, the only way this change would impact a plugin API authors would be if they were somehow relying on the previous behavior that would result in an invalid language in some cases, e.g. with spaces in the path (I cannot imagine why or how). Otherwise, it's a fix that would benefit all plugins using that API.

Copy link
Copy Markdown
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect — thanks very much @HiDeoo! 🌟

@delucis delucis added the 🌟 patch Change that triggers a patch release label Jul 15, 2025
@delucis delucis merged commit 7bd02e3 into withastro:main Jul 15, 2025
15 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Jul 15, 2025
HiDeoo added a commit to shubham-padia/starlight that referenced this pull request Jul 16, 2025
* main:
  Small updates to `tailwind` template (withastro#3303)
  docs: showcase `astro-d2` and "Starlight Plugins by Example" (withastro#3302)
  [ci] release (withastro#3301)
  Fix `absolutePathToLang()` issue (withastro#3298)
Yoxnear pushed a commit to Yoxnear/starlight-custom that referenced this pull request Jul 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🌟 core Changes to Starlight’s main package 🌟 patch Change that triggers a patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When the starlight project is located on a path with blank, some test run fails

2 participants