Skip to content

Support passing generateId to docsLoader()#3272

Merged
delucis merged 8 commits intomainfrom
chris/generate-id
Jul 16, 2025
Merged

Support passing generateId to docsLoader()#3272
delucis merged 8 commits intomainfrom
chris/generate-id

Conversation

@delucis
Copy link
Copy Markdown
Member

@delucis delucis commented Jul 2, 2025

Closes #3227

This PR makes it possible to pass generateId to Starlight’s docsLoader().

Currently, using generateId to customize content slugs is only possible by ejecting back up to Astro’s generic glob() loader, but this requires figuring out the pattern and base Starlight uses if you want to mirror the docsLoader() behaviour, e.g.:

-   loader: docsLoader(),
+   loader: glob({
+     pattern: "**/[^_]*.{md,mdx}",
+     base: "./src/content/docs",
+     generateId: customMethod
+   }),

After this PR you can keep using docsLoader(), and just add your ID method:

-   loader: docsLoader(),
+   loader: docsLoader({ generateId: customMethod }),

I did not also add an equivalent method to the i18nLoader as customizing slugs for languages seems much less common, and it would be more liable to break given how we load translation files in non-content collection contexts.

A big part of this PR was also figuring out how best to document this, as we currently only document our loaders in passing. This PR adds a “Configuring content collections” section to the config reference which documents all our loaders and schemas. I kept this minimal for now and linked out to existing relevant docs rather than duplicating or moving things around, but I’d welcome feedback on this new section.


It’s perhaps worth noting that there is some risk in supporting custom generateId methods. Especially for multilingual sites.

There are a few code paths running outside of the Astro/Vite context where Starlight uses raw file paths directly rather than the content collection ID. This is notably the case for remark/rehype plugins. For example, we use the file path to work out the current language for remark plugins. This means it won’t be possible to use generateId for entirely novel structures like storing localized content in sub-directories or something.

For example, moving the locale from a file name to the front of the ID like this wouldn’t work for our remark plugins:

// Example input: { entry: 'tutorials/intro/en.md' }
generateId: ({ entry }) => {
  const extensionlessPath = entry.split('.').slice(0, -1).join('.'); // "tutorials/intro/en"
  const segments = extensionlessPath.split('/');                     // ["tutorials", "intro", "en"]
  return [segments.at(-1), ...segments.slice(0, -1)].join('/');      // "en/tutorials/intro"
},

But for common scenarios like preserving uppercase characters or special characters like . inside IDs, it should work pretty OK although it is likely to break compatibility with plugins like https://github.com/HiDeoo/starlight-links-validator that rely on being able to predict the ID format based on the filepath.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Jul 2, 2025

🦋 Changeset detected

Latest commit: 24557af

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 Minor

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 2, 2025

Deploy Preview for astro-starlight ready!

Name Link
🔨 Latest commit 24557af
🔍 Latest deploy log https://app.netlify.com/projects/astro-starlight/deploys/6877aea9880f8000083b8771
😎 Deploy Preview https://deploy-preview-3272--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
Accessibility: 100
Best Practices: 92
SEO: 100
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 📚 docs Documentation website changes 🌟 core Changes to Starlight’s main package labels Jul 2, 2025
@astrobot-houston
Copy link
Copy Markdown
Contributor

astrobot-houston commented Jul 2, 2025

Lunaria Status Overview

🌕 This pull request will trigger status changes.

Learn more

By default, every PR changing files present in the Lunaria configuration's files property will be considered and trigger status changes accordingly.

You can change this by adding one of the keywords present in the ignoreKeywords property in your Lunaria configuration file in the PR's title (ignoring all files) or by including a tracker directive in the merged commit's description.

Tracked Files

Locale File Note
en manual-setup.mdx Source changed, localizations will be marked as outdated.
en reference/configuration.mdx Source changed, localizations will be marked as outdated.
Warnings reference
Icon Description
🔄️ The source for this localization has been updated since the creation of this pull request, make sure all changes in the source have been applied.

@astrobot-houston
Copy link
Copy Markdown
Contributor

astrobot-houston commented Jul 2, 2025

size-limit report 📦

Path Size
/index.html 7.21 KB (0%)
/_astro/*.js 25.76 KB (0%)
/_astro/*.css 14.25 KB (0%)

@delucis delucis added the 🌟 minor Change that triggers a minor release label Jul 2, 2025
Copy link
Copy Markdown
Member

@HiDeoo HiDeoo left a comment

Choose a reason for hiding this comment

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

Impressive how exposing such feature can be done quite easily and beautifully 👏 Like you mentioned, the bulk of the work is definitely the documentation (which imo is perfect).

Left a tiny comment, also wonder if there are some places where it could be relevant to link to this new documentation section, e.g.:

Nothing blocking, just some thoughts.

Like you mentioned and as we discussed earlier, there are definitely some potential limits to this feature or potential issues that could arise, but I still think it is a great addition, and in the case of plugins, if it's ever an issue, it should be relatively easy to support an equivalent option at the plugin level, to use a custom function instead of the default assumed github-slugger one.

delucis and others added 2 commits July 3, 2025 18:34
@delucis
Copy link
Copy Markdown
Member Author

delucis commented Jul 3, 2025

OK, perfect — thanks for the helpful feedback!

I decided against linking the two extend places you spotted (thanks for hunting those down) as the reference material for those is really thin, and don’t add to what you already have in those sections. But I added the links for docsLoader and docsSchema as those seem more helpful.

@delucis delucis added this to the v0.35 milestone Jul 4, 2025
@delucis delucis added the ✅ approved Pull requests that have been approved and are ready to merge when next cutting a release label Jul 4, 2025
trueberryless added a commit to trueberryless/withastro-starlight that referenced this pull request Jul 10, 2025
@delucis delucis merged commit e7fe267 into main Jul 16, 2025
16 checks passed
@delucis delucis deleted the chris/generate-id branch July 16, 2025 13:58
@astrobot-houston astrobot-houston mentioned this pull request Jul 16, 2025
Yoxnear pushed a commit to Yoxnear/starlight-custom that referenced this pull request Jul 23, 2025
Co-authored-by: HiDeoo <494699+HiDeoo@users.noreply.github.com>
keytech42 added a commit to keytech42/CoMB that referenced this pull request Sep 2, 2025
Root cause: topic overview MDX files had `draft: true` in frontmatter.
They were excluded from the production build by Astro/Starlight, which
caused 404s in preview and production.

Also includes framework upgrades to enable upcoming slug work:
- Astro: v5.8.2 → v5.13.5
- Starlight: v0.34.3 → v0.35.2

From Starlight v0.35.0, custom slug generation is supported to decouple
file structure from public URLs.

Refs: withastro/starlight#3272
keytech42 added a commit to keytech42/CoMB that referenced this pull request Sep 2, 2025
Root cause: 'Topic' overview MDX files had `draft: true` in frontmatter.
They were excluded from the production build by Astro/Starlight, which
caused 404s in preview and production.

This commit also includes framework upgrades to enable upcoming slug work:
- Astro: v5.8.2 → v5.13.5
- Starlight: v0.34.3 → v0.35.2

From Starlight v0.35.0, custom slug generation is supported to decouple
file structure from public URLs.

Refs: withastro/starlight#3272
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

✅ approved Pull requests that have been approved and are ready to merge when next cutting a release 🌟 core Changes to Starlight’s main package 📚 docs Documentation website changes 🌟 minor Change that triggers a minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using locales with regions (es-AR) makes build output more nested directories than necessary

3 participants