Skip to content

Commit e7ad64e

Browse files
committed
Fix arnog#590. Deps. Export mathjsonToLatex and latexToMathjson, and mathjson as an outputFormat
1 parent 5305425 commit e7ad64e

12 files changed

+11286
-245
lines changed

dist/mathlive.js

+5,607-64
Large diffs are not rendered by default.

dist/mathlive.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/mathlive.min.mjs

+1-1
Large diffs are not rendered by default.

dist/mathlive.mjs

+5,599-59
Large diffs are not rendered by default.

dist/public/mathfield.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { ParseMode, MacroDictionary, Style } from './core';
1313
| `"spoken-ssml-withHighlighting"`|Like `"spoken-ssml"` but with additional annotations necessary for synchronized higlighting (read aloud)|
1414
| `"mathML"` | A string of MathML markup|
1515
*/
16-
export declare type OutputFormat = 'latex' | 'latex-expanded' | 'json' | 'json-2' | 'spoken' | 'spoken-text' | 'spoken-ssml' | 'spoken-ssml-withHighlighting' | 'mathML' | 'ASCIIMath';
16+
export declare type OutputFormat = 'latex' | 'latex-expanded' | 'mathjson' | 'json' | 'json-2' | 'spoken' | 'spoken-text' | 'spoken-ssml' | 'spoken-ssml-withHighlighting' | 'mathML' | 'ASCIIMath';
1717
export declare type InsertOptions = {
1818
/** If `"auto"` or omitted, the current mode is used */
1919
mode?: ParseMode | 'auto';

examples/test-cases/index.html

+9-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
<h1>MathLive Test Cases</h1>
2828
</header>
2929
<main>
30-
<div class="mathfield" id="mf">(x,,2)</div>
30+
<div class="mathfield" id="mf">a=</div>
31+
<!-- (x,,2) -->
3132
<!-- -123, 456.789, -->
3233
<!-- x_5 -->
3334
<!-- -5-3-2 -->
@@ -82,6 +83,7 @@ <h1>Examples</h1>
8283

8384
<script type="module">
8485
import MathLive from '/dist/mathlive.mjs';
86+
import { makeMathField, mathjsonToLatex, latexToMathjson } from '/dist/mathlive.mjs';
8587

8688
// prettier-ignore
8789
const TESTING_SAMPLES = [
@@ -1134,12 +1136,12 @@ <h1>Examples</h1>
11341136

11351137

11361138
let errors = [];
1137-
let expr = MathLive.latexToAST(latex, {
1139+
let expr = latexToMathjson(latex, {
11381140
form: 'canonical',
11391141
onError: (err) =>
11401142
errors.push('parse: ' + err.code + (err.arg ? ' ' + err.arg : '')),
11411143
});
1142-
let jsonLatex = MathLive.astToLatex(expr, {
1144+
let jsonLatex = mathjsonToLatex(expr, {
11431145
precision: 100, onError: (err) =>
11441146
errors.push('emit: ' + err.code + (err.arg ? ' ' + err.arg : '')),
11451147
});
@@ -1154,13 +1156,13 @@ <h1>Examples</h1>
11541156
MathLive.latexToMarkup(jsonLatex);
11551157

11561158
errors = [];
1157-
expr = MathLive.latexToAST(latex, {
1159+
expr = latexToMathjson(latex, {
11581160
form: 'full',
11591161
preserveLatex: true,
11601162
onError: (err) =>
11611163
errors.push('parse: ' + err.code + (err.arg ? ' ' + err.arg : '')),
11621164
});
1163-
jsonLatex = MathLive.astToLatex(expr, {
1165+
jsonLatex = mathjsonToLatex(expr, {
11641166
precision: 100, onError: (err) =>
11651167
errors.push('emit: ' + err.code + (err.arg ? ' ' + err.arg : '')),
11661168
});
@@ -1174,7 +1176,7 @@ <h1>Examples</h1>
11741176
MathLive.latexToMarkup(jsonLatex);
11751177

11761178
errors = [];
1177-
expr = MathLive.latexToAST(latex, {
1179+
expr = latexToMathjson(latex, {
11781180
form: 'full',
11791181
invisibleOperator: '',
11801182
superscriptOperator: '',
@@ -1203,7 +1205,7 @@ <h1>Examples</h1>
12031205
}
12041206
let mf;
12051207
if (true) {
1206-
mf = MathLive.makeMathField('mf', {
1208+
mf = makeMathField('mf', {
12071209
// fontsDirectory: '/dist/fonts/',
12081210
// letterShapeStyle: 'french', // 'french', 'upright', 'tex'
12091211
smartFence: true,

package-lock.json

+29-78
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@
8888
"@types/css-font-loading-module": "0.0.4",
8989
"@types/jest": "^26.0.10",
9090
"@types/node": "^14.6.0",
91-
"@typescript-eslint/eslint-plugin": "^3.9.0",
92-
"@typescript-eslint/parser": "^3.9.0",
93-
"@typescript-eslint/typescript-estree": "^3.9.0",
91+
"@typescript-eslint/eslint-plugin": "^3.9.1",
92+
"@typescript-eslint/parser": "^3.9.1",
93+
"@typescript-eslint/typescript-estree": "^3.9.1",
9494
"autoprefixer": "^9.8.6",
9595
"check-node-version": "^4.0.3",
9696
"cssnano": "^4.1.10",
@@ -107,7 +107,7 @@
107107
"postcss-cli": "^7.1.1",
108108
"prettier": "^2.0.5",
109109
"rimraf": "^3.0.2",
110-
"rollup": "^2.26.3",
110+
"rollup": "^2.26.4",
111111
"rollup-plugin-copy": "^3.3.0",
112112
"rollup-plugin-eslint": "^7.0.0",
113113
"rollup-plugin-node-resolve": "^5.2.0",

rollup.config.js

+2
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,15 @@ const ROLLUP = [
173173
format: 'es',
174174
file: `${BUILD_DIRECTORY}/mathlive.mjs`,
175175
sourcemap: !PRODUCTION,
176+
exports: 'named',
176177
},
177178
// UMD file, suitable for import, <script> and require()
178179
{
179180
format: 'umd',
180181
name: 'MathLive',
181182
file: `${BUILD_DIRECTORY}/mathlive.js`,
182183
sourcemap: !PRODUCTION,
184+
exports: 'named',
183185
},
184186
]
185187
: [

src/editor/mathfield-class.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ import coreStylesheet from '../../css/core.less';
8383

8484
import popoverStylesheet from '../../css/popover.less';
8585
import keystrokeCaptionStylesheet from '../../css/keystroke-caption.less';
86-
// import { parseLatex } from '../math-json/math-json';
86+
import { parseLatex } from '../math-json/math-json';
8787
import { atomtoMathJson } from '../addons/math-json';
8888

8989
export class MathfieldPrivate implements Mathfield {
@@ -685,11 +685,13 @@ export class MathfieldPrivate implements Mathfield {
685685
result = atomToSpeakableText(root, this.config);
686686
this.config.textToSpeechMarkup = saveTextToSpeechMarkup;
687687
// this.config.atomIdsSettings = savedAtomIdsSettings; // @revisit
688+
} else if (format === 'mathjson') {
689+
const json = parseLatex(root.toLatex(true), {
690+
form: 'canonical',
691+
});
692+
result = JSON.stringify(json);
688693
} else if (format === 'json') {
689694
const json = atomtoMathJson(root);
690-
// const json = parseLatex(root.toLatex(true), {
691-
// form: 'canonical',
692-
// });
693695
result = JSON.stringify(json);
694696
} else if (format === 'json-2') {
695697
const json = atomtoMathJson(root);

src/mathlive.ts

+26-26
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ import { atomsToMathML } from './addons/math-ml';
3434
import './addons/definitions-metadata';
3535
import { AutoRenderOptionsPrivate } from './addons/auto-render';
3636
import { ErrorCode as MathJsonErrorCode } from './math-json/public';
37-
// import { ErrorCode, Form, Expression, Dictionary } from './math-json/public';
38-
// import { parseLatex, emitLatex } from './math-json/math-json';
39-
// import { ParseLatexOptions, EmitLatexOptions } from './math-json/latex/public';
37+
import { ErrorCode, Form, Expression, Dictionary } from './math-json/public';
38+
import { parseLatex, emitLatex } from './math-json/math-json';
39+
import { ParseLatexOptions, EmitLatexOptions } from './math-json/latex/public';
4040

4141
function latexToMarkup(
4242
text: string,
@@ -157,29 +157,29 @@ function astToLatex(expr: MathJson, options: MathJsonLatexOptions): string {
157157
);
158158
// return emitLatex(expr, options);
159159
}
160-
// function latexToAST(
161-
// latex: string,
162-
// options?: ParseLatexOptions & {
163-
// macros?: MacroDictionary;
164-
// onError?: ErrorListener<ErrorCode>;
165-
// form?: Form | Form[];
166-
// }
167-
// ): Expression {
168-
// options = options ?? {};
169-
// options.macros = { ...MACROS, ...(options.macros ?? {}) };
170-
171-
// return parseLatex(latex, options);
172-
// }
173-
174-
// function astToLatex(
175-
// expr: Expression,
176-
// options: EmitLatexOptions & {
177-
// dictionary?: Dictionary;
178-
// onError?: ErrorListener<ErrorCode>;
179-
// }
180-
// ): string {
181-
// return emitLatex(expr, options);
182-
// }
160+
export function latexToMathjson(
161+
latex: string,
162+
options?: ParseLatexOptions & {
163+
macros?: MacroDictionary;
164+
onError?: ErrorListener<ErrorCode>;
165+
form?: Form | Form[];
166+
}
167+
): Expression {
168+
options = options ?? {};
169+
options.macros = { ...MACROS, ...(options.macros ?? {}) };
170+
171+
return parseLatex(latex, options);
172+
}
173+
174+
export function mathjsonToLatex(
175+
expr: Expression,
176+
options: EmitLatexOptions & {
177+
dictionary?: Dictionary;
178+
onError?: ErrorListener<ErrorCode>;
179+
}
180+
): string {
181+
return emitLatex(expr, options);
182+
}
183183

184184
function latexToSpeakableText(
185185
latex: string,

src/public/mathfield.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { ParseMode, MacroDictionary, Style } from './core';
1717
export type OutputFormat =
1818
| 'latex'
1919
| 'latex-expanded'
20+
| 'mathjson' // Work progress next generation MathJson
2021
| 'json'
2122
| 'json-2'
2223
| 'spoken'

0 commit comments

Comments
 (0)