Skip to content

Commit

Permalink
Export slugify function (#566)
Browse files Browse the repository at this point in the history
* Prefix slug with dash

* Update test snapshots

* Add changeset

* Export slugify function

* Revert test changes
  • Loading branch information
jackyef authored Apr 5, 2024
1 parent 9012179 commit a9e5276
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions .changeset/tall-crabs-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
"markdown-to-jsx": patch
---

Browsers assign element with `id` to the global scope using the value as the variable name. E.g.: `<h1 id="analytics">` can be referenced via `window.analytics`.
This can be a problem when a name conflict happens. For instance, pages that expect `analytics.push()` to be a function will stop working if the an element with an `id` of `analytics` exists in the page.

In this change, we export the `slugify` function so that users can easily augment it.
This can be used to avoid variable name conflicts by giving the element a different `id`.

```js
import { slugify } from 'markdown-to-jsx';

options={{
slugify: str => {
let result = slugify(str)

return result ? '-' + str : result;
}
}}
```
2 changes: 1 addition & 1 deletion index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ function unquote(str: string) {

// based on https://stackoverflow.com/a/18123682/1141611
// not complete, but probably good enough
function slugify(str: string) {
export function slugify(str: string) {
return str
.replace(/[ÀÁÂÃÄÅàáâãäåæÆ]/g, 'a')
.replace(/[çÇ]/g, 'c')
Expand Down

0 comments on commit a9e5276

Please sign in to comment.