diff --git a/css/app.css b/css/app.css index 38e3db0..44c4b91 100644 --- a/css/app.css +++ b/css/app.css @@ -191,6 +191,11 @@ body { border-top: 1px dotted #777; } +#content h2:hover, +#content h3:hover { + color: #ED1C24; +} + #content img { max-width: 90%; display: block; diff --git a/js/ditto.js b/js/ditto.js index 43cc264..c088504 100644 --- a/js/ditto.js +++ b/js/ditto.js @@ -70,12 +70,22 @@ function init_sidebar_section() { } function init_back_to_top_button() { - $(ditto.back_to_top_id).show(); - $(ditto.back_to_top_id).on("click", function() { - $("html body").animate({ - scrollTop: 0 - }, 200); - }); + $(ditto.back_to_top_id).show(); + $(ditto.back_to_top_id).on('click', goTop); +} + +function goTop(e) { + if(e) e.preventDefault(); + $('html body').animate({ + scrollTop: 0 + }, 200); + history.pushState(null, null, '#' + location.hash.split('#')[1]); +} + +function goSection(sectionId){ + $('html, body').animate({ + scrollTop: ($('#' + sectionId).offset().top) + }, 300); } function init_edit_button() { @@ -100,101 +110,127 @@ function init_edit_button() { function replace_symbols(text) { // replace symbols with underscore - return text.replace(/[&\/\\#,+=()$~%.'":*?<>{}\ \]\[]/g, "_"); + return text + .replace(/, /g, ',') + .replace(/[&\/\\#,.+=$~%'":*?<>{}\ \]\[]/g, "-") + .replace(/[()]/g, ''); } function li_create_linkage(li_tag, header_level) { - // add custom id and class attributes - html_safe_tag = replace_symbols(li_tag.text()); - li_tag.attr("id", html_safe_tag); - li_tag.attr("class", "link"); + // add custom id and class attributes + html_safe_tag = replace_symbols(li_tag.text()); + li_tag.attr('data-src', html_safe_tag); + li_tag.attr("class", "link"); - // add click listener - on click scroll to relevant header section - $(ditto.content_id + " li#" + li_tag.attr("id")).click(function() { - // scroll to relevant section - var header = $( - ditto.content_id + " h" + header_level + "." + li_tag.attr("id") - ); - $('html, body').animate({ - scrollTop: header.offset().top - }, 200); + // add click listener - on click scroll to relevant header section + li_tag.click(function(e) { + e.preventDefault(); + // scroll to relevant section + var header = $( + ditto.content_id + " h" + header_level + "." + li_tag.attr('data-src') + ); + $('html, body').animate({ + scrollTop: header.offset().top + }, 200); - // highlight the relevant section - original_color = header.css("color"); - header.animate({ color: "#ED1C24", }, 500, function() { - // revert back to orig color - $(this).animate({color: original_color}, 2500); - }); + // highlight the relevant section + original_color = header.css("color"); + header.animate({ color: "#ED1C24", }, 500, function() { + // revert back to orig color + $(this).animate({color: original_color}, 2500); }); + history.pushState(null, null, '#' + location.hash.split('#')[1] + '#' + li_tag.attr('data-src')); + }); } function create_page_anchors() { - // create page anchors by matching li's to headers - // if there is a match, create click listeners - // and scroll to relevant sections + // create page anchors by matching li's to headers + // if there is a match, create click listeners + // and scroll to relevant sections - // go through header level 1 to 3 - for (var i = 1; i <= 4; i++) { - // parse all headers - var headers = []; - $('#content h' + i).map(function() { - headers.push($(this).text()); - $(this).addClass(replace_symbols($(this).text())); - this.id = 'h' + i + '-' + replace_symbols($(this).text()); - }); + // go through header level 1 to 3 + for (var i = 2; i <= 4; i++) { + // parse all headers + var headers = []; + $('#content h' + i).map(function() { + var content = $(this).text(); + headers.push(content); + $(this).addClass(replace_symbols(content)); + this.id = replace_symbols(content); + $(this).hover(function () { + $(this).html(content + + ' § ⇧'); + }, function () { + $(this).html(content); + }); + $(this).on('click', 'a.section-link', function(event) { + event.preventDefault(); + history.pushState(null, null, '#' + location.hash.split('#')[1] + '#' + replace_symbols(content)); + goSection(replace_symbols(content)); + }); + }); - if ((i === 2) && headers.length !== 0) { - var ul_tag = $('