-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MathJax for proper rendering of MahtJax content
- Loading branch information
Showing
14 changed files
with
177 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -314,4 +314,5 @@ pyrightconfig.json | |
|
||
.vscode | ||
output | ||
tmp | ||
tmp | ||
scraper/src/libretexts2zim/mathjax |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[files.assets.config] | ||
target_dir="src/libretexts2zim" | ||
|
||
[files.assets.actions."mathjax"] | ||
action="extract_items" | ||
source="https://github.com/mathjax/MathJax/archive/refs/tags/3.2.2.zip" | ||
zip_paths=["MathJax-3.2.2"] | ||
target_paths=["mathjax"] | ||
remove = ["mathjax/.github","mathjax/.gitignore","mathjax/.travis.yml","mathjax/bower.json","mathjax/composer.json","mathjax/CONTRIBUTING.md","mathjax/package.json"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
content | ||
content | ||
mathjax |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
Directive to handle DOM manipulation to add/remove MathJax everytime the page change | ||
This is a bit hackhish but the only reliable solution found so far, and probably the | ||
most robust one. | ||
The dynamic behavior of removing / adding back MathJax is wanted/necessary because we | ||
need to dynamically set the PageIndex macro to dynamically display proper figures | ||
/ equations / ... numbering. | ||
This directive MUST be used only once in the whole Vue.JS application. | ||
MathJax settings are an adaptation of libretexts.org settings, for MathJax 3 (including | ||
extensions now removed or not yet supported or included by default). | ||
*/ | ||
import type { DirectiveBinding } from 'vue' | ||
|
||
let front: string | undefined = undefined | ||
|
||
const frontFromTitle = function (title: string): string { | ||
// Computes front value from page title. | ||
// E.g. if page title is `1.2: The Scientific Method` then front is `1.2.` | ||
let front: string = '' | ||
if (title.includes(':')) { | ||
front = title.split(':')[0] | ||
if (front.includes('.')) { | ||
const parts: string[] = front.split('.') | ||
front = parts.map((int) => (int.includes('0') ? parseInt(int, 10) : int)).join('.') | ||
} | ||
front += '.' | ||
} | ||
return front | ||
} | ||
|
||
const removeMathJax = function () { | ||
const script = document.getElementById('mathjax-script') | ||
if (script) script.remove() | ||
if (window.MathJax) delete window.MathJax | ||
} | ||
|
||
const addMathJax = function (front: string) { | ||
window.MathJax = { | ||
section: front, | ||
tex: { | ||
tags: 'all', | ||
macros: { | ||
PageIndex: ['{' + front + '#1}'.toString(), 1] | ||
}, | ||
autoload: { | ||
color: [], | ||
colorv2: ['color'] | ||
}, | ||
packages: { '[+]': ['noerrors', 'mhchem', 'tagFormat', 'color', 'cancel'] } | ||
}, | ||
loader: { | ||
load: ['[tex]/noerrors', '[tex]/mhchem', '[tex]/tagFormat', '[tex]/colorv2', '[tex]/cancel'] | ||
}, | ||
svg: { | ||
scale: 0.85 | ||
}, | ||
options: { | ||
menuOptions: { | ||
settings: { | ||
zoom: 'Double-Click', | ||
zscale: '150%' | ||
} | ||
} | ||
} | ||
} | ||
const script = document.createElement('script', { | ||
id: 'mathjax-script' | ||
} as ElementCreationOptions) | ||
script.src = './mathjax/es5/tex-svg.js' | ||
script.async = false | ||
document.head.appendChild(script) | ||
} | ||
|
||
const vMathJax = { | ||
mounted(el: HTMLElement, binding: DirectiveBinding) { | ||
front = frontFromTitle(binding.value.title) | ||
removeMathJax() // "just-in-case" | ||
addMathJax(front) | ||
}, | ||
updated(el: HTMLElement, binding: DirectiveBinding) { | ||
// Reload MathJax only if title has changed (allow to debounce update which might be | ||
// called multiple times) | ||
const new_front = frontFromTitle(binding.value.title) | ||
if (new_front == front) { | ||
return | ||
} | ||
front = new_front | ||
removeMathJax() | ||
addMathJax(front) | ||
}, | ||
unmounted() { | ||
removeMathJax() | ||
} | ||
} | ||
|
||
export default vMathJax |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// declare window.MathJax so that we can manipulate it without TS errors | ||
declare global { | ||
interface Window { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
MathJax: any | ||
} | ||
} | ||
|
||
export {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters