From f26f41fde37bddbd13a94c5cbacbb648b955e0c0 Mon Sep 17 00:00:00 2001 From: Adam Hess Date: Thu, 20 Oct 2016 22:02:16 -0700 Subject: [PATCH] slugify section headers The current section headers are url encoded. Because of that they have some funny characters like %20. We can clean that up by removing all of the non-word characters before placing them in the anchor. --- src/theme/book.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/theme/book.js b/src/theme/book.js index 231b02ab..0d71a197 100644 --- a/src/theme/book.js +++ b/src/theme/book.js @@ -55,11 +55,12 @@ $( document ).ready(function() { // Add anchors for all content headers content.find("h1, h2, h3, h4, h5").wrap(function(){ var wrapper = $(""); - 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... // Remove any existing anchor then append the new one // 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; });