summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCheng-Han, Wu2016-02-24 10:03:35 +0800
committerCheng-Han, Wu2016-02-24 10:03:35 +0800
commit1da0b87714e725dcd540f389b118c581fd93ccb7 (patch)
treee9b94733dc4cefdbdd0553cf91a4af287eeb0626
parentc509abbc395553bc2c09052a715c2339fd4a7a0e (diff)
Updated and fixed mathjax parsing, now using full featured plugin
-rw-r--r--public/js/extra.js15
-rw-r--r--public/vendor/markdown-it-mathjax.js120
-rw-r--r--public/views/foot.ejs1
-rw-r--r--public/views/pretty.ejs1
4 files changed, 123 insertions, 14 deletions
diff --git a/public/js/extra.js b/public/js/extra.js
index 90b9a70c..ce00d0e3 100644
--- a/public/js/extra.js
+++ b/public/js/extra.js
@@ -677,6 +677,7 @@ md.use(window.markdownitMark);
md.use(window.markdownitIns);
md.use(window.markdownitSub);
md.use(window.markdownitSup);
+md.use(window.markdownitMathjax);
md.renderer.rules.image = function (tokens, idx, options, env, self) {
tokens[idx].attrJoin('class', 'raw');
@@ -768,19 +769,6 @@ var gistPlugin = new Plugin(
return code;
}
);
-//mathjax
-var mathjaxPlugin = new Plugin(
- // regexp to match
- /^\$\$\n([\d\D]*?)\n\$\$$|\$([\d\D]*?)\$/,
-
- // this function will be called when something matches
- function (match, utils) {
- if (match.index == 0 && (md.meta.mathjax || typeof md.meta.mathjax == "undefined"))
- return '<span class="mathjax raw">' + match[0] + '</span>';
- else
- return match.input.slice(0, match[0].length);
- }
-);
//TOC
var tocPlugin = new Plugin(
// regexp to match
@@ -863,7 +851,6 @@ md.use(metaPlugin);
md.use(youtubePlugin);
md.use(vimeoPlugin);
md.use(gistPlugin);
-md.use(mathjaxPlugin);
md.use(tocPlugin);
md.use(slidesharePlugin);
md.use(speakerdeckPlugin); \ No newline at end of file
diff --git a/public/vendor/markdown-it-mathjax.js b/public/vendor/markdown-it-mathjax.js
new file mode 100644
index 00000000..01c6fa43
--- /dev/null
+++ b/public/vendor/markdown-it-mathjax.js
@@ -0,0 +1,120 @@
+// modified from https://github.com/classeur/markdown-it-mathjax
+
+(function(root, factory) {
+ if (typeof exports === 'object') {
+ module.exports = factory()
+ } else {
+ root.markdownitMathjax = factory()
+ }
+})(this, function() {
+ function math(state, silent) {
+ var startMathPos = state.pos
+ if (state.src.charCodeAt(startMathPos) !== 0x5C /* \ */) {
+ return false
+ }
+ var match = state.src.slice(++startMathPos).match(/^(?:\\\[|\\\(|begin\{([^}]*)\})/)
+ if (!match) {
+ return false
+ }
+ startMathPos += match[0].length
+ var type, endMarker, includeMarkers
+ if (match[0] === '\\[') {
+ type = 'display_math'
+ endMarker = '\\\\]'
+ } else if (match[0] === '\\(') {
+ type = 'inline_math'
+ endMarker = '\\\\)'
+ } else if (match[1]) {
+ type = 'math'
+ endMarker = '\\end{' + match[1] + '}'
+ includeMarkers = true
+ }
+ var endMarkerPos = state.src.indexOf(endMarker, startMathPos)
+ if (endMarkerPos === -1) {
+ return false
+ }
+ var nextPos = endMarkerPos + endMarker.length
+ if (!silent) {
+ var token = state.push(type + '_open', 'span', 1);
+ token.attrs = [ ['class', 'mathjax raw'] ];
+ token = state.push(type, '', 0);
+ token.content = includeMarkers ?
+ state.src.slice(state.pos, nextPos) : state.src.slice(startMathPos, endMarkerPos)
+ token = state.push(type + '_close', 'span', -1);
+ }
+ state.pos = nextPos
+ return true
+ }
+
+ function texMath(state, silent) {
+ var startMathPos = state.pos
+ if (state.src.charCodeAt(startMathPos) !== 0x24 /* $ */) {
+ return false
+ }
+
+ // Parse tex math according to http://pandoc.org/README.html#math
+ var endMarker = '$'
+ var afterStartMarker = state.src.charCodeAt(++startMathPos)
+ if (afterStartMarker === 0x24 /* $ */) {
+ endMarker = '$$'
+ if (state.src.charCodeAt(++startMathPos) === 0x24 /* $ */) {
+ // 3 markers are too much
+ return false
+ }
+ } else {
+ // Skip if opening $ is succeeded by a space character
+ if (afterStartMarker === 0x20 /* space */ || afterStartMarker === 0x09 /* \t */ || afterStartMarker === 0x0a /* \n */) {
+ return false
+ }
+ }
+ var endMarkerPos = state.src.indexOf(endMarker, startMathPos)
+ if (endMarkerPos === -1) {
+ return false
+ }
+ if (state.src.charCodeAt(endMarkerPos - 1) === 0x5C /* \ */) {
+ return false
+ }
+ var nextPos = endMarkerPos + endMarker.length
+ if (endMarker.length === 1) {
+ // Skip if $ is preceded by a space character
+ var beforeEndMarker = state.src.charCodeAt(endMarkerPos - 1)
+ if (beforeEndMarker === 0x20 /* space */ || beforeEndMarker === 0x09 /* \t */ || beforeEndMarker === 0x0a /* \n */) {
+ return false
+ }
+ // Skip if closing $ is succeeded by a digit (eg $5 $10 ...)
+ var suffix = state.src.charCodeAt(nextPos)
+ if (suffix >= 0x30 && suffix < 0x3A) {
+ return false
+ }
+ }
+
+ if (!silent) {
+ var type = endMarker.length === 1 ? 'inline_math' : 'display_math';
+ var token = state.push(type + '_open', 'span', 1)
+ token.attrs = [ ['class', 'mathjax raw'] ]
+ token = state.push(type, '', 0);
+ token.content = state.src.slice(startMathPos, endMarkerPos);
+ token = state.push(type + '_close', 'span', -1);
+ }
+ state.pos = nextPos
+ return true
+ }
+
+ function escapeHtml(html) {
+ return html.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/\u00a0/g, ' ')
+ }
+
+ return function(md) {
+ md.inline.ruler.before('escape', 'math', math)
+ md.inline.ruler.push('texMath', texMath)
+ md.renderer.rules.math = function(tokens, idx) {
+ return escapeHtml(tokens[idx].content)
+ }
+ md.renderer.rules.inline_math = function(tokens, idx) {
+ return '\\(' + escapeHtml(tokens[idx].content) + '\\)'
+ }
+ md.renderer.rules.display_math = function(tokens, idx) {
+ return '\\[' + escapeHtml(tokens[idx].content) + '\\]'
+ }
+ }
+})
diff --git a/public/views/foot.ejs b/public/views/foot.ejs
index a77ef864..20db896c 100644
--- a/public/views/foot.ejs
+++ b/public/views/foot.ejs
@@ -33,6 +33,7 @@
<script src="<%- url %>/vendor/markdown-it-ins/dist/markdown-it-ins.min.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-sub/dist/markdown-it-sub.min.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-sup/dist/markdown-it-sup.min.js" defer></script>
+<script src="<%- url %>/vendor/markdown-it-mathjax.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-regexp.js" defer></script>
<script src="<%- url %>/vendor/gist-embed.js" defer></script>
<script src="<%- url %>/vendor/lz-string/libs/lz-string.min.js" defer></script>
diff --git a/public/views/pretty.ejs b/public/views/pretty.ejs
index b07b94ff..9dd61d7d 100644
--- a/public/views/pretty.ejs
+++ b/public/views/pretty.ejs
@@ -85,6 +85,7 @@
<script src="<%- url %>/vendor/markdown-it-ins/dist/markdown-it-ins.min.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-sub/dist/markdown-it-sub.min.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-sup/dist/markdown-it-sup.min.js" defer></script>
+<script src="<%- url %>/vendor/markdown-it-mathjax.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-regexp.js" defer></script>
<script src="<%- url %>/vendor/gist-embed.js" defer></script>
<script src="<%- url %>/vendor/string.min.js" defer></script>