+// from django/contrib/admin/static/admin/js/urlify.js
+var LATIN_MAP = {
+ 'À': 'A', 'Á': 'A', 'Â': 'A', 'Ã': 'A', 'Ä': 'A', 'Å': 'A', 'Æ': 'AE',
+ 'Ç': 'C', 'È': 'E', 'É': 'E', 'Ê': 'E', 'Ë': 'E', 'Ì': 'I', 'Í': 'I',
+ 'Î': 'I', 'Ï': 'I', 'Ð': 'D', 'Ñ': 'N', 'Ò': 'O', 'Ó': 'O', 'Ô': 'O',
+ 'Õ': 'O', 'Ö': 'O', 'Ő': 'O', 'Ø': 'O', 'Ù': 'U', 'Ú': 'U', 'Û': 'U',
+ 'Ü': 'U', 'Ű': 'U', 'Ý': 'Y', 'Þ': 'TH', 'Ÿ': 'Y', 'ß': 'ss', 'à': 'a',
+ 'á': 'a', 'â': 'a', 'ã': 'a', 'ä': 'a', 'å': 'a', 'æ': 'ae', 'ç': 'c',
+ 'è': 'e', 'é': 'e', 'ê': 'e', 'ë': 'e', 'ì': 'i', 'í': 'i', 'î': 'i',
+ 'ï': 'i', 'ð': 'd', 'ñ': 'n', 'ò': 'o', 'ó': 'o', 'ô': 'o', 'õ': 'o',
+ 'ö': 'o', 'ő': 'o', 'ø': 'o', 'ù': 'u', 'ú': 'u', 'û': 'u', 'ü': 'u',
+ 'ű': 'u', 'ý': 'y', 'þ': 'th', 'ÿ': 'y'
+};
+
+function downcode(string) {
+ return string.toLowerCase().replace(/[^A-Za-z0-9\[\] ]/g,function(a){ return LATIN_MAP[a]||a }).replace(/[^-\w\s]/g, '').replace(/^\s+|\s+$/g, '').replace(/[-\s]+/g, '-');
+};
+
+function remove_auto_anchors() {
+ $('article .wiki-anchor-auto').each(function(idx, anchor) {
+ $(anchor).parent().removeAttr('id');
+ $(anchor).remove();
+ });
+}
+
+function auto_anchors() {
+ $('article h2, article h3, article h4').each(function(idx, elem) {
+ var $elem = $(elem);
+ if ($elem.attr('id')) return;
+ if ($elem.find('.wiki-anchor').length) return;
+ $elem.attr('id', downcode($elem.text()));
+ $('<a class="wiki-anchor wiki-anchor-auto" href="#' + $elem.attr('id') + '">¶</a>').appendTo($elem);
+ });
+}
+
+function create_toc() {
+ $('#toc').remove();
+ if ($('article h2').length == 0) return;
+ $div_toc = $('<div id="toc"><ul></ul></div>');
+ $div_toc_ul = $div_toc.find('ul');
+ var li_titles = Array();
+ $('article h2').each(function(idx, elem) {
+ var $elem = $(elem);
+ var slug = elem.id;
+ var $a_title = $('<a></a>', {href: '#' + slug, text: $elem.text().replace(/¶$/, '')});
+ var $li_title = $('<li></li>');
+ $li_title[0].related_position = $(elem).position().top;
+ li_titles.push($li_title[0]);
+ $a_title.appendTo($li_title);
+ $li_title.appendTo($div_toc_ul);
+ });
+ $('article h1').first().after($div_toc);
+
+ li_titles = li_titles.reverse();
+
+ $(window).on('load', function() {
+ // update positions after images have been loaded
+ $('article h2').each(function(idx, elem) {
+ $('#toc li')[idx].related_position = $(elem).position().top;
+ });
+ $(window).trigger('scroll');
+ });
+
+ var scroll_timeout_id = null;
+ $(window).on('scroll', function() {
+ if (scroll_timeout_id) clearTimeout(scroll_timeout_id);
+ scroll_timeout_id = setTimeout(function() { // throttle
+ scroll_timeout_id = null;
+ var current_position = window.scrollY;
+ $('#toc li').removeClass('active');
+ for (const li_title of li_titles) {
+ if (li_title.related_position < current_position - 25) {
+ $(li_title).addClass('active');
+ break;
+ }
+ }
+ }, 50);
+ });
+};
+