UNPKG

@wordpress/url

Version:
8 lines (7 loc) 1.96 kB
{ "version": 3, "sources": ["../src/clean-for-slug.ts"], "sourcesContent": ["/**\n * External dependencies\n */\nimport removeAccents from 'remove-accents';\n\n/**\n * Performs some basic cleanup of a string for use as a post slug.\n *\n * This replicates some of what `sanitize_title_with_dashes()` does in WordPress core, but\n * is only designed to approximate what the slug will be.\n *\n * Converts Latin-1 Supplement and Latin Extended-A letters to basic Latin\n * letters. Removes combining diacritical marks. Converts whitespace, periods,\n * and forward slashes to hyphens. Removes any remaining non-word characters\n * except hyphens. Converts remaining string to lowercase. It does not account\n * for octets, HTML entities, or other encoded characters.\n *\n * @param string Title or slug to be processed.\n *\n * @return Processed string.\n */\nexport function cleanForSlug( string: string ): string {\n\tif ( ! string ) {\n\t\treturn '';\n\t}\n\treturn (\n\t\tremoveAccents( string )\n\t\t\t// Convert &nbsp, &ndash, and &mdash to hyphens.\n\t\t\t.replace( /( |–|—)/g, '-' )\n\t\t\t// Convert each group of whitespace, periods, and forward slashes to a hyphen.\n\t\t\t.replace( /[\\s\\./]+/g, '-' )\n\t\t\t// Remove all HTML entities.\n\t\t\t.replace( /&\\S+?;/g, '' )\n\t\t\t// Remove anything that's not a letter, number, underscore or hyphen.\n\t\t\t.replace( /[^\\p{L}\\p{N}_-]+/gu, '' )\n\t\t\t// Convert to lowercase\n\t\t\t.toLowerCase()\n\t\t\t// Replace multiple hyphens with a single one.\n\t\t\t.replace( /-+/g, '-' )\n\t\t\t// Remove any remaining leading or trailing hyphens.\n\t\t\t.replace( /(^-+)|(-+$)/g, '' )\n\t);\n}\n"], "mappings": ";AAGA,OAAO,mBAAmB;AAkBnB,SAAS,aAAc,QAAyB;AACtD,MAAK,CAAE,QAAS;AACf,WAAO;AAAA,EACR;AACA,SACC,cAAe,MAAO,EAEpB,QAAS,6BAA6B,GAAI,EAE1C,QAAS,aAAa,GAAI,EAE1B,QAAS,WAAW,EAAG,EAEvB,QAAS,sBAAsB,EAAG,EAElC,YAAY,EAEZ,QAAS,OAAO,GAAI,EAEpB,QAAS,gBAAgB,EAAG;AAEhC;", "names": [] }