tani / markdown-it-mathjax3

Add Math to your Markdown with a MathJax plugin for Markdown-it
MIT License
61 stars 9 forks source link

How to access MathJax #63

Open zcysxy opened 1 year ago

zcysxy commented 1 year ago

Sorry I am not very familiar with web development. I want to configure MathJax following the official document like the following example

image

However, it requires to access MathJax within the configure. I tried the following code

const markdownIt = require("markdown-it");
const mathjax = require("markdown-it-mathjax3");

let markdownLib = markdownIt({
  breaks: true,
  html: true,
})
  .use(require("markdown-it-mathjax3"), {
    tex: {
      inlineMath: [["$", "$"]],
    },
    options: {
      skipHtmlTags: { "[-]": ["pre"] },
    },
    startup: {
      ready: () => {
        mathjax.startup.defaultReady();
        mathjax.tex2chtml('\\def\\R{\\mathbb{R}}');
        console.log('hi mathjax');
      }
    }
  })

But it does not seem to work. Thanks!

zcysxy commented 1 year ago

The use case is that I want to render some TeX commands at startup, e.g., defining some macros like \def\R{\mathbb{R}} for every document.

bill-ion commented 10 months ago

If you read the source code of this plugin, you will find that it only receive tex option and svg option. So the answer is simply that the startup option is not supported so far.

function plugin(md: MarkdownIt, options: any) {
  // Default options

  const documentOptions = {
    InputJax: new TeX({ packages: AllPackages,  ...options?.tex }),
    OutputJax: new SVG({ fontCache: 'none',  ...options?.svg })
  }
  const convertOptions = {
    display: false
  }

  // set MathJax as the renderer for markdown-it-simplemath
  md.inline.ruler.after("escape", "math_inline", math_inline);
  md.block.ruler.after("blockquote", "math_block", math_block, {
    alt: ["paragraph", "reference", "blockquote", "list"],
  });
  md.renderer.rules.math_inline = function (tokens: Token[], idx: number) {
    convertOptions.display = false;
    return renderMath(tokens[idx].content, documentOptions, convertOptions)
  };
  md.renderer.rules.math_block = function (tokens: Token[], idx: number) {
    convertOptions.display = true;
    return renderMath(tokens[idx].content, documentOptions, convertOptions)
  };
};