Skip to content
New issue

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

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

Already on GitHub? # to your account

Added a math parsing feature #591

Closed
wants to merge 1 commit into from
Closed

Conversation

shovon
Copy link

@shovon shovon commented May 18, 2015

The parser expects a block surrounded in $$ ... $$ or an inline component surrounded in $ ... $. You supply the renderer, via a math option, which is an object with a render method that returns a string, representing the outputed math.

This is different from #180, in that the renderer is not limited to just MathJax, but KaTeX will work as well.

The parser expects a block surrounded in `$$ ... $$` or an inline
component surround in `$ ... $`. You supply the renderer, via a
`math` option, which is an object with a `render` method that returns
a string, representing the outputed math.
@joshbruce
Copy link
Member

Closing as missing tests and not current focus per #956

@joshbruce joshbruce closed this Dec 26, 2017
@qubyte
Copy link

qubyte commented Jan 3, 2018

A quick and dirty solution I took to this (which I use for server rendering of my own files, not anything user submitted) is to add a custom highlighter for code blocks tagged as mathematics. It's not perfect (results in SVG embedded in pre tags), but it does work for non-inline maths. Note: this requires marked to be called with a callback since mathjax is asynchronous.

const marked = require('marked');
const mathjax = require('mathjax-node');
const renderer = new marked.Renderer();

const codeRenderer = renderer.code;

renderer.code = function (code, lang, escaped) {
  if (lang === 'mathematics') {
    return `<object class="mathematics">${code}</object>`;
  }

  return codeRenderer.call(this, code, lang, escaped);
};

marked.setOptions({
  async highlight(code, language, callback) {
    if (language !== 'mathematics') {
      // Highlighting for other languages
    }

    const result = await mathjax.typeset({ math: code, format: 'TeX', svg: true });

    if (result.errors) {
      return callback(result.errors);
    }

    callback(null, result.svg);
  },
  renderer
});

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants