Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,424 changes: 1,424 additions & 0 deletions js/asciimath.js

Large diffs are not rendered by default.

52 changes: 25 additions & 27 deletions js/diagramflowseq.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function drawAllMermaid() {
}
}

function renderKatex(srcMath, isDisplay) {
function renderKatex(srcMath, isDisplay, useAM) {
const unEscape = function (html) {
return html
.replace(/&/g, '&')
Expand All @@ -139,7 +139,11 @@ function renderKatex(srcMath, isDisplay) {
};
let repMath = "";
srcMath = unEscape(srcMath);

try {
if (useAM && renderKatex.AMenabled) {
srcMath = asciimath.am2tex(srcMath, isDisplay);
}
repMath = katex.renderToString(srcMath, {displayMode: isDisplay});
}
catch(err) {
Expand All @@ -151,7 +155,7 @@ function renderKatex(srcMath, isDisplay) {

function replaceMathString(src) {
var out = src;
var pattern = /(\$`)((?:\\.|[\s\S])+?)(`\$)|(\${1,2})((?:\\.|[\s\S])+?)\4|(\\\[)((?:\\.|[\s\S])+?)(\\])|(\\\()((?:\\.|[\s\S])+?)(\\\))/g;
var pattern = /(\$`)((?:\\.|[\s\S])+?)(`\$)|(\${1,2})((?:\\.|[\s\S])+?)\4|(\\\[)((?:\\.|[\s\S])+?)(\\])|(\\\()((?:\\.|[\s\S])+?)(\\\))|(``)([^]+?)(``)/g;
var mc = null;
var codeBegin = src.search('<code>');
var codeEnd = src.search('</code>');
Expand All @@ -162,6 +166,7 @@ function replaceMathString(src) {
} else {
var srcMath = "";
var isDisplay = false;
var useAM = false;
if (mc[1]) { //match $` `$
isDisplay = false;
srcMath = mc[2];
Expand All @@ -174,15 +179,20 @@ function replaceMathString(src) {
} else if (mc[9]) { //match \\( \\)
isDisplay = false;
srcMath = mc[10];
} else if (mc[12]) { //match ``
isDisplay = false;
srcMath = mc[13];
useAM = true;
}

var repMath = renderKatex(srcMath, isDisplay);
var repMath = renderKatex(srcMath, isDisplay, useAM);
if (repMath && repMath.length !== 0) {
out = out.replace(mc[0], repMath);
}
}
}
return out.replace(/\\<span/g, '<span');
return out.replace(/\\<span/g, '<span')
.replace(/<annotation[^<]*<\/annotation>/g, '')
}

function prepareSpecialCode(lang, code) {
Expand All @@ -202,6 +212,8 @@ function prepareSpecialCode(lang, code) {
}
} else if (lang === "math") {
retStr = renderKatex(code, true);
} else if (lang === "asciimath" || lang === "AM") {
retStr = renderKatex(code, true, true);
} else if (lang === "mermaid") {
var mermiadId = genNextMermaidDivId();
retStr = '<div id=\"' + mermiadId + '\">' + code + '</div>\n';
Expand All @@ -212,29 +224,20 @@ function prepareSpecialCode(lang, code) {
function isStartMultiMath(src) {
var pattern = /^(\s*)(\${2})|^(\s*)(\\\[)/g;
var npt = /(\${2})((?:\\.|[\s\S])+?)\1|(\\\[)((?:\\.|[\s\S])+?)(\\])/g;
var mc = null;
var ret = false;
if (null != (mc = pattern.exec(src)) && null == npt.exec(src)) {
ret = true;
}
return ret;
return pattern.test(src) && !npt.test(src);
}

function isEndMultiMath(src) {
var pattern = /(\${2})(\s*)$|(\\])(\s*)$/g;
var mc = null;
var ret = false;
if (null != (mc = pattern.exec(src))) {
ret = true;
}
return ret;
var pattern = /(\${2})(\s*)$|(\\])(\s*)$|(``)(\s*)$/g;
return pattern.test(src);
}

function prepareDiagram(data) {
function prepareDiagram(data, AMenabled) {
renderKatex.AMenabled = AMenabled
var lines = data.split('\n');
var retStr = "";
var curStatus = "";
var preLangs = ["flow", "sequence", "puml", "math", "mermaid"];
var preLangs = ["flow", "sequence", "puml", "math", "mermaid", "asciimath", "AM"];
var lang = "";
var tmpCode = "";
var isInCode = function () {
Expand All @@ -250,7 +253,7 @@ function prepareDiagram(data) {
return preLangs.indexOf(lang) != -1;
}
var isStartCode = function(src) {
var pattern = /^(`{3,})(\w*)/g;
var pattern = /^\s*(`{3,})(\w*)/g;
var mc = null;
var ret = false;
if (null != (mc = pattern.exec(src))) {
Expand All @@ -260,13 +263,8 @@ function prepareDiagram(data) {
return ret;
}
var isEndCode = function(src) {
var pattern = /^(`{3,})(\w*)/g;
var mc = null;
var ret = false;
if (null != (mc = pattern.exec(src))) {
ret = true;
}
return ret;
var pattern = /^\s*(`{3,})(\w*)/g;
return pattern.test(src)
}

resetDivId();
Expand Down
16 changes: 8 additions & 8 deletions js/markdownify.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@
// Onload, take the DOM of the page, get the markdown formatted text out and
// apply the converter.
function makeHtml(data) {
storage.get(['supportMath', 'katex', 'toc'], function(items) {
storage.get(['supportMath', 'katex', 'toc', 'asciimath'], function(items) {
// Convert MarkDown to HTML
var preHtml = data;
if (items.katex) {
config.markedOptions.katex = true;
preHtml = diagramFlowSeq.prepareDiagram(preHtml);
preHtml = diagramFlowSeq.prepareDiagram(preHtml, items.asciimath);
}

if (items.toc) {
Expand Down Expand Up @@ -233,7 +233,7 @@
});
}

storage.get(['exclude_exts', 'disable_markdown', 'katex', 'html'], function(items) {
storage.get(['exclude_exts', 'disable_markdown', 'katex', 'html', 'asciimath'], function(items) {
if (items.disable_markdown) {
return;
}
Expand Down Expand Up @@ -284,11 +284,11 @@
} else {
stopAutoReload();
}
} else if(key == 'disable_markdown') {
location.reload();
} else if(key == 'supportMath') {
location.reload();
} else if(key == 'katex') {
} else if(key === 'disable_markdown'
|| key === 'supportMath'
|| key === 'katex'
|| key === 'asciimath'
) {
location.reload();
}
}
Expand Down
46 changes: 27 additions & 19 deletions js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,15 @@ function message(text, type) {
}, 3000);
}

storage.get(['katex', 'html', 'toc'], function(items) {
if(items.katex) {
$('#katex').prop('checked', 'checked');
} else {
$('#katex').removeProp('checked');
}
if(items.toc) {
$('#toc').prop('checked', 'checked');
} else {
$('#toc').removeProp('checked');
}

if(items.html) {
$('#html').prop('checked', 'checked');
} else {
$('#html').removeProp('checked');
}
var options = ['katex', 'html', 'toc', 'asciimath']
storage.get(options, function(items) {
options.forEach(function(key) {
if (items[key]) {
$('#' + key).prop('checked', 'checked');
} else {
$('#' + key).removeProp('checked');
}
});
});

// auto-reload
Expand All @@ -48,21 +40,37 @@ $('#html').change(function() {
storage.set({'html' : 1});
} else {
storage.remove('html');
$('#katex').removeProp('checked');
storage.remove('katex');
storage.remove('asciimath');
$('#katex').prop('checked', false);
$('#asciimath').prop('checked', false);
}
});

$('#katex').change(function() {
if($(this).prop('checked')) {
storage.set({'katex' : 1});
$('#html').prop('checked', 'checked');
storage.set({'html' : 1});
$('#html').prop('checked', true);
} else {
storage.remove('katex');
storage.remove('asciimath');
$('#asciimath').prop('checked', false);
}
});

$('#asciimath').change(function() {
if($(this).prop('checked')) {
storage.set({'html' : 1});
storage.set({'katex' : 1});
storage.set({'asciimath': 1});
$('#html').prop('checked', true);
$('#katex').prop('checked', true);
} else {
storage.remove('asciimath');
}
})

$('#auto-reload').change(function() {
if($(this).prop('checked')) {
storage.set({'auto_reload' : 1});
Expand Down
1 change: 1 addition & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"js/flowchart.min.js",
"js/diagramflowseq.js",
"js/katex.min.js",
"js/asciimath.js",
"js/rawdeflate.js",
"js/platumlencode.js",
"js/mermaid.min.js"
Expand Down
16 changes: 15 additions & 1 deletion options.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,21 @@ <h1>MarkDown Preview Plus Options</h1>
<h2>Miscs</h2>
</div>
<div class="form-group checkbox-inline">
<label class="checkbox"><input type="checkbox" id="katex">KaTeX support inline(<code>$math$ or \(math\)</code>) and line(<code>$$math$$ or \[math\]</code></label>
<label class="checkbox">
<input type="checkbox" id="katex">
KaTeX support: inline <code>$math$ or \(math\)</code>,
block <code>$$math$$</code>, <code>\[math\]</code> or <pre>
```math
math
```</pre>
</label>
<label class="checkbox">
<input type="checkbox" id="asciimath">
Asciimath support: inline <code>``math``</code>, block: <pre>
```AM
math
```</pre>
</label>
<label class="checkbox"><input type="checkbox" id="auto-reload"> Enable auto-reload</label>
<label class="checkbox"><input type="checkbox" id="toc"> Enable Table Of Content</label>
</div>
Expand Down
54 changes: 54 additions & 0 deletions test/test_asciimath.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
asciimath formula test
-------------------

* inline data ``Z_(0..n)`` and formula ``1/x`` mix test

* mix inline formula ``Z_{0..n}`` outline formula

``A_i = B_i + C_{i} sum_{k=0}^{i} D_k E^k + dx``

and few formula in Paragraph test

``f(x) = ( Gamma((n+1)/2) )/( sqrt(n pi) Gamma(n/2) ) (1 + x^2/n)^(-(n+1)/2)``

formula in bolb **``e^2/(1-x)``**

* gt formula ``Re(z) > 0``test, lt ``Re(z) < [0];`` test

* test one dollar \$200 est

* Display formula
```asciimath
Gamma(x) = int_0^(+oo) t^(x-1)"e"^-t dt
```
`` f(x) = sqrt(sum_0^100 n) ``

* Double backslash with parentheses.
`` A_i = B_i + C_i sum_(k=0)^i D_k E^k ``

* Test multi-row formula

`` {: 1, 2, 3; 4, 5, 6; 7, 8, 9; :} ``
```AM
[
1, 2, 3;
4, 5, 6;
7, 8, 9;
]
```

* test formula in php code

```php
$subject = $this->secureHeader($subject);
$params = [$kind, $address, $name];
$this->setError($error_message);
$this->edebug($error_message);
```

* test formula in table

head1|head2
-----|------
1 | data: ``Z_(0..n)``
2 | formula ``{:1/(x-1):}^2``