From 84d7bfc02fe4cbe53d744c0d181685f15df4fbad Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 6 Jul 2017 17:15:37 -0400 Subject: [PATCH] bug fixes --- assets/javascripts/initializers/discourse-math.js.es6 | 9 ++++++++- .../lib/discourse-markdown/discourse-math.js.es6 | 2 +- spec/pretty_text_spec.rb | 7 ++++--- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/assets/javascripts/initializers/discourse-math.js.es6 b/assets/javascripts/initializers/discourse-math.js.es6 index 1437251..ec47847 100644 --- a/assets/javascripts/initializers/discourse-math.js.es6 +++ b/assets/javascripts/initializers/discourse-math.js.es6 @@ -24,6 +24,12 @@ function ensureMathJax(){ function decorate(elem, isPreview){ const $elem= $(elem); + + if ($elem.data('applied-mathjax')){ + return; + } + $elem.data('applied-mathjax', true); + const tag = elem.tagName === "DIV" ? "div" : "span"; const display = tag === "div" ? "; mode=display" : ""; @@ -36,7 +42,7 @@ function decorate(elem, isPreview){ Em.run.later(this, ()=> { window.MathJax.Hub.Queue(() => { // don't bother processing previews removed from DOM - if (elem.parentElement.offsetParent !== null) { + if (elem.parentElement && elem.parentElement.offsetParent !== null) { window.MathJax.Hub.Typeset($math[0], ()=> { $elem.remove(); $mathWrapper.show(); @@ -51,6 +57,7 @@ function mathjax($elem) { if (mathElems.length > 0) { const isPreview = $elem.hasClass('d-editor-preview'); + ensureMathJax().then(()=>{ mathElems.each((idx,elem) => decorate(elem, isPreview)); }); diff --git a/assets/javascripts/lib/discourse-markdown/discourse-math.js.es6 b/assets/javascripts/lib/discourse-markdown/discourse-math.js.es6 index bfd8a40..77f2d2c 100644 --- a/assets/javascripts/lib/discourse-markdown/discourse-math.js.es6 +++ b/assets/javascripts/lib/discourse-markdown/discourse-math.js.es6 @@ -59,7 +59,7 @@ function inlineMath(state, silent) { if (found+1 <= posMax) { let next = state.src.charCodeAt(found+1); - if (!isSafeBoundary(next, state.md)) { + if (next && !isSafeBoundary(next, state.md)) { return false; } } diff --git a/spec/pretty_text_spec.rb b/spec/pretty_text_spec.rb index d1040e1..87d2bf9 100644 --- a/spec/pretty_text_spec.rb +++ b/spec/pretty_text_spec.rb @@ -31,12 +31,13 @@ describe PrettyText do end it 'can handle inline edge cases' do + expect(PrettyText.cook ",$+500\\$").not_to include('math') + expect(PrettyText.cook "$+500$").to include('math') expect(PrettyText.cook ",$+500$,").to include('math') expect(PrettyText.cook "200$ + 500$").not_to include('math') - expect(PrettyText.cook ",$+500\$x").not_to include('math') - expect(PrettyText.cook "y$+500\$").not_to include('math') + expect(PrettyText.cook ",$+500$x").not_to include('math') + expect(PrettyText.cook "y$+500$").not_to include('math') expect(PrettyText.cook "($ +500 $)").to include('math') - expect(PrettyText.cook ",$+500\$").not_to include('math') end it 'can handle inline math' do