Skip to content

Commit f26f41f

Browse files
committed
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.
1 parent b91f817 commit f26f41f

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/theme/book.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ $( document ).ready(function() {
5555
// Add anchors for all content headers
5656
content.find("h1, h2, h3, h4, h5").wrap(function(){
5757
var wrapper = $("<a class=\"header\">");
58-
wrapper.attr("name", $(this).text());
58+
var header_name = $(this).text().trim().replace(/\W/g, '-')
59+
wrapper.attr("name", header_name);
5960
// Add so that when you click the link actually shows up in the url bar...
6061
// Remove any existing anchor then append the new one
6162
// ensuring eg. no spaces are present within it ie. they become %20
62-
wrapper.attr("href", $(location).attr('href').split("#")[0] + "#" + encodeURIComponent($(this).text().trim()) );
63+
wrapper.attr("href", $(location).attr('href').split("#")[0] + "#" + header_name );
6364
return wrapper;
6465
});
6566

0 commit comments

Comments
 (0)