katex.js (2249B)
1 /* eslint no-console:0 */ 2 /** 3 * This is the main entry point for KaTeX. Here, we expose functions for 4 * rendering expressions either to DOM nodes or to markup strings. 5 * 6 * We also expose the ParseError class to check if errors thrown from KaTeX are 7 * errors in the expression, or errors in javascript handling. 8 */ 9 10 const ParseError = require("./src/ParseError"); 11 const Settings = require("./src/Settings"); 12 13 const buildTree = require("./src/buildTree"); 14 const parseTree = require("./src/parseTree"); 15 const utils = require("./src/utils"); 16 17 /** 18 * Parse and build an expression, and place that expression in the DOM node 19 * given. 20 */ 21 let render = function(expression, baseNode, options) { 22 utils.clearNode(baseNode); 23 24 const settings = new Settings(options); 25 26 const tree = parseTree(expression, settings); 27 const node = buildTree(tree, expression, settings).toNode(); 28 29 baseNode.appendChild(node); 30 }; 31 32 // KaTeX's styles don't work properly in quirks mode. Print out an error, and 33 // disable rendering. 34 if (typeof document !== "undefined") { 35 if (document.compatMode !== "CSS1Compat") { 36 typeof console !== "undefined" && console.warn( 37 "Warning: KaTeX doesn't work in quirks mode. Make sure your " + 38 "website has a suitable doctype."); 39 40 render = function() { 41 throw new ParseError("KaTeX doesn't work in quirks mode."); 42 }; 43 } 44 } 45 46 /** 47 * Parse and build an expression, and return the markup for that. 48 */ 49 const renderToString = function(expression, options) { 50 const settings = new Settings(options); 51 52 const tree = parseTree(expression, settings); 53 return buildTree(tree, expression, settings).toMarkup(); 54 }; 55 56 /** 57 * Parse an expression and return the parse tree. 58 */ 59 const generateParseTree = function(expression, options) { 60 const settings = new Settings(options); 61 return parseTree(expression, settings); 62 }; 63 64 module.exports = { 65 render: render, 66 renderToString: renderToString, 67 /** 68 * NOTE: This method is not currently recommended for public use. 69 * The internal tree representation is unstable and is very likely 70 * to change. Use at your own risk. 71 */ 72 __parse: generateParseTree, 73 ParseError: ParseError, 74 };