mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-05 11:18:39 +00:00
* Nested environments of macro definitions * Rename environment -> namespace * \def support * Clean up \df@tag at beginning * \global\def support * Fix \global via new setMacro helper * Fix caching behavior and build array on top of it Also avoid double lookup of macros * Add tests * Add argument tests * Remove global pointer * Note about macros object being modified * add __defineMacro * Add \global\def test * More \global tests * Constant-time lookup Rewrite to use an "undo" stack similar to TeX, so get and set are constant-time operations. get() still has to check two objects: one with all current settings, and then the built-ins. Local set() sets the current value and (when appropriate) adds an undo operation to the undo stack. Global set() still takes time linear in the number of groups, possibly changing the undo operation at every level. `Namespace` now refers to a space of things like macros or lengths. * Add \def to-dos * Put optional arguments in their own group * Rename `pushNamespace` -> `beginGroup` * Wrap each expression in a group namespace * Add comments
187 lines
5.5 KiB
JavaScript
187 lines
5.5 KiB
JavaScript
// @flow
|
|
/* eslint no-console:0 */
|
|
/**
|
|
* This is the main entry point for KaTeX. Here, we expose functions for
|
|
* rendering expressions either to DOM nodes or to markup strings.
|
|
*
|
|
* We also expose the ParseError class to check if errors thrown from KaTeX are
|
|
* errors in the expression, or errors in javascript handling.
|
|
*/
|
|
|
|
import ParseError from "./src/ParseError";
|
|
import Settings from "./src/Settings";
|
|
|
|
import { buildTree, buildHTMLTree } from "./src/buildTree";
|
|
import parseTree from "./src/parseTree";
|
|
import buildCommon from "./src/buildCommon";
|
|
import domTree from "./src/domTree";
|
|
import utils from "./src/utils";
|
|
|
|
import type {SettingsOptions} from "./src/Settings";
|
|
import type ParseNode from "./src/ParseNode";
|
|
|
|
import { defineSymbol } from './src/symbols';
|
|
import { defineMacro } from './src/macros';
|
|
|
|
import { version } from "./package.json";
|
|
|
|
/**
|
|
* Parse and build an expression, and place that expression in the DOM node
|
|
* given.
|
|
*/
|
|
let render = function(
|
|
expression: string,
|
|
baseNode: Node,
|
|
options: SettingsOptions,
|
|
) {
|
|
utils.clearNode(baseNode);
|
|
const node = renderToDomTree(expression, options).toNode();
|
|
baseNode.appendChild(node);
|
|
};
|
|
|
|
// KaTeX's styles don't work properly in quirks mode. Print out an error, and
|
|
// disable rendering.
|
|
if (typeof document !== "undefined") {
|
|
if (document.compatMode !== "CSS1Compat") {
|
|
typeof console !== "undefined" && console.warn(
|
|
"Warning: KaTeX doesn't work in quirks mode. Make sure your " +
|
|
"website has a suitable doctype.");
|
|
|
|
render = function() {
|
|
throw new ParseError("KaTeX doesn't work in quirks mode.");
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Parse and build an expression, and return the markup for that.
|
|
*/
|
|
const renderToString = function(
|
|
expression: string,
|
|
options: SettingsOptions,
|
|
): string {
|
|
const markup = renderToDomTree(expression, options).toMarkup();
|
|
return markup;
|
|
};
|
|
|
|
/**
|
|
* Parse an expression and return the parse tree.
|
|
*/
|
|
const generateParseTree = function(
|
|
expression: string,
|
|
options: SettingsOptions,
|
|
): ParseNode<*>[] {
|
|
const settings = new Settings(options);
|
|
return parseTree(expression, settings);
|
|
};
|
|
|
|
/**
|
|
* If the given error is a KaTeX ParseError and options.throwOnError is false,
|
|
* renders the invalid LaTeX as a span with hover title giving the KaTeX
|
|
* error message. Otherwise, simply throws the error.
|
|
*/
|
|
const renderError = function(
|
|
error,
|
|
expression: string,
|
|
options: Settings,
|
|
) {
|
|
if (options.throwOnError || !(error instanceof ParseError)) {
|
|
throw error;
|
|
}
|
|
const node = buildCommon.makeSpan(["katex-error"],
|
|
[new domTree.symbolNode(expression)]);
|
|
node.setAttribute("title", error.toString());
|
|
node.setAttribute("style", `color:${options.errorColor}`);
|
|
return node;
|
|
};
|
|
|
|
/**
|
|
* Generates and returns the katex build tree. This is used for advanced
|
|
* use cases (like rendering to custom output).
|
|
*/
|
|
const renderToDomTree = function(
|
|
expression: string,
|
|
options: SettingsOptions,
|
|
) {
|
|
const settings = new Settings(options);
|
|
try {
|
|
const tree = parseTree(expression, settings);
|
|
return buildTree(tree, expression, settings);
|
|
} catch (error) {
|
|
return renderError(error, expression, settings);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Generates and returns the katex build tree, with just HTML (no MathML).
|
|
* This is used for advanced use cases (like rendering to custom output).
|
|
*/
|
|
const renderToHTMLTree = function(
|
|
expression: string,
|
|
options: SettingsOptions,
|
|
) {
|
|
const settings = new Settings(options);
|
|
try {
|
|
const tree = parseTree(expression, settings);
|
|
return buildHTMLTree(tree, expression, settings);
|
|
} catch (error) {
|
|
return renderError(error, expression, settings);
|
|
}
|
|
};
|
|
|
|
export default {
|
|
/**
|
|
* Current KaTeX version
|
|
*/
|
|
version,
|
|
/**
|
|
* Renders the given LaTeX into an HTML+MathML combination, and adds
|
|
* it as a child to the specified DOM node.
|
|
*/
|
|
render,
|
|
/**
|
|
* Renders the given LaTeX into an HTML+MathML combination string,
|
|
* for sending to the client.
|
|
*/
|
|
renderToString,
|
|
/**
|
|
* KaTeX error, usually during parsing.
|
|
*/
|
|
ParseError,
|
|
/**
|
|
* Parses the given LaTeX into KaTeX's internal parse tree structure,
|
|
* without rendering to HTML or MathML.
|
|
*
|
|
* NOTE: This method is not currently recommended for public use.
|
|
* The internal tree representation is unstable and is very likely
|
|
* to change. Use at your own risk.
|
|
*/
|
|
__parse: generateParseTree,
|
|
/**
|
|
* Renders the given LaTeX into an HTML+MathML internal DOM tree
|
|
* representation, without flattening that representation to a string.
|
|
*
|
|
* NOTE: This method is not currently recommended for public use.
|
|
* The internal tree representation is unstable and is very likely
|
|
* to change. Use at your own risk.
|
|
*/
|
|
__renderToDomTree: renderToDomTree,
|
|
/**
|
|
* Renders the given LaTeX into an HTML internal DOM tree representation,
|
|
* without MathML and without flattening that representation to a string.
|
|
*
|
|
* NOTE: This method is not currently recommended for public use.
|
|
* The internal tree representation is unstable and is very likely
|
|
* to change. Use at your own risk.
|
|
*/
|
|
__renderToHTMLTree: renderToHTMLTree,
|
|
/**
|
|
* adds a new symbol to internal symbols table
|
|
*/
|
|
__defineSymbol: defineSymbol,
|
|
/**
|
|
* adds a new macro to builtin macro list
|
|
*/
|
|
__defineMacro: defineMacro,
|
|
};
|