mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-05 03:08:40 +00:00
Port katex, buildTree, parseTree to @flow. (#989)
This commit is contained in:
committed by
Kevin Barabash
parent
dc0c3970c2
commit
1d8cf3fa08
26
katex.js
26
katex.js
@@ -1,3 +1,4 @@
|
||||
// @flow
|
||||
/* eslint no-console:0 */
|
||||
/**
|
||||
* This is the main entry point for KaTeX. Here, we expose functions for
|
||||
@@ -14,11 +15,18 @@ import buildTree from "./src/buildTree";
|
||||
import parseTree from "./src/parseTree";
|
||||
import utils from "./src/utils";
|
||||
|
||||
import type {SettingsOptions} from "./src/Settings";
|
||||
import type ParseNode from "./src/ParseNode";
|
||||
|
||||
/**
|
||||
* Parse and build an expression, and place that expression in the DOM node
|
||||
* given.
|
||||
*/
|
||||
let render = function(expression, baseNode, options) {
|
||||
let render = function(
|
||||
expression: string,
|
||||
baseNode: Node,
|
||||
options: SettingsOptions,
|
||||
) {
|
||||
utils.clearNode(baseNode);
|
||||
|
||||
const settings = new Settings(options);
|
||||
@@ -47,7 +55,10 @@ if (typeof document !== "undefined") {
|
||||
/**
|
||||
* Parse and build an expression, and return the markup for that.
|
||||
*/
|
||||
const renderToString = function(expression, options) {
|
||||
const renderToString = function(
|
||||
expression: string,
|
||||
options: SettingsOptions,
|
||||
): string {
|
||||
const settings = new Settings(options);
|
||||
|
||||
const tree = parseTree(expression, settings);
|
||||
@@ -57,20 +68,23 @@ const renderToString = function(expression, options) {
|
||||
/**
|
||||
* Parse an expression and return the parse tree.
|
||||
*/
|
||||
const generateParseTree = function(expression, options) {
|
||||
const generateParseTree = function(
|
||||
expression: string,
|
||||
options: SettingsOptions,
|
||||
): ParseNode[] {
|
||||
const settings = new Settings(options);
|
||||
return parseTree(expression, settings);
|
||||
};
|
||||
|
||||
|
||||
module.exports = {
|
||||
render: render,
|
||||
renderToString: renderToString,
|
||||
render,
|
||||
renderToString,
|
||||
/**
|
||||
* 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,
|
||||
ParseError: ParseError,
|
||||
ParseError,
|
||||
};
|
||||
|
@@ -6,7 +6,7 @@
|
||||
|
||||
import utils from "./utils";
|
||||
|
||||
type SettingsOptions = {
|
||||
export type SettingsOptions = {
|
||||
displayMode?: boolean;
|
||||
throwOnError?: boolean;
|
||||
errorColor?: string;
|
||||
|
@@ -1,3 +1,4 @@
|
||||
// @flow
|
||||
import buildHTML from "./buildHTML";
|
||||
import buildMathML from "./buildMathML";
|
||||
import buildCommon from "./buildCommon";
|
||||
@@ -5,7 +6,14 @@ import Options from "./Options";
|
||||
import Settings from "./Settings";
|
||||
import Style from "./Style";
|
||||
|
||||
const buildTree = function(tree, expression, settings) {
|
||||
import type ParseNode from "./ParseNode";
|
||||
import type domTree from "./domTree";
|
||||
|
||||
const buildTree = function(
|
||||
tree: ParseNode[],
|
||||
expression: string,
|
||||
settings: Settings,
|
||||
): domTree.span {
|
||||
settings = settings || new Settings({});
|
||||
|
||||
let startStyle = Style.TEXT;
|
||||
|
@@ -1,3 +1,4 @@
|
||||
// @flow
|
||||
/**
|
||||
* Provides a single function for parsing an expression using a Parser
|
||||
* TODO(emily): Remove this
|
||||
@@ -5,10 +6,13 @@
|
||||
|
||||
import Parser from "./Parser";
|
||||
|
||||
import type ParseNode from "./ParseNode";
|
||||
import type Settings from "./Settings";
|
||||
|
||||
/**
|
||||
* Parses an expression using a Parser, then returns the parsed result.
|
||||
*/
|
||||
const parseTree = function(toParse, settings) {
|
||||
const parseTree = function(toParse: string, settings: Settings): ParseNode[] {
|
||||
if (!(typeof toParse === 'string' || toParse instanceof String)) {
|
||||
throw new TypeError('KaTeX can only parse string typed expression');
|
||||
}
|
||||
|
Reference in New Issue
Block a user