Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@vincenttam/showdown-katex

VincentTam16MIT0.10.1

Showdown extension that adds LaTeX, ASCIImath and mhchem support

markdown, showdown, latex, asciimath, katex, mhchem

readme

showdown-katex

npm install showdown-katex

Showdown extension to render LaTeX math and AsciiMath using KaTeX;

Special characters do not need escaping

Works well alongside bootmark

Config

You can customize what gets passed to the katex renderer by passing a config object.

These are the defaults:

{
  displayMode: true,
  throwOnError: false, // allows katex to fail silently
  errorColor: '#ff0000',
  delimiters: [
    { left: "$$", right: "$$", display: false },
    { left: '~', right: '~', display: false, asciimath: true },
  ],
}

Examples:

<script>
  const converter = new showdown.Converter({
    extensions: [
      showdownKatex({
        // maybe you want katex to throwOnError
        throwOnError: true,
        // disable displayMode
        displayMode: false,
        // change errorColor to blue
        errorColor: '#1500ff',
      }),
    ],
  });
  converter.makeHtml('~x=2~');
</script>

Check katex for more details.

Default Delimiters

Format Left Right Display mode
Latex $$ $$ false
Asciimath ~ ~ false

To define custom delimiters simply define a delimiters property in the config as an array of objects. Each object MUST have a left (string) property with the left delimiter, and a right (string) property with the right delimiter. The oject may also have a display (boolean) property if the delimiter should use display mode instead of inline mode, and an asciimath (boolean) id the delimiter is Asciimath instead of Latex.

Custom delimiters won't disable the defaults, so you can use both custom and default delimiters.

const converter = new showdown.Converter({
  extensions: [
    showdownKatex({
      delimiters: [{ left: '( ͡° ͜ʖ ͡°)', right: '( ͡° ͜ʖ ͡°)', asciimath: true }],
    }),
  ],
});
converter.makeHtml(
  'some text here, ( ͡° ͜ʖ ͡°) E=mc^2 ( ͡° ͜ʖ ͡°), you can still use ~ E=Mc^2 ~',
);

FOUC

If your page suffers from a "Flash Of Unstyled Content," add this to your <body> tag:

<body style="display:none;" onload="document.body.style.display='block'">

This hides the body and shows it only when the JavaScript has loaded.

Math Example

```asciimath
x = (-b +- sqrt(b^2-4ac)) / (2a)
```
x = (-b +- sqrt(b^2-4ac)) / (2a)
```latex
x=\frac{ -b\pm\sqrt{ b^2-4ac } } {2a}
```
x=\frac{ -b\pm\sqrt{ b^2-4ac } } {2a}

They will both render the exact same thing. If the examples don't render correctly click here.

Chemical equations example

Examples copied from mhchem's page.

```latex
\ce{Hg^2+ ->[I-] HgI2 ->[I-] [Hg^{II}I4]^2-}
```
\ce{Hg^2+ ->[I-] HgI2 ->[I-] [Hg^{II}I4]^2-}

changelog

showdown-katex changelog

Future

BREAKING

NEW

FIX

0.9.0 2022-10-18

  • upgrade dependencies
    • KaTeX: 0.11.1 → 0.16.2
    • Showdown: 1.9.1 → 2.1.0

      NEW

  • added KaTeX mhchem extension for writing chemical equations

0.8.0 2020-03-12

  • upgrade dependencies

    FIX

  • documentation build system: npm run docs
  • user provided delimiters take priority

0.7.0 2019-08-03

NEW

  • nodejs support: polyfill DOMParser and document with jsdom package and add cjs build in lib/ (#7, #11)

    POSSIBLE BREAKING

  • point package.json.main to cjs build
  • point package.json.browser to umd build

0.6.0

FIX

  • rendering math inside inline code and code blocks

    BREAKING

  • make $$ latex delimiter inline instead of display mode
  • use code block style to use display mode
    • remove \\[ ... \\] latex delimiter
    • remove \\( ... \\) latex delimiter
    • remove && asciimath delimiter

0.5.0 2019-01-28

NEW

  • update katex dependency to 0.10.0

    FIXED

  • && asciimath separator seems to work now ¯\_(ツ)_/¯
  • correct files now uploaded to npm
  • eslint errors

0.4.0 2017-01-10

NEW

  • rename to showdown-katex
  • disallow global config
  • move window.katexLatex to window.showdownKatex
  • katex is now bundled with showdownKatex, so there's no need to include externally anymore.

    FIXED

  • add inline latex examples
  • add inline asciimath examples
  • remove && as a delimiter for asciimath in displayMode. it was giving too mouch trouble. For asciimath in displayMode, use code block style with the lang set to asciimath. so:

      && E=mc^2 &&

    becomes

      ```asciimath
      E=mc^2
      ```

MIGRATION

  • update references to katex-latex in files and showdown extension and update to showdown-katex
  • if previously using global config, it should now be passed to the global showdownKatex function which returns a showdown extension that can be passed as part of the extensions array

      // before
      window.katexLatex.config = {
        throwOnError: true,
      };
      // now
      const converter = new showdown.Converter({
        extensions: [showdownKatex({
          throwOnError: true,
        })]
      });
      converter.makeHtml('~x=2~')

    if not using custom config, just list "showdown-katex" in the extensions

0.3.1 2017-09-17

FIXED

  • publish to unpkg
  • remove from bower

0.3.0 2017-09-16

MIGRATION

  • there is no longer a version without the asciimath to tex script, so there is no katex-latex.bundle.js. just include katex-latex.js or katex-latex.min.js.
  • if you want inline math rendering make sure to include the auto-render extension

NEW

  • moved from gulp to npm scripts and rollup based build system
  • moved dist/ASCIIMathTeXImg.js to src/asciimath-to-tex.js, adopting it into the source of the project
  • treeshaking the extra functions in asciimath-to-tex.js
  • moved to es6 thanks to babel and rollup
  • added katex auto-render extension integration. it is not bundled along with katex-latex. so there is inline latex and asciimath support now!
  • exports showdown extension function as default and defines window.katexLatex function
  • show source expression on hover as the <span>'s title
  • set up testing environment with ava

FIXED

  • wrapping <div> was substituted with a <span> to allow for flexibility
  • demo now pretty prints normal code by using bootmark
  • no more jquery dependency!
  • better organized examples

0.2.0 2016-10-10

NEW

  • asciimath support
  • custom asciimath syntax
  • allow users to customize katex rendering through window.katex.config object
  • bundle file includes what's necessary to render asciimath, but not KaTeX it self

FIXED

  • ...

0.1.0 2016-10-09

NEW

  • doesn't require characters to be escaped
  • support custom latex syntax on markdown
  • plays well with bootmark

FIXED

  • ...