Skip to content

Commit

Permalink
don't replace mbox when used as intended in LaTeX
Browse files Browse the repository at this point in the history
  • Loading branch information
daviestar committed Nov 19, 2021
1 parent c9a2693 commit 2329d67
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
27 changes: 27 additions & 0 deletions compiler/src/pre-parse/__test__/convert-block-tex.test.ts
Expand Up @@ -41,6 +41,33 @@ describe('convertNewPageToDirective', () => {
expect(ignoreWhitespace(md)).toBe(ignoreWhitespace(expected));
});

it('should reformat a \\mbox{ } to pagebreak directive', async () => {
const { md } = await testProcessor(`
\\mbox{ }
`);

const expected = unindentStringAndTrim(`
::pagebreak
`);

expect(ignoreWhitespace(md)).toBe(ignoreWhitespace(expected));
});

it('should NOT reformat a \\mbox{x} to pagebreak directive', async () => {
const { md } = await testProcessor(String.raw`
\begin{equation}
\mathrm{sign}(x)=
\begin{cases}
1 & \mbox{if } $x>0$\\
0 & \mbox{if } $x=0$\\
-1 & \mbox{if } $x<0$\\
\end{cases}
\end{equation}
`);

expect(md.includes('::pagebreak')).toBe(false);
});

it('should be idempotent', async () => {
const { md } = await testProcessor(`
\\newpage
Expand Down
17 changes: 16 additions & 1 deletion compiler/src/pre-parse/convert-block-tex.ts
@@ -1,8 +1,23 @@
const blockList = ['\\newpage', '\\pagebreak', '\\mbox'];
const blockList = ['\\newpage', '\\pagebreak'];

export function convertNewPageToDirective(contents: string) {
return contents
.split('\n')
.map((a) => (blockList.some((b) => a.includes(b)) ? '::pagebreak' : a))
.join('\n');
}

export function convertEmptyMBoxToDirective(contents: string) {
return contents
.split('\n')
.map((line) => {
if (
line.includes('\\mbox') &&
line.replace('{', '').replace('}', '').trim() === '\\mbox'
) {
return '::pagebreak';
}
return line;
})
.join('\n');
}
6 changes: 5 additions & 1 deletion compiler/src/pre-parse/index.ts
@@ -1,7 +1,10 @@
import { VFile } from 'vfile';

import { allowNoWhitespaceBeforeHeading } from './allow-no-whitespace-before-heading';
import { convertNewPageToDirective } from './convert-block-tex';
import {
convertEmptyMBoxToDirective,
convertNewPageToDirective,
} from './convert-block-tex';
import { convertTextBfToMd, convertUrlToMd } from './convert-inline-tex';
import { convertMacroToDirective } from './convert-macro-to-directive';
import { reformatPandocSimpleTables } from './reformat-pandoc-simple-tables';
Expand All @@ -21,6 +24,7 @@ export function preParsePhase(file: VFile) {
result = convertTextBfToMd(result);
result = convertUrlToMd(result);
result = convertNewPageToDirective(result);
result = convertEmptyMBoxToDirective(result);
result = reformatPandocSimpleTables(result);
file.contents = result;
return file;
Expand Down

0 comments on commit 2329d67

Please sign in to comment.