From 3f2088ad1c4b1180c68cfe36e798741ef2456d4b Mon Sep 17 00:00:00 2001 From: Shumpei Shiina Date: Tue, 4 Jan 2022 00:23:49 +0900 Subject: [PATCH] Support GitLab-style math expressions ($`...`$ and ```math ... ```) --- README.md | 4 ++-- background/mathjax.js | 1 + content/index.js | 9 +++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6652732..d4eb335 100644 --- a/README.md +++ b/README.md @@ -118,8 +118,8 @@ When enabled the extension will make a GET request every second to: The following `mathjax` delimiters are supported: -- inline math: `\(math\)` and `$math$` -- display math: `\[math\]` and `$$math$$` +- inline math: `\(math\)`, `$math$`, and ``$`math`$`` +- display math: `\[math\]`, `$$math$$`, and math code blocks (```` ```math ````) The following rules apply to your content when `mathjax` is enabled: diff --git a/background/mathjax.js b/background/mathjax.js index 4bd9214..3d51cbe 100644 --- a/background/mathjax.js +++ b/background/mathjax.js @@ -7,6 +7,7 @@ md.mathjax = () => { /\\\[[^`]*?\\\]/, /\\begin\{.*?\}[^`]*?\\end\{.*?\}/, /\$[^`\n]*?\$/, + /\$`[^`\n]*?`\$/, ] .map((regex) => `(?:${regex.source})`).join('|'), 'gi') diff --git a/content/index.js b/content/index.js index 34875fd..41140d8 100644 --- a/content/index.js +++ b/content/index.js @@ -104,6 +104,14 @@ function mount () { dom.push(m('script', { src: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/MathJax.js' })) + dom.push(m('script', {type: 'text/javascript'}, ` + ;(() => { + var mathblocks = document.querySelectorAll('code.language-math') + mathblocks.forEach(mathblock => { + mathblock.parentNode.replaceWith("$$" + mathblock.textContent + "$$") + }) + })() + `)) } if (state.content.mermaid) { dom.push(m('script', { @@ -336,6 +344,7 @@ var mathjax = ` inlineMath: [ ['$', '$'], ['\\\\(', '\\\\)'], + ['$\`', '\`$'], ], displayMath: [ ['$$', '$$'],