[katex] Add contrib/auto-render

This commit is contained in:
bLue
2019-02-08 21:29:05 +08:00
parent 510f81374c
commit ef54ca7995
4 changed files with 115 additions and 30 deletions
+64
View File
@@ -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<RenderMathInElementSpecificOptionsDelimiters>;
/**
* A list of DOM node types to ignore when recursing through
*
* @default ["script", "noscript", "style", "textarea", "pre", "code"]
*/
ignoredTags?: ReadonlyArray<keyof HTMLElementTagNameMap>;
/**
* 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;
}
+1
View File
@@ -0,0 +1 @@
/// <reference path="auto-render.d.ts" />
+21 -18
View File
@@ -2,24 +2,27 @@
// Project: http://khan.github.io/KaTeX/
// Definitions by: Michael Randolph <https://github.com/mrand01>
// Kevin Nguyen <https://github.com/knguyen0125>
// bLue <https://github.com/dreamerblue>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="contrib/index.d.ts" />
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;
+29 -12
View File
@@ -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
}
}
}
}
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);
}
}