Home / GitHub Page

Weird parsing problem with inline asciimath

Hi !
I’m trying to add a simple asciimath (http://asciimath.org/) implementation to Joplin. I proceeded by first duplicating the katex.js rule and adding my new file (asciimath.js) in MdToHtml.js.

Asciimath blocks works like a charm but inline math seems to be ignored, and i can’t find where i broke it…

Here is the diff (katex.js/asciimath.js)

9a10
> const AsciiMathParser = require('./asciimath2tex.js')
10a12
> const asciiparser = new AsciiMathParser();
48c50
< // Assumes that there is a "$" at state.src[pos]
---
> // Assumes that there is a "£" at state.src[pos]
74c76
< function math_inline(state, silent) {
---
> function asciimath_inline(state, silent) {
77c79
< 	if (state.src[state.pos] !== '$') {
---
> 	if (state.src[state.pos] !== '£') {
84c86
< 			state.pending += '$';
---
> 			state.pending += '£';
96,97c98,99
< 	while ((match = state.src.indexOf('$', match)) !== -1) {
< 		// Found potential $, look for escapes, pos will point to
---
> 	while ((match = state.src.indexOf('£', match)) !== -1) {
> 		// Found potential £, look for escapes, pos will point to
111c113
< 	// No closing delimter found.  Consume $ and continue.
---
> 	// No closing delimter found.  Consume £ and continue.
114c116
< 			state.pending += '$';
---
> 			state.pending += '£';
120c122
< 	// Check if we have empty content, ie: $$.  Do not parse.
---
> 	// Check if we have empty content, ie: ùù.  Do not parse.
123c125
< 			state.pending += '$$';
---
> 			state.pending += '££';
133c135
< 			state.pending += '$';
---
> 			state.pending += '£';
140,141c142,143
< 		token = state.push('math_inline', 'math', 0);
< 		token.markup = '$';
---
> 		token = state.push('asciimath_inline', 'math', 0);
> 		token.markup = '£';
149c151
< function math_block(state, start, end, silent) {
---
> function asciimath_block(state, start, end, silent) {
162c164
< 	if (state.src.slice(pos, pos + 2) !== '$$') {
---
> 	if (state.src.slice(pos, pos + 2) !== '££') {
172c174
< 	if (firstLine.trim().slice(-2) === '$$') {
---
> 	if (firstLine.trim().slice(-2) === '££') {
197c199
< 				.slice(-2) === '$$'
---
> 				.slice(-2) === '££'
199c201
< 			lastPos = state.src.slice(0, max).lastIndexOf('$$');
---
> 			lastPos = state.src.slice(0, max).lastIndexOf('££');
207c209
< 	token = state.push('math_block', 'math', 0);
---
> 	token = state.push('asciimath_block', 'math', 0);
211c213
< 	token.markup = '$$';
---
> 	token.markup = '££';
253a256
>                 latex = asciiparser.parse(latex);
255c258
< 					return `<span class="joplin-editable"><span class="joplin-source" data-joplin-language="katex" data-joplin-source-open="$" data-joplin-source-close="$">${md.utils.escapeHtml(latex)}</span>${renderToStringWithCache(latex, options)}</span>`;
---
> 					return `<span class="joplin-editable"><span class="joplin-source" data-joplin-language="katex" data-joplin-source-open="£" data-joplin-source-close="£">${md.utils.escapeHtml(latex)}</span>${renderToStringWithCache(latex, options)}</span>`;
262c265
< 			const inlineRenderer = function(tokens, idx) {
---
> 			const asciimathInlineRenderer = function(tokens, idx) {
268a272
> 				latex = asciiparser.parse(latex);
270c274
< 					return `<div class="joplin-editable"><pre class="joplin-source" data-joplin-language="katex" data-joplin-source-open="$$&#10;" data-joplin-source-close="&#10;$$&#10;">${md.utils.escapeHtml(latex)}</pre>${renderToStringWithCache(latex, options)}</div>`;
---
> 					return `<div class="joplin-editable"><pre class="joplin-source" data-joplin-language="katex" data-joplin-source-open="££&#10;" data-joplin-source-close="&#10;££&#10;">${md.utils.escapeHtml(latex)}</pre>${renderToStringWithCache(latex, options)}</div>`;
277c281
< 			const blockRenderer = function(tokens, idx) {
---
> 			const asciimathBlockRenderer = function(tokens, idx) {
282,283c286,287
< 			md.inline.ruler.after('escape', 'math_inline', math_inline);
< 			md.block.ruler.after('blockquote', 'math_block', math_block, {
---
> 			md.inline.ruler.after('escape', 'asciimath_inline', asciimath_inline);
> 			md.block.ruler.after('blockquote', 'asciimath_block', asciimath_block, {
286,287c290,291
< 			md.renderer.rules.math_inline = inlineRenderer;
< 			md.renderer.rules.math_block = blockRenderer;
---
> 			md.renderer.rules.asciimath_inline = asciimathInlineRenderer;
> 			md.renderer.rules.asciimath_block = asciimathBlockRenderer;

If you’re up to help me and you need more informations i’ll be glad to provide it :smiley: