Skip to content

Commit

Permalink
make more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
kevodwyer committed Feb 24, 2024
1 parent 0bd412e commit 746bd91
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
31 changes: 29 additions & 2 deletions assets/apps/markup-viewer/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,39 @@ function typeset(code) {
.catch((err) => console.log('Typeset failed: ' + err.message));
return MathJax.startup.promise;
}

function isDigit(char) {
return !isNaN(parseInt(char));
}
function encodeMath(text) {
var startIdx = -1;
let token = "$";
let replacementToken = "<span>$</span>";
let indexesToReplace = [];
var done = false;
while(!done) {
startIdx = text.indexOf(token, startIdx + 1);
if (startIdx > -1) {
if (startIdx < text.length -1 && (isDigit(text[startIdx + 1]) )){ // || text[startIdx + 1] == ' ')) {
indexesToReplace.push(startIdx);
}
} else {
done = true;
}
}
let indexesToReplaceReversed = indexesToReplace.reverse();
for(var i = 0; i < indexesToReplaceReversed.length; i++) {
let idx = indexesToReplaceReversed[i];
let before = text.substring(0, idx);
let after = text.substring(idx + token.length);
text = before + replacementToken + after;
}
return text;
}
function addMathJax(text) {
let callback = () => {
typeset(() => {
const node = document.getElementById('sanitized');
node.innerHTML = text;
node.innerHTML = encodeMath(text);
return [node];
}).then(() => {
console.log('math typeset complete');
Expand Down
6 changes: 6 additions & 0 deletions assets/apps/markup-viewer/toastui-editor-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10226,6 +10226,12 @@ var baseConvertors = {
},
customInline: function (node, context, convertors) {
var info = node.info.trim().toLowerCase();
if (convertors == null) {
return {
type: 'text',
content: '$$',
};
}
var customConvertor = convertors[info];
if (customConvertor) {
return customConvertor(node, context);
Expand Down

0 comments on commit 746bd91

Please sign in to comment.