summaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/slugify.ts
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2021-12-21 15:05:04 +0100
committerArmand Philippot <git@armandphilippot.com>2021-12-21 15:05:04 +0100
commitae17973618c5ad5500ae69738da222187a09b019 (patch)
tree20edaca351c3d9b0df1b986e12255b9be7b9052d /src/utils/helpers/slugify.ts
parenta367f605b842ad0a71a63025da15ac62ed0364a5 (diff)
chore: add a hook to build headings tree
Diffstat (limited to 'src/utils/helpers/slugify.ts')
-rw-r--r--src/utils/helpers/slugify.ts18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/utils/helpers/slugify.ts b/src/utils/helpers/slugify.ts
new file mode 100644
index 0000000..37619bc
--- /dev/null
+++ b/src/utils/helpers/slugify.ts
@@ -0,0 +1,18 @@
+/**
+ * Convert a text into a slug or id.
+ * https://gist.github.com/codeguy/6684588#gistcomment-3332719
+ *
+ * @param {string} text Text to slugify.
+ */
+export const slugify = (text: string) => {
+ return text
+ .toString()
+ .normalize('NFD')
+ .replace(/[\u0300-\u036f]/g, '')
+ .toLowerCase()
+ .trim()
+ .replace(/\s+/g, '-')
+ .replace(/[^\w\-]+/g, '-')
+ .replace(/\-\-+/g, '-')
+ .replace(/^-|-$/g, '');
+};