forked from nodejs/nodejs.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateSlug.js
More file actions
25 lines (21 loc) · 766 Bytes
/
createSlug.js
File metadata and controls
25 lines (21 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* eslint-disable @typescript-eslint/explicit-function-return-type */
function createSlug(title) {
let slug = title.toLowerCase().trim();
const sets = [
{ to: 'nodejs', from: /node.js/ }, // Replace node.js
{ to: '-and-', from: /&/ }, // Replace &
{ to: '-', from: /[/_,:;\\ .]/g }, // Replace /_,:;\. and whitespace
];
sets.forEach(set => {
slug = slug.replace(set.from, set.to);
});
return (
slug
// eslint-disable-next-line no-useless-escape
.replace(/[^\w\-]+/g, '') // Remove any non word characters
.replace(/--+/g, '-') // Replace multiple hyphens with single
.replace(/^-/, '') // Remove any leading hyphen
.replace(/-$/, '')
); // Remove any trailing hyphen
}
module.exports = createSlug;