Files
KaTeX/static/main.js
Erik Demaine 7ab4f76e16 Implement strict mode (replacing unicodeTextInMathMode) (#1278)
* Implement strict mode (replacing unicodeTextInMathMode)

Add new "strict" setting (default value false) that can take a boolean
(whether to throw an error or silently ignore), string ("ignore",
"warn", or "error"), or a function possibly returning such a value.
This enables a variety of ways of handling or ignoring transgressions
from "true" LaTeX behavior, making KaTeX easier to use while still
providing the ability for strict LaTeX adherance.

Resolve #1226, implementing that spec, for two existing
transgressions from regular LaTeX:

* src/functions/kern.js had some errors and warnings about use of
  (units in) math vs. text mode commands.
* The former setting unicodeTextInMathMode (not in any released version)
  needed to be set to true to enable Unicode text symbols in math mode.

Now these are controlled by the strict setting.  By default, KaTeX is now
very permissive, but if desired, the user can request warnings or errors.

* Rewrite strict description

* Add tests for strict functions

* Stricter type for strict

* Switch default strict setting to "warn"

* Fix new flow error

* Fix another flow bug
2018-05-13 14:27:30 -04:00

100 lines
3.0 KiB
JavaScript

/* eslint no-console:0 */
/**
* This is the webpack entry point for the test page.
*/
import katex from '../katex.webpack.js';
import './main.css';
import queryString from 'query-string';
function init() {
const input = document.getElementById("input");
let math = document.getElementById("math");
const permalink = document.getElementById("permalink");
input.addEventListener("input", reprocess, false);
permalink.addEventListener("click", setSearch);
const options = {displayMode: true, throwOnError: false, macros: {}};
const query = queryString.parse(window.location.search);
if (query.text) {
input.value = query.text;
}
// Use `display=0` or `displayMode=0` (or `=f`/`=false`/`=n`/`=no`)
// to turn off displayMode (which is on by default).
const displayQuery = (query.displayMode || query.display);
if (displayQuery && displayQuery.match(/^(0|f|n)/)) {
options.displayMode = false;
}
// Use `strict=warn` for warning strict mode or `strict=error`
// (or `=1`/`=t`/`=true`/`=y`/`=yes`)
// to turn off displayMode (which is on by default).
if (query.strict) {
if (query.strict.match(/^(1|t|y|e)/)) {
options.strict = "error";
} if (query.strict && query.strict.match(/^(w)/)) {
options.strict = "warn";
}
}
// The `before` or `pre` search parameter puts normal text before the math.
// The `after` or `post` search parameter puts normal text after the math.
// Example use: testing baseline alignment.
if (query.before || query.after || query.pre || query.post) {
const mathContainer = math;
mathContainer.id = "math-container";
if (query.before || query.pre) {
const before = document.createTextNode(query.before || query.pre);
mathContainer.appendChild(before);
}
math = document.createElement("span");
math.id = "math";
mathContainer.appendChild(math);
if (query.after || query.post) {
const after = document.createTextNode(query.after || query.post);
mathContainer.appendChild(after);
}
}
// Macros can be specified via `\command=expansion` or single-character
// `c=expansion`.
Object.getOwnPropertyNames(query).forEach((key) => {
if (key.match(/^\\|^[^]$/)) {
options.macros[key] = query[key];
}
});
reprocess();
function setSearch() {
const query = queryString.parse(window.location.search);
query.text = input.value;
window.location.search = queryString.stringify(query);
}
function reprocess() {
try {
katex.render(input.value, math, options);
} catch (e) {
if (e.__proto__ === katex.ParseError.prototype) {
console.error(e);
} else {
throw e;
}
}
}
if (module.hot) {
module.hot.accept('../katex.webpack.js', reprocess);
}
}
init();
export default katex;