# KnowledgeBase

Share immutable knowledge across parsers, transform engines, and documents.

https://texform.dev/api/js/knowledge-base

## `KnowledgeBase`

Kind: class

```ts
KnowledgeBase(options: KnowledgeBaseOptions | undefined)
```

Immutable knowledge shared by parsers, engines and documents.

It defines the known commands, environments, characters, and delimiters that parsing, transforms, and `Document` construction and edits check against. Share one instance across a [Parser](/api/js/parser#Parser), [TransformEngine](/api/js/transform-engine#TransformEngine), and their documents. Enumerations are sorted by name and returned as copies.

```ts
const knowledgeBase = new KnowledgeBase({ packages: ['ams'] });
const { document } = new Parser({ knowledgeBase }).parse(String.raw`\frac{a}{b}`);
```

- [Parser](https://texform.dev/api/js/parser.md#Parser)
- [TransformEngine](https://texform.dev/api/js/transform-engine.md#TransformEngine)

#### `KnowledgeBase`

```ts
KnowledgeBase(options: KnowledgeBaseOptions | undefined)
```

Build the knowledge base once. Throws [TexformConfigError](/api/js/utilities#TexformConfigError) for an invalid package name or item.

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `options` | KnowledgeBaseOptions | undefined | — |  |

#### `free`

```ts
free(): void
```

**Returns** `void`

#### `[Symbol.dispose]`

```ts
[Symbol.dispose](): void
```

**Returns** `void`

#### `isSame`

```ts
isSame(other: KnowledgeBase): boolean
```

Whether `other` is the same instance; equality is identity, not recipe.

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `other` | KnowledgeBase | — |  |

**Returns** `boolean`

#### `packages`

```ts
packages(): string[]
```

**Returns** `string[]`

#### `commands`

```ts
commands(mode: RuntimeContentMode): CommandInfo[]
```

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `mode` | RuntimeContentMode | — |  |

**Returns** `CommandInfo[]`

#### `environments`

```ts
environments(mode: RuntimeContentMode): EnvInfo[]
```

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `mode` | RuntimeContentMode | — |  |

**Returns** `EnvInfo[]`

#### `characters`

```ts
characters(mode: RuntimeContentMode): CharacterInfo[]
```

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `mode` | RuntimeContentMode | — |  |

**Returns** `CharacterInfo[]`

#### `delimiters`

```ts
delimiters(): DelimiterInfo[]
```

**Returns** `DelimiterInfo[]`

#### `isDelimiterControl`

```ts
isDelimiterControl(name: string): boolean
```

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `name` | string | — |  |

**Returns** `boolean`

#### `knowsCommandName`

```ts
knowsCommandName(name: string): boolean
```

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `name` | string | — |  |

**Returns** `boolean`

#### `knowsEnvName`

```ts
knowsEnvName(name: string): boolean
```

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `name` | string | — |  |

**Returns** `boolean`

#### `knowsCharacterName`

```ts
knowsCharacterName(name: string): boolean
```

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `name` | string | — |  |

**Returns** `boolean`

#### `lookupCharacter`

```ts
lookupCharacter(name: string, mode: RuntimeContentMode): CharacterInfo | null
```

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `name` | string | — |  |
| `mode` | RuntimeContentMode | — |  |

**Returns** `CharacterInfo | null`

#### `lookupCommand`

```ts
lookupCommand(name: string, mode: RuntimeContentMode): CommandInfo | null
```

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `name` | string | — |  |
| `mode` | RuntimeContentMode | — |  |

**Returns** `CommandInfo | null`

#### `lookupEnv`

```ts
lookupEnv(name: string, mode: RuntimeContentMode): EnvInfo | null
```

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `name` | string | — |  |
| `mode` | RuntimeContentMode | — |  |

**Returns** `EnvInfo | null`

#### `lookupExplicitCommand`

```ts
lookupExplicitCommand(name: string, mode: RuntimeContentMode): CommandInfo | null
```

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `name` | string | — |  |
| `mode` | RuntimeContentMode | — |  |

**Returns** `CommandInfo | null`

## `KnowledgeBaseOptions`

Kind: interface

Construction options for an immutable [KnowledgeBase](/api/js/knowledge-base#KnowledgeBase).

Omit `packages` to load the default runtime packages, not every package in
the catalog. Use `listPackages()` to see the available names; an unknown name
throws [TexformConfigError](/api/js/utilities#TexformConfigError).

| Name | Type | Description |
| --- | --- | --- |
| `packages` | string[] | undefined | Package names to load; omit to load the default runtime packages. |
| `items` | ContextItem[] | undefined | Custom command/environment/delimiter-control knowledge to inject. |
| `removeCommands` | string[] | undefined | Command names to drop from the loaded knowledge. |
| `removeEnvironments` | string[] | undefined | Environment names to drop from the loaded knowledge. |
| `removeDelimiterControls` | string[] | undefined | Delimiter-control names to drop from the loaded knowledge. |

- [KnowledgeBase](https://texform.dev/api/js/knowledge-base.md#KnowledgeBase)
- [TexformConfigError](https://texform.dev/api/js/utilities.md#TexformConfigError)

## `DelimiterInfo`

Kind: interface

| Name | Type | Description |
| --- | --- | --- |
| `name` | string |  |
| `isControlSequence` | boolean |  |
| `allowedMode` | string |  |
| `unicodeValue` | string |  |
| `attributes` | { mathvariant: string | null } |  |
| `package` | string |  |

## `ConstructionOptions`

Kind: interface

Options for the `create*` methods. `mode` is the context mode of the detached subtree. It defaults to text for `createInlineMath`, math for
`createDelimitedGroup`, `createScripted`, `createPrime`, and `createInfix`, the group's own
mode for `createGroup`, and the document root mode otherwise.

| Name | Type | Description |
| --- | --- | --- |
| `mode` | RuntimeContentMode | undefined |  |

