feat(KaTeX): add copy-tex and mhchem for KaTeX

This commit is contained in:
Dillon 2020-02-03 21:36:46 +08:00
parent 77d2decf86
commit d605de7f06
7 changed files with 86 additions and 6 deletions

1
assets/css/lib/katex/copy-tex.min.css vendored Normal file
View file

@ -0,0 +1 @@
.katex,.katex-display{user-select:all;-moz-user-select:all;-webkit-user-select:all;-ms-user-select:all}

1
assets/js/lib/katex/copy-tex.min.js vendored Normal file
View file

@ -0,0 +1 @@
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var n=t();for(var r in n)("object"==typeof exports?exports:e)[r]=n[r]}}("undefined"!=typeof self?self:this,function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=1)}([function(e,t,n){},function(e,t,n){"use strict";n.r(t);n(0);var r={inline:["$","$"],display:["$$","$$"]},o=function(e,t){void 0===t&&(t=r);for(var n=e.querySelectorAll(".katex-mathml + .katex-html"),o=0;o<n.length;o++){var l=n[o];l.remove?l.remove(null):l.parentNode.removeChild(l)}for(var i=e.querySelectorAll(".katex-mathml"),a=0;a<i.length;a++){var u=i[a],f=u.querySelector("annotation");f&&(u.replaceWith?u.replaceWith(f):u.parentNode.replaceChild(f,u),f.innerHTML=t.inline[0]+f.innerHTML+t.inline[1])}for(var c=e.querySelectorAll(".katex-display annotation"),d=0;d<c.length;d++){var p=c[d];p.innerHTML=t.display[0]+p.innerHTML.substr(t.inline[0].length,p.innerHTML.length-t.inline[0].length-t.inline[1].length)+t.display[1]}return e};document.addEventListener("copy",function(e){var t=window.getSelection();if(!t.isCollapsed){var n=t.getRangeAt(0).cloneContents();if(n.querySelector(".katex-mathml")){for(var r=[],l=0;l<n.childNodes.length;l++)r.push(n.childNodes[l].outerHTML);e.clipboardData.setData("text/html",r.join("")),e.clipboardData.setData("text/plain",o(n).textContent),e.preventDefault()}}})}]).default});

1
assets/js/lib/katex/mhchem.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -133,8 +133,18 @@ dateFormatToUse = "2006-01-02"
# public git repo url to link lastmod git commit only then enableGitInfo is true
gitRepo = ""
# whether to render mathematical formulas
math = true
# mathematical formulas (KaTeX)
[params.math]
enable = true
block_left_delimiter = ""
block_right_delimiter = ""
inline_left_delimiter = "$"
inline_right_delimiter = "$"
# KaTeX extension copy_tex
copy_tex = true
# KaTeX extension mhchem
mhchem = true
# whether to show link to Raw Markdown content of the post
linkToMarkdown = true
@ -159,6 +169,9 @@ dateFormatToUse = "2006-01-02"
katex_css = ''
katex_js = ''
katex_auto_render_js = ''
katex_copy_tex_css = ''
katex_copy_tex_js = ''
katex_mhchem_js = ''
mermaid_js = ''
echarts_js = ''
echarts_macarons_js = ''

View file

@ -133,8 +133,18 @@ dateFormatToUse = "2006-01-02"
# 用于生成文章上次修改时间的公共 git 仓库路径,仅在 enableGitInfo 设为 true 时有效
gitRepo = ""
# 是否渲染数学公式
math = true
# 数学公式 (KaTeX https://katex.org/)
[params.math]
enable = true
block_left_delimiter = ""
block_right_delimiter = ""
inline_left_delimiter = "$"
inline_right_delimiter = "$"
# KaTeX 插件 copy_tex
copy_tex = true
# KaTeX 插件 mhchem
mhchem = true
# 是否在文章页面显示原始 Markdown 文档链接
linkToMarkdown = true
@ -159,6 +169,9 @@ dateFormatToUse = "2006-01-02"
katex_css = ''
katex_js = ''
katex_auto_render_js = ''
katex_copy_tex_css = ''
katex_copy_tex_js = ''
katex_mhchem_js = ''
mermaid_js = ''
echarts_js = ''
echarts_macarons_js = ''

