rapier-math 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +15 -5
  2. package/dist/accessible.css +6 -0
  3. package/dist/accessible.d.ts +10 -0
  4. package/dist/accessible.mjs +18 -0
  5. package/dist/classic.d.ts +8 -2
  6. package/dist/combined.d.ts +4 -0
  7. package/dist/combined.mjs +2 -0
  8. package/dist/index.d.ts +11 -0
  9. package/dist/index.mjs +6 -2980
  10. package/dist/integration.d.ts +3 -1
  11. package/dist/integration.mjs +13 -19
  12. package/dist/markdown-it.mjs +7 -4
  13. package/dist/mathml.d.ts +5 -0
  14. package/dist/mathml.mjs +3 -0
  15. package/dist/rapier-math.js +595 -143
  16. package/dist/rehype.mjs +13 -3
  17. package/docs/api.md +38 -2
  18. package/docs/math-components.json +6 -5
  19. package/docs/math-resources.json +150 -54
  20. package/docs/semantics.md +228 -0
  21. package/examples/README.md +10 -0
  22. package/examples/accessible.html +27 -0
  23. package/examples/accessible.mjs +42 -0
  24. package/examples/semantic.mjs +11 -0
  25. package/package.json +16 -2
  26. package/src/math/api.js +16 -55
  27. package/src/math/browser.js +4 -0
  28. package/src/math/classic.js +7 -0
  29. package/src/math/combined.js +13 -0
  30. package/src/math/fallbacks.js +5 -0
  31. package/src/math/font.js +3 -0
  32. package/src/math/layout.js +6 -37
  33. package/src/math/mathml.js +406 -0
  34. package/src/math/package-build.mjs +24 -11
  35. package/src/math/package-layout.mjs +3 -0
  36. package/src/math/package-readme.mjs +14 -4
  37. package/src/math/parser.js +14 -11
  38. package/src/math/responsive.js +4 -0
  39. package/src/math/semantic-api.js +12 -0
  40. package/src/math/source.js +67 -0
  41. package/src/math/symbols.js +3 -0
  42. package/src/math/variants.js +41 -0
  43. package/src/tools/build-math.mjs +19 -10
  44. package/tests/adoption.mjs +34 -0
  45. package/tests/check.mjs +13 -5
  46. package/tests/semantics.mjs +340 -0
  47. package/tests/verify.mjs +3 -3
@@ -2,6 +2,8 @@
2
2
  import type {RenderOptions, RenderResult, MathError} from './index.js';
3
3
  import type {MathSession} from './stream.js';
