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 • 6.26 kB
JavaScript
let currentThemeName="";function scrollToSubHash(e){let t=!0;if(e){const s=document.getElementById(e);s&&(t=!1,window.setTimeout((()=>{s.scrollIntoView({behavior:"smooth"})}),300))}t&&window.scrollTo({top:0,behavior:"smooth"})}function starter(e,t){let s="Home",n="https://unpkg.com/smartdown/dist/",o=n+"lib/resources/",r=window.location.origin+window.location.pathname,i="",a="gist/",l="#smartdown-output",c=null,d={cloud:"/gallery/resources/cloud.jpg",badge:"/gallery/resources/badge.svg",hypercube:"/gallery/resources/Hypercube.svg",StalactiteStalagmite:"/gallery/resources/StalactiteStalagmite.svg",church:"/gallery/resources/church.svg",lighthouse:"/gallery/resources/lighthouse.svg",barn:"/gallery/resources/barn.svg","medieval-gate":"/gallery/resources/medieval-gate.svg"},m={},u="",h="",w="";"string"==typeof smartdownBaseURL&&(n=smartdownBaseURL),"string"==typeof smartdownResourceURL&&(o=smartdownResourceURL),"string"==typeof smartdownDefaultHome&&(s=smartdownDefaultHome),"string"==typeof smartdownGistPathPrefix&&(i=smartdownGistPathPrefix),"string"==typeof smartdownGistHashPrefix&&(a=smartdownGistHashPrefix),"string"==typeof smartdownRawPrefix&&(r=smartdownRawPrefix),"string"==typeof smartdownOutputDivSelector&&(l=smartdownOutputDivSelector),"function"==typeof smartdownPostLoadMutator&&(c=smartdownPostLoadMutator),"object"==typeof smartdownMedia&&(d=Object.assign(d,smartdownMedia));let f=r;function p(e,t,n,o){const i=new XMLHttpRequest;i.addEventListener("load",(function(){!function(e,t,n,o,i){c&&(e=c(e,t,n,s)),m=smartdown.partitionMultipart(e);const a=document.getElementById(o);u="#"+t,f!==r&&(u="#"+n);let l=window.location.search;""!==currentThemeName&&(l=`?theme=${currentThemeName}`);let d="_default_";if(0!==t.indexOf("http:")&&0!==t.indexOf("https:")&&t.indexOf(":")>=0){const e=t.split(":");d=e[e.length-1]}smartdown.setHome(m[d],a,(function(){let e="#"+t;i&&"undefined"!==i&&(e+="#"+i),window.history.pushState({},"",window.location.pathname+e+l),scrollToSubHash(i),a.id||(a.id="smartdown-output-"+String(Math.random()).slice(2)),smartdown.startAutoplay(a)}))}(this.responseText,e,t,n,o)})),i.open("GET",t),i.send()}function g(e,t){let s=null,n=null;0!==e.indexOf("#")&&(e="#"+e);const o=e.split("#");if(o.length>1){if(s=o[1],n=o[2],n&&(n=n.split("?")[0]),""!==u&&"#"+s===u)return window.history.replaceState({},"",window.location.pathname+e),void scrollToSubHash(n)}else console.log("... illformatted cardKeyWithHash",e);const c=new RegExp("^/?("+i+")?"+a+"([^/]+)/([^/]+)(/(\\w*))?$","g").exec(s);if(c){h=c[2],w=c[3];const e=c[5]||"Home";s=e}const d=m[s];if(d){const e=document.querySelectorAll(l)[0];smartdown.setHome(d,e,(function(){e.id||(e.id="smartdown-output-"+String(Math.random()).slice(2)),smartdown.startAutoplay(e)}))}else if(0===s.indexOf("http")){h="",w="";const e=s.lastIndexOf("/");e>0&&(f=s.slice(0,e+1)),p(s,s,t,n)}else if(0===s.indexOf("/"))h="",w="",f=r,p(s,s,t);else if(""!==h&&""!==w){const e="https://api.github.com/gists/"+w,o=new XMLHttpRequest;o.addEventListener("load",(function(){const o=JSON.parse(this.responseText).files[s+".md"];if(o){const e=o.raw_url;s=a+h+"/"+w+"/"+s,f="https://gist.githubusercontent.com/"+h+"/"+w+"/raw/",p(s,e,t,n)}else console.log('Unable to locate Gist for "',s,'" ',e)})),o.open("GET",e),o.send()}else if(s.indexOf(":")>=0){const e=s.split(":").slice(0,-1).join(":");p(s,f+(""===e?"":e+".md"),t,n)}else{h="",w="";const e=s.endsWith(".md")?"":".md";let o=f+s+e;f.endsWith("/public/smartdown/")&&(o=f.split("/").slice(0,-2).join("/")+"/"+s+e),p(s,o,t,n)}}function x(e){const t=document.getElementById("smartdown-outer-container");t&&""!==e&&([...t.classList].forEach((e=>{0===e.indexOf("smartdown-theme-")&&t.classList.remove(e)})),t.classList.add(`smartdown-theme-${e}`))}const y=smartdown.defaultCalcHandlers;function b(){let e=f,t=window.location.hash;const s=t.indexOf("?");if(s>=0&&(t=t.slice(0,s)),i.length>0&&window.location.pathname.endsWith(i)){const s=new RegExp("^/?("+i+")?"+a+"([^/]+)/([^/]+)(/(\\w*))?$","g").exec(t);s&&(e="https://gist.githubusercontent.com/"+s[2].replace("#","")+"/"+s[3]+"/raw/")}else if(a.length>0&&0===t.indexOf("#"+a)){const s=new RegExp("^#"+a+"([^/]+)/([^/]+)(/(\\w*))?$","g").exec(t);s&&(e="https://gist.githubusercontent.com/"+s[1]+"/"+s[2]+"/raw/")}else if(0===t.indexOf("#https://gist.githubusercontent.com/")){const s=new RegExp("^#https://gist.githubusercontent.com/([^/]+)/([^/]+)/.*$","g").exec(t);s&&(e="https://gist.githubusercontent.com/"+s[1]+"/"+s[2]+"/raw/")}return e}const O=[{prefix:"/block/",replace:b},{prefix:"block/",replace:b},{prefix:"assets/",replace:r+"assets/"},{prefix:"/assets/",replace:r+"assets/"},{prefix:"content/",replace:r+"content/"},{prefix:"/content/",replace:r+"content/"},{prefix:"/gallery/resources/",replace:""===o?"/gallery/resources/":o},{prefix:"/gallery/DataElements.csv",replace:"/smartdown/"===n?"/smartdown/gallery/DataElements.csv":"/gallery/DataElements.csv"},{prefix:"/resources/",replace:""===o?"/resources/":o}];window.onhashchange=function(e){const t=e.oldURL,n=t.indexOf("#"),o=t.indexOf("?");let r="";o>=0&&(r=n>o?t.slice(o,n-1):t.slice(o));let i=window.location.hash,a="";const c=i.indexOf("?");if(c>=0&&(a=i.slice(c+1),i=i.slice(0,c),0===a.indexOf("theme=")&&(currentThemeName=a.slice(6),x(currentThemeName))),u===i)console.log("...locationHashChanged INHIBIT",window.location.hash,i,u),scrollToSubHash();else{let t=i.slice(1);""===t?t=s:-1===t.indexOf("/")&&(h="",w="",t="#"+t);let n=null;const o=t.split("#");o.length>1&&(t=o[1],n=o[2],""===t&&(t=u),t=t+"#"+n+r),g(t,document.querySelectorAll(l)[0].id),e.preventDefault(),e.stopImmediatePropagation()}return!1},t||(t=function(){!function(e){let t=window.location.hash;if(e){const s=t.split("/"),n=e.split("/");t=e,4===n.length&&4===s.length&&(n[3]=s[3],t=n.join("/"))}let n="";const o=t.indexOf("?");if(o>=0){n=t.slice(o+1),t=t.slice(0,o);const e=n.indexOf("?");e>=0&&(n=n.slice(0,e));const s=new URLSearchParams(n);currentThemeName=s.get("theme")||""}if(""===currentThemeName){const e=window.location.search,t=new URLSearchParams(e);currentThemeName=t.get("theme")||""}x(currentThemeName),""===t&&(t=s),g(t,document.querySelectorAll(l)[0].id)}(e)}),smartdown.initialize(d,n,t,g,y,O)}window.smartdownStarter=starter;