From 6116961b32d28de49db6e88947bc10951120e3b2 Mon Sep 17 00:00:00 2001 From: James-Yu Date: Fri, 26 May 2023 13:54:05 +0800 Subject: [PATCH 1/3] Add getParser() to inject plugin options --- packages/unified-latex-util-parse/libs/parse.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/unified-latex-util-parse/libs/parse.ts b/packages/unified-latex-util-parse/libs/parse.ts index cb39696f..7a766ed9 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,14 @@ 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 +} \ No newline at end of file From a6678258dcaf8f0ebd03b6c32ef29e73f8cf95da Mon Sep 17 00:00:00 2001 From: James-Yu Date: Fri, 26 May 2023 14:05:16 +0800 Subject: [PATCH 2/3] Add doc of getParser() to README --- packages/unified-latex-util-parse/README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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` From d1747e6f55de38b37dbf860e0949f242bca5e844 Mon Sep 17 00:00:00 2001 From: James Yu Date: Wed, 31 May 2023 21:00:58 +1200 Subject: [PATCH 3/3] Prettify util-parse/parse.ts --- packages/unified-latex-util-parse/libs/parse.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/unified-latex-util-parse/libs/parse.ts b/packages/unified-latex-util-parse/libs/parse.ts index 7a766ed9..f5412383 100644 --- a/packages/unified-latex-util-parse/libs/parse.ts +++ b/packages/unified-latex-util-parse/libs/parse.ts @@ -19,6 +19,10 @@ export function parse(str: string): Ast.Root { * @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 -} \ No newline at end of file +export function getParser( + options?: PluginOptions +): FrozenProcessor { + return options + ? unified().use(unifiedLatexFromString, options).freeze() + : parser; +}