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

Special characters excluded from content collection slug #11458

Closed
1 task
nicolabovolato opened this issue Jul 12, 2024 · 18 comments
Closed
1 task

Special characters excluded from content collection slug #11458

nicolabovolato opened this issue Jul 12, 2024 · 18 comments
Labels
- P2: nice to have Not breaking anything but nice to have (priority) feat: content collections Related to the Content Collections feature (scope)

Comments

@nicolabovolato
Copy link

Astro Info

Astro                    v4.11.5
Node                     v20.15.0
System                   macOS (arm64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             @astrojs/preact
                         @astrojs/tailwind
                         @astrojs/mdx
                         @astrojs/partytown
                         @astrojs/sitemap

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

On my personal website there's among others a basic tutorial for C and one for C++.

I'm currently migrating from pages to content collections, but il looks like the + sign gets stripped in the slug.

09:13:48 [ERROR] [DuplicateContentEntrySlugError] blog contains multiple entries with the same slug: `it/tutorial-c/introduzione`. Slugs must be unique.
Entries: 
- /src/content/blog/it/tutorial-c++/introduzione/index.mdx
- /src/content/blog/it/tutorial-c/introduzione/index.mdx
  Error reference:
    https://docs.astro.build/en/reference/errors/duplicate-content-entry-slug-error/
  Stack trace:
    at REDACTED/node_modules/astro/dist/content/vite-plugin-content-virtual-mod.js:213:19

What's the expected result?

From what I found in the docs, there's no mention of any character stripping when generating the slug.
I think these character should be kept as it also provides better compatibility with Astro pages.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-ywpupp

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Jul 12, 2024
@Princesseuh Princesseuh added - P3: minor bug An edge case that only affects very specific usage (priority) feat: content collections Related to the Content Collections feature (scope) and removed needs triage Issue needs to be triaged labels Jul 16, 2024
@bgentry
Copy link

bgentry commented Jul 21, 2024

I believe I actually just ran into this as well. I'm trying to build a content collection for a software library that includes versioned URLs (so I can have docs for all versions). A pkg collection with content files like src/content/pkg/v0.0.1/sub/index.mdoc will have its slug returned as v001/sub. I can of course override that slug back to its "true" form of v0.0.1/sub, but I'll have to do that for every single file—definitely not ideal!

I would love to be able to opt out of sanitizing actually-supported characters out of my slugs in some fashion.

@matthewp
Copy link
Contributor

You can set your own custom slug on individual entries: https://docs.astro.build/en/guides/content-collections/#defining-custom-slugs

I don't think we want to change our default slugging algorithm.

@bgentry
Copy link

bgentry commented Jul 30, 2024

Wasn’t suggesting altering any defaults, and yes I’m aware that the slug can be customized (I mentioned above that I understood this). The problem is in my case I have to do that for every single file, all to work around a slug algorithm that really isn’t documented and certainly isn’t customizable.

Ideally I would be able to customize this behavior globally or within a collection so that I don’t need to override the frontmatter in every single file (which I’m having to do in a script due to my content being autogenerated).

@matthewp
Copy link
Contributor

A global config way of doing it could work. Do you have any suggestions as to what that would look like?

@bgentry
Copy link

bgentry commented Jul 30, 2024

Conceptually it seems like the slug transformer takes a file path string as input, and returns a transformed string as output. If the current logic can be easily wrapped into a function of that form, then a collection level slugTransform or slugTransformer or slugNormalize setting of that form would work. I am not too opinionated about the naming here, you would probably have better ideas about how to fit it in!

@matthewp
Copy link
Contributor

slugTransform probably fits with how we tend to name things. Any chance you'd be able to contribute this change?

@bgentry
Copy link

bgentry commented Jul 30, 2024

I wish I could, but I have a new baby coming any day now and I’m spread very thin atm 😅 If things settle down during my leave I may be able to pick it up in a month or two.

I think the proposal sounds great though. This would really make the content collections a lot more flexible for different use cases imo.

@matthewp matthewp added - P2: nice to have Not breaking anything but nice to have (priority) and removed - P3: minor bug An edge case that only affects very specific usage (priority) labels Jul 30, 2024
@matthewp
Copy link
Contributor

cc @ascorbic how does content layer treat slugs? Is it still a special thing? Is this type of request something that could be incorporated there?

@ematipico
Copy link
Member

It's already handled. The glob loader provide a generatedId function that allows to customise the slug of the content collections

@ascorbic
Copy link
Contributor

ascorbic commented Jul 31, 2024

It uses IDs instead of slugs, but you can provide a generateID function to glob to take over generating the ID, which would let you handle that kind of thing.

https://github.com/withastro/astro/blob/content-layer/packages/astro/src/content/loaders/glob.ts#L30

@matthewp
Copy link
Contributor

Ok, in that case we probably won't add an option for the v1 CC and encourage people to switch to the new API.

@bgentry
Copy link

bgentry commented Jul 31, 2024

Amazing! I'll be eagerly awaiting the new content layer / #11360 🚀 🙏

@matthewp
Copy link
Contributor

Going to close as there's nothing actionable to do here at this time.

@matthewp matthewp closed this as not planned Won't fix, can't repro, duplicate, stale Jul 31, 2024
@nicolabovolato
Copy link
Author

@ematipico
Copy link
Member

@nicolabovolato

Content layer solves this problem. The current content collections we will slowly be phased out

@nicolabovolato
Copy link
Author

@ematipico

I'm guessing that it truly is experimental then 😄

@ascorbic
Copy link
Contributor

@nicolabovolato you need to move your files out of src/content. Right now they're being processed using the old content collections.

@nicolabovolato
Copy link
Author

Thanks @ascorbic, working fine now 🙌🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P2: nice to have Not breaking anything but nice to have (priority) feat: content collections Related to the Content Collections feature (scope)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants