diff --git a/dist/vue3-mathjax.es.js b/dist/vue3-mathjax.es.js index a05b1e5..9114b68 100644 --- a/dist/vue3-mathjax.es.js +++ b/dist/vue3-mathjax.es.js @@ -1,4 +1,4 @@ -import { defineComponent, onMounted, getCurrentInstance, nextTick, onUpdated, renderSlot } from "vue"; +import { defineComponent, onMounted, getCurrentInstance, nextTick, onUpdated, onUnmounted, renderSlot } from "vue"; let mathJaxInjected = false; let mathJaxReady = false; let pendingQueue = []; @@ -6,6 +6,12 @@ function mathJax() { return window.MathJax; } function injectMathJaxScript() { + const instance = mathJax(); + if (!instance || !instance.version) { + mathJaxInjected = false; + mathJaxReady = false; + pendingQueue.splice(0, pendingQueue.length); + } if (!mathJaxInjected) { const mathJaxScript = document.createElement("script"); mathJaxScript.src = "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"; @@ -43,6 +49,7 @@ function initMathJax(options = {}, callback) { if (v.type == "async") v.callback(); }); + pendingQueue.splice(0, pendingQueue.length); callback && callback(); } }, @@ -104,9 +111,9 @@ const _sfc_main = /* @__PURE__ */ defineComponent({ setup(__props) { let el; const renderMathJax = async () => { - if (el) { - await renderByMathJax(el); - } + if (!el) + return; + await renderByMathJax(el); }; onMounted(() => { var _a, _b, _c; @@ -119,9 +126,10 @@ const _sfc_main = /* @__PURE__ */ defineComponent({ onUpdated(() => { var _a, _b, _c; el = (_c = (_b = (_a = getCurrentInstance()) == null ? void 0 : _a.vnode) == null ? void 0 : _b.el) == null ? void 0 : _c.parentNode; - nextTick(() => { - renderMathJax(); - }); + renderMathJax(); + }); + onUnmounted(() => { + el = null; }); return (_ctx, _cache) => { return renderSlot(_ctx.$slots, "default"); diff --git a/dist/vue3-mathjax.umd.js b/dist/vue3-mathjax.umd.js index ffaa55b..900868d 100644 --- a/dist/vue3-mathjax.umd.js +++ b/dist/vue3-mathjax.umd.js @@ -1 +1 @@ -(function(e,i){typeof exports=="object"&&typeof module!="undefined"?module.exports=i(require("vue")):typeof define=="function"&&define.amd?define(["vue"],i):(e=typeof globalThis!="undefined"?globalThis:e||self,e["vue-mathjax"]=i(e.Vue))})(this,function(e){"use strict";let i=!1,p=!1,s=[];function h(){return window.MathJax}function y(){if(!i){const t=document.createElement("script");t.src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js",t.async=!0,document.head.appendChild(t),i=!0}}function d(t={},a){const n=Object.assign({},{tex:{inlineMath:[["$","$"]],displayMath:[["$$","$$"]],processEnvironments:!0,processRefs:!0},options:{skipHtmlTags:["script","noscript","style","textarea","pre","code","annotation","annotation-xml"],ignoreHtmlClass:"tex2jax_ignore"},startup:{pageReady:()=>{p=!0,h().typeset(s.map(r=>r.el)),s.forEach(r=>{r.type=="async"&&r.callback()}),a&&a()}},svg:{fontCache:"global"}},t);window.MathJax=n,y()}function l(t){if(!p){Array.isArray(t)?s.concat(t.map(a=>({type:"sync",el:a}))):s.push({type:"sync",el:t});return}window.MathJax.typeset(Array.isArray(t)?t:[t])}async function u(t){return new Promise((a,c)=>{if(p)return window.MathJax.typesetPromise(Array.isArray(t)?t:[t]);if(Array.isArray(t)){for(let n=0;n{a&&await u(a)};return e.onMounted(()=>{var n,r,o;d(),a=(o=(r=(n=e.getCurrentInstance())==null?void 0:n.vnode)==null?void 0:r.el)==null?void 0:o.parentNode,e.nextTick(()=>{c()})}),e.onUpdated(()=>{var n,r,o;a=(o=(r=(n=e.getCurrentInstance())==null?void 0:n.vnode)==null?void 0:r.el)==null?void 0:o.parentNode,e.nextTick(()=>{c()})}),(n,r)=>e.renderSlot(n.$slots,"default")}});function m(t){t.component("math-jax",f)}var x={install:m,MathJax:f,initMathJax:d,renderByMathJax:u,renderByMathJaxSync:l};return x}); +(function(n,i){typeof exports=="object"&&typeof module!="undefined"?module.exports=i(require("vue")):typeof define=="function"&&define.amd?define(["vue"],i):(n=typeof globalThis!="undefined"?globalThis:n||self,n["vue-mathjax"]=i(n.Vue))})(this,function(n){"use strict";let i=!1,c=!1,s=[];function d(){return window.MathJax}function h(){const e=d();if((!e||!e.version)&&(i=!1,c=!1,s.splice(0,s.length)),!i){const t=document.createElement("script");t.src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js",t.async=!0,document.head.appendChild(t),i=!0}}function u(e={},t){const a=Object.assign({},{tex:{inlineMath:[["$","$"]],displayMath:[["$$","$$"]],processEnvironments:!0,processRefs:!0},options:{skipHtmlTags:["script","noscript","style","textarea","pre","code","annotation","annotation-xml"],ignoreHtmlClass:"tex2jax_ignore"},startup:{pageReady:()=>{c=!0,d().typeset(s.map(r=>r.el)),s.forEach(r=>{r.type=="async"&&r.callback()}),s.splice(0,s.length),t&&t()}},svg:{fontCache:"global"}},e);window.MathJax=a,h()}function y(e){if(!c){Array.isArray(e)?s.concat(e.map(t=>({type:"sync",el:t}))):s.push({type:"sync",el:e});return}window.MathJax.typeset(Array.isArray(e)?e:[e])}async function l(e){return new Promise((t,p)=>{if(c)return window.MathJax.typesetPromise(Array.isArray(e)?e:[e]);if(Array.isArray(e)){for(let a=0;a{!t||await l(t)};return n.onMounted(()=>{var a,r,o;u(),t=(o=(r=(a=n.getCurrentInstance())==null?void 0:a.vnode)==null?void 0:r.el)==null?void 0:o.parentNode,n.nextTick(()=>{p()})}),n.onUpdated(()=>{var a,r,o;t=(o=(r=(a=n.getCurrentInstance())==null?void 0:a.vnode)==null?void 0:r.el)==null?void 0:o.parentNode,p()}),n.onUnmounted(()=>{t=null}),(a,r)=>n.renderSlot(a.$slots,"default")}});function m(e){e.component("math-jax",f)}var x={install:m,MathJax:f,initMathJax:u,renderByMathJax:l,renderByMathJaxSync:y};return x}); diff --git a/package.json b/package.json index 11fbc3f..e25ab38 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "vue3-mathjax", "private": false, - "version": "0.0.1", + "version": "0.0.2", "main": "dist/vue3-mathjax.umd.js", "module": "dist/vue3-mathjax.es.js", "types": "types/index.d.ts", diff --git a/src/App.vue b/src/App.vue index e80d3d0..80b4a45 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,14 +1,39 @@ + +