Last active
March 28, 2019 13:04
-
-
Save tahir-hassan/8237c192c9c0695db99d90e1e1839843 to your computer and use it in GitHub Desktop.
Anki -- Using Showdown and HighlightJS for a solution that works across both Anki Desktop and AnkiDroid (version 1)
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
<div class="front-content"> | |
{{Front}} | |
</div> | |
<script> | |
function addStylesheet(src, callback) { | |
var s = document.createElement('link'); | |
s.rel = 'stylesheet'; | |
s.href = src; | |
s.onload = callback; | |
document.head.appendChild(s); | |
} | |
function addScript(src, callback) { | |
var s = document.createElement('script'); | |
s.src = src; | |
s.type = "text/javascript"; | |
s.onload = callback; | |
document.body.appendChild(s); | |
} | |
function replaceAllWhitespaceWithSpace(str) { | |
return str.replace(/[\t\v\f \u00a0\u2000-\u200b\u2028-\u2029\u3000]/g, ' '); | |
} | |
addStylesheet('https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.5/styles/default.min.css', function() { | |
}); | |
addScript('https://cdnjs.cloudflare.com/ajax/libs/showdown/1.9.0/showdown.min.js', function() { | |
addScript('https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.5/highlight.min.js', function() { | |
function processShowdownDivs() { | |
var showdownConverter = new showdown.Converter(); | |
showdownConverter.setFlavor('github'); | |
document.querySelectorAll('div.front-content').forEach((div) => { | |
var rawText = div.innerText; | |
var text = replaceAllWhitespaceWithSpace(rawText); | |
var html = showdownConverter.makeHtml(text); | |
var newDiv = document.createElement('div'); | |
newDiv.innerHTML = html; | |
newDiv.querySelectorAll('pre code').forEach((block) => { | |
hljs.highlightBlock(block); | |
}); | |
div.parentNode.insertBefore(newDiv, div.nextSibling); | |
div.style.display = 'none'; | |
}); | |
}; | |
processShowdownDivs(); | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment