markdown-it-mathjax3
Advanced tools
Comparing version 2.0.3 to 2.1.0
226
index.js
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
/* Process inline math */ | ||
@@ -9,57 +10,45 @@ /* | ||
*/ | ||
/*jslint node: true */ | ||
'use strict'; | ||
const {mathjax} = require('mathjax-full/js/mathjax') | ||
const {TeX} = require('mathjax-full/js/input/tex') | ||
const {SVG} = require('mathjax-full/js/output/svg') | ||
const {liteAdaptor} = require('mathjax-full/js/adaptors/liteAdaptor') | ||
const html = require('mathjax-full/js/handlers/html') | ||
const {AllPackages} = require('mathjax-full/js/input/tex/AllPackages') | ||
const adaptor = liteAdaptor() | ||
html.RegisterHTMLHandler(adaptor) | ||
const tex = new TeX({packages: AllPackages}) | ||
const svg = new SVG({fontCache: 'none'}) | ||
const mathDocument = mathjax.document('', {InputJax: tex, OutputJax: svg}) | ||
const mathjax_1 = require("mathjax-full/js/mathjax"); | ||
const tex_1 = require("mathjax-full/js/input/tex"); | ||
const svg_1 = require("mathjax-full/js/output/svg"); | ||
const liteAdaptor_1 = require("mathjax-full/js/adaptors/liteAdaptor"); | ||
const html_1 = require("mathjax-full/js/handlers/html"); | ||
const AllPackages_1 = require("mathjax-full/js/input/tex/AllPackages"); | ||
const adaptor = liteAdaptor_1.liteAdaptor(); | ||
html_1.RegisterHTMLHandler(adaptor); | ||
const tex = new tex_1.TeX({ packages: AllPackages_1.AllPackages }); | ||
const svg = new svg_1.SVG({ fontCache: "none" }); | ||
const mathDocument = mathjax_1.mathjax.document("", { InputJax: tex, OutputJax: svg }); | ||
// Test if potential opening or closing delimieter | ||
// Assumes that there is a "$" at state.src[pos] | ||
function isValidDelim(state, pos) { | ||
var prevChar, nextChar, | ||
max = state.posMax, | ||
can_open = true, | ||
can_close = true; | ||
prevChar = pos > 0 ? state.src.charCodeAt(pos - 1) : -1; | ||
nextChar = pos + 1 <= max ? state.src.charCodeAt(pos + 1) : -1; | ||
let max = state.posMax, can_open = true, can_close = true; | ||
const prevChar = pos > 0 ? state.src.charCodeAt(pos - 1) : -1, nextChar = pos + 1 <= max ? state.src.charCodeAt(pos + 1) : -1; | ||
// Check non-whitespace conditions for opening and closing, and | ||
// check that closing delimeter isn't followed by a number | ||
if (prevChar === 0x20/* " " */ || prevChar === 0x09/* \t */ || | ||
(nextChar >= 0x30/* "0" */ && nextChar <= 0x39/* "9" */)) { | ||
if (prevChar === 0x20 /* " " */ || | ||
prevChar === 0x09 /* \t */ || | ||
(nextChar >= 0x30 /* "0" */ && nextChar <= 0x39) /* "9" */) { | ||
can_close = false; | ||
} | ||
if (nextChar === 0x20/* " " */ || nextChar === 0x09/* \t */) { | ||
if (nextChar === 0x20 /* " " */ || nextChar === 0x09 /* \t */) { | ||
can_open = false; | ||
} | ||
return { | ||
can_open: can_open, | ||
can_close: can_close | ||
can_close: can_close, | ||
}; | ||
} | ||
function math_inline(state, silent) { | ||
var start, match, token, res, pos, esc_count; | ||
if (state.src[state.pos] !== "$") {return false;} | ||
res = isValidDelim(state, state.pos); | ||
if (state.src[state.pos] !== "$") { | ||
return false; | ||
} | ||
let res = isValidDelim(state, state.pos); | ||
if (!res.can_open) { | ||
if (!silent) {state.pending += "$";} | ||
if (!silent) { | ||
state.pending += "$"; | ||
} | ||
state.pos += 1; | ||
return true; | ||
} | ||
// First check for and bypass all properly escaped delimieters | ||
@@ -69,60 +58,65 @@ // This loop will assume that the first leading backtick can not | ||
// we have found an opening delimieter already. | ||
start = state.pos + 1; | ||
match = start; | ||
const start = state.pos + 1; | ||
let match = start; | ||
while ((match = state.src.indexOf("$", match)) !== -1) { | ||
// Found potential $, look for escapes, pos will point to | ||
// first non escape when complete | ||
pos = match - 1; | ||
while (state.src[pos] === "\\") {pos -= 1;} | ||
let pos = match - 1; | ||
while (state.src[pos] === "\\") { | ||
pos -= 1; | ||
} | ||
// Even number of escapes, potential closing delimiter found | ||
if (((match - pos) % 2) == 1) {break;} | ||
if ((match - pos) % 2 == 1) { | ||
break; | ||
} | ||
match += 1; | ||
} | ||
// No closing delimter found. Consume $ and continue. | ||
if (match === -1) { | ||
if (!silent) {state.pending += "$";} | ||
if (!silent) { | ||
state.pending += "$"; | ||
} | ||
state.pos = start; | ||
return true; | ||
} | ||
// Check if we have empty content, ie: $$. Do not parse. | ||
if (match - start === 0) { | ||
if (!silent) {state.pending += "$$";} | ||
if (!silent) { | ||
state.pending += "$$"; | ||
} | ||
state.pos = start + 1; | ||
return true; | ||
} | ||
// Check for valid closing delimiter | ||
res = isValidDelim(state, match); | ||
if (!res.can_close) { | ||
if (!silent) {state.pending += "$";} | ||
if (!silent) { | ||
state.pending += "$"; | ||
} | ||
state.pos = start; | ||
return true; | ||
} | ||
if (!silent) { | ||
token = state.push('math_inline', 'math', 0); | ||
const token = state.push("math_inline", "math", 0); | ||
token.markup = "$"; | ||
token.content = state.src.slice(start, match); | ||
} | ||
state.pos = match + 1; | ||
return true; | ||
} | ||
function math_block(state, start, end, silent) { | ||
var firstLine, lastLine, next, lastPos, found = false, token, | ||
pos = state.bMarks[start] + state.tShift[start], | ||
max = state.eMarks[start] | ||
if (pos + 2 > max) {return false;} | ||
if (state.src.slice(pos, pos + 2) !== '$$') {return false;} | ||
let next, lastPos; | ||
let found = false, pos = state.bMarks[start] + state.tShift[start], max = state.eMarks[start], lastLine = ""; | ||
if (pos + 2 > max) { | ||
return false; | ||
} | ||
if (state.src.slice(pos, pos + 2) !== "$$") { | ||
return false; | ||
} | ||
pos += 2; | ||
firstLine = state.src.slice(pos, max); | ||
if (silent) {return true;} | ||
if (firstLine.trim().slice(-2) === '$$') { | ||
let firstLine = state.src.slice(pos, max); | ||
if (silent) { | ||
return true; | ||
} | ||
if (firstLine.trim().slice(-2) === "$$") { | ||
// Single line expression | ||
@@ -132,12 +126,9 @@ firstLine = firstLine.trim().slice(0, -2); | ||
} | ||
for (next = start; !found;) { | ||
next++; | ||
if (next >= end) {break;} | ||
if (next >= end) { | ||
break; | ||
} | ||
pos = state.bMarks[next] + state.tShift[next]; | ||
max = state.eMarks[next]; | ||
if (pos < max && state.tShift[next] < state.blkIndent) { | ||
@@ -147,84 +138,57 @@ // non-empty line with negative indent should stop the list: | ||
} | ||
if (state.src.slice(pos, max).trim().slice(-2) === '$$') { | ||
lastPos = state.src.slice(0, max).lastIndexOf('$$'); | ||
if (state.src.slice(pos, max).trim().slice(-2) === "$$") { | ||
lastPos = state.src.slice(0, max).lastIndexOf("$$"); | ||
lastLine = state.src.slice(pos, lastPos); | ||
found = true; | ||
} | ||
} | ||
state.line = next + 1; | ||
token = state.push('math_block', 'math', 0); | ||
const token = state.push("math_block", "math", 0); | ||
token.block = true; | ||
token.content = (firstLine && firstLine.trim() ? firstLine + '\n' : '') | ||
+ state.getLines(start + 1, next, state.tShift[start], true) | ||
+ (lastLine && lastLine.trim() ? lastLine : ''); | ||
token.content = | ||
(firstLine && firstLine.trim() ? firstLine + "\n" : "") + | ||
state.getLines(start + 1, next, state.tShift[start], true) + | ||
(lastLine && lastLine.trim() ? lastLine : ""); | ||
token.map = [start, state.line]; | ||
token.markup = '$$'; | ||
token.markup = "$$"; | ||
return true; | ||
} | ||
module.exports = function math_plugin(md, options) { | ||
module.exports = function (md, options) { | ||
// Default options | ||
options = options || {}; | ||
// set MathJax as the renderer for markdown-it-simplemath | ||
var mathjaxInline = function (latex) { | ||
md.inline.ruler.after("escape", "math_inline", math_inline); | ||
md.block.ruler.after("blockquote", "math_block", math_block, { | ||
alt: ["paragraph", "reference", "blockquote", "list"], | ||
}); | ||
md.renderer.rules.math_inline = function (tokens, idx) { | ||
options.display = false; | ||
try { | ||
return adaptor.outerHTML(mathDocument.convert(latex, options)); | ||
} | ||
catch (error) { | ||
if (options.throwOnError) {console.log(error);} | ||
return latex; | ||
} | ||
return adaptor.outerHTML(mathDocument.convert(tokens[idx].content, options)); | ||
}; | ||
var inlineRenderer = function (tokens, idx) { | ||
return mathjaxInline(tokens[idx].content); | ||
md.renderer.rules.math_block = function (tokens, idx) { | ||
options.display = true; | ||
return adaptor.outerHTML(mathDocument.convert(tokens[idx].content, options)); | ||
}; | ||
var mathjaxBlock = function (latex) { | ||
options.display = true; | ||
try { | ||
return "<p>" + adaptor.outerHTML(mathDocument.convert(latex, options)) + "</p>"; | ||
} | ||
catch (error) { | ||
if (options.throwOnError) {console.log(error);} | ||
return latex; | ||
} | ||
} | ||
var blockRenderer = function (tokens, idx) { | ||
return mathjaxBlock(tokens[idx].content) + '\n'; | ||
} | ||
md.inline.ruler.after('escape', 'math_inline', math_inline); | ||
md.block.ruler.after('blockquote', 'math_block', math_block, { | ||
alt: ['paragraph', 'reference', 'blockquote', 'list'] | ||
}); | ||
md.renderer.rules.math_inline = inlineRenderer; | ||
md.renderer.rules.math_block = blockRenderer; | ||
md.renderer.render = function (tokens, options, env) { | ||
var i, len, type, | ||
result = '', | ||
rules = this.rules; | ||
for (i = 0, len = tokens.length; i < len; i++) { | ||
type = tokens[i].type; | ||
if (type === 'inline') { | ||
result += this.renderInline(tokens[i].children, options, env); | ||
} else if (typeof rules[type] !== 'undefined') { | ||
let result = "", rules = this.rules; | ||
for (let i = 0, len = tokens.length; i < len; i++) { | ||
const type = tokens[i].type; | ||
if (type === "inline") { | ||
result += this.renderInline(tokens[i].children || [], options, env); | ||
} | ||
else if (typeof rules[type] !== "undefined") { | ||
result += rules[tokens[i].type](tokens, i, options, env, this); | ||
} else { | ||
result += this.renderToken(tokens, i, options, env); | ||
} | ||
else { | ||
result += this.renderToken(tokens, i, options); | ||
} | ||
} | ||
result += `<style>${adaptor.textContent(svg.styleSheet(mathDocument))}</style>` | ||
const noMath = tokens.every(function isNotMath(token) { | ||
return token.tag !== "math" && (Array.isArray(token.children) ? token.children.every(isNotMath) : true); | ||
}); | ||
if (!noMath) { | ||
result += `<style>${adaptor.textContent(svg.styleSheet(mathDocument))}</style>`; | ||
} | ||
return result; | ||
}; | ||
}; |
{ | ||
"name": "markdown-it-mathjax3", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"description": "Fast math support for markdown-it with MathJax", | ||
@@ -10,7 +10,2 @@ "main": "index.js", | ||
}, | ||
"files":[ | ||
"index.js", | ||
"README.md", | ||
"LICENSE" | ||
], | ||
"keywords": [ | ||
@@ -30,6 +25,17 @@ "markdown", | ||
"devDependencies": { | ||
"markdown-it": "^6.0.0", | ||
"markdown-it-testgen": "^0.1.4", | ||
"tape": "^4.5.1" | ||
} | ||
"@types/markdown-it": "^10.0.0", | ||
"@types/punycode": "^2.1.0", | ||
"prettier": "^2.0.4", | ||
"typescript": "^3.8.3" | ||
}, | ||
"scripts": { | ||
"build": "tsc", | ||
"format": "prettier --write *.ts *.json" | ||
}, | ||
"files": [ | ||
"LICENSE", | ||
"README.md", | ||
"index.js", | ||
"package.json" | ||
] | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
189
10592
4
1