From ef54ca7995197aac67c5c2b2709d2a4decdb96ef Mon Sep 17 00:00:00 2001 From: bLue Date: Fri, 8 Feb 2019 21:29:05 +0800 Subject: [PATCH] [katex] Add contrib/auto-render --- types/katex/contrib/auto-render.d.ts | 64 ++++++++++++++++++++++++++++ types/katex/contrib/index.d.ts | 1 + types/katex/index.d.ts | 39 +++++++++-------- types/katex/katex-tests.ts | 41 ++++++++++++------ 4 files changed, 115 insertions(+), 30 deletions(-) create mode 100644 types/katex/contrib/auto-render.d.ts create mode 100644 types/katex/contrib/index.d.ts diff --git a/types/katex/contrib/auto-render.d.ts b/types/katex/contrib/auto-render.d.ts new file mode 100644 index 0000000000..efef3ffb7d --- /dev/null +++ b/types/katex/contrib/auto-render.d.ts @@ -0,0 +1,64 @@ +declare module 'katex/dist/contrib/auto-render' { + import { KatexOptions } from 'katex'; + + export interface RenderMathInElementSpecificOptionsDelimiters { + /** + * A string which starts the math expression (i.e. the left delimiter) + */ + left: string; + /** + * A string which ends the math expression (i.e. the right delimiter) + */ + right: string; + /** + * A boolean of whether the math in the expression should be rendered in display mode or not + */ + display: boolean + } + + export interface RenderMathInElementSpecificOptions { + /** + * A list of delimiters to look for math + * + * @default [ + * {left: "$$", right: "$$", display: true}, + * {left: "\\(", right: "\\)", display: false}, + * {left: "\\[", right: "\\]", display: true} + * ] + */ + delimiters?: ReadonlyArray; + /** + * A list of DOM node types to ignore when recursing through + * + * @default ["script", "noscript", "style", "textarea", "pre", "code"] + */ + ignoredTags?: ReadonlyArray; + /** + * A list of DOM node class names to ignore when recursing through + * + * @default [] + */ + ignoredClasses?: string[]; + /** + * A callback method returning a message and an error stack in case of an critical error during rendering + * @param msg Message generated by KaTeX + * @param err Caught error + * + * @default console.error + */ + errorCallback?(msg: string, err: Error): void; + } + + /** + * renderMathInElement options contain KaTeX render options and renderMathInElement specific options + */ + export type RenderMathInElementOptions = KatexOptions & RenderMathInElementSpecificOptions; + + /** + * Auto-render TeX expressions in HTML element + * @param elem HTML element to auto-render + * @param options Render options + */ + function renderMathInElement(elem: HTMLElement, options?: RenderMathInElementOptions): void; + export default renderMathInElement; +} diff --git a/types/katex/contrib/index.d.ts b/types/katex/contrib/index.d.ts new file mode 100644 index 0000000000..a365569333 --- /dev/null +++ b/types/katex/contrib/index.d.ts @@ -0,0 +1 @@ +/// diff --git a/types/katex/index.d.ts b/types/katex/index.d.ts index ae126b3444..75633ad7f4 100644 --- a/types/katex/index.d.ts +++ b/types/katex/index.d.ts @@ -2,24 +2,27 @@ // Project: http://khan.github.io/KaTeX/ // Definitions by: Michael Randolph // Kevin Nguyen +// bLue // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +/// + export as namespace katex; /** Documentation: https://katex.org/docs/options.html */ export interface KatexOptions { /** - * If `true`, math will be rendered in display mode + * If `true`, math will be rendered in display mode * (math in display style and center math on page) - * + * * If `false`, math will be rendered in inline mode * @default false */ displayMode?: boolean; /** - * If `true`, KaTeX will throw a `ParseError` when + * If `true`, KaTeX will throw a `ParseError` when * it encounters an unsupported command or invalid LaTex - * + * * If `false`, KaTeX will render unsupported commands as * text, and render invalid LaTeX as its source code with * hover text giving the error, in color given by errorColor @@ -32,56 +35,56 @@ export interface KatexOptions { errorColor?: string; /** * A collection of custom macros. - * + * * See `src/macros.js` for its usage */ macros?: any; /** * If `true`, `\color` will work like LaTeX's `\textcolor` * and takes 2 arguments - * + * * If `false`, `\color` will work like LaTeX's `\color` * and takes 1 argument - * + * * In both cases, `\textcolor` works as in LaTeX - * + * * @default false */ colorIsTextColor?: boolean; /** * All user-specified sizes will be caped to `maxSize` ems - * + * * If set to Infinity, users can make elements and space * arbitrarily large - * + * * @default Infinity */ maxSize?: number; /** * Limit the number of macro expansions to specified number - * + * * If set to `Infinity`, marco expander will try to fully expand * as in LaTex - * + * * @default 1000 */ maxExpand?: number; /** * Allowed protocols in `\href` - * + * * Use `_relative` to allow relative urls - * + * * Use `*` to allow all protocols */ - allowedProtocols?: string[] + allowedProtocols?: string[]; /** * If `false` or `"ignore"`, allow features that make * writing in LaTex convenient but not supported by LaTex - * + * * If `true` or `"error"`, throw an error for such transgressions - * + * * If `"warn"`, warn about behavior via `console.warn` - * + * * @default "warn" */ strict?: boolean | string | Function; diff --git a/types/katex/katex-tests.ts b/types/katex/katex-tests.ts index 6fd614c82c..6b3e223927 100644 --- a/types/katex/katex-tests.ts +++ b/types/katex/katex-tests.ts @@ -1,16 +1,33 @@ import katexLib = require('katex'); +import renderMathInElement, { RenderMathInElementOptions } from 'katex/dist/contrib/auto-render'; class KatexTest { - constructor() { - katexLib.render('My Latex String', document.createElement('div')); + constructor() { + katexLib.render('My Latex String', document.createElement('div')); - try { - let options: katexLib.KatexOptions = { throwOnError: true }; - let value: string = katexLib.renderToString('My Latex String', options); - } catch (error) { - if (error instanceof katexLib.ParseError) { - //do something with this error - } - } - } -} \ No newline at end of file + try { + let options: katexLib.KatexOptions = { throwOnError: true }; + let value: string = katexLib.renderToString('My Latex String', options); + } catch (error) { + if (error instanceof katexLib.ParseError) { + //do something with this error + } + } + + const renderMathInElementOptions: RenderMathInElementOptions = { + delimiters: [ + { left: "$$", right: "$$", display: true }, + { left: "\\[", right: "\\]", display: true }, + { left: "$", right: "$", display: false }, + { left: "\\(", right: "\\)", display: false }, + ], + errorCallback(msg: string, err: Error): void { + console.error(msg, err); + //do something with this error + } + }; + const container = document.createElement('div'); + container.innerText = 'LaTeX string $c = \\pm\\sqrt{a^2 + b^2}$'; + renderMathInElement(container, renderMathInElementOptions); + } +}