ᕕ( ᐛ )ᕗ Jimyag's Blog

Hexo-NexT使用LaTeX公式

在使用next主题的过程中,碰到写的markdown中有LaTeX公式不显示的问题,遂查找资料解决。

  1. 更换Hexo默认渲染引擎

    Hexo默认的渲染引擎是 marked,但是 marked 不支持 MathJax。所以需要更换Hexo的markdown渲染引擎为hexo-renderer-kramed引擎,后者支持MathJax公式输出。

    npm uninstall hexo-renderer-marked --save
    npm install hexo-renderer-kramed --save
    
  2. 激活MathJax

    /blog/themes/next/config.yml中找到# MathJax Support修改为

    mathjax:
      enable: true
      per_page: true
    
  3. 修改kramed语法解释

    /blog/node_modules/kramed/lib/rules/inline.js

      escape: /^\\([\\`*{}\[\]()#$+\-.!_>])/,
      autolink: /^<([^ >]+(@|:\/)[^ >]+)>/,
      url: noop,
      html: /^<!--[\s\S]*?-->|^<(\w+(?!:\/|[^\w\s@]*@)\b)*?(?:"[^"]*"|'[^']*'|[^'">])*?>([\s\S]*?)?<\/\1>|^<(\w+(?!:\/|[^\w\s@]*@)\b)(?:"[^"]*"|'[^']*'|[^'">])*?>/,
      link: /^!?\[(inside)\]\(href\)/,
      reflink: /^!?\[(inside)\]\s*\[([^\]]*)\]/,
      nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
      reffn: /^!?\[\^(inside)\]/,
      strong: /^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,
      em: /^\b_((?:__|[\s\S])+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,
      code: /^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/,
      br: /^ {2,}\n(?!\s*$)/,
      del: noop,
      text: /^[\s\S]+?(?=[\\<!\[_*`$]| {2,}\n|$)/,
      math: /^\$\$\s*([\s\S]*?[^\$])\s*\$\$(?!\$)/,
    

    替换为

      escape: /^\\([`*\[\]()#$+\-.!_>])/,
      autolink: /^<([^ >]+(@|:\/)[^ >]+)>/,
      url: noop,
      html: /^<!--[\s\S]*?-->|^<(\w+(?!:\/|[^\w\s@]*@)\b)*?(?:"[^"]*"|'[^']*'|[^'">])*?>([\s\S]*?)?<\/\1>|^<(\w+(?!:\/|[^\w\s@]*@)\b)(?:"[^"]*"|'[^']*'|[^'">])*?>/,
      link: /^!?\[(inside)\]\(href\)/,
      reflink: /^!?\[(inside)\]\s*\[([^\]]*)\]/,
      nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
      reffn: /^!?\[\^(inside)\]/,
      strong: /^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,
      em: /^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,
    
  4. 在markdown开头添加语句

    mathjax: true
    
  5. 测试

    $$1 = \frac{2}{2}$$

在HEXO博客中使用LaTeX公式的简单方法_Loy Fan-CSDN博客

#Web #教程