UNPKG

smartdown

Version:

A library for translating, rendering and interacting with Smartdown documents. Smartdown is an extension of Markdown that provides richer media support and reactive programming capability.

1 lines 3.1 kB
const smartdown=window.smartdown;function calcWikidataImageUtil(e,n,a,t){let s=a=decodeURIComponent(a);if(0===a.indexOf("[")){const e=JSON.parse(a);Array.isArray(e)&&(s="",e.forEach((function(n,a){s+=n,a<e.length-1&&(s+="|")})))}const o=new XMLHttpRequest;o.addEventListener("load",(function(){const n=this.responseText,a=JSON.parse(n).query.pages,s=[];a.forEach((function(n){e&&n.thumbnail?s.push(n.thumbnail.source):!e&&n.original&&s.push(n.original.source)})),s.elementType="image",t(s)}));let i="https://en.wikipedia.org/w/api.php?action=query&prop=pageimages%7Cpageterms&format=json&origin=*&wbptterms=description&redirects=&formatversion=2";i+="&titles="+encodeURI(s),i+="&piprop=thumbnail%7Cname%7Coriginal&pithumbsize=300",o.open("GET",i),o.send()}function calcWikidataImages(e,n,a){return calcWikidataImageUtil(!1,e,n,a)}function calcWikidataThumbs(e,n,a){return calcWikidataImageUtil(!0,e,n,a)}function calcWikidata(e,n,a){let t=n=decodeURIComponent(n);if(0===n.indexOf("[")){const e=JSON.parse(n);Array.isArray(e)&&(t="",e.forEach((function(n,a){t+=n,a<e.length-1&&(t+="|")})))}const s=new XMLHttpRequest;s.addEventListener("load",(function(){const e=this.responseText,n=JSON.parse(e).query.pages,t=[];window.lodashEach(n,(function(e){t.push({title:e.displaytitle,url:e.canonicalurl})})),t.elementType="title/url",a(t)}));let o="https://en.wikipedia.org/w/api.php?action=query&format=json&prop=info&origin=*&redirects=";o+="&titles="+encodeURI(t),o+="&inprop=displaytitle%7Curl",s.open("GET",o),s.send()}const tsvCalcHandlerCache={};function calcTSV(e,n,a){0===(n=decodeURIComponent(n)).indexOf("/http")&&(n=n.slice(1));const t=smartdown.expandHrefWithLinkRules(n,smartdown.linkRules);if(smartdown.d3){const n="CACHE_"+e+"_"+t;let s=tsvCalcHandlerCache[n];s?s.inProgressHandlers.length>0?s.inProgressHandlers.push(a):a(s.cachedResult):(s=tsvCalcHandlerCache[n]={inProgressHandlers:[a],cachedResult:null},smartdown.axios.get(t).then((function(e){const n=smartdown.d3.tsvParseRows(e.data);for(s.cachedResult=n;s.inProgressHandlers.length>0;)s.inProgressHandlers.pop()(n)})).catch((function(e){console.log("err",e)})))}else smartdown.ensureExtension("d3",(function(){calcTSV(e,t,a)}))}const csvCalcHandlerCache={};function calcCSV(e,n,a){0===(n=decodeURIComponent(n)).indexOf("/http")&&(n=n.slice(1));const t=smartdown.expandHrefWithLinkRules(n,smartdown.linkRules);if(smartdown.d3){const n="CACHE_"+e+"_"+t;let s=csvCalcHandlerCache[n];s?s.inProgressHandlers.length>0?(console.log("MISS cacheKey adding handler",n),s.inProgressHandlers.push(a)):a(s.cachedResult):(console.log("MISS cacheKey",n),s=csvCalcHandlerCache[n]={inProgressHandlers:[a],cachedResult:null},smartdown.axios.get(t).then((function(e){const n=smartdown.d3.csvParseRows(e.data);for(s.cachedResult=n;s.inProgressHandlers.length>0;)s.inProgressHandlers.pop()(n)})).catch((function(e){console.log("err",e)})))}else smartdown.ensureExtension("d3",(function(){calcCSV(e,t,a)}))}smartdown.defaultCalcHandlers={wikidataImages:calcWikidataImages,wikidataThumbs:calcWikidataThumbs,wikidata:calcWikidata,tsv:calcTSV,csv:calcCSV};