rapier-math 1.1.0 → 1.1.1
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.
- package/README.md +23 -195
- package/{index.mjs → dist/index.mjs} +1 -1
- package/{rapier-math-1.1.0.js → dist/rapier-math.js} +1 -1
- package/{src/math/README.md → docs/api.md} +9 -9
- package/{math-components.json → docs/math-components.json} +6 -6
- package/{src/shell → docs}/math-resources.json +62 -36
- package/examples/browser.mjs +1 -1
- package/examples/worker.mjs +1 -1
- package/package.json +31 -55
- package/src/math/api.js +1 -1
- package/src/math/package-build.mjs +0 -13
- package/src/math/package-layout.mjs +17 -0
- package/src/math/package-readme.mjs +42 -5
- package/src/tools/build-math.mjs +36 -17
- package/{src/math/adoption-verify.mjs → tests/adoption.mjs} +1 -1
- package/{check.mjs → tests/check.mjs} +8 -8
- package/tests/verify.mjs +46 -0
- package/LICENSE-MIT +0 -21
- package/adoption-check.mjs +0 -149
- package/math-LICENSE.txt +0 -21
- package/math-font-LICENSE.txt +0 -457
- package/src/math/classic.d.ts +0 -4
- package/src/math/delimiters.d.ts +0 -8
- package/src/math/delimiters.mjs +0 -39
- package/src/math/examples/README.md +0 -25
- package/src/math/examples/browser.html +0 -10
- package/src/math/examples/browser.mjs +0 -24
- package/src/math/examples/formula.mjs +0 -12
- package/src/math/examples/markdown.mjs +0 -9
- package/src/math/examples/react.tsx +0 -19
- package/src/math/examples/stream.mjs +0 -19
- package/src/math/examples/unified.mjs +0 -26
- package/src/math/examples/worker.mjs +0 -7
- package/src/math/index.d.ts +0 -43
- package/src/math/integration.d.ts +0 -9
- package/src/math/integration.mjs +0 -28
- package/src/math/markdown-it.d.ts +0 -6
- package/src/math/markdown-it.mjs +0 -45
- package/src/math/markdown-rules.mjs +0 -65
- package/src/math/package-check.mjs +0 -118
- package/src/math/package-verify.mjs +0 -45
- package/src/math/rehype.d.ts +0 -6
- package/src/math/rehype.mjs +0 -37
- package/src/package.json +0 -9
- package/stream.d.ts +0 -14
- package/stream.mjs +0 -46
- package/verify.mjs +0 -45
- /package/{classic.d.ts → dist/classic.d.ts} +0 -0
- /package/{delimiters.d.ts → dist/delimiters.d.ts} +0 -0
- /package/{delimiters.mjs → dist/delimiters.mjs} +0 -0
- /package/{index.d.ts → dist/index.d.ts} +0 -0
- /package/{integration.d.ts → dist/integration.d.ts} +0 -0
- /package/{integration.mjs → dist/integration.mjs} +0 -0
- /package/{markdown-it.d.ts → dist/markdown-it.d.ts} +0 -0
- /package/{markdown-it.mjs → dist/markdown-it.mjs} +0 -0
- /package/{markdown-rules.mjs → dist/markdown-rules.mjs} +0 -0
- /package/{rehype.d.ts → dist/rehype.d.ts} +0 -0
- /package/{rehype.mjs → dist/rehype.mjs} +0 -0
- /package/{src/math → dist}/stream.d.ts +0 -0
- /package/{src/math → dist}/stream.mjs +0 -0
- /package/{src/math/FONT-SOURCE.md → docs/font-source.md} +0 -0
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
import {render} from '../index.mjs';
|
|
3
|
-
self.onmessage = ({data: {id, source}}) => {
|
|
4
|
-
try { self.postMessage({id, source, result: render(source)}); }
|
|
5
|
-
catch (error) { self.postMessage({id, source, error: {code: error.code, message: error.message}}); }
|
|
6
|
-
};
|
|
7
|
-
// Keep a request id per message; install a reply only if it is still current.
|
package/src/math/index.d.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
export interface MathError {
|
|
3
|
-
code: string;
|
|
4
|
-
message: string;
|
|
5
|
-
/** Zero-based UTF-16 offset in the formula. */
|
|
6
|
-
offset: number;
|
|
7
|
-
command?: string;
|
|
8
|
-
messageTruncated?: boolean;
|
|
9
|
-
commandTruncated?: boolean;
|
|
10
|
-
/** True only when refused oversized source is not embedded in diagnostic SVG. */
|
|
11
|
-
sourceOmitted?: boolean;
|
|
12
|
-
}
|
|
13
|
-
export interface RenderOptions {
|
|
14
|
-
displayMode?: boolean;
|
|
15
|
-
maxWidth?: number;
|
|
16
|
-
screen?: {fontSize: number; pixelRatio: number; dark: boolean};
|
|
17
|
-
}
|
|
18
|
-
export interface RenderResult {
|
|
19
|
-
svg: string;
|
|
20
|
-
width: number;
|
|
21
|
-
height: number;
|
|
22
|
-
depth: number;
|
|
23
|
-
lines: number;
|
|
24
|
-
fitted: boolean;
|
|
25
|
-
errors: MathError[];
|
|
26
|
-
}
|
|
27
|
-
/** Parser nodes are inspectable; the internal node vocabulary can grow. */
|
|
28
|
-
export interface MathNode {type: string; [field: string]: unknown}
|
|
29
|
-
/** Browser nodes (including Element, Document and DocumentFragment) satisfy this
|
|
30
|
-
* structural type without requiring the DOM type library in server projects. */
|
|
31
|
-
export interface MathContainer {readonly nodeType: number}
|
|
32
|
-
export const version: string;
|
|
33
|
-
/** Oversized source returns a bounded resource-limit diagnostic with sourceOmitted. */
|
|
34
|
-
export function render(source: unknown, options?: RenderOptions): RenderResult;
|
|
35
|
-
export function renderToString(source: unknown, options?: RenderOptions): string;
|
|
36
|
-
export function parse(source: string): MathNode;
|
|
37
|
-
export function fit(container: MathContainer): number;
|
|
38
|
-
export function observe(container: MathContainer): () => void;
|
|
39
|
-
declare const RapierMath: Readonly<{
|
|
40
|
-
version: typeof version; render: typeof render; renderToString: typeof renderToString;
|
|
41
|
-
parse: typeof parse; fit: typeof fit; observe: typeof observe;
|
|
42
|
-
}>;
|
|
43
|
-
export default RapierMath;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
import type {RenderOptions} from './index.js';
|
|
3
|
-
export interface IntegrationOptions {
|
|
4
|
-
renderOptions?: RenderOptions;
|
|
5
|
-
/** Per document. Default 1024. Exhaustion leaves subsequent math as source. */
|
|
6
|
-
maxFormulas?: number;
|
|
7
|
-
/** Combined successful SVG code units per document. Default 16 * 1024 * 1024. */
|
|
8
|
-
maxOutputLength?: number;
|
|
9
|
-
}
|
package/src/math/integration.mjs
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
import {render} from './index.mjs';
|
|
3
|
-
|
|
4
|
-
export const escapeText = text => text.replace(/[&<>"'\r]/g, char =>
|
|
5
|
-
({'&': '&', '<': '<', '>': '>', '"': '"', "'": ''', '\r': ' '}[char]));
|
|
6
|
-
|
|
7
|
-
// One budget per document render. Rejection leaves the host's text node intact.
|
|
8
|
-
export function createFormulaRenderer(options = {}) {
|
|
9
|
-
const maxFormulas = options.maxFormulas ?? 1024, maxOutputLength = options.maxOutputLength ?? 16 * 1024 * 1024;
|
|
10
|
-
for (const value of [maxFormulas, maxOutputLength])
|
|
11
|
-
if (!Number.isSafeInteger(value) || value < 0) throw new RangeError('Math integration budgets must be finite nonnegative integers.');
|
|
12
|
-
let count = 0, length = 0;
|
|
13
|
-
return (source, displayMode) => {
|
|
14
|
-
let result;
|
|
15
|
-
if (count >= maxFormulas || length >= maxOutputLength) return {errors: [{code: 'resource-limit', offset: 0,
|
|
16
|
-
message: 'The document math budget is exhausted.'}]};
|
|
17
|
-
count++;
|
|
18
|
-
try { result = render(source, {...options.renderOptions, displayMode}); }
|
|
19
|
-
catch (error) { return {errors: [{code: error.code || 'invalid-tex', message: String(error.message), offset: error.offset || 0}]}; }
|
|
20
|
-
if (result.errors.length) return {errors: result.errors};
|
|
21
|
-
if (result.svg.length > maxOutputLength - length) {
|
|
22
|
-
length = maxOutputLength;
|
|
23
|
-
return {errors: [{code: 'resource-limit', offset: 0, message: 'The document math output budget is exhausted.'}]};
|
|
24
|
-
}
|
|
25
|
-
length += result.svg.length;
|
|
26
|
-
return result;
|
|
27
|
-
};
|
|
28
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
import type MarkdownIt from 'markdown-it';
|
|
3
|
-
import type {DelimiterOptions} from './delimiters.js';
|
|
4
|
-
import type {IntegrationOptions} from './integration.js';
|
|
5
|
-
export interface MarkdownMathOptions extends DelimiterOptions, IntegrationOptions {}
|
|
6
|
-
export default function rapierMath(md: InstanceType<typeof MarkdownIt>, options?: MarkdownMathOptions): void;
|
package/src/math/markdown-it.mjs
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
import {installMathRules} from './markdown-rules.mjs';
|
|
3
|
-
import {createFormulaRenderer, escapeText} from './integration.mjs';
|
|
4
|
-
|
|
5
|
-
export default function rapierMath(md, options = {}) {
|
|
6
|
-
installMathRules(md, options);
|
|
7
|
-
let session;
|
|
8
|
-
function withSession(renderer, render, tokens, opts, env) {
|
|
9
|
-
const previous = session, current = {render: createFormulaRenderer(options), children: new WeakSet()};
|
|
10
|
-
const visit = tokens => {
|
|
11
|
-
for (const token of tokens) {
|
|
12
|
-
if (token.children) { current.children.add(token.children); visit(token.children); }
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
visit(tokens);
|
|
16
|
-
session = current;
|
|
17
|
-
try { return render.call(renderer, tokens, opts, env); }
|
|
18
|
-
finally { session = previous; }
|
|
19
|
-
}
|
|
20
|
-
const render = md.renderer.render, renderInline = md.renderer.renderInline;
|
|
21
|
-
md.renderer.render = function(tokens, opts, env) {
|
|
22
|
-
return withSession(this, render, tokens, opts, env);
|
|
23
|
-
};
|
|
24
|
-
md.renderer.renderInline = function(tokens, opts, env) {
|
|
25
|
-
// The block renderer's inline children share its document budget. A separate
|
|
26
|
-
// inline tree, including a nested host renderInline call, owns a fresh one.
|
|
27
|
-
if (session?.children.has(tokens)) return renderInline.call(this, tokens, opts, env);
|
|
28
|
-
return withSession(this, renderInline, tokens, opts, env);
|
|
29
|
-
};
|
|
30
|
-
function formula(tokens, index, _options, env) {
|
|
31
|
-
const token = tokens[index], display = token.type === 'math_block';
|
|
32
|
-
const result = (session?.render || createFormulaRenderer(options))(token.content, display);
|
|
33
|
-
if (result.errors.length) {
|
|
34
|
-
(env.rapierMathErrors ||= []).push({source: token.content, errors: result.errors});
|
|
35
|
-
return '<code data-math-error="' + escapeText(result.errors[0].code) + '">' +
|
|
36
|
-
escapeText(token.meta?.mathSource ?? token.content) + '</code>';
|
|
37
|
-
}
|
|
38
|
-
return result.svg;
|
|
39
|
-
}
|
|
40
|
-
md.renderer.rules.math_inline = formula;
|
|
41
|
-
md.renderer.rules.math_block = (tokens, index, opts, env) => {
|
|
42
|
-
const result = formula(tokens, index, opts, env);
|
|
43
|
-
return tokens[index].block ? '<div class="math-display">' + result + '</div>\n' : result;
|
|
44
|
-
};
|
|
45
|
-
}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
// Original neutral adapter. The host owns Markdown structure and normalization.
|
|
3
|
-
import {createMathMatcher} from './delimiters.mjs';
|
|
4
|
-
|
|
5
|
-
export function installMathRules(md, {singleDollar = false, brackets = true, ruleName = 'rapier_math', inLink = false} = {}) {
|
|
6
|
-
// The host's image-alt renderer ignores unknown token types. Keep math as
|
|
7
|
-
// literal source in text-only output, while leaving escaping to that host.
|
|
8
|
-
const renderText = md.renderer.renderInlineAsText;
|
|
9
|
-
md.renderer.renderInlineAsText = function(tokens, options, env) {
|
|
10
|
-
return renderText.call(this, tokens.map(token =>
|
|
11
|
-
token.type === 'math_inline' || token.type === 'math_block'
|
|
12
|
-
? {...token, type: 'text', content: token.meta?.mathSource ?? token.content}
|
|
13
|
-
: token), options, env);
|
|
14
|
-
};
|
|
15
|
-
const matchers = new WeakMap();
|
|
16
|
-
function match(state, at, limit) {
|
|
17
|
-
let entry = matchers.get(state);
|
|
18
|
-
if (!entry || entry.source !== state.src) {
|
|
19
|
-
entry = {source: state.src, match: createMathMatcher(state.src, {singleDollar, brackets})};
|
|
20
|
-
matchers.set(state, entry);
|
|
21
|
-
}
|
|
22
|
-
return entry.match(at, limit);
|
|
23
|
-
}
|
|
24
|
-
md.inline.ruler.before('escape', ruleName, (state, silent) => {
|
|
25
|
-
if (!inLink && state.linkLevel > 0) return false;
|
|
26
|
-
if (state.src[state.pos] !== '$' && state.src[state.pos] !== '\\') return false;
|
|
27
|
-
const span = match(state, state.pos, state.posMax);
|
|
28
|
-
if (!span) return false;
|
|
29
|
-
if (!silent) {
|
|
30
|
-
const token = state.push(span.displayMode ? 'math_block' : 'math_inline', '', 0);
|
|
31
|
-
token.markup = span.open;
|
|
32
|
-
token.content = state.src.slice(span.bodyStart, span.bodyEnd);
|
|
33
|
-
token.meta = {mathSource: state.src.slice(span.start, span.end)};
|
|
34
|
-
}
|
|
35
|
-
state.pos = span.end;
|
|
36
|
-
return true;
|
|
37
|
-
});
|
|
38
|
-
md.block.ruler.before('fence', ruleName, (state, startLine, endLine, silent) => {
|
|
39
|
-
// An indented code block belongs to the host even when it begins with dollars.
|
|
40
|
-
if (state.sCount[startLine] - state.blkIndent >= 4) return false;
|
|
41
|
-
const start = state.bMarks[startLine] + state.tShift[startLine];
|
|
42
|
-
if (state.src.slice(start, start + 2) !== '$$' && (!brackets || state.src.slice(start, start + 2) !== '\\[')) return false;
|
|
43
|
-
const span = match(state, start, state.eMarks[endLine - 1]);
|
|
44
|
-
if (!span?.displayMode) return false;
|
|
45
|
-
let last = startLine;
|
|
46
|
-
while (last < endLine && state.eMarks[last] < span.end) last++;
|
|
47
|
-
if (last >= endLine || span.bodyEnd < state.bMarks[last] + state.tShift[last] ||
|
|
48
|
-
state.src.slice(span.end, state.eMarks[last]).trim()) return false;
|
|
49
|
-
if (silent) return true;
|
|
50
|
-
const parts = [];
|
|
51
|
-
for (let line = startLine; line <= last; line++) {
|
|
52
|
-
const from = line === startLine ? span.bodyStart : state.bMarks[line] + state.tShift[line];
|
|
53
|
-
const to = line === last ? span.bodyEnd : state.eMarks[line];
|
|
54
|
-
parts.push(state.src.slice(from, to));
|
|
55
|
-
}
|
|
56
|
-
const token = state.push('math_block', 'math', 0);
|
|
57
|
-
token.block = true;
|
|
58
|
-
token.markup = span.open;
|
|
59
|
-
token.content = parts.join('\n');
|
|
60
|
-
token.meta = {mathSource: span.open + token.content + span.close};
|
|
61
|
-
token.map = [startLine, last + 1];
|
|
62
|
-
state.line = last + 1;
|
|
63
|
-
return true;
|
|
64
|
-
}, {alt: ['paragraph', 'reference', 'blockquote', 'list']});
|
|
65
|
-
}
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
// Package entry, exact authored text and hostile-content boundaries; no appearance assertions.
|
|
3
|
-
import assert from 'node:assert/strict';
|
|
4
|
-
import {readFile} from 'node:fs/promises';
|
|
5
|
-
import {Script, createContext} from 'node:vm';
|
|
6
|
-
import {mathModule} from './src/math/package-build.mjs';
|
|
7
|
-
|
|
8
|
-
const read = path => readFile(new URL(path, import.meta.url), 'utf8');
|
|
9
|
-
const pkg = JSON.parse(await read('./package.json'));
|
|
10
|
-
const resources = JSON.parse(await read('./src/shell/math-resources.json'));
|
|
11
|
-
const classicSource = await read('./' + resources.bundle);
|
|
12
|
-
assert.equal(pkg.name, 'rapier-math');
|
|
13
|
-
assert.equal(pkg.version, resources.version);
|
|
14
|
-
assert.equal(pkg.type, 'module');
|
|
15
|
-
assert.equal(pkg.license, 'SEE LICENSE IN LICENSE');
|
|
16
|
-
assert.equal(Object.keys(pkg.dependencies || {}).length, 0);
|
|
17
|
-
assert.equal(Object.keys(pkg.scripts || {}).length, 0);
|
|
18
|
-
assert.equal(await read('./index.mjs'), mathModule(classicSource), 'exact generated module');
|
|
19
|
-
const rendererLicense = await read('./LICENSE-MIT'), fontLicense = await read('./math-font-LICENSE.txt');
|
|
20
|
-
for (const name of ['./LICENSE', './index.mjs', './' + resources.bundle]) {
|
|
21
|
-
const text = await read(name);
|
|
22
|
-
assert(text.includes(rendererLicense.trim()), name + ': renderer license');
|
|
23
|
-
assert(text.includes(fontLicense.trim()), name + ': complete font license');
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// Initialize Node's lazy Web API globals before observing module purity. This
|
|
27
|
-
// allocates a local Response only; no network request or renderer evaluation.
|
|
28
|
-
new Response();
|
|
29
|
-
const globals = Object.getOwnPropertyDescriptors(globalThis);
|
|
30
|
-
const module = await import('./index.mjs');
|
|
31
|
-
const afterImport = Object.getOwnPropertyDescriptors(globalThis);
|
|
32
|
-
assert.deepEqual(Reflect.ownKeys(afterImport), Reflect.ownKeys(globals), 'module adds no globals');
|
|
33
|
-
for (const key of Reflect.ownKeys(globals)) for (const field of Object.keys(globals[key]))
|
|
34
|
-
assert(Object.is(afterImport[key][field], globals[key][field]), 'module preserves global ' + String(key));
|
|
35
|
-
assert.deepEqual(Object.keys(module).sort(), ['default', 'fit', 'observe', 'parse', 'render', 'renderToString', 'version']);
|
|
36
|
-
for (const name of Object.keys(module.default)) assert.equal(module[name], module.default[name], 'named/default export ' + name);
|
|
37
|
-
assert.equal(module.version, resources.version);
|
|
38
|
-
assert(Object.isFrozen(module.default));
|
|
39
|
-
|
|
40
|
-
const context = createContext({atob, URL});
|
|
41
|
-
new Script(classicSource, {filename: resources.bundle}).runInContext(context);
|
|
42
|
-
const classic = context.RapierMath;
|
|
43
|
-
const plain = value => JSON.parse(JSON.stringify(value));
|
|
44
|
-
const unescape = text => text.replace(/&(amp|lt|gt|quot|apos|#\d+|#x[\da-f]+);/gi, (_, entity) =>
|
|
45
|
-
entity[0] === '#' ? String.fromCodePoint(entity[1] === 'x' ? parseInt(entity.slice(2), 16) : Number(entity.slice(1))) :
|
|
46
|
-
({amp: '&', lt: '<', gt: '>', quot: '"', apos: "'"})[entity]);
|
|
47
|
-
function sourceAlternative(result, source) {
|
|
48
|
-
const label = /\baria-label="([^"]*)"/.exec(result.svg)?.[1];
|
|
49
|
-
const title = /<title>([^<]*)<\/title>/.exec(result.svg)?.[1];
|
|
50
|
-
const utf16 = /\bdata-tex-utf16="([0-9a-f]+)"/.exec(result.svg)?.[1];
|
|
51
|
-
if (utf16) assert.equal(utf16.match(/.{4}/g).map(part => String.fromCharCode(parseInt(part, 16))).join(''), source, 'exact UTF-16 alternative');
|
|
52
|
-
else { assert.equal(unescape(label), source, 'exact aria alternative'); assert.equal(unescape(title), source, 'exact title alternative'); }
|
|
53
|
-
}
|
|
54
|
-
function inert(svg) {
|
|
55
|
-
assert(!/<\s*(?:script|foreignObject|iframe|object|embed|image|style|animate|set)\b/i.test(svg), 'no executable or remote SVG element');
|
|
56
|
-
for (const tag of svg.match(/<[^>]+>/g) || []) {
|
|
57
|
-
const attributes = [...tag.matchAll(/\s([^\s=]+)="([^"]*)"/g)];
|
|
58
|
-
for (const [, name, value] of attributes) {
|
|
59
|
-
assert(!/^on/i.test(name), 'no event attribute');
|
|
60
|
-
if (/^(?:href|xlink:href)$/i.test(name)) assert(/^https?:\/\//i.test(unescape(value)), 'only HTTP(S) links');
|
|
61
|
-
if (!/^(?:aria-label|data-tex-utf16)$/.test(name)) assert(!/url\s*\(|javascript:|vbscript:/i.test(unescape(value)), 'no executable attribute');
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const examples = [
|
|
67
|
-
[String.raw`\frac{-b\pm\sqrt{b^2-4ac}}{2a}`, {displayMode: true}],
|
|
68
|
-
[String.raw`\begin{aligned}a&=b+c\\d&=e+f\end{aligned}`, {displayMode: true, maxWidth: 4}],
|
|
69
|
-
[String.raw`\newcommand{\squareof}[1]{#1^2}\squareof{x}`, {screen: {fontSize: 16, pixelRatio: 2, dark: false}}],
|
|
70
|
-
[String.raw`\text{A < B & "C" 'D'} + α`, {}],
|
|
71
|
-
[String.raw`\href{https://example.com/?a=1&b=2}{x}`, {}],
|
|
72
|
-
[String.raw`\unsupported{content}`, {}],
|
|
73
|
-
['x\u0000y\ud800z\udfff', {}],
|
|
74
|
-
];
|
|
75
|
-
for (const [source, options] of examples) {
|
|
76
|
-
const result = module.render(source, options);
|
|
77
|
-
assert.deepEqual(plain(result), plain(classic.render(source, options)), 'module/classic result');
|
|
78
|
-
assert.equal(module.renderToString(source, options), result.svg, 'string export agrees with render');
|
|
79
|
-
sourceAlternative(result, source); inert(result.svg);
|
|
80
|
-
}
|
|
81
|
-
assert.deepEqual(plain(module.parse(examples[0][0])), plain(classic.parse(examples[0][0])), 'module/classic parse');
|
|
82
|
-
assert.equal(module.render(examples.at(-1)[0]).errors[0].code, 'invalid-source-character');
|
|
83
|
-
assert.equal(module.render(examples.at(-2)[0]).errors[0].code, 'unsupported-command');
|
|
84
|
-
|
|
85
|
-
const hostile = [
|
|
86
|
-
String.raw`\text{</title><script>alert(1)</script>}`,
|
|
87
|
-
String.raw`\href{javascript:alert(1)}{x}`,
|
|
88
|
-
String.raw`\href{data:text/html,test}{x}`,
|
|
89
|
-
String.raw`\href{https://example.com/" onload="alert(1)}{x}`,
|
|
90
|
-
String.raw`\color{url(https://example.com/payload)}x`,
|
|
91
|
-
String.raw`\def\again{\again}\again`,
|
|
92
|
-
];
|
|
93
|
-
for (const source of hostile) {
|
|
94
|
-
const result = module.render(source);
|
|
95
|
-
sourceAlternative(result, source); inert(result.svg);
|
|
96
|
-
assert.deepEqual(plain(result), plain(classic.render(source)), 'module/classic hostile result');
|
|
97
|
-
}
|
|
98
|
-
for (const source of hostile.slice(1)) assert(module.render(source).errors.length, 'unsafe or exhausted input produces a diagnostic');
|
|
99
|
-
for (const source of ['x'.repeat(1_000_001), '\ud800'.repeat(1_000_001), '&'.repeat(8_000_000)]) {
|
|
100
|
-
const result = module.render(source);
|
|
101
|
-
assert.equal(result.errors[0].code, 'resource-limit');
|
|
102
|
-
assert.equal(result.errors[0].sourceOmitted, true);
|
|
103
|
-
assert(result.svg.includes('data-math-source-omitted="true"'));
|
|
104
|
-
assert(!result.svg.includes('data-tex-utf16'), 'refused source never creates a carrier');
|
|
105
|
-
assert(result.svg.length < 100_000, 'refusal diagnostic is fixed-size');
|
|
106
|
-
assert.equal(module.renderToString(source), result.svg);
|
|
107
|
-
assert.deepEqual(plain(result), plain(classic.render(source)), 'module/classic refusal result');
|
|
108
|
-
inert(result.svg);
|
|
109
|
-
}
|
|
110
|
-
const longCommand = '\\' + 'a'.repeat(10_000), diagnostic = module.render(longCommand);
|
|
111
|
-
assert(diagnostic.errors[0].messageTruncated && diagnostic.errors[0].commandTruncated);
|
|
112
|
-
sourceAlternative(diagnostic, longCommand);
|
|
113
|
-
assert(diagnostic.errors[0].message.length <= 1025 && diagnostic.errors[0].command.length <= 128);
|
|
114
|
-
const unicodeFailure = module.render('\\begin{' + '😀'.repeat(600) + '}x');
|
|
115
|
-
assert(unicodeFailure.errors[0].message.isWellFormed(), 'diagnostic admission cannot split a source surrogate pair');
|
|
116
|
-
assert.equal(module.render('x+1').errors.length, 0, 'refused input cannot poison later rendering');
|
|
117
|
-
console.log(JSON.stringify({name: pkg.name, version: module.version, module: 'native ESM; no global changes',
|
|
118
|
-
rebuild: 'exact module', classicAgreement: examples.length + hostile.length, sourceAlternatives: 'exact', hostileContent: 'inert', licenses: 'complete'}));
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
// Verify a standalone package and rebuild its payload from the carried source.
|
|
3
|
-
import assert from 'node:assert/strict';
|
|
4
|
-
import {readFile} from 'node:fs/promises';
|
|
5
|
-
import {createHash} from 'node:crypto';
|
|
6
|
-
import {dirname, resolve} from 'node:path';
|
|
7
|
-
import {fileURLToPath} from 'node:url';
|
|
8
|
-
|
|
9
|
-
const root = dirname(fileURLToPath(import.meta.url));
|
|
10
|
-
const source = resolve(root, 'src');
|
|
11
|
-
const read = path => readFile(resolve(root, path));
|
|
12
|
-
const hash = (bytes, algorithm = 'sha256', encoding = 'hex') => createHash(algorithm).update(bytes).digest(encoding);
|
|
13
|
-
const resources = JSON.parse(await read('src/shell/math-resources.json'));
|
|
14
|
-
const payload = await read(resources.bundle);
|
|
15
|
-
assert.equal(payload.length, resources.bytes, 'payload length');
|
|
16
|
-
assert.equal(hash(payload), resources.sha256, 'payload SHA-256');
|
|
17
|
-
assert.equal(hash(payload, 'sha384', 'base64'), resources.sha384, 'payload SHA-384');
|
|
18
|
-
for (const input of [...resources.sources, ...resources.recipe, ...resources.adoption]) {
|
|
19
|
-
const bytes = await read('src/' + input.file);
|
|
20
|
-
assert.equal(bytes.length, input.bytes, input.file + ': length');
|
|
21
|
-
assert.equal(hash(bytes), input.sha256, input.file + ': SHA-256');
|
|
22
|
-
if (input.packageFile) assert.deepEqual(await read(input.packageFile), bytes, input.packageFile + ': exact adoption source');
|
|
23
|
-
}
|
|
24
|
-
const rendererLicense = await read('src/math/LICENSE-MIT');
|
|
25
|
-
for (const file of ['LICENSE-MIT', 'math-LICENSE.txt']) assert.deepEqual(await read(file), rendererLicense, file);
|
|
26
|
-
const fontLicense = await read('src/' + resources.font.licenseFile);
|
|
27
|
-
assert.equal(hash(fontLicense), resources.font.licenseSha256, 'font license SHA-256');
|
|
28
|
-
assert.deepEqual(await read('math-font-LICENSE.txt'), fontLicense, 'font license bytes');
|
|
29
|
-
assert(payload.includes(rendererLicense.toString('utf8').trim()), 'payload renderer license');
|
|
30
|
-
assert(payload.includes(fontLicense.toString('utf8').trim()), 'payload font license');
|
|
31
|
-
const sbom = JSON.parse(await read('math-components.json'));
|
|
32
|
-
const font = sbom.packages.find(item => item.name === resources.font.name);
|
|
33
|
-
assert.equal(font?.copyrightText, resources.font.copyright, 'font copyright');
|
|
34
|
-
assert.equal(font.licenseDeclared, resources.font.license, 'font license identity');
|
|
35
|
-
assert.equal(sbom.hasExtractedLicensingInfos.find(item => item.licenseId === resources.font.license)?.extractedText,
|
|
36
|
-
fontLicense.toString('utf8'), 'complete extracted font license');
|
|
37
|
-
const {buildMath} = await import('./src/tools/build-math.mjs');
|
|
38
|
-
const {mathReadme} = await import('./src/math/package-readme.mjs');
|
|
39
|
-
assert.equal((await read('README.md')).toString('utf8'), mathReadme(resources), 'generated README source');
|
|
40
|
-
for (const [file, owner] of [['check.mjs', 'package-check.mjs'], ['verify.mjs', 'package-verify.mjs'], ['adoption-check.mjs', 'adoption-verify.mjs']])
|
|
41
|
-
assert.deepEqual(await read(file), await read('src/math/' + owner), file + ': exact carried owner');
|
|
42
|
-
await buildMath({root: source, updatePins: false});
|
|
43
|
-
assert.deepEqual(await read('src/shell/vendor/' + resources.bundle), payload, 'rebuilt payload bytes');
|
|
44
|
-
console.log(JSON.stringify({file: resources.bundle, bytes: payload.length, sha256: resources.sha256,
|
|
45
|
-
sources: resources.sources.length + resources.recipe.length, adoptionFiles: resources.adoption.length, rebuild: 'exact', licenses: 'complete'}));
|
package/src/math/rehype.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
import type {Root} from 'hast';
|
|
3
|
-
import type {VFile} from 'vfile';
|
|
4
|
-
import type {IntegrationOptions} from './integration.js';
|
|
5
|
-
/** Install after sanitization. Recognizes remark-rehype math code nodes. */
|
|
6
|
-
export default function rehypeRapierMath(options?: IntegrationOptions): (tree: Root, file: VFile) => void;
|
package/src/math/rehype.mjs
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
import {fromHtml} from 'hast-util-from-html';
|
|
3
|
-
import {createFormulaRenderer} from './integration.mjs';
|
|
4
|
-
|
|
5
|
-
// Run AFTER the host sanitizer. Only known math code nodes are replaced, using
|
|
6
|
-
// fresh renderer output parsed into HAST. Authored raw nodes never become HTML here.
|
|
7
|
-
export default function rehypeRapierMath(options = {}) {
|
|
8
|
-
return (tree, file) => {
|
|
9
|
-
const render = createFormulaRenderer(options);
|
|
10
|
-
function math(node) {
|
|
11
|
-
const names = node?.properties?.className;
|
|
12
|
-
if (node?.type !== 'element' || node.tagName !== 'code' || !Array.isArray(names) ||
|
|
13
|
-
!names.includes('language-math') || !names.some(name => name === 'math-inline' || name === 'math-display') ||
|
|
14
|
-
!Array.isArray(node.children) || !node.children.every(child => child.type === 'text' && typeof child.value === 'string')) return null;
|
|
15
|
-
return {source: node.children.map(child => child.value).join(''), display: names.includes('math-display')};
|
|
16
|
-
}
|
|
17
|
-
function visit(parent) {
|
|
18
|
-
for (let index = 0; index < (parent.children?.length || 0); index++) {
|
|
19
|
-
const node = parent.children[index];
|
|
20
|
-
const code = node.type === 'element' && node.tagName === 'pre' && node.children?.length === 1 ? node.children[0] : node;
|
|
21
|
-
const input = math(code);
|
|
22
|
-
if (!input) { visit(node); continue; }
|
|
23
|
-
const result = render(input.source, input.display);
|
|
24
|
-
if (result.errors.length) {
|
|
25
|
-
for (const error of result.errors) {
|
|
26
|
-
const message = file?.message(error.message, {place: code.position, source: 'rapier-math', ruleId: error.code});
|
|
27
|
-
if (message) { message.mathSource = input.source; message.mathOffset = error.offset; }
|
|
28
|
-
}
|
|
29
|
-
continue;
|
|
30
|
-
}
|
|
31
|
-
const svg = fromHtml(result.svg, {fragment: true}).children[0];
|
|
32
|
-
parent.children[index] = input.display ? {type: 'element', tagName: 'div', properties: {className: ['math-display']}, children: [svg]} : svg;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
visit(tree);
|
|
36
|
-
};
|
|
37
|
-
}
|
package/src/package.json
DELETED
package/stream.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
export interface MarkdownSnapshot {source: string; html: string; revision: number; final: boolean}
|
|
3
|
-
export interface MarkdownStream {
|
|
4
|
-
/** null means keep the previous preview; the chunk was still accepted. */
|
|
5
|
-
write(chunk: string): MarkdownSnapshot | null;
|
|
6
|
-
finish(): MarkdownSnapshot;
|
|
7
|
-
abort(): void;
|
|
8
|
-
/** Replace a message by resetting, then writing the replacement source. */
|
|
9
|
-
reset(): number;
|
|
10
|
-
readonly revision: number;
|
|
11
|
-
}
|
|
12
|
-
export function createMarkdownStream(renderMarkdown: (source: string) => string, options?: {
|
|
13
|
-
maxSourceLength?: number; previewEvery?: number;
|
|
14
|
-
}): MarkdownStream;
|
package/stream.mjs
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
// Full Markdown previews have no append-only promise. Geometric admission bounds
|
|
3
|
-
// the total source passed to a synchronous host, including the final batch pass.
|
|
4
|
-
export function createMarkdownStream(renderMarkdown, {maxSourceLength = 1_000_000, previewEvery = 256} = {}) {
|
|
5
|
-
if (typeof renderMarkdown !== 'function') throw new TypeError('Supply a synchronous Markdown renderer.');
|
|
6
|
-
for (const value of [maxSourceLength, previewEvery])
|
|
7
|
-
if (!Number.isSafeInteger(value) || value < 1) throw new RangeError('Stream limits must be positive finite integers.');
|
|
8
|
-
let chunks = [], length = 0, threshold = previewEvery, closed = false, revision = 0, rendering = false;
|
|
9
|
-
function open() {
|
|
10
|
-
if (closed || rendering) throw new Error(closed ? 'This stream is closed; reset it before writing.' : 'A stream renderer cannot reenter its stream.');
|
|
11
|
-
}
|
|
12
|
-
function snapshot(final) {
|
|
13
|
-
const source = chunks.join('');
|
|
14
|
-
chunks = source ? [source] : [];
|
|
15
|
-
// Failed previews consumed work too. Further chunks must still wait for the
|
|
16
|
-
// next geometric threshold; only an explicit finish retries immediately.
|
|
17
|
-
threshold = Math.max(previewEvery, length * 2);
|
|
18
|
-
rendering = true;
|
|
19
|
-
let html;
|
|
20
|
-
try {
|
|
21
|
-
html = renderMarkdown(source);
|
|
22
|
-
if (typeof html !== 'string') throw new TypeError('The Markdown renderer must return a string synchronously.');
|
|
23
|
-
} finally { rendering = false; }
|
|
24
|
-
if (final) { closed = true; chunks = []; length = 0; }
|
|
25
|
-
return {source, html, revision, final};
|
|
26
|
-
}
|
|
27
|
-
return {
|
|
28
|
-
write(chunk) {
|
|
29
|
-
open();
|
|
30
|
-
if (typeof chunk !== 'string') throw new TypeError('Decode transport bytes to strings before writing.');
|
|
31
|
-
if (chunk.length > maxSourceLength - length)
|
|
32
|
-
throw Object.assign(new RangeError('The message exceeds its source budget; this chunk was not accepted.'), {code: 'resource-limit'});
|
|
33
|
-
if (!chunk.length) return null;
|
|
34
|
-
chunks.push(chunk); length += chunk.length; revision++;
|
|
35
|
-
return length >= threshold ? snapshot(false) : null;
|
|
36
|
-
},
|
|
37
|
-
finish() { open(); revision++; return snapshot(true); },
|
|
38
|
-
abort() {
|
|
39
|
-
if (rendering) throw new Error('A stream renderer cannot abort its stream.');
|
|
40
|
-
if (closed) return;
|
|
41
|
-
chunks = []; length = 0; closed = true; revision++;
|
|
42
|
-
},
|
|
43
|
-
reset() { if (rendering) throw new Error('A stream renderer cannot reset its stream.'); chunks = []; length = 0; threshold = previewEvery; closed = false; return ++revision; },
|
|
44
|
-
get revision() { return revision; },
|
|
45
|
-
};
|
|
46
|
-
}
|
package/verify.mjs
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
// Verify a standalone package and rebuild its payload from the carried source.
|
|
3
|
-
import assert from 'node:assert/strict';
|
|
4
|
-
import {readFile} from 'node:fs/promises';
|
|
5
|
-
import {createHash} from 'node:crypto';
|
|
6
|
-
import {dirname, resolve} from 'node:path';
|
|
7
|
-
import {fileURLToPath} from 'node:url';
|
|
8
|
-
|
|
9
|
-
const root = dirname(fileURLToPath(import.meta.url));
|
|
10
|
-
const source = resolve(root, 'src');
|
|
11
|
-
const read = path => readFile(resolve(root, path));
|
|
12
|
-
const hash = (bytes, algorithm = 'sha256', encoding = 'hex') => createHash(algorithm).update(bytes).digest(encoding);
|
|
13
|
-
const resources = JSON.parse(await read('src/shell/math-resources.json'));
|
|
14
|
-
const payload = await read(resources.bundle);
|
|
15
|
-
assert.equal(payload.length, resources.bytes, 'payload length');
|
|
16
|
-
assert.equal(hash(payload), resources.sha256, 'payload SHA-256');
|
|
17
|
-
assert.equal(hash(payload, 'sha384', 'base64'), resources.sha384, 'payload SHA-384');
|
|
18
|
-
for (const input of [...resources.sources, ...resources.recipe, ...resources.adoption]) {
|
|
19
|
-
const bytes = await read('src/' + input.file);
|
|
20
|
-
assert.equal(bytes.length, input.bytes, input.file + ': length');
|
|
21
|
-
assert.equal(hash(bytes), input.sha256, input.file + ': SHA-256');
|
|
22
|
-
if (input.packageFile) assert.deepEqual(await read(input.packageFile), bytes, input.packageFile + ': exact adoption source');
|
|
23
|
-
}
|
|
24
|
-
const rendererLicense = await read('src/math/LICENSE-MIT');
|
|
25
|
-
for (const file of ['LICENSE-MIT', 'math-LICENSE.txt']) assert.deepEqual(await read(file), rendererLicense, file);
|
|
26
|
-
const fontLicense = await read('src/' + resources.font.licenseFile);
|
|
27
|
-
assert.equal(hash(fontLicense), resources.font.licenseSha256, 'font license SHA-256');
|
|
28
|
-
assert.deepEqual(await read('math-font-LICENSE.txt'), fontLicense, 'font license bytes');
|
|
29
|
-
assert(payload.includes(rendererLicense.toString('utf8').trim()), 'payload renderer license');
|
|
30
|
-
assert(payload.includes(fontLicense.toString('utf8').trim()), 'payload font license');
|
|
31
|
-
const sbom = JSON.parse(await read('math-components.json'));
|
|
32
|
-
const font = sbom.packages.find(item => item.name === resources.font.name);
|
|
33
|
-
assert.equal(font?.copyrightText, resources.font.copyright, 'font copyright');
|
|
34
|
-
assert.equal(font.licenseDeclared, resources.font.license, 'font license identity');
|
|
35
|
-
assert.equal(sbom.hasExtractedLicensingInfos.find(item => item.licenseId === resources.font.license)?.extractedText,
|
|
36
|
-
fontLicense.toString('utf8'), 'complete extracted font license');
|
|
37
|
-
const {buildMath} = await import('./src/tools/build-math.mjs');
|
|
38
|
-
const {mathReadme} = await import('./src/math/package-readme.mjs');
|
|
39
|
-
assert.equal((await read('README.md')).toString('utf8'), mathReadme(resources), 'generated README source');
|
|
40
|
-
for (const [file, owner] of [['check.mjs', 'package-check.mjs'], ['verify.mjs', 'package-verify.mjs'], ['adoption-check.mjs', 'adoption-verify.mjs']])
|
|
41
|
-
assert.deepEqual(await read(file), await read('src/math/' + owner), file + ': exact carried owner');
|
|
42
|
-
await buildMath({root: source, updatePins: false});
|
|
43
|
-
assert.deepEqual(await read('src/shell/vendor/' + resources.bundle), payload, 'rebuilt payload bytes');
|
|
44
|
-
console.log(JSON.stringify({file: resources.bundle, bytes: payload.length, sha256: resources.sha256,
|
|
45
|
-
sources: resources.sources.length + resources.recipe.length, adoptionFiles: resources.adoption.length, rebuild: 'exact', licenses: 'complete'}));
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|