Skip to content

Commit a9e5276

Browse files
authoredApr 5, 2024··
Export slugify function (#566)
* Prefix slug with dash * Update test snapshots * Add changeset * Export slugify function * Revert test changes
1 parent 9012179 commit a9e5276

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed
 

‎.changeset/tall-crabs-flow.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
"markdown-to-jsx": patch
3+
---
4+
5+
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`.
6+
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.
7+
8+
In this change, we export the `slugify` function so that users can easily augment it.
9+
This can be used to avoid variable name conflicts by giving the element a different `id`.
10+
11+
```js
12+
import { slugify } from 'markdown-to-jsx';
13+
14+
options={{
15+
slugify: str => {
16+
let result = slugify(str)
17+
18+
return result ? '-' + str : result;
19+
}
20+
}}
21+
```

‎index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ function unquote(str: string) {
588588

589589
// based on https://stackoverflow.com/a/18123682/1141611
590590
// not complete, but probably good enough
591-
function slugify(str: string) {
591+
export function slugify(str: string) {
592592
return str
593593
.replace(/[ÀÁÂÃÄÅàáâãä忯]/g, 'a')
594594
.replace(/[çÇ]/g, 'c')

0 commit comments

Comments
 (0)
Please sign in to comment.