var ditto = { // page element ids content_id: "#content", sidebar_id: "#sidebar", edit_id: "#edit", back_to_top_id: "#back_to_top", loading_id: "#loading", error_id: "#error", // display elements sidebar: true, edit_button: true, back_to_top_button: true, // initialize function run: initialize }; function initialize() { // initialize sidebar and buttons if (ditto.sidebar) { init_sidebar_section(); } if (ditto.back_to_top_button) { init_back_to_top_button(); } if (ditto.edit_button) { init_edit_button(); } // page router router(); $(window).on('hashchange', router); } function init_sidebar_section() { $.get(ditto.sidebar_file, function(data) { $(ditto.sidebar_id).html(marked(data)); }, "text").fail(function() { alert("Opps! can't find the sidebar file to display!"); }); } 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); }); } function init_edit_button() { if (ditto.base_url === null) { alert("Error! You didn't set 'base_url' when calling ditto.run()!"); } else { $(ditto.edit_id).show(); $(ditto.edit_id).on("click", function() { var hash = location.hash.replace("#", "/"); if (hash === "") { hash = "/" + ditto.index.replace(".md", ""); } window.open(ditto.base_url + hash + ".md"); // open is better than redirecting, as the previous page history // with redirect is a bit messed up }); } } function replace_symbols(text) { // replace symbols with underscore return text.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 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); // 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); }); }); } 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 // 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()); }); if (i === 2){ var ul_tag = $('