Add configurable error callback (#658)

Replace console.error with a configurable option. This mimcs the current
default behaviour to guaranteee compatibility with previous versions.
Update errorCallback to the README.md
This commit is contained in:
Jörn Lenoch
2017-07-30 17:27:29 +02:00
committed by Kevin Barabash
parent 2da06d541e
commit 2eb32a8775
2 changed files with 10 additions and 5 deletions

View File

@@ -83,8 +83,11 @@ in addition to two auto-render-specific keys:
- `ignoredTags`: This is a list of DOM node types to ignore when recursing
through. The default value is
`["script", "noscript", "style", "textarea", "pre", "code"]`.
`["script", "noscript", "style", "textarea", "pre", "code"]`.
- `errorCallback`: A callback method returning a message and an error stack
in case of an critical error during rendering. The default uses `console.error`.
Note that the `displayMode` property of the options object is ignored, and is
instead taken from the `display` key of the corresponding entry in the
`delimiters` key.
`delimiters` key.

View File

@@ -19,7 +19,6 @@ const splitWithDelimiters = function(text, delimiters) {
*/
const renderMathInText = function(text, optionsCopy) {
const data = splitWithDelimiters(text, optionsCopy.delimiters);
const fragment = document.createDocumentFragment();
for (let i = 0; i < data.length; i++) {
@@ -37,7 +36,7 @@ const renderMathInText = function(text, optionsCopy) {
if (!(e instanceof katex.ParseError)) {
throw e;
}
console.error(
optionsCopy.errorCallback(
"KaTeX auto-render: Failed to parse `" + data[i].data +
"` with ",
e
@@ -85,6 +84,10 @@ const defaultAutoRenderOptions = {
ignoredTags: [
"script", "noscript", "style", "textarea", "pre", "code",
],
errorCallback: function(msg, err) {
console.error(msg, err);
},
};
const renderMathInElement = function(elem, options) {
@@ -93,7 +96,6 @@ const renderMathInElement = function(elem, options) {
}
const optionsCopy = Object.assign({}, defaultAutoRenderOptions, options);
renderElem(elem, optionsCopy);
};