Skip to content

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

@MoritzLost

Description

@MoritzLost

📚 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    add new contentDocument something that is not in docs. May require testing, confirmation, or affect other pages.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions