diff --git a/packages/unified-latex-util-parse/README.md b/packages/unified-latex-util-parse/README.md index bdfd2e3b..728682a2 100644 --- a/packages/unified-latex-util-parse/README.md +++ b/packages/unified-latex-util-parse/README.md @@ -160,6 +160,21 @@ function parseMinimal(str: String): Ast.Root; | :---- | :------- | | str | `String` | +## `getParser(PluginOptions)` + +Returns the default `unified-latex` parser if the argument is `undefined`, or +create a new one with the provided `unifiedLatexFromString` options. + +```typescript +function getParser(options?: PluginOptions): FrozenProcessor; +``` + +**Parameters** + +| Param | Type | +| :----- | :-------------- | +| option | `PluginOptions` | + # Types ## `PluginOptions` diff --git a/packages/unified-latex-util-parse/libs/parse.ts b/packages/unified-latex-util-parse/libs/parse.ts index cb39696f..f5412383 100644 --- a/packages/unified-latex-util-parse/libs/parse.ts +++ b/packages/unified-latex-util-parse/libs/parse.ts @@ -1,8 +1,9 @@ import * as Ast from "@unified-latex/unified-latex-types"; -import { unified } from "unified"; +import { type FrozenProcessor, unified } from "unified"; import { unifiedLatexFromString } from "./plugin-from-string"; +import type { PluginOptions } from "./plugin-from-string"; -const parser = unified().use(unifiedLatexFromString).freeze(); +let parser = unified().use(unifiedLatexFromString).freeze(); /** * Parse the string into an AST. @@ -10,3 +11,18 @@ const parser = unified().use(unifiedLatexFromString).freeze(); export function parse(str: string): Ast.Root { return parser.parse(str); } + +/** + * Returns the default `unified-latex` parser, or create a new one with the + * provided `unifiedLatexFromString` options + * @param options Plugin options of `unifiedLatexFromString` plugin. + * @returns The default `unified-latex` parser if `options` is `undefined`, or a + * newly created `unified-latex` parser with the provided `options`. + */ +export function getParser( + options?: PluginOptions +): FrozenProcessor { + return options + ? unified().use(unifiedLatexFromString, options).freeze() + : parser; +}