mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-05 19:28:39 +00:00
Add optional pre-process callback to the auto-renderer. (#1784)
* Add optional pre-process callback to the auto-renderer. * Rewrite `preProcess` callback signature. * Add test. * Make test future-proof. * Add root folder to the node module paths.
This commit is contained in:
committed by
Kevin Barabash
parent
ce3840d4ce
commit
f10de9617e
@@ -26,11 +26,14 @@ const renderMathInText = function(text, optionsCopy) {
|
||||
fragment.appendChild(document.createTextNode(data[i].data));
|
||||
} else {
|
||||
const span = document.createElement("span");
|
||||
const math = data[i].data;
|
||||
let math = data[i].data;
|
||||
// Override any display mode defined in the settings with that
|
||||
// defined by the text itself
|
||||
optionsCopy.displayMode = data[i].display;
|
||||
try {
|
||||
if (optionsCopy.preProcess) {
|
||||
math = optionsCopy.preProcess(math);
|
||||
}
|
||||
katex.render(math, span, optionsCopy);
|
||||
} catch (e) {
|
||||
if (!(e instanceof katex.ParseError)) {
|
||||
|
@@ -4,6 +4,7 @@
|
||||
/* global describe: false */
|
||||
|
||||
import splitAtDelimiters from "../splitAtDelimiters";
|
||||
import renderMathInElement from "../auto-render";
|
||||
|
||||
beforeEach(function() {
|
||||
expect.extend({
|
||||
@@ -234,3 +235,19 @@ describe("A delimiter splitter", function() {
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Pre-process callback", function() {
|
||||
it("replace `-squared` with `^2 `", function() {
|
||||
const el1 = document.createElement('div');
|
||||
el1.textContent = 'Circle equation: $x-squared + y-squared = r-squared$.';
|
||||
const el2 = document.createElement('div');
|
||||
el2.textContent = 'Circle equation: $x^2 + y^2 = r^2$.';
|
||||
const delimiters = [{left: "$", right: "$", display: false}];
|
||||
renderMathInElement(el1, {
|
||||
delimiters,
|
||||
preProcess: math => math.replace(/-squared/g, '^2'),
|
||||
});
|
||||
renderMathInElement(el2, {delimiters});
|
||||
expect(el1.innerHTML).toEqual(el2.innerHTML);
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user