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.

114 lines (97 loc) 3.2 kB
/* eslint-disable */ /* global smartdown */ var defaultHome = 'Home'; var baseURL = 'https://smartdown.site/'; if (typeof smartdownBaseURL === 'string') { baseURL = smartdownBaseURL; } if (typeof smartdownDefaultHome === 'string') { defaultHome = smartdownDefaultHome; } /* Stuff needed for inline-mode */ var scriptMap; function cardLoadedInline(sourceText, cardKey, cardURL) { document.body.scrollTop = 0; // For Chrome, Safari and Opera document.documentElement.scrollTop = 0; // For IE and Firefox /* eslint no-invalid-this: 0 */ multiparts = smartdown.partitionMultipart(sourceText); var output = document.getElementById('smartdown-output'); smartdown.setHome(multiparts._default_, output, function() { smartdown.startAutoplay(output); inhibitHash = '#' + cardKey; window.location.hash = inhibitHash; }); } function relativeCardLoaderInline(cardKey) { var part = multiparts[cardKey]; if (part) { var output = document.getElementById('smartdown-output'); smartdown.setHome(part, output, function() { smartdown.startAutoplay(output); }); } else { var cardURL = window.location.origin + window.location.pathname + cardKey + '.md'; if (cardKey.indexOf('http') === 0 || cardKey.indexOf('/') === 0) { cardURL = cardKey; var oReq = new XMLHttpRequest(); oReq.addEventListener("load", function() { cardLoadedInline(this.responseText, cardKey, cardURL); }); oReq.open("GET", cardURL); oReq.send(); } else { if (!cardKey.endsWith('.md')) { cardKey += '.md'; } var text = scriptMap[cardKey]; text = smartdown.decodeInlineScript(text); cardLoadedInline(text, cardKey, cardURL); } } } function buildScriptMapThenLoadHome() { smartdown.loadCardsFromDocumentScripts(); scriptMap = {}; smartdown.smartdownScripts.forEach(function(s) { var text = s.text; if (false) { if (s.id === 'README.md') { text += '\n\n---\n\n[Back to Home](:@Home)\n\n'; } } scriptMap[s.id] = text; }); loadHome(); } /* EndStuff needed for inline-mode */ var svgIcons = { 'hypercube': baseURL + '/lib/resources/Hypercube.svg', 'StalactiteStalagmite': baseURL + '/lib/resources/StalactiteStalagmite.svg', 'church': baseURL + '/lib/resources/church.svg', 'lighthouse': baseURL + '/lib/resources/lighthouse.svg', 'barn': baseURL + '/lib/resources/barn.svg', 'medieval-gate': baseURL + '/lib/resources/medieval-gate.svg' }; var multiparts = {}; var inhibitHash = ''; function loadHome() { var hash = window.parent.location.hash || window.location.hash; if (hash === '') { hash = defaultHome; } hash = hash.replace(/#/g, ''); relativeCardLoaderInline(hash); } var calcHandlers = smartdown.defaultCalcHandlers; const replace = window.location.origin + window.location.pathname; const linkRules = [ ]; function locationHashChanged() { if (inhibitHash !== location.hash) { // relativeCardLoaderInline(location.hash.slice(1)); } } window.onhashchange = locationHashChanged; smartdown.initialize(svgIcons, baseURL, buildScriptMapThenLoadHome, relativeCardLoaderInline, calcHandlers, linkRules);