astro icon indicating copy to clipboard operation
astro copied to clipboard

🐛 BUG: Add an API to provide custom slugify function

Open naiyerasif opened this issue 2 years ago • 5 comments

Would it be possible to let folks pass their own slugify function to generate slugs for markdown headings? Some usecases for this are

  • backward compatibility (if I'm migrating from some other framework)
  • customization of slugs (one off custom transformations)
  • handling special usecases for non-latin languages (like Urdu)

Possible alternative

If it is too much, @sindresorhus/slugify handles a lot more polyglot usecases. Might be helpful for a better out of box experience.

Participation

  • [ ] I am willing to submit a pull request for this issue.

Note

  • Cross-posting this from Discord discussion. Let me know if this needs to be moved to RFC repo as a discussion.
  • Also, maybe there should be a template for feature requests (since this is one).

naiyerasif avatar May 28 '22 16:05 naiyerasif

Hi, slugging headings is done by rehype-slug, which does not support providing custom slugify functions at the moment. This can be done by using another rehype plugin which can support custom slugify function or uses @sindresorhus/slugify.

That said, I don't see how we can add an API in Astro for custom slugify functions.

retronav avatar May 29 '22 06:05 retronav

This can be done by writing a rehype plugin that can accept a slugify function as an option.

The API for the plugin should

  • have a way to cache the slugify function
  • define a clear interface for counter reset (in case of slug generation with counter)

naiyerasif avatar May 30 '22 13:05 naiyerasif

Looking at this, what you suggest @naiyerasif could be done. If you both are interested in collaborating and getting together to work on a mvp for this would be a great first step in getting something like this into Astro's ecosystem. Im going to label this as a good first issue, and something that would be nice to have to see if we can get some more support in developing this idea further.

aFuzzyBear avatar May 30 '22 13:05 aFuzzyBear

Hi, I would like to pick this up.

Here's what I am thinking: allow passing a slugify function in astro config under markdown and use it for slugifying markdown headings if passed, else use the default. Let me know if I am missing something.

niftytyro avatar Aug 24 '22 18:08 niftytyro

Apparently, no changes are needed in astro 😅. @naiyerasif has already created a plugin that takes a custom slugify function - @microflash/rehype-slugify.

So this can easily be achieved by specifying the plugin in astro config.

niftytyro avatar Aug 27 '22 07:08 niftytyro