Skip to content

Commit dd0d22c

Browse files
committed
Fix double typesetting issue
1 parent 87c2190 commit dd0d22c

File tree

7 files changed

+71
-65
lines changed

7 files changed

+71
-65
lines changed

BbCode/Math.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@ private static function loadMathjax(\XF\BbCode\Renderer\AbstractRenderer $render
1616
public static function renderMathTag($tagChildren, $tagOption, $tag,
1717
array $options, \XF\BbCode\Renderer\AbstractRenderer $renderer)
1818
{
19-
if (count($tagChildren) == 0)
19+
$pre = '$$\[';
20+
$post = '$$\]';
21+
if (strpos($tag['tag'], 'imath') === 0) {
22+
$pre = '$$\(';
23+
$post = '$$\)';
24+
}
25+
if (count($tagChildren) == 0
26+
|| !is_string($tagChildren[0])
27+
|| strlen($tagChildren[0]) == 0)
2028
return '';
2129
self::loadMathjax($renderer);
22-
return '$$\[' . $tagChildren[0] . '$$\]';
23-
}
24-
25-
public static function renderInlineMathTag($tagChildren, $tagOption, $tag,
26-
array $options, \XF\BbCode\Renderer\AbstractRenderer $renderer)
27-
{
28-
if (count($tagChildren) == 0)
29-
return '';
30-
self::loadMathjax($renderer);
31-
return '$$\(' . $tagChildren[0] . '$$\)';
30+
return $pre . $tagChildren[0] . $post;
3231
}
3332
}

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
1. You need [XenForo 2.x](https://xenforo.com/). You can setup a Scotch Box
66
environment as described in the [Xenforo 2.x Development
77
Documentation](https://xenforo.com/xf2-docs/dev/scotchbox/)
8-
2. Fork TwitchPiper
8+
2. Fork BbMath
99
3. Move in the `src/addon/` folder of your development enviroment and `mkdir Inforge && cd Inforge`
1010
4. `git clone https://github.com/<your-username>/BbMath && cd BbMath`
1111
5. `git remote add upstream https://github.com/InforgeNet/BbMath`
Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,54 @@
1-
window.MathJax = {
2-
loader: {
3-
load: ['ui/safe']
4-
},
5-
options: {
6-
enableMenu: false
7-
},
8-
tex: {
9-
inlineMath: [['$$\\(', '$$\\)']],
10-
displayMath: [['$$\\[', '$$\\]']],
11-
processEscapes: false,
12-
processRefs: true,
13-
processEnvironments: false,
14-
tags: 'ams'
15-
},
16-
svg: {
17-
fontCache: 'global'
18-
}
19-
};
1+
var BbMath = window.BbMath || {};
202

21-
(function() {
22-
var script = document.createElement('script');
23-
script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js';
24-
script.async = true;
25-
document.head.appendChild(script);
26-
})();
3+
!function($, window, document, _undefined)
4+
{
5+
"use strict";
276

28-
var retypesetDisabled = false;
7+
BbMath.MathJax = XF.create({
8+
options: {
9+
mathjax: {
10+
loader: {
11+
load: ['ui/safe'],
12+
},
13+
enableMenu: false,
14+
tex: {
15+
inlineMath: [['$$\\(', '$$\\)']],
16+
displayMath: [['$$\\[', '$$\\]']],
17+
processEscapes: false,
18+
processRefs: true,
19+
processEnvironments: false,
20+
tags: 'ams',
21+
},
22+
svg: {
23+
fontCache: 'global',
24+
},
25+
},
26+
mathjaxUrl: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js'
27+
},
2928

30-
function retypesetMathjax()
31-
{
32-
$(this).off('DOMSubtreeModified', retypesetMathjax);
33-
if (retypesetDisabled) {
34-
setTimeout(retypesetMathjax, 500);
35-
return;
36-
}
37-
retypesetDisabled = true;
38-
$(this).on('DOMSubtreeModified', retypesetMathjax);
39-
window.MathJax.typesetPromise().then(() => {
40-
retypesetDisabled = false;
41-
}).catch((err) => console.log(err.message));
42-
}
29+
__construct: function(options) {
30+
window.MathJax = this.options.mathjax;
31+
},
32+
33+
load: function() {
34+
XF.loadScript(this.options.mathjaxUrl, XF.proxy(this, 'loadSuccess'));
35+
},
4336

44-
$(window).on('load', function() {
45-
$('article.message, article.resourceBody-main, blockquote.message-body').each(function() {
46-
$(this).on('DOMSubtreeModified', retypesetMathjax);
37+
loadSuccess: function() {
38+
$(document).on('xf:layout', XF.proxy(this, 'retypeset'));
39+
},
40+
41+
retypeset: function() {
42+
MathJax.startup.promise = MathJax.startup.promise
43+
.then(() => MathJax.typesetPromise())
44+
.catch((err) => console.log('MathJax typeset failed: ' + err.message));
45+
return MathJax.startup.promise;
46+
},
47+
});
48+
49+
$(document).on('xf:page-load-complete', function() {
50+
var mathjax = new BbMath.MathJax();
51+
mathjax.load();
4752
});
48-
});
53+
}
54+
(jQuery, window, document);

_output/bb_codes/_metadata.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"imath.json": {
3-
"hash": "2608cc85133fd555483293fb02c28a5a"
3+
"hash": "76f85b2621baad104fe9d590e0fff036"
44
},
55
"math.json": {
6-
"hash": "0e73d0c917ba4a663b3fb8481e3e5287"
6+
"hash": "c31776ee8f321354e6fa827faec500cd"
77
}
88
}

_output/bb_codes/imath.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
"replace_html_email": "",
66
"replace_text": "",
77
"callback_class": "Inforge\\BbMath\\BbCode\\Math",
8-
"callback_method": "renderInlineMathTag",
8+
"callback_method": "renderMathTag",
99
"option_regex": "",
10-
"trim_lines_after": 1,
10+
"trim_lines_after": 0,
1111
"plain_children": true,
12-
"disable_smilies": false,
12+
"disable_smilies": true,
1313
"disable_nl2br": false,
1414
"disable_autolink": true,
1515
"allow_empty": false,

_output/bb_codes/math.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"option_regex": "",
1010
"trim_lines_after": 1,
1111
"plain_children": true,
12-
"disable_smilies": false,
12+
"disable_smilies": true,
1313
"disable_nl2br": false,
1414
"disable_autolink": true,
1515
"allow_empty": false,

addon.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
{
22
"legacy_addon_id": "",
33
"title": "[Inforge] BbMath",
4-
"description": "Add BBCode to support equations and inline math.",
5-
"version_id": 1000170,
6-
"version_string": "1.0.1",
4+
"description": "Add BBCode to support equations and inline math using LaTeX.",
5+
"version_id": 1000270,
6+
"version_string": "1.0.2",
77
"dev": "Inforge",
88
"dev_url": "https://www.inforge.net",
99
"faq_url": "https://github.com/InforgeNet/BbMath/wiki/Frequently-Asked-Questions",
1010
"support_url": "https://github.com/InforgeNet/BbMath/issues",
1111
"extra_urls": {
1212
"Forum": "https://www.inforge.net/forum",
13-
"GitHub": "https://github.com/InforgeNet/BbMath"
13+
"GitHub": "https://github.com/InforgeNet/BbMath",
14+
"Maintainer (Niccolò Scatena)": "mailto:[email protected]"
1415
},
1516
"require": {
1617
"XF": [

0 commit comments

Comments
 (0)