Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide documentation on generating custom IDs using the glob loader #10703

Open
MoritzLost opened this issue Jan 14, 2025 · 1 comment
Open
Labels
add new content Document something that is not in docs. May require testing, confirmation, or affect other pages.

Comments

@MoritzLost
Copy link

MoritzLost commented Jan 14, 2025

📚 Subject area/topic

Content Collections

📋 Page(s) affected (or suggested, for new content)

https://docs.astro.build/en/guides/content-collections/

📋 Description of content that is out-of-date or incorrect

The glob loader generates IDs automatically based on the file path, but transforms those to lowercase and applies some other custom replacements:

https://github.com/withastro/astro/blob/c30070b9271e4c494e7cbf3a1c45515782034911/packages/astro/src/content/loaders/glob.ts#L35-L46
https://github.com/withastro/astro/blob/1072c76703b725919cf52f63cf254a3fba90c030/packages/astro/src/content/utils.ts#L403-L408

This is a breaking change from the behaviour of the slug property in Astro 4.

The glob loader accepts a generateId function to customize the way those IDs are generated, but that argument isn't documented anywhere. The page on Content Collections should be expanded:

  • Mention how the ID is generated by default (e.g. that it uses GitHub Slugger), and maybe what caveats come with that (for example, that uppercase will be transformed to lowercase by default).
  • Mention the possibility to use a custom generateId function, preferably with some example code.

For example, to retain uppercase letters in the ID, I used this:

loader: glob({
    pattern: '**/*.json',
    base: './src/content/foo',
    generateId: ({ entry }) => entry.replace(/\.json$/, ''),
}),

🖥️ Reproduction in StackBlitz (if reporting incorrect content or code samples)

No response

@sarah11918 sarah11918 added the add new content Document something that is not in docs. May require testing, confirmation, or affect other pages. label Jan 14, 2025
@sarah11918
Copy link
Member

Thank you @MoritzLost ! This will pair nicely with #10147 where we want to spend more time documenting each of the built-in loaders anyway. It's very helpful to specifically have your example for generateId which isn't yet in docs at all! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
add new content Document something that is not in docs. May require testing, confirmation, or affect other pages.
Projects
None yet
Development

No branches or pull requests

2 participants