Hexo-NexT使用LaTeX公式
在使用next主题的过程中,碰到写的markdown中有LaTeX公式不显示的问题,遂查找资料解决。
-
更换Hexo默认渲染引擎
Hexo默认的渲染引擎是 marked,但是 marked 不支持 MathJax。所以需要更换Hexo的markdown渲染引擎为hexo-renderer-kramed引擎,后者支持MathJax公式输出。
npm uninstall hexo-renderer-marked --save npm install hexo-renderer-kramed --save
-
激活MathJax
在
/blog/themes/next/config.yml
中找到# MathJax Support
修改为mathjax: enable: true per_page: true
-
修改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])+?)\*(?!\*)/,
-
在markdown开头添加语句
mathjax: true
-
测试
$$1 = \frac{2}{2}$$