Skip to content

Commit

Permalink
Merge pull request #33 from James-Yu/parser-with-option
Browse files Browse the repository at this point in the history
Add getParser() to inject plugin options
  • Loading branch information
siefkenj committed Jun 1, 2023
2 parents 88aa256 + d1747e6 commit 98831af
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
15 changes: 15 additions & 0 deletions packages/unified-latex-util-parse/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ast.Root, Ast.Root, Ast.Root, void>;
```

**Parameters**

| Param | Type |
| :----- | :-------------- |
| option | `PluginOptions` |

# Types

## `PluginOptions`
Expand Down
20 changes: 18 additions & 2 deletions packages/unified-latex-util-parse/libs/parse.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
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.
*/
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<Ast.Root, Ast.Root, Ast.Root, void> {
return options
? unified().use(unifiedLatexFromString, options).freeze()
: parser;
}

0 comments on commit 98831af

Please sign in to comment.