Last active
March 13, 2019 23:21
-
-
Save R3V1Z3/594e4278368773e69e4c691f0f1c8a4e to your computer and use it in GitHub Desktop.
JavaScript to automatically convert keypress shortcuts in isolated code elements like <code>Enter</code> to kbd elements like <kbd>Enter</kbd>.
This file contains hidden or 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
let code = document.querySelectorAll("code"); | |
code.forEach( el => el.classList.add("prettyprint") ); | |
code.forEach( i => { | |
let found = false; | |
let h = i.innerHTML.toLowerCase(); | |
let c = ['tab', 'esc', 'return', 'enter']; | |
if ( c.includes(h) ) found = true; | |
if ( h.length === 1 ) found = true; | |
c = ['ctrl', 'alt', 'cmd', 'shift', 'win', 'super', 'kbd']; | |
let s = c.some( e => { | |
if ( h.startsWith(e + "-") ) return found = true; | |
}); | |
let u = h.charCodeAt(0); | |
if (u === 55356 ) { | |
i.innerHTML = i.innerHTML.substring(2); | |
found = true; | |
} | |
if (!found) return; | |
let kbd = document.createElement('kbd'); | |
kbd.innerHTML = i.innerHTML; | |
i.parentNode.replaceChild(kbd, i); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment