Skip to content

Instantly share code, notes, and snippets.

@lior-amsalem
Created October 17, 2021 18:43
Show Gist options
  • Save lior-amsalem/b21f43f4511c824ea69227ccbc851c98 to your computer and use it in GitHub Desktop.
Save lior-amsalem/b21f43f4511c824ea69227ccbc851c98 to your computer and use it in GitHub Desktop.
Given url extract information from the URL and create a breadcrumbs. see example
/**
generateBC("mysite.com/pictures/holidays.html", " : ") == '<a href="/">HOME</a> : <a href="/pictures/">PICTURES</a> : <span class="active">HOLIDAYS</span>'
**/
function generateBC(url, separator) {
url = url.replace(/(http[s]?:\/\/[A-Za-z0-9]{3,}\.[A-Za-z]{2,})/g,'');
let r = '',
BC = ['/HOME'];
BC.push(...url.match(/\/(?!index)([A-Za-z0-9-]+)/gi)||[]);
BC.map((a,b,array) => {
r += (array.length-1 === b) ? '<span class="active">' + shortTitle(a) + '</span>' : '<a href="' + array.slice(1,b+1).join("") + '/">' + shortTitle(a) + '</a>' + separator;
});
return r;
}
function shortTitle(str) {
let exclude = ["the","of","in","from","by","with","and", "or", "for", "to", "at", "a"];
str = str.replace('/','').toUpperCase();
if(str.length <= 30)
return str.replace(/-/g, ' ');
return str.split('-').map((a,b,array) => {
return exclude.indexOf(a.toLowerCase()) === -1 ? a[0] : null;
}).join('').replace(/-/g, ' ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment