From a9e527655b788818d2edd49d6a230857cfc2730f Mon Sep 17 00:00:00 2001 From: Jacky Efendi Date: Sat, 6 Apr 2024 02:05:04 +0700 Subject: [PATCH] Export `slugify` function (#566) * Prefix slug with dash * Update test snapshots * Add changeset * Export slugify function * Revert test changes --- .changeset/tall-crabs-flow.md | 21 +++++++++++++++++++++ index.tsx | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 .changeset/tall-crabs-flow.md diff --git a/.changeset/tall-crabs-flow.md b/.changeset/tall-crabs-flow.md new file mode 100644 index 00000000..8ea7184f --- /dev/null +++ b/.changeset/tall-crabs-flow.md @@ -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.: `

` 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; + } +}} +``` diff --git a/index.tsx b/index.tsx index 898ad642..4b6a9a74 100644 --- a/index.tsx +++ b/index.tsx @@ -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')