Created
January 9, 2017 21:36
-
-
Save adamburvill/4c54004cb3d0c275de8a4242c6096ab5 to your computer and use it in GitHub Desktop.
H2 Links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$(document).ready(function() { | |
function convertToSlug(text) { | |
return text.toLowerCase().replace(/[^\w ]+/g, '').replace(/ +/g, '-'); | |
} | |
function convertH2(h2) { | |
h2 = $(h2); | |
h2Text = h2.text(); | |
h2Text = convertToSlug(h2Text); | |
h2.attr("id", h2Text); | |
} | |
function setupH2s() { | |
h2s = $('h2.category'); | |
for (var i = 0; i < h2s.length; i++) { | |
convertH2(h2s[i]); | |
} | |
} | |
function createQuickLinkAnchor(h2) { | |
h2 = $(h2); | |
innerText = h2.text(); | |
return "<a href='#" + convertToSlug(innerText) + "'>" + innerText + "</a>"; | |
} | |
function setupQuickLinks() { | |
quicklinks = $('#anchor-links'); | |
if (quicklinks.length > 0) { | |
h2s = $('h2.category'); | |
linksContainer = $('<div class="links-container"></div>') | |
quicklinks.append('<h2>Quick Links:</h2>'); | |
for (var i = 0; i < h2s.length; i++) { | |
anchor = createQuickLinkAnchor(h2s[i]); | |
linksContainer.append(anchor); | |
} | |
;quicklinks.append(linksContainer); | |
} | |
} | |
setupH2s(); | |
setupQuickLinks(); | |
}); | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment