Skip to content

Commit dc1ee58

Browse files
committed
fix toc.js to escape special characters in href
1 parent 289270c commit dc1ee58

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

js/toc/toc.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,17 @@
4040
html = settings.title + ' <'+settings.listType+' class="jekyll-toc">';
4141

4242
var back_to_top = function(id) {
43-
return '<a href="#' +id+ '" title="'+settings.backToTopTitle+'" class="jekyll-toc-anchor jekyll-toc-back-to-top"><span class="jekyll-toc-icon">'+settings.backToTopText+'</span></a>';
43+
return '<a href="#' +fixedEncodeURIComponent(id)+ '" title="'+settings.backToTopTitle+'" class="jekyll-toc-anchor jekyll-toc-back-to-top"><span class="jekyll-toc-icon">'+settings.backToTopText+'</span></a>';
4444
}
4545

4646
var link_here = function(id) {
47-
return '<a href="#' +id+ '" title="'+settings.linkHereTitle+'" class="jekyll-toc-anchor jekyll-toc-link-here"><span class="jekyll-toc-icon">'+settings.linkHereText+'</span></a>';
47+
return '<a href="#' +fixedEncodeURIComponent(id)+ '" title="'+settings.linkHereTitle+'" class="jekyll-toc-anchor jekyll-toc-link-here"><span class="jekyll-toc-icon">'+settings.linkHereText+'</span></a>';
48+
}
49+
50+
function fixedEncodeURIComponent (str) {
51+
return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
52+
return '%' + c.charCodeAt(0).toString(16);
53+
});
4854
}
4955

5056
function force_update_hash(hash) {
@@ -59,18 +65,18 @@
5965
this_level = get_level(header);
6066
var header_id = $(header).attr('id');
6167
if (this_level === level) // same level as before; same indenting
62-
html += "<li><a href='#" + header.id + "'>" + header.innerHTML + "</a>";
68+
html += "<li><a href='#" + fixedEncodeURIComponent(header.id) + "'>" + header.innerHTML + "</a>";
6369
else if (this_level <= level){ // higher level than before; end parent ol
6470
for(i = this_level; i < level; i++) {
6571
html += "</li></"+settings.listType+">"
6672
}
67-
html += "<li><a href='#" + header.id + "'>" + header.innerHTML + "</a>";
73+
html += "<li><a href='#" + fixedEncodeURIComponent(header.id) + "'>" + header.innerHTML + "</a>";
6874
}
6975
else if (this_level > level) { // lower level than before; expand the previous to contain a ol
7076
for(i = this_level; i > level; i--) {
7177
html += "<"+settings.listType+"><li>"
7278
}
73-
html += "<a href='#" + header.id + "'>" + header.innerHTML + "</a>";
79+
html += "<a href='#" + fixedEncodeURIComponent(header.id) + "'>" + header.innerHTML + "</a>";
7480
}
7581
level = this_level; // update for the next one
7682

@@ -92,7 +98,7 @@
9298
if (settings.linkHeader) {
9399
$(header).addClass('jekyll-toc-header');
94100
$(header).children('span.jekyll-toc-wrapper').on( 'click', function( ) {
95-
force_update_hash(header_id);
101+
force_update_hash(fixedEncodeURIComponent(header_id));
96102
});
97103
}
98104
});

0 commit comments

Comments
 (0)