-
-
Notifications
You must be signed in to change notification settings - Fork 180
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
Export slugify
function
#566
Export slugify
function
#566
Conversation
🦋 Changeset detectedLatest commit: ebccbc1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
1bbdf05
to
7d0b5a9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense to me :-)
@jackyef could you add a changeset please with the explanation of what it's fixing and why? |
@quantizor I added a changeset file, not sure if the wordings fit the convention of the project, feel free to edit it as you please! |
Looks perfect 👍🏼 |
Thinking about this further, it is kind of a breaking change. I'm wondering if we should make this the default behavior in v8 and for v7 ship the original So basically we do this: // note the added `export` statement
export function slugify(str: string) {
return str
.replace(/[ÀÁÂÃÄÅàáâãäåæÆ]/g, 'a')
.replace(/[çÇ]/g, 'c')
.replace(/[ðÐ]/g, 'd')
.replace(/[ÈÉÊËéèêë]/g, 'e')
.replace(/[ÏïÎîÍíÌì]/g, 'i')
.replace(/[Ññ]/g, 'n')
.replace(/[øØœŒÕõÔôÓóÒò]/g, 'o')
.replace(/[ÜüÛûÚúÙù]/g, 'u')
.replace(/[ŸÿÝý]/g, 'y')
.replace(/[^a-z0-9- ]/gi, '')
.replace(/ /gi, '-')
.toLowerCase()
} There already is an import { slugify } from 'markdown-to-jsx';
options={{
slugify: str => {
let result = slugify(str)
return result ? '-' + str : result;
}
}} That fixes things for v7 immediately for your use and then in v8 we will add that functionality as standard. Sound good? |
Actually, now that I think about it. I am thinking about this all wrong. Prefixing the id with I agree. The more simple solution of exporting |
I liked your idea from the PoV that it's less likely to conflict with global variables. Can bikeshed in v8 👍 |
Hey, thanks for the helpful library!
I recently encountered an edge case related to the heading
id
. In short, our application was calling something likeanalytics.push()
and for some reasons the invocation was failing in a page that renders the<Markdown />
component.After a closer inspection, apparently it was caused by a
### Analytics
in our markdown content. The<h3>
element was assigned anid="analytics"
and the browser assigned this to the global scope (e.g.:window
), causinganalytics.push()
to no longer be a function. This is due to the related 3rd party library doing initialization as follows:The solution in this PR is to prefix theid
with a-
, making it an invalid JS variable name so it can't pollute the global scope. The browser behavior of automatically scrolling to the corresponding landmark still works as expected (e.g.: visiting/page#-section-heading
will still automatically scroll the page to element withid="-section-heading"
.The solution in this PR is exporting the
slugify
function so users can easily augment it to avoid the issue.