Skip to content

Commit 7d4217e

Browse files
committed
Do not re-wrap align environments, and do not apply AsciiMath to LaTeX (autodetect).
1 parent 17bc5ca commit 7d4217e

3 files changed

Lines changed: 61 additions & 13 deletions

File tree

corsscripts/ascii/filters/010_latexwrap.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,29 @@ import findtextindex from './findtextindex.js';
88
// col 3 – relation symbol and right-hand side
99
// A \text{…} that is not \text{or/and/if} is pushed into a 4th column.
1010
export default function latexwrap(lines) {
11+
// Do not attempt to wrap LaTeX which is already aligned.
12+
const nonest = [
13+
'align',
14+
'flalign',
15+
'alignat',
16+
'xalignat',
17+
'xxalignat',
18+
'gather',
19+
'multline',
20+
'equation',
21+
'split',
22+
'subequations'
23+
];
24+
const skipenv = nonest.flatMap(token => [`\\begin{${token}}`, `\\begin{${token}*}`]);
25+
var skip = false;
26+
for (let str of lines) {
27+
if (findtextindex(str, skipenv) !== false) {
28+
skip = true;
29+
}
30+
}
31+
if (skip) {
32+
return lines;
33+
}
1134
const output = [`\\[\\begin{align*}`];
1235
for (let str of lines) {
1336
str = str.trim();

corsscripts/ascii/markdownitrules.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,18 @@ export default function markdownitrules(mdit, options) {
2222
mdit.renderer.rules.code_inline = function(tokens, idx) {
2323
const code = tokens[idx].content;
2424
const inlineWrap = (s) => `\\(${s}\\)`;
25+
if (isLaTeX(code)) {
26+
return inlineWrap(code);
27+
}
2528
return inlineWrap(window.AMparseMath(code, true));
2629
};
2730

2831
mdit.renderer.rules.asciimath_block = function(tokens, idx) {
2932
const code = tokens[idx].content;
33+
const blockWrap = (s) => `\\[${s}\\]`;
34+
if (isLaTeX(code)) {
35+
return blockWrap(code);
36+
}
3037
return applyFilters(code, true);
3138
};
3239

@@ -41,6 +48,24 @@ export default function markdownitrules(mdit, options) {
4148
return applyFilters(code, false);
4249
};
4350

51+
function isLaTeX(code) {
52+
// Do not attempt to apply ASCIIMath to blocks which are already LaTeX.
53+
const islatex = [
54+
'^{',
55+
'_{',
56+
'\\left',
57+
'\\right',
58+
'\\begin',
59+
];
60+
if (islatex.some(s => code.includes(s))) {
61+
return true;
62+
};
63+
// Use of a general control code.
64+
// (Yes, this duplicates \left, \right, \begin above...)
65+
const regex = /\\[a-zA-Z]+/;
66+
return regex.test(code);
67+
};
68+
4469
function splitBlock(code) {
4570
return code.split(/\r?\n/).map(line => line.trim()).filter(line => line !== '');
4671
}

corsscripts/ascii/stackascii.bundle.js

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)