View file

@ -314,3 +314,9 @@ int a = 1;
$$ c = \pm\sqrt{a^2 + b^2} $$
Left \\( c = \pm\sqrt{a^2 + b^2} \\) Right
Left $ c = \pm\sqrt{a^2 + b^2} $ Right
$ \ce{CO2 + C -> 2 CO} $
$ \ce{Hg^2+ ->[I-] HgI2 ->[I-] [Hg^{II}I4]^2-} $

View file

@ -54,7 +54,8 @@
{{- end -}}
{{- /* KaTeX https://github.com/KaTeX/KaTeX */ -}}
{{- if .Site.Params.math | and (ne .Params.math false) -}}
{{- if .Site.Params.math.enable | and (ne .Params.math false) -}}
{{- $math := .Site.Params.math -}}
{{- $katex_css := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.katex_css -}}
{{- $katex_css = .Site.Params.cdn.katex_css -}}
@ -74,10 +75,54 @@
{{- $katex_auto_render_js = .Site.Params.cdn.katex_auto_render_js -}}
{{- else -}}
{{- $res := resources.Get "js/lib/katex/auto-render.min.js" -}}
{{- $katex_auto_render_js = printf "<script defer src=\"%s\" onload=\"renderMathInElement(document.body);\"></script>" $res.RelPermalink -}}
{{- $katex_auto_render_js = printf "<script defer src=\"%s\"></script>" $res.RelPermalink -}}
{{- end -}}
{{- $katex := delimit (slice $katex_css $katex_js $katex_auto_render_js) "" -}}
{{- if $math.copy_tex -}}
{{- $katex_copy_tex_css := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.katex_copy_tex_css -}}
{{- $katex_copy_tex_css = .Site.Params.cdn.katex_copy_tex_css -}}
{{- else -}}
{{- $res := resources.Get "css/lib/katex/copy-tex.min.css" -}}
{{- $katex_copy_tex_css = printf "<link rel=\"stylesheet\" href=\"%s\">" $res.RelPermalink -}}
{{- end -}}
{{- $katex_copy_tex_js := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.katex_copy_tex_js -}}
{{- $katex_copy_tex_js = .Site.Params.cdn.katex_copy_tex_js -}}
{{- else -}}
{{- $res := resources.Get "js/lib/katex/copy-tex.min.js" -}}
{{- $katex_copy_tex_js = printf "<script defer src=\"%s\"></script>" $res.RelPermalink -}}
{{- end -}}
{{- $katex = delimit (slice $katex $katex_copy_tex_css $katex_copy_tex_js) "" -}}
{{- end -}}
{{- if $math.mhchem -}}
{{- $katex_mhchem_js := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.katex_mhchem_js -}}
{{- $katex_mhchem_js = .Site.Params.cdn.katex_mhchem_js -}}
{{- else -}}
{{- $res := resources.Get "js/lib/katex/mhchem.min.js" -}}
{{- $katex_mhchem_js = printf "<script defer src=\"%s\"></script>" $res.RelPermalink -}}
{{- end -}}
{{- $katex = delimit (slice $katex $katex_mhchem_js) "" -}}
{{- end -}}
{{- $katex | safeHTML -}}
<script>
document.addEventListener("DOMContentLoaded", function () {
renderMathInElement(document.body, {
delimiters: [
{ left: "$$", right: "$$", display: true },
{ left: "\\(", right: "\\)", display: false },
{ left: "\\[", right: "\\]", display: true },
{{- if and $math.block_left_delimiter $math.block_right_delimiter -}}
{ left: "{{ $math.block_left_delimiter }}", right: "{{ $math.block_right_delimiter }}", display: true },
{{- end -}}
{{- if and $math.inline_left_delimiter $math.inline_right_delimiter -}}
{ left: "{{ $math.inline_left_delimiter }}", right: "{{ $math.inline_right_delimiter }}", display: false },
{{- end -}}
]
});
});
</script>
{{- end -}}
{{- /* mermaid https://github.com/knsv/mermaid */ -}}