4
4
  export interface IntegrationOptions {
5
+ /** Insert SVG with assistive MathML. Include accessible.css. */
6
+ accessible?: boolean;
5
7
  /** With an explicit session, plain-data options and screen are captured once per document render. */
6
8
  renderOptions?: RenderOptions;
7
9
  /** Per document. Default 1024. Exhaustion leaves subsequent math as source. */
@@ -12,5 +14,5 @@ export interface IntegrationOptions {
12
14
  export type FormulaResult = (RenderResult & {markup: string}) | {errors: MathError[]};
13
15
  /** Internal adapter owner; every call creates a fresh document admission budget. */
14
16
  export function createFormulaRenderer(options?: IntegrationOptions, session?: MathSession):
15
- (source: string, displayMode: boolean) => FormulaResult;
17
+ (source: string, displayMode: boolean, inLink?: boolean) => FormulaResult;
16
18
  export function escapeText(text: string): string;
@@ -1,5 +1,7 @@
1
1
  // SPDX-License-Identifier: MIT
2
2
  import {render} from './index.mjs';
3
+ import {renderAccessible} from './accessible.mjs';
4
+ import {mathDiagnostic} from '../src/math/source.js';
3
5
  import {assertMathSession, reuseFormula} from './reuse.mjs';
4
6
 
5
7
  export const escapeText = text => text.replace(/[&<>"'\r]/g, char =>
@@ -65,24 +67,10 @@ function failure(message) {
65
67
  return {errors: [{code: 'resource-limit', offset: 0, message}]};
66
68
  }
67
69
 
68
- function thrownError(error) {
69
- const message = String(error?.message ?? error), command = error?.command;
70
- const result = {code: typeof error?.code === 'string' ? error.code : 'invalid-tex',
71
- message: message.length > 1024 ? message.slice(0, 1024).replace(/[\ud800-\udbff]$/, '') + '…' : message,
72
- offset: Number.isFinite(error?.offset) ? error.offset : 0};
73
- if (message.length > 1024 || error?.messageTruncated) result.messageTruncated = true;
74
- if (command !== undefined) {
75
- result.command = String(command).slice(0, 128);
76
- if (String(command).length > 128 || error?.commandTruncated) result.commandTruncated = true;
77
- }
78
- if (error?.sourceOmitted) result.sourceOmitted = true;
79
- return result;
80
- }
81
-
82
70
  // One insertion boundary preserves the renderer payload and supplies the complete
83
71
  // markup consumed by both adapters. Admission is charged after materialization.
84
72
  function materialize(result) {
85
- return {...result, markup: result.svg};
73
+ return {...result, markup: result.html ?? result.svg};
86
74
  }
87
75
 
88
76
  // One budget per document render, never per env/session/parser instance.
@@ -91,22 +79,28 @@ export function createFormulaRenderer(options = {}, session) {
91
79
  const maxFormulas = options.maxFormulas ?? 1024, maxOutputLength = options.maxOutputLength ?? 16 * 1024 * 1024;
92
80
  for (const value of [maxFormulas, maxOutputLength])
93
81
  if (!Number.isSafeInteger(value) || value < 0) throw new RangeError('Math integration budgets must be finite nonnegative integers.');
82
+ const accessible = !!options.accessible;
94
83
  const captured = session === undefined ? null : configuration(options.renderOptions);
95
84
  let count = 0, length = 0;
96
- return (source, displayMode) => {
85
+ return (source, displayMode, inLink = false) => {
97
86
  assertMathSession(session);
98
87
  if (count >= maxFormulas || length >= maxOutputLength) return failure('The document math budget is exhausted.');
99
88
  count++;
100
89
  let result;
101
90
  try {
102
- result = reuseFormula(session, source, captured?.key == null ? null : Number(!!displayMode) + '|' + captured.key,
91
+ result = reuseFormula(session, source, captured?.key == null ? null : Number(accessible) + '|' + Number(!!displayMode) + '|' + captured.key,
103
92
  () => {
104
93
  if (captured && Object.hasOwn(captured, 'error')) throw captured.error;
105
- return render(source, {...(captured ? captured.options : options.renderOptions), displayMode});
94
+ const renderOptions = {...(captured ? captured.options : options.renderOptions), displayMode};
95
+ if (!accessible) return render(source, renderOptions);
96
+ const {semantics, ...artifact} = renderAccessible(source, renderOptions);
97
+ return artifact;
106
98
  });
107
- } catch (error) { return {errors: [thrownError(error)]}; }
99
+ } catch (error) { return {errors: [mathDiagnostic(error)]}; }
108
100
  if (result.errors.length) return {errors: result.errors.map(error => ({...error}))};
109
101
  const output = materialize(result);
102
+ if (inLink && /<a\s/.test(output.markup)) return {errors: [{code: 'nested-link', offset: 0,
103
+ message: 'A formula inside a link cannot contain another authored link.'}]};
110
104
  if (output.markup.length > maxOutputLength - length) {
111
105
  length = maxOutputLength;
112
106
  return failure('The document math output budget is exhausted.');
@@ -10,10 +10,13 @@ export default function rapierMath(md, options = {}) {
10
10
  const previous = document, mathSession = env?.rapierMathSession;
11
11
  assertMathSession(mathSession);
12
12
  try {
13
- const current = {render: createFormulaRenderer(options, mathSession), parents: new WeakMap(), activeTokens: tokens};
14
- const visit = tokens => {
13
+ const current = {render: createFormulaRenderer(options, mathSession), parents: new WeakMap(), linked: new WeakSet(), activeTokens: tokens};
14
+ const visit = (tokens, depth = 0) => {
15
15
  for (const token of tokens) {
16
- if (token.children) { current.parents.set(token.children, tokens); visit(token.children); }
16
+ if (token.type === 'link_close') depth--;
17
+ if (depth > 0) current.linked.add(token);
18
+ if (token.type === 'link_open') depth++;
19
+ if (token.children) { current.parents.set(token.children, tokens); visit(token.children, depth); }
17
20
  }
18
21
  };
19
22
  visit(tokens);
@@ -40,7 +43,7 @@ export default function rapierMath(md, options = {}) {
40
43
  };
41
44
  function formula(tokens, index, _options, env) {
42
45
  const token = tokens[index], display = token.type === 'math_block';
43
- const result = (document?.render || createFormulaRenderer(options, env?.rapierMathSession))(token.content, display);
46
+ const result = (document?.render || createFormulaRenderer(options, env?.rapierMathSession))(token.content, display, document?.linked.has(token));
44
47
  if (result.errors.length) {
45
48
  (env.rapierMathErrors ||= []).push({source: token.content, errors: result.errors});
46
49
  return '<code data-math-error="' + escapeText(result.errors[0].code) + '">' +
@@ -0,0 +1,5 @@
1
+ // SPDX-License-Identifier: MIT
2
+ import type {MathMLOptions, MathMLResult} from './index.js';
3
+ export {parse} from './index.js';
4
+ export type {MathNode, MathError, MathMLOptions, MathMLResult} from './index.js';
5
+ export function toMathML(source: unknown, options?: MathMLOptions): MathMLResult;
@@ -0,0 +1,3 @@
1
+ // SPDX-License-Identifier: MIT
2
+ export {parseMathTex as parse} from '../src/math/parser.js';
3
+ export {mathToMathML as toMathML} from '../src/math/semantic-api.js';