Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support custom katex instance #24

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ $$\begin{array}{c}
\end{array}$$
```

### Katex extension
```tsx
import MarkdownIt from 'markdown-it'
import katex from 'katex'
import 'katex/contrib/mhchem'
import 'katex/contrib/copy-tex'
const md = new MarkdownIt()
katexPlugin(md, {
katex,
})

const result = md.render('# Math Rulez! \n $\\sqrt{3x-1}+(1+x)^2$');
const chemResult = md.render('$\\ce{Hg^2+ ->[I-] HgI2 ->[I-] [Hg^{II}I4]^2-}$')
```

## Syntax

Math parsing in markdown is designed to agree with the conventions set by pandoc:
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import katex from 'katex';
import localKatex from 'katex';
import type * as StateBlock from 'markdown-it/lib/rules_block/state_block';
import type StateCore from 'markdown-it/lib/rules_core/state_core';
import type * as StateInline from 'markdown-it/lib/rules_inline/state_inline';
Expand Down Expand Up @@ -459,6 +459,7 @@ function escapeHtml(unsafe: string): string {


export default function (md: import('markdown-it'), options?: MarkdownKatexOptions) {
const katex = options?.katex ?? localKatex;
const enableBareBlocks = options?.enableBareBlocks;
const enableMathBlockInHtml = options?.enableMathBlockInHtml;
const enableMathInlineInHtml = options?.enableMathInlineInHtml;
Expand Down
6 changes: 6 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type MarkdownIt from 'markdown-it';
import Katex from "katex"

export interface MarkdownKatexOptions {
/**
Expand Down Expand Up @@ -31,6 +32,11 @@ export interface MarkdownKatexOptions {
* Controls if an exception is thrown on katex errors.
*/
readonly throwOnError?: boolean;

/**
* Support for custom katex instance for extension such as mhchem
*/
katex?: typeof Katex
}

export default function (md: MarkdownIt, options?: MarkdownKatexOptions): MarkdownIt;