Merge pull request #173 from HParker/slugify-section-anchors

slugify section headers
This commit is contained in:
Mathieu David 2016-10-21 22:12:40 +02:00 committed by GitHub
commit ecae442d25
1 changed files with 3 additions and 2 deletions

View File

@ -55,11 +55,12 @@ $( document ).ready(function() {
// Add anchors for all content headers // Add anchors for all content headers
content.find("h1, h2, h3, h4, h5").wrap(function(){ content.find("h1, h2, h3, h4, h5").wrap(function(){
var wrapper = $("<a class=\"header\">"); var wrapper = $("<a class=\"header\">");
wrapper.attr("name", $(this).text()); var header_name = $(this).text().trim().replace(/\W/g, '-')
wrapper.attr("name", header_name);
// Add so that when you click the link actually shows up in the url bar... // Add so that when you click the link actually shows up in the url bar...
// Remove any existing anchor then append the new one // Remove any existing anchor then append the new one
// ensuring eg. no spaces are present within it ie. they become %20 // ensuring eg. no spaces are present within it ie. they become %20
wrapper.attr("href", $(location).attr('href').split("#")[0] + "#" + encodeURIComponent($(this).text().trim()) ); wrapper.attr("href", $(location).attr('href').split("#")[0] + "#" + header_name );
return wrapper; return wrapper;
}); });