-
-
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.
Merge pull request #31 from openzim/add_mathjax
Add mathjax to properly render mathjax content
- Loading branch information
Showing
14 changed files
with
170 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,14 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import mathjaxService from '../mathjax' | ||
|
||
describe('MathJaxService', () => { | ||
it('computes front 1.2. properly', () => { | ||
expect(mathjaxService.frontFromTitle('1.2: The Scientific Method')).toBe('1.2.') | ||
}) | ||
it('computes front 1.3. properly', () => { | ||
expect(mathjaxService.frontFromTitle('1.3: The Foo Method')).toBe('1.3.') | ||
}) | ||
it('computes front 1. properly', () => { | ||
expect(mathjaxService.frontFromTitle('1: The Title Method')).toBe('1.') | ||
}) | ||
}) |
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,79 @@ | ||
/* | ||
Service to handle DOM manipulation to add/remove MathJax everytime needed. | ||
This is a bit hackhish to remove and and back MathJax, but it is 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. | ||
MathJax settings are an adaptation of libretexts.org settings, for MathJax 3 (including | ||
extensions now removed or not yet supported or included by default). | ||
*/ | ||
|
||
class MathJaxService { | ||
front: string | undefined = undefined | ||
|
||
frontFromTitle(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 | ||
} | ||
|
||
removeMathJax() { | ||
const script = document.getElementById('mathjax-script') | ||
if (script) script.remove() | ||
if (window.MathJax) delete window.MathJax | ||
} | ||
|
||
addMathJax(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' | ||
document.head.appendChild(script) | ||
} | ||
} | ||
|
||
const mathjaxService = new MathJaxService() | ||
Object.freeze(mathjaxService) | ||
|
||
export default mathjaxService |
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