-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlugify.js
More file actions
31 lines (26 loc) · 755 Bytes
/
Slugify.js
File metadata and controls
31 lines (26 loc) · 755 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
26
27
28
29
30
/**
{
"api": 1,
"name": "Slugify",
"description": "Converts selected line to a URL-friendly slug",
"author": "Pradeep Gowda @btbytes",
"icon": "scissors",
"tags": "url,trim,slug,convert,format"
}
**/
function slugify(text) {
return text
.toLowerCase()
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "") // remove accents
.replace(/[^a-z0-9]+/g, "-") // non-alphanumerics to hyphen
.replace(/^-+|-+$/g, ""); // trim hyphens
}
function main(state) {
state.text = slugify(state.text);
}
/** Note: 2025-08-03
* I wrote the first version of this using ChatGPT, but it got
* 1. the metadata JSON format wrong and formatted like Javadoc
* 2. it did not operate on state.text but instead directly on the object
*/