Last active
November 27, 2018 11:40
-
-
Save BrianGenisio/5df8d774ab1eebcce4964810c36b06e7 to your computer and use it in GitHub Desktop.
Convert *bold* and _italics_ into unicode
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 cp(c) { | |
return c.codePointAt(0); | |
} | |
function base(c) { | |
if (/[a-z]/.test(c)) { | |
return [cp('a'), cp('𝗮'), cp('𝘢')]; | |
} else if (/[A-Z]/.test(c)) { | |
return [cp('A'), cp('𝗔'), cp('𝘈')]; | |
} | |
} | |
function format(str, baseIndex) { | |
return str.split('') | |
.map(function(s) {return base(s) ? String.fromCodePoint(cp(s) - base(s)[0] + base(s)[baseIndex]) : s}).join(''); | |
} | |
const bold = function(str) {return format(str, 1);}; | |
const italic = function(str) {return format(str, 2);}; | |
function transform(str) { | |
return str.replace(/_([^_]*)_/g, function(_, c) { return italic(c); }).replace(/\*([^\*]*)\*/g, function(_, c) { return bold(c);}); | |
} | |
function copyToClipboard(str) { | |
const el = document.createElement('textarea'); | |
el.value = str; | |
document.body.appendChild(el); | |
el.select(); | |
document.execCommand('copy'); | |
document.body.removeChild(el); | |
}; | |
navigator.clipboard.readText().then(function(text) { | |
copyToClipboard(transform(text)) | |
}); | |
function output(str) { | |
document.getElementById("out").innerHTML = str; | |
} | |
output(transform("_this_ *IS* a *test* _ABCDE_ and *AGAIN* _DEFGH_")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment