├── .editorconfig
├── .gitmodules
├── LICENSE
├── README.md
├── SimpleMathJax.php
├── SimpleMathJaxHooks.php
├── extension.json
└── resources
└── ext.SimpleMathJax.js
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | indent_style = tab
5 | indent_size = 4
6 | end_of_line = lf
7 | charset = utf-8
8 | trim_trailing_whitespace = true
9 | insert_final_newline = true
10 |
11 | [*.md]
12 | trim_trailing_whitespace = false
13 |
--------------------------------------------------------------------------------
/.gitmodules:
--------------------------------------------------------------------------------
1 | [submodule "resources/MathJax"]
2 | path = resources/MathJax
3 | url = https://github.com/mathjax/MathJax.git
4 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Jmnote
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | The SimpleMathJax extension enables MathJax, a Javascript library, for typesetting TeX formula in MediaWiki inside math environments.
2 |
3 | https://www.mediawiki.org/wiki/Extension:SimpleMathJax
4 |
5 |
6 | # Installation
7 | * git clone in extensions directory
8 | * Using CDN is recommended. Because it's much faster than using local resources in most cases. ("the benefits of using a CDN")
9 | ```Bash
10 | $ git clone https://github.com/jmnote/SimpleMathJax.git
11 | ```
12 |
13 | * (Optional) If you want to use not CDN but local mathjax scripts, you can use git clone recursive.
14 | ```Bash
15 | $ git clone --recursive https://github.com/jmnote/SimpleMathJax.git
16 | ```
17 |
18 | * LocalSettings.php
19 | ```PHP
20 | wfLoadExtension( 'SimpleMathJax' );
21 | ```
22 |
23 | # Optional Settings
24 | | Setting name | Description | default value | custom value example |
25 | | ------------------------ | -------------------------------- | ------------- | --------------------------- |
26 | | `$wgSmjUseCdn` | use CDN or local scripts | true | false |
27 | | `$wgSmjUseChem` | enable chem tag | true | false |
28 | | `$wgSmjEnableMenu` | MathJax.options.enableMenu | true | false |
29 | | `$wgSmjDisplayMath` | MathJax.tex.displayMath | [] | [['$$','$$'],['\\[','\\]']] |
30 | | `$wgSmjExtraInlineMath` | MathJax.tex.inlineMath | [] | [['\\(', '\\)']] |
31 | | `$wgSmjScale` | MathJax.chtml.scale | 1 | 1.5 |
32 | | `$wgSmjDisplayAlign` | MathJax.chtml.displayAlign | center | left |
33 | | `$wgSmjWrapDisplaystyle` | wrap with displaystyle | true | false |
34 |
35 | If you want to change font size, set `$wgSmjScale`.
36 | ```PHP
37 | wfLoadExtension( 'SimpleMathJax' );
38 | $wgSmjScale = 1.5;
39 | ```
40 |
41 | If you want to use local module, set `$wgSmjUseCdn`.
42 | ```PHP
43 | wfLoadExtension( 'SimpleMathJax' );
44 | $wgSmjUseCdn = false;
45 | ```
46 |
47 | If you want to enable some extra inlineMath symbol pairs, set `$wgSmjExtraInlineMath`.
48 | ```PHP
49 | wfLoadExtension( 'SimpleMathJax' );
50 | $wgSmjExtraInlineMath = [["$","$"],["\\(","\\)"]];
51 | ```
52 |
53 | If you want to disable MathJax context menu, set `$wgSmjEnableMenu`.
54 | ```PHP
55 | wfLoadExtension( 'SimpleMathJax' );
56 | $wgSmjEnableMenu = false;
57 | ```
58 |
59 | # Hooks
60 | The hook `SimpleMathJaxAttributes` is available to add attributes to the span around the math. This hook provides you with the opportunity to ensure that your own code does not interfere with MathJax's rendering of math.
61 |
62 | For instance, if Lingo's JS functions are called before MathJax is invoked, then it is possible that Lingo will change the text so that MathJax could no longer render the math.
63 |
64 | Lingo understands that [it should not touch anything inside an element with the class `noglossary`](https://www.mediawiki.org/wiki/Extension:Lingo#Excluding_text_from_markup) so the following code can be used to keep Lingo from ruining math:
65 | ```PHP
66 | $wfHook['SimpleMathJaxAttributes']
67 | = function ( array &attributes, string $tex ) {
68 | $attributes['class'] = 'noglossary';
69 | }
70 | ```
71 |
--------------------------------------------------------------------------------
/SimpleMathJax.php:
--------------------------------------------------------------------------------
1 | addJsConfigVars( 'wgSmjUseCdn', $wgSmjUseCdn );
10 | $wgOut->addJsConfigVars( 'wgSmjUseChem', $wgSmjUseChem );
11 | $wgOut->addJsConfigVars( 'wgSmjDisplayMath', $wgSmjDisplayMath );
12 | $wgOut->addJsConfigVars( 'wgSmjExtraInlineMath', $wgSmjExtraInlineMath );
13 | $wgOut->addJsConfigVars( 'wgSmjScale', $wgSmjScale );
14 | $wgOut->addJsConfigVars( 'wgSmjEnableMenu', $wgSmjEnableMenu );
15 | $wgOut->addJsConfigVars( 'wgSmjDisplayAlign', $wgSmjDisplayAlign );
16 | $wgOut->addModules( [ 'ext.SimpleMathJax' ] );
17 | $wgOut->addModules( [ 'ext.SimpleMathJax.mobile' ] ); // For MobileFrontend
18 |
19 | $parser->setHook( 'math', __CLASS__ . '::renderMath' );
20 | if( $wgSmjUseChem ) $parser->setHook( 'chem', __CLASS__ . '::renderChem' ); }
21 |
22 | public static function renderMath($tex, array $args, Parser $parser, PPFrame $frame ) {
23 | global $wgSmjWrapDisplaystyle;
24 | $tex = str_replace('\>', '\;', $tex);
25 | $tex = str_replace('<', '\lt ', $tex);
26 | $tex = str_replace('>', '\gt ', $tex);
27 | if( $wgSmjWrapDisplaystyle ) $tex = "\displaystyle{ $tex }";
28 | return self::renderTex($tex, $parser);
29 | }
30 |
31 | public static function renderChem($tex, array $args, Parser $parser, PPFrame $frame ) {
32 | return self::renderTex("\ce{ $tex }", $parser);
33 | }
34 |
35 | private static function renderTex($tex, $parser) {
36 | $hookContainer = MediaWiki\MediaWikiServices::getInstance()->getHookContainer();
37 | $attributes = [ "style" => "opacity:.5", "class" => "smj-container" ];
38 | $hookContainer->run( "SimpleMathJaxAttributes", [ &$attributes, $tex ] );
39 | $element = Html::Element( "span", $attributes, "[math]{$tex}[/math]" );
40 | return [$element, 'markerType'=>'nowiki'];
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/extension.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "SimpleMathJax",
3 | "version": "0.8.4",
4 | "author": "jmnote",
5 | "url": "https://www.mediawiki.org/wiki/Extension:SimpleMathJax",
6 | "description": "render TeX between
and
",
7 | "license-name": "GPL-2.0+",
8 | "type": "parserhook",
9 | "AutoloadClasses": {
10 | "SimpleMathJaxHooks": "SimpleMathJaxHooks.php"
11 | },
12 | "config": {
13 | "SmjUseCdn": {"value":true, "description":"true to load mathjax from CDN"},
14 | "SmjUseChem": {"value":true, "description":"true to enabled chem tag"},
15 | "SmjDisplayMath": {"value":[], "description":"MathJax.tex.displayMath"},
16 | "SmjExtraInlineMath": {"value":[], "description":"MathJax.tex.inlineMath"},
17 | "SmjScale": {"value":1, "description":"MathJax.chtml.scale"},
18 | "SmjEnableMenu": {"value":true, "description":"MathJax.options.enableMenu"},
19 | "SmjDisplayAlign": {"value":"left", "description":"MathJax.chtml.displayAlign"},
20 | "SmjWrapDisplaystyle": {"value":true, "description":"true to wrap with displaystyle"}
21 | },
22 | "Hooks": {
23 | "ParserFirstCallInit": "SimpleMathJaxHooks::onParserFirstCallInit"
24 | },
25 | "ResourceModules": {
26 | "ext.SimpleMathJax": {
27 | "scripts": ["resources/ext.SimpleMathJax.js"],
28 | "targets": ["desktop", "mobile"]
29 | }
30 | },
31 | "ResourceFileModulePaths": {
32 | "localBasePath": "",
33 | "remoteExtPath": "SimpleMathJax"
34 | },
35 | "manifest_version": 2
36 | }
37 |
--------------------------------------------------------------------------------
/resources/ext.SimpleMathJax.js:
--------------------------------------------------------------------------------
1 | mw.hook( 'wikipage.content' ).add( function ( $content ) {
2 | window.MathJax = {
3 | tex: {
4 | inlineMath: mw.config.get('wgSmjExtraInlineMath').concat([['[math]','[/math]']]),
5 | displayMath: mw.config.get('wgSmjDisplayMath'),
6 | packages: mw.config.get('wgSmjUseChem') ? {'[+]': ['mhchem']} : {},
7 | macros: {
8 | AA: "{\u00c5}",
9 | alef: "{\\aleph}",
10 | alefsym: "{\\aleph}",
11 | Alpha: "{\\mathrm{A}}",
12 | and: "{\\land}",
13 | ang: "{\\angle}",
14 | Bbb: "{\\mathbb}",
15 | Beta: "{\\mathrm{B}}",
16 | bold: "{\\mathbf}",
17 | bull: "{\\bullet}",
18 | C: "{\\mathbb{C}}",
19 | Chi: "{\\mathrm{X}}",
20 | clubs: "{\\clubsuit}",
21 | cnums: "{\\mathbb{C}}",
22 | Complex: "{\\mathbb{C}}",
23 | coppa: "{\u03D9}",
24 | Coppa: "{\u03D8}",
25 | Dagger: "{\\ddagger}",
26 | Digamma: "{\u03DC}",
27 | darr: "{\\downarrow}",
28 | dArr: "{\\Downarrow}",
29 | Darr: "{\\Downarrow}",
30 | dashint: "{\\unicodeInt{x2A0D}}",
31 | ddashint: "{\\unicodeInt{x2A0E}}",
32 | diamonds: "{\\diamondsuit}",
33 | empty: "{\\emptyset}",
34 | Epsilon: "{\\mathrm{E}}",
35 | Eta: "{\\mathrm{H}}",
36 | euro: "{\u20AC}",
37 | exist: "{\\exists}",
38 | geneuro: "{\u20AC}",
39 | geneuronarrow: "{\u20AC}",
40 | geneurowide: "{\u20AC}",
41 | H: "{\\mathbb{H}}",
42 | hAar: "{\\Leftrightarrow}",
43 | harr: "{\\leftrightarrow}",
44 | Harr: "{\\Leftrightarrow}",
45 | hearts: "{\\heartsuit}",
46 | image: "{\\Im}",
47 | infin: "{\\infty}",
48 | Iota: "{\\mathrm{I}}",
49 | isin: "{\\in}",
50 | Kappa: "{\\mathrm{K}}",
51 | koppa: "{\u03DF}",
52 | Koppa: "{\u03DE}",
53 | lang: "{\\langle}",
54 | larr: "{\\leftarrow}",
55 | Larr: "{\\Leftarrow}",
56 | lArr: "{\\Leftarrow}",
57 | lrarr: "{\\leftrightarrow}",
58 | Lrarr: "{\\Leftrightarrow}",
59 | lrArr: "{\\Leftrightarrow}",
60 | Mu: "{\\mathrm{M}}",
61 | N: "{\\mathbb{N}}",
62 | natnums: "{\\mathbb{N}}",
63 | Nu: "{\\mathrm{N}}",
64 | O: "{\\emptyset}",
65 | oiint: "{\\unicodeInt{x222F}}",
66 | oiiint: "{\\unicodeInt{x2230}}",
67 | ointctrclockwise: "{\\unicodeInt{x2233}}",
68 | officialeuro: "{\u20AC}",
69 | Omicron: "{\\mathrm{O}}",
70 | or: "{\\lor}",
71 | P: "{\u00B6}",
72 | pagecolor: ["",1],
73 | part: "{\\partial}",
74 | plusmn: "{\\pm}",
75 | Q: "{\\mathbb{Q}}",
76 | R: "{\\mathbb{R}}",
77 | rang: "{\\rangle}",
78 | rarr: "{\\rightarrow}",
79 | Rarr: "{\\Rightarrow}",
80 | rArr: "{\\Rightarrow}",
81 | real: "{\\Re}",
82 | reals: "{\\mathbb{R}}",
83 | Reals: "{\\mathbb{R}}",
84 | Rho: "{\\mathrm{P}}",
85 | sdot: "{\\cdot}",
86 | sampi: "{\u03E1}",
87 | Sampi: "{\u03E0}",
88 | sect: "{\\S}",
89 | spades: "{\\spadesuit}",
90 | stigma: "{\u03DB}",
91 | Stigma: "{\u03DA}",
92 | sub: "{\\subset}",
93 | sube: "{\\subseteq}",
94 | supe: "{\\supseteq}",
95 | Tau: "{\\mathrm{T}}",
96 | textvisiblespace: "{\u2423}",
97 | thetasym: "{\\vartheta}",
98 | uarr: "{\\uparrow}",
99 | uArr: "{\\Uparrow}",
100 | Uarr: "{\\Uparrow}",
101 | unicodeInt: ["{\\mathop{\\vcenter{\\mathchoice{\\huge\\unicode{#1}\\,}{\\unicode{#1}}{\\unicode{#1}}{\\unicode{#1}}}\\,}\\nolimits}", 1],
102 | varcoppa: "{\u03D9}",
103 | varstigma: "{\u03DB}",
104 | varointclockwise: "{\\unicodeInt{x2232}}",
105 | vline: ["{\\smash{\\large\\lvert #1}", 0],
106 | weierp: "{\\wp}",
107 | Z: "{\\mathbb{Z}}",
108 | Zeta: "{\\mathrm{Z}}"
109 | }
110 | },
111 | chtml: {
112 | scale: mw.config.get('wgSmjScale'),
113 | displayAlign: mw.config.get('wgSmjDisplayAlign')
114 | },
115 | loader: {
116 | load: mw.config.get('wgSmjUseChem') ? ['[tex]/mhchem'] : []
117 | },
118 | startup: {
119 | pageReady: () => {
120 | return MathJax.startup.defaultPageReady().then(() => {
121 | $(".MathJax").parent().css('opacity',1);
122 | });
123 | }
124 | }
125 | };
126 | (function () {
127 | var script = document.createElement('script');
128 | script.src = mw.config.get('wgSmjUseCdn')
129 | ? 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js'
130 | : mw.config.get('wgExtensionAssetsPath') + '/SimpleMathJax/resources/MathJax/es5/tex-chtml.js';
131 | script.async = true;
132 | document.head.appendChild(script);
133 | })();
134 | });
135 |
--------------------------------------------------------------------------------