Git Product home page Git Product logo

markdown-it-katex's Introduction

markdown-it-katex

Add Math to your Markdown

Build Status

KaTeX is a faster alternative to MathJax. This plugin makes it easy to support in your markdown.

Need convincing?

Usage

Install markdown-it

npm install markdown-it

Install the plugin

npm install markdown-it-katex

Use it in your javascript

var md = require('markdown-it')(),
    mk = require('markdown-it-katex');

md.use(mk);

// double backslash is required for javascript strings, but not html input
var result = md.render('# Math Rulez! \n  $\\sqrt{3x-1}+(1+x)^2$');

Include the KaTeX stylesheet in your html:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.5.1/katex.min.css">

If you're using the default markdown-it parser, I also recommend the github stylesheet:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/github-markdown-css/2.2.1/github-markdown.css"/>

KaTeX options can be supplied with the second argument to use.

md.use(mk, {"throwOnError" : false, "errorColor" : " #cc0000"});

Examples

Inline

Surround your LaTeX with a single $ on each side for inline rendering.

$\sqrt{3x-1}+(1+x)^2$

Block

Use two ($$) for block rendering. This mode uses bigger symbols and centers the result.

$$\begin{array}{c}

\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} &
= \frac{4\pi}{c}\vec{\mathbf{j}}    \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\

\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\

\nabla \cdot \vec{\mathbf{B}} & = 0

\end{array}$$

Syntax

Math parsing in markdown is designed to agree with the conventions set by pandoc:

Anything between two $ characters will be treated as TeX math. The opening $ must
have a non-space character immediately to its right, while the closing $ must
have a non-space character immediately to its left, and must not be followed
immediately by a digit. Thus, $20,000 and $30,000 won’t parse as math. If for some
reason you need to enclose text in literal $ characters, backslash-escape them and
they won’t be treated as math delimiters.

Math Syntax Support

KaTeX is based on TeX and LaTeX. Support for both is growing. Here's a list of currently supported functions:

Function Support in KaTeX

markdown-it-katex's People

Contributors

cbreeden avatar waylonflinn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

markdown-it-katex's Issues

Markdown-it-katex with React.js

I'm using Markdown-it-katex with React and it's rendering beautifully, but I have some issues with console errors popping up due to unrecognized props. For example, when I paste in

$$ \sqrt{4} = 2 $$

into my markdown file, the console reads:

bundle.js:461 Warning: Unknown prop `encoding` on <annotation> tag. Remove this prop from the element. For details, see https://fb.me/react-unknown-prop
    in annotation
    in semantics
    in math
    in span
    in span
    in span
    in p
    in div (created by Post)
    in Post (created by PostsViewer)
    in div (created by PostsViewer)
    in PostsViewer (created by RegularPostsViewerWrapper)
    in RegularPostsViewerWrapper (created by Route)
    in Route
    in Switch
    in Router

This happens for other props as well, with more complicated equations. I was wondering whether I would be able to selectively disable attributes within the plugin given some options before React parses the returned HTML. Please let me know!

\iff not working

Tested on Chrome 60.0.3112.90 and Edge. Other functionalities like \in and \sqrt work as expected. markdown-it-katex was installed as in the docs, bundled with browserify and the bundle was imported in the HTML.

Don't catch error if throwOnError: true

Hi, many thanks for the library!
Why do we want to catch errors here and here if throwOnError: true is passed — leaving no chance to show error in the UI? I would rather use throw insead of console.logs here and catch error in the outside.

output html is incorrect

Hey @waylonflinn

Great work here:) I found that the output text is incorrect. the latex math span is not wrapped inside paragraph when stand alone, and it is also not wrapped in paragraph when it is after an image.

I'm pretty brain dead now, if you get around to look at this, let me know!

Ge

Add \( and \) support

For render the inline equation that enclose with \( and \), and the block equation that enclose with \[ and \], thanks!

Mathjax support with parsed markdown fails

Hi markdown-it team,

I should note at the offset that markdown-it + markdown-it-katex plugin parses most equations properly, except when there is markdown syntax in the equation.

Currently, I have a markdown document with a latex formula that looks like this:

\begin{equation}c_i = \begin{cases}
- \log{(\hat{y}_i)} & \text{for }y_i = 1 \\
- \log{(1 - \hat{y}_i)} & \text{for }y_i = 0
\end{cases}\end{equation}

Notice the - in the equation. Markdown-it sees these - marks and parses them as li elements which breaks the latex rendering (as latex doesn't know what to do with <ul><li>. Is there a way to tell markdown-it to skip rendering anything inside of the \begin{equation} and \end{equation} syntax? Alternative suggestions work too... I'm kind of stuck on this issue.

Thanks!

Unicode-Support

Hi,

when I'm using special letters like äöü in \text{}, the while equation will not be rendered :(
But it worked with Katex's demo (added \text{äöü} to the black box) so I think that the bug may be in this package.

Long math equations overflow the page

Hello,

I use markdown-it-katex to add math equations for my blog powered by VuePress. But when the equation is longer than the size of screen (especially on the mobile phone), it will overflow the page, which looks very weird.

IMG_0072.jpeg

I have noticed that many sites's math equations are able to swipe left like the code block to prevent the equations from overflowing the page. I wonder whether markdown-it-katex supports this feature?

Thanks a lot!

TeX isn't parseable

As part of what I do with Markdown, I have to parse the Markdown itself, token by token. For text, I normally just use the token's content.

However, in TeX code within Markdown, parsing breaks due to backslashes in the TeX being interpreted as escapes. For example, the code

$1+1 \, = 2$

Will be parsed as

$1 + 1 , = 2$

Producing a comma in the rendered TeX, when \, should be a 'small space'.

I'm not sure if this is even within scope of this package, but if this plugin could adjust the parsing so that escapes don't break the TeX that'd be amazing.

another XSS

\unicode{} also allows for XSS

$\unicode{<img src=x onerror=alert(1)>}$

XSS when parsing math expression

Look at: https://github.com/waylonflinn/markdown-it-katex/blob/master/index.js#L168

Once the parser returns error, it would return katex without sanitizing as HTML tags.

To trigger this catch block, you can easily put one more "%" character.

Try it on live demo http://waylonflinn.github.io/markdown-it-katex/

  1. Input the data as $<img src=a onerror=alert(1)>$ , nothing happens
  2. Try $<img src=a onerror=alert(1)>%$ you can see the alert dialog.

Consider this affects many real-world products.

Pandoc-crossref and block math

Pandoc-crossref (https://hackage.haskell.org/package/pandoc-crossref-0.1.0.0) is a very useful tool for equation numbering and cross references as markdown dialects usually don't support them natively. However, markdown-it-katex does not work properly with the syntax of pandoc-crossref. It seems that markdown-it-katex requires the ending $$ delimiter to be at the tail of the last line, but the label tags of pandoc-crossref, like {#eq:my-label}, must be put after the ending $$ delimiter. Would you care of this?

What is the status of this repo?

@waylonflinn

This repo is no longer compatible with the latest versions of Katex - which means that it is necessary to use a forked update ala @iktakahiro/markdown-it-katex to get katex working as intended.

Would it please be possible to clarify the status of the repo and whether there are plans to continue maintaining it? If no plans to maintain, whether it would be possible to cede the project to a new active maintainer, maybe @iktakahiro?

Given the name of the repo it would be opportune to keep this working, as this is the most apparent option when people are searching for markdown-it + katex.

Thanks.

AMD support for markdown-it-katex

Hi,

I need to use markdown-it-katex with AMD support. I have already made change locally and its working. I want to share this with the community. Do you accept pull requests at this time?

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.