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

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 joshbruce added this to Queue in PRs Dec 1, 2017
@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
});

@joshbruce joshbruce moved this from Queue to Close in PRs Jan 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
PRs
Close
Development

Successfully merging this pull request may close these issues.

None yet

3 participants