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

Option to capitalize slugs #19

Closed
4 tasks done
bennycode opened this issue Oct 18, 2023 · 5 comments
Closed
4 tasks done

Option to capitalize slugs #19

bennycode opened this issue Oct 18, 2023 · 5 comments
Labels
🙅 no/wontfix This is not (enough of) an issue for this project 👎 phase/no Post cannot or will not be acted on

Comments

@bennycode
Copy link

Initial checklist

Problem

Currently the only option provided by the plugin is a prefix. It would be nice to add further options so slugs can be written in capital letters. It would be also cool if a transformer could be defined to modify the generated slugs yourself.

Solution

Adding a "capitalize" option or adding a callback where developers can defined their own slug customizations.

Alternatives

Only by writing your own slug plugin.

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Oct 18, 2023
@wooorm
Copy link
Member

wooorm commented Oct 18, 2023

Hey! This project is intentionally simple. So that everyone uses the same slugs. It follows GitHub. So that what you write in markdown will work here. And in VS Code. In as many places.

The goal is to not add other options. It would go against the goal of everyone using the same slugs. The alternative, writing your own plugin, is fine! We recommend learning about ASTs, and this is a good example how to learn about them.

@wooorm wooorm closed this as completed Oct 18, 2023
@wooorm wooorm added the 🙅 no/wontfix This is not (enough of) an issue for this project label Oct 18, 2023
@github-actions

This comment has been minimized.

@github-actions github-actions bot added 👎 phase/no Post cannot or will not be acted on and removed 🤞 phase/open Post is being triaged manually labels Oct 18, 2023
@bennycode
Copy link
Author

Thanks for your explanation. I really like the idea of creating slugs similar to how GitHub does it, and I want to use this in my projects.

I am building an Astro blog, and I need to make sure it supports some links from an older blogging system. In that older system, some of the links are like #TS2003, but the plugin changes them to #ts2003. I can't update the old content, so how can I fix this and change the way the slug is created by the rehype-slug plugin? I was thinking about building my own transformer that under-the-hood calls rehype-slug and then just runs the toUpperCase on top of it?

Also, I'm wondering if there's a way to choose which heading levels get slugs/IDs. For example, I only want IDs for H2 headings and not for H3 headings. Is there a way to do that?

@wooorm
Copy link
Member

wooorm commented Oct 18, 2023

I recommend changing the content.

See the source: https://github.com/rehypejs/rehype-slug/blob/main/lib/index.js.
Copy/paste it, remove what you don’t need, change it to do what you want.

toUpperCase makes everything uppercase, you probably don’t want that.

headingRank(node) returns 1, 2, etc for headings. Return for 1 if you don’t want h1.

@bennycode
Copy link
Author

I appreciate your input. I will try to gradually transition to the new slug structure. For now, I'm using JavaScript within my Astro pages to convert incoming legacy slugs (capitalized in my case) into the new format.

Example:

<script>
  (() => {
    const matchTS = /TS\d+/g;
    const hash = window.location.hash;
    const matches = hash.match(matchTS);
    if (matches) {
      window.location.hash = hash.replace('TS', 'ts');
    }
  })();
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🙅 no/wontfix This is not (enough of) an issue for this project 👎 phase/no Post cannot or will not be acted on
Development

No branches or pull requests

2 participants