Skip to content

Commit 7829440

Browse files
committed
Add configuration options for pre- and post-filters
1 parent 4d1e195 commit 7829440

7 files changed

Lines changed: 76 additions & 19 deletions

File tree

testsuite/tests/util/FunctionList.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, test, expect } from '@jest/globals';
2-
import {FunctionList} from '#js/util/FunctionList.js';
2+
import {FunctionList, AnyFunction} from '#js/util/FunctionList.js';
33

44
//
55
// Set up a function list with 6 functions that captures output
@@ -43,6 +43,15 @@ describe('FunctionList functionality', () => {
4343
expect(item).toBe(fn);
4444
});
4545

46+
test('Adding a list of items', () => {
47+
const fns = [(_: any) => {}, [(_: any) => {}, 1]] as [AnyFunction, [AnyFunction, number]];
48+
const list = new FunctionList(fns);
49+
expect(Array.from(list)).toEqual([
50+
{item: fns[1][0], priority: 1},
51+
{item: fns[0], priority: 5},
52+
]);
53+
});
54+
4655
test('Removing one item', () => {
4756
const list = new FunctionList();
4857
const fn = list.add(FN(0));

ts/core/InputJax.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ export abstract class AbstractInputJax<N, T, D> implements InputJax<N, T, D> {
129129
/**
130130
* The default options for the input jax
131131
*/
132-
public static OPTIONS: OptionList = {};
132+
public static OPTIONS: OptionList = {
133+
preFilters: [],
134+
postFilters: [],
135+
};
133136

134137
/**
135138
* The actual options supplied to the input jax
@@ -163,8 +166,8 @@ export abstract class AbstractInputJax<N, T, D> implements InputJax<N, T, D> {
163166
constructor(options: OptionList = {}) {
164167
const CLASS = this.constructor as typeof AbstractInputJax;
165168
this.options = userOptions(defaultOptions({}, CLASS.OPTIONS), options);
166-
this.preFilters = new FunctionList();
167-
this.postFilters = new FunctionList();
169+
this.preFilters = new FunctionList(this.options.preFilters);
170+
this.postFilters = new FunctionList(this.options.postFilters);
168171
}
169172

170173
/**

ts/core/OutputJax.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ export interface OutputJax<N, T, D> {
4747
options: OptionList;
4848

4949
/**
50-
* Lists of post-filters to call after typesetting the math
50+
* Lists of pre-filters to call after typesetting the math
51+
*/
52+
preFilters: FunctionList;
53+
54+
/**
55+
* Lists of post-filters to call before typesetting the math
5156
*/
5257
postFilters: FunctionList;
5358

@@ -130,13 +135,21 @@ export abstract class AbstractOutputJax<N, T, D> implements OutputJax<N, T, D> {
130135
/**
131136
* The default options for the output jax
132137
*/
133-
public static OPTIONS: OptionList = {};
138+
public static OPTIONS: OptionList = {
139+
preFilters: [],
140+
postFilters: [],
141+
};
134142

135143
/**
136144
* The actual options supplied to the output jax
137145
*/
138146
public options: OptionList;
139147

148+
/**
149+
* Filters to run before the output is processed
150+
*/
151+
public preFilters: FunctionList;
152+
140153
/**
141154
* Filters to run after the output is processed
142155
*/
@@ -153,7 +166,8 @@ export abstract class AbstractOutputJax<N, T, D> implements OutputJax<N, T, D> {
153166
constructor(options: OptionList = {}) {
154167
const CLASS = this.constructor as typeof AbstractOutputJax;
155168
this.options = userOptions(defaultOptions({}, CLASS.OPTIONS), options);
156-
this.postFilters = new FunctionList();
169+
this.preFilters = new FunctionList(this.options.preFilters);
170+
this.postFilters = new FunctionList(this.options.postFilters);
157171
}
158172

159173
/**

ts/input/mathml.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export class MathML<N, T, D> extends AbstractInputJax<N, T, D> {
5757
public static OPTIONS: OptionList = defaultOptions({
5858
parseAs: 'html', // Whether to use HTML or XML parsing for the MathML string
5959
forceReparse: false, // Whether to force the string to be reparsed, or use the one from the document DOM
60+
mmlFilters: [], // Filters to add to the mmlFilters lost
6061
FindMathML: null, // The FindMathML instance to override the default one
6162
MathMLCompile: null, // The MathMLCompile instance to override the default one
6263
/*
@@ -92,11 +93,10 @@ export class MathML<N, T, D> extends AbstractInputJax<N, T, D> {
9293
MathMLCompile.OPTIONS
9394
);
9495
super(mml);
95-
this.findMathML =
96-
this.options['FindMathML'] || new FindMathML<N, T, D>(find);
96+
this.findMathML = this.options.FindMathML || new FindMathML<N, T, D>(find);
9797
this.mathml =
98-
this.options['MathMLCompile'] || new MathMLCompile<N, T, D>(compile);
99-
this.mmlFilters = new FunctionList();
98+
this.options.MathMLCompile || new MathMLCompile<N, T, D>(compile);
99+
this.mmlFilters = new FunctionList(this.options.mmlFilters);
100100
}
101101

102102
/**

ts/input/tex.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,15 @@ export class TeX<N, T, D> extends AbstractInputJax<N, T, D> {
157157
userOptions(parseOptions.options, rest);
158158
configuration.config(this);
159159
TeX.tags(parseOptions, configuration);
160-
this.postFilters.add(FilterUtil.cleanSubSup, -7);
161-
this.postFilters.add(FilterUtil.setInherited, -6);
162-
this.postFilters.add(FilterUtil.checkScriptlevel, -5);
163-
this.postFilters.add(FilterUtil.moveLimits, -4);
164-
this.postFilters.add(FilterUtil.cleanStretchy, -3);
165-
this.postFilters.add(FilterUtil.cleanAttributes, -2);
166-
this.postFilters.add(FilterUtil.combineRelations, -1);
160+
this.postFilters.addList([
161+
[FilterUtil.cleanSubSup, -7],
162+
[FilterUtil.setInherited, -6],
163+
[FilterUtil.checkScriptlevel, -5],
164+
[FilterUtil.moveLimits, -4],
165+
[FilterUtil.cleanStretchy, -3],
166+
[FilterUtil.cleanAttributes, -2],
167+
[FilterUtil.combineRelations, -1],
168+
]);
167169
}
168170

169171
/**

ts/output/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ export abstract class CommonOutputJax<
414414
this.math = math;
415415
this.container = node;
416416
this.pxPerEm = math.metrics.ex / this.font.params.x_height;
417+
this.executeFilters(this.preFilters, math, html, node);
417418
this.nodeMap = new Map<MmlNode, WW>();
418419
math.root.attributes.getAllInherited().overflow =
419420
this.options.displayOverflow;

ts/util/FunctionList.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
import { PrioritizedList, PrioritizedListItem } from './PrioritizedList.js';
2525

26-
type AnyFunction = (...args: unknown[]) => unknown;
26+
export type AnyFunction = (...args: unknown[]) => unknown;
27+
export type AnyFunctionDef = AnyFunction | [AnyFunction, number];
28+
export type AnyFunctionList = AnyFunctionDef[];
2729

2830
/*****************************************************************/
2931
/**
@@ -38,6 +40,32 @@ export interface FunctionListItem extends PrioritizedListItem<AnyFunction> {}
3840
*/
3941

4042
export class FunctionList extends PrioritizedList<AnyFunction> {
43+
/**
44+
* @override
45+
* @param {AnyFunctionList} list The initial list of functions to add
46+
*/
47+
constructor(list: AnyFunctionList = null) {
48+
super();
49+
if (list) {
50+
this.addList(list);
51+
}
52+
}
53+
54+
/**
55+
* Add a list of filter functions, possibly with priorities.
56+
*
57+
* @param {AnyFunctionList} list The list of functions to add
58+
*/
59+
public addList(list: AnyFunctionList) {
60+
for (const item of list) {
61+
if (Array.isArray(item)) {
62+
this.add(item[0], item[1]);
63+
} else {
64+
this.add(item);
65+
}
66+
}
67+
}
68+
4169
/**
4270
* Executes the functions in the list (in prioritized order),
4371
* passing the given data to the functions. If any return

0 commit comments

Comments
 (0)