-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
clean-for-slug.js
33 lines (32 loc) · 973 Bytes
/
clean-for-slug.js
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
31
32
33
/**
* External dependencies
*/
import { deburr, trim } from 'lodash';
/**
* Performs some basic cleanup of a string for use as a post slug.
*
* This replicates some of what `sanitize_title()` does in WordPress core, but
* is only designed to approximate what the slug will be.
*
* Converts Latin-1 Supplement and Latin Extended-A letters to basic Latin
* letters. Removes combining diacritical marks. Converts whitespace, periods,
* and forward slashes to hyphens. Removes any remaining non-word characters
* except hyphens. Converts remaining string to lowercase. It does not account
* for octets, HTML entities, or other encoded characters.
*
* @param {string} string Title or slug to be processed.
*
* @return {string} Processed string.
*/
export function cleanForSlug( string ) {
if ( ! string ) {
return '';
}
return trim(
deburr( string )
.replace( /[\s\./]+/g, '-' )
.replace( /[^\p{L}\p{N}_-]+/gu, '' )
.toLowerCase(),
'-'
);
}