diff --git a/_js/anchor-links.js b/_js/anchor-links.js new file mode 100644 index 00000000..c8a1c868 --- /dev/null +++ b/_js/anchor-links.js @@ -0,0 +1,30 @@ +// Add anchors to headings client-side, which prevents them from showing up +// in RSS feeds. See https://github.com/facebook/react/issues/4124. +(function() { + var selector = '.inner-content h2, .inner-content h3, .inner-content h4'; + var elements = document.querySelectorAll(selector); + for (var i = 0; i < elements.length; i++) { + var textMethod = document.body.textContent ? "textContent" : "innerText"; + var roughText = elements[i][textMethod]; + + // Regex rule for making the title URL-friendly. + var urlFriendlyText = roughText.trim() + .toLowerCase() + .replace(/\s+/g, '-') + .replace(/[^A-Za-z0-9\-_.\p{Cyrillic}\p{Hangul}\p{Hiragana}\p{Katakana}\p{Han}]/g, ''); + + // Create the anchor we'll jump to. + var anchor = document.createElement('a'); + anchor.className = 'anchor'; + anchor.name = urlFriendlyText; + elements[i].insertBefore(anchor, elements[i].firstChild); + + // Create the clickable "#" icon. + var hashLink = document.createElement('a'); + var icon = document.createTextNode("#"); + hashLink.appendChild(icon); + hashLink.className = 'hash-link'; + hashLink.href = '#' + urlFriendlyText; + elements[i].appendChild(hashLink); + } +}()); diff --git a/_layouts/default.html b/_layouts/default.html index 5e10b984..1819afec 100644 --- a/_layouts/default.html +++ b/_layouts/default.html @@ -61,6 +61,7 @@
+