From 72c439e6c3280470bd87059532a5ea553f6fe1c4 Mon Sep 17 00:00:00 2001 From: Dmitry Blotsky Date: Wed, 2 Mar 2016 15:13:05 -0800 Subject: [PATCH] CB-10739 Reverse-engineering GitHub's slugifying function. --- www/static/js/docs.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/www/static/js/docs.js b/www/static/js/docs.js index c2221cc16d8..8a11442c274 100644 --- a/www/static/js/docs.js +++ b/www/static/js/docs.js @@ -17,19 +17,25 @@ $(document).ready(function () { - function slugify(text) { + function slugifyLikeGitHub(originalText) { + + var text = originalText; + + // convert to lowercase text = text.toLowerCase(); - // replace unaccepted characters with spaces + // replace spaces with dashes + text = text.replace(/ /g, '-'); + + // remove unaccepted characters // NOTE: - // a better regex would have been /[^\d\s\w]/ug, but the 'u' flag + // a better regex would have been /[^\d\s\w-_]/ug, but the 'u' flag // (Unicode) is not supported in some browsers, and we support // many languages that use Unicode characters - text = text.replace(/[\[\]\(\)\=\+\?\.\,]/g, ' '); + text = text.replace(/[\[\]\(\)\=\+\?\!\.\,\{\}\\\/\>\<]/g, ''); - // trim whitespace and replace runs of whitespace with single dashes + // trim remaining whitespace text = text.trim(); - text = text.replace(/ +/g, '-'); return text; } @@ -40,7 +46,7 @@ $(document).ready(function () { } else if (heading.name) { return heading.name; } else { - return slugify(heading.innerText); + return slugifyLikeGitHub(heading.innerText); } }