Created
April 18, 2018 06:51
-
-
Save mikesol/9cbd19f7f4974041b037d98f9d5343f9 to your computer and use it in GitHub Desktop.
A hack for fenced code tabs in mkdocs material
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
document.addEventListener('DOMContentLoaded', function () { | |
var hashcache = {}; | |
var langcache = {}; | |
var ariacache = {}; | |
Array.prototype.forEach.call(document.querySelectorAll('ul.nav.nav-tabs'), function(tablist) { | |
Array.prototype.forEach.call(tablist.querySelectorAll('li.nav-item'), function(tabitem) { | |
Array.prototype.forEach.call(tabitem.querySelectorAll('a'), function (anchor) { | |
var lang = anchor.getAttribute("data-lang"); | |
var aria = anchor.getAttribute("aria-controls"); | |
var obj = { | |
tabitem: tabitem, | |
anchor: anchor, | |
hash: anchor.hash, | |
lang: lang, | |
aria: aria | |
}; | |
hashcache[anchor.hash] = obj; | |
ariacache[aria] = obj; | |
if (!langcache.hasOwnProperty(lang)) { | |
langcache[lang] = []; | |
} | |
langcache[lang].push(obj); | |
}); | |
}); | |
}); | |
Array.prototype.forEach.call(document.querySelectorAll('div.tab-pane'), function(tabpane) { | |
ariacache[tabpane.id].pane = tabpane | |
}); | |
window.addEventListener('hashchange', onhashchange); | |
onhashchange(); | |
function onhashchange() { | |
var lang = hashcache[location.hash].lang; | |
Array.prototype.forEach.call(Object.keys(langcache), function(maybelang) { | |
if (lang === maybelang) { | |
Array.prototype.forEach.call(langcache[maybelang], function(ooo) { | |
Array.prototype.forEach.call([ooo.tabitem, ooo.pane], function(obj) { | |
classes = obj.className.split(' '); | |
if (classes.indexOf('active') === -1) { | |
classes.push('active'); | |
obj.className = classes.join(' '); | |
} | |
}); | |
}); | |
} else { | |
Array.prototype.forEach.call(langcache[maybelang], function(ooo) { | |
Array.prototype.forEach.call([ooo.tabitem, ooo.pane], function(obj) { | |
classes = obj.className.split(' '); | |
var idx = classes.indexOf('active') | |
if (idx > -1) { | |
delete classes[idx]; | |
obj.className = classes.join(' '); | |
} | |
}); | |
}); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment