Created
March 13, 2015 20:04
-
-
Save alxarch/bf11c0f6f7272f70a2dd to your computer and use it in GitHub Desktop.
katex parse text
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
| (function () { | |
| var replacements = { | |
| "α": "\\alpha", | |
| "β": "\\beta", | |
| "γ": "\\gamma", | |
| "δ": "\\delta", | |
| "ε": "\\epsilon", | |
| "ζ": "\\zeta", | |
| "η": "\\eta", | |
| "θ": "\\theta", | |
| "ι": "\\iota", | |
| "κ": "\\kappa", | |
| "λ": "\\lamda", | |
| "μ": "\\mi", | |
| "ν": "\\ni", | |
| "ξ": "\\xe", | |
| "ο": "\\omikron", | |
| "π": "\\pi", | |
| "ρ": "\\rho", | |
| "σ": "\\sigma", | |
| "τ": "\\taf", | |
| "υ": "\\ypsilon", | |
| "φ": "\\fi", | |
| "χ": "\\chi", | |
| "ψ": "\\psi", | |
| "ω": "\\omega", | |
| }; | |
| var TEXT_NODE = document.TEXT_NODE; | |
| var ELEMENT_NODE = document.ELEMENT_NODE; | |
| var walk = function (node, delimiter) { | |
| var i, frag; | |
| switch (node.nodeType) { | |
| case ELEMENT_NODE: | |
| if (node.tagName.toLowerCase() === "script") return; | |
| for (i = node.childNodes.length - 1; i >= 0; i--) { | |
| walk(node.childNodes[i], delimiter); | |
| } | |
| break; | |
| case TEXT_NODE: | |
| if (node.textContent.indexOf(delimiter) < 0) return; | |
| frag = document.createElement("span"); | |
| node.textContent.split(delimiter).forEach(function(part, i) { | |
| if (0 === i % 2) { | |
| frag.appendChild(document.createTextNode(part)); | |
| } | |
| txt = part.replace(/[αβξδεζηθικλμνξοπρστυφχψω]/g, function(m) { | |
| return replacements[m]; | |
| }); | |
| var span = document.createElement("span"); | |
| try { | |
| katex.render(txt, span); | |
| } | |
| catch (error) { | |
| console.error(error.message); | |
| span.textContent = part; | |
| } | |
| frag.appendChild(span); | |
| }); | |
| node.parentNode.replaceChild(frag, node); | |
| break; | |
| default: | |
| return; | |
| } | |
| } | |
| // walk(document.body, "~~~"); | |
| // walk(document.body, "~"); | |
| })() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment