Skip to content

Commit 3e2c672

Browse files
authored
Merge pull request #718 from mathjax/pathfilter-config
Add ability to configure loader path filters
2 parents eacfc38 + ad4be22 commit 3e2c672

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

ts/components/loader.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ declare var document: Document;
4242
* Function used to determine path to a given package.
4343
*/
4444
export type PathFilterFunction = (data: {name: string, original: string, addExtension: boolean}) => boolean;
45-
export type PathFilterList = {[name: string]: PathFilterFunction};
45+
export type PathFilterList = (PathFilterFunction | [PathFilterFunction, number])[];
4646

4747
/**
4848
* Update the configuration structure to include the loader configuration
@@ -57,6 +57,7 @@ export interface MathJaxConfig extends MJConfig {
5757
ready?: PackageReady; // A function to call when MathJax is ready
5858
failed?: PackageFailed; // A function to call when MathJax fails to load
5959
require?: (url: string) => any; // A function for loading URLs
60+
pathFilters?: PathFilterList; // List of path filters (and optional priorities) to add
6061
[name: string]: any; // Other configuration blocks
6162
};
6263
}
@@ -73,7 +74,7 @@ export interface MathJaxObject extends MJObject {
7374
preLoad: (...names: string[]) => void; // Indicate that packages are already loaded by hand
7475
defaultReady: () => void; // The function performed when all packages are loaded
7576
getRoot: () => string; // Find the root URL for the MathJax files
76-
pathFilters: PathFilterList; // the filters to use for looking for package paths
77+
pathFilters: FunctionList; // the filters to use for looking for package paths
7778
};
7879
startup?: any;
7980
}
@@ -117,6 +118,7 @@ export const PathFilters: {[name: string]: PathFilterFunction} = {
117118
}
118119
return true;
119120
}
121+
120122
};
121123

122124

@@ -216,9 +218,9 @@ export namespace Loader {
216218
/**
217219
* The default filters to use.
218220
*/
219-
pathFilters.add(PathFilters.source, 1);
220-
pathFilters.add(PathFilters.normalize, 2);
221-
pathFilters.add(PathFilters.prefix, 5);
221+
pathFilters.add(PathFilters.source, 0);
222+
pathFilters.add(PathFilters.normalize, 10);
223+
pathFilters.add(PathFilters.prefix, 20);
222224
}
223225

224226
/**
@@ -229,6 +231,7 @@ export const MathJax = MJGlobal as MathJaxObject;
229231
/*
230232
* If the loader hasn't been added to the MathJax variable,
231233
* Add the loader configuration, library, and data objects.
234+
* Add any path filters from the configuration.
232235
*/
233236
if (typeof MathJax.loader === 'undefined') {
234237

@@ -242,12 +245,23 @@ if (typeof MathJax.loader === 'undefined') {
242245
load: [],
243246
ready: Loader.defaultReady.bind(Loader),
244247
failed: (error: PackageError) => console.log(`MathJax(${error.package || '?'}): ${error.message}`),
245-
require: null
248+
require: null,
249+
pathFilters: [],
246250
});
247251
combineWithMathJax({
248252
loader: Loader
249253
});
250254

255+
//
256+
// Add any path filters from the configuration
257+
//
258+
for (const filter of MathJax.config.loader.pathFilters) {
259+
if (Array.isArray(filter)) {
260+
Loader.pathFilters.add(filter[0], filter[1]);
261+
} else {
262+
Loader.pathFilters.add(filter);
263+
}
264+
}
251265
}
252266

253267
/**

0 commit comments

Comments
 (0)