added a javascript build pipeline for transpiling and minifying; updated readme

This commit is contained in:
Robert Clarke 2019-05-08 09:54:41 +01:00
parent f5420dac1e
commit 4847d3f171
17 changed files with 3870 additions and 9 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

View file

@ -1,8 +0,0 @@
(function () {
var burger = document.querySelector('.burger');
var menu = document.querySelector('#' + burger.dataset.target);
burger.addEventListener('click', function () {
burger.classList.toggle('is-active');
menu.classList.toggle('is-active');
});
})();

View file

@ -1,184 +0,0 @@
function debounce(func, wait) {
var timeout;
return function () {
var context = this;
var args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function () {
timeout = null;
func.apply(context, args);
}, wait);
};
}
// Taken from mdbook
// The strategy is as follows:
// First, assign a value to each word in the document:
// Words that correspond to search terms (stemmer aware): 40
// Normal words: 2
// First word in a sentence: 8
// Then use a sliding window with a constant number of words and count the
// sum of the values of the words within the window. Then use the window that got the
// maximum sum. If there are multiple maximas, then get the last one.
// Enclose the terms in <b>.
function makeTeaser(body, terms) {
var TERM_WEIGHT = 40;
var NORMAL_WORD_WEIGHT = 2;
var FIRST_WORD_WEIGHT = 8;
var TEASER_MAX_WORDS = 30;
var stemmedTerms = terms.map(function (w) {
return elasticlunr.stemmer(w.toLowerCase());
});
var termFound = false;
var index = 0;
var weighted = []; // contains elements of ["word", weight, index_in_document]
// split in sentences, then words
var sentences = body.toLowerCase().split(". ");
for (var i in sentences) {
var words = sentences[i].split(" ");
var value = FIRST_WORD_WEIGHT;
for (var j in words) {
var word = words[j];
if (word.length > 0) {
for (var k in stemmedTerms) {
if (elasticlunr.stemmer(word).startsWith(stemmedTerms[k])) {
value = TERM_WEIGHT;
termFound = true;
}
}
weighted.push([word, value, index]);
value = NORMAL_WORD_WEIGHT;
}
index += word.length;
index += 1; // ' ' or '.' if last word in sentence
}
index += 1; // because we split at a two-char boundary '. '
}
if (weighted.length === 0) {
return body;
}
var windowWeights = [];
var windowSize = Math.min(weighted.length, TEASER_MAX_WORDS);
// We add a window with all the weights first
var curSum = 0;
for (var i = 0; i < windowSize; i++) {
curSum += weighted[i][1];
}
windowWeights.push(curSum);
for (var i = 0; i < weighted.length - windowSize; i++) {
curSum -= weighted[i][1];
curSum += weighted[i + windowSize][1];
windowWeights.push(curSum);
}
// If we didn't find the term, just pick the first window
var maxSumIndex = 0;
if (termFound) {
var maxFound = 0;
// backwards
for (var i = windowWeights.length - 1; i >= 0; i--) {
if (windowWeights[i] > maxFound) {
maxFound = windowWeights[i];
maxSumIndex = i;
}
}
}
var teaser = [];
var startIndex = weighted[maxSumIndex][2];
for (var i = maxSumIndex; i < maxSumIndex + windowSize; i++) {
var word = weighted[i];
if (startIndex < word[2]) {
// missing text from index to start of `word`
teaser.push(body.substring(startIndex, word[2]));
startIndex = word[2];
}
// add <em/> around search terms
if (word[1] === TERM_WEIGHT) {
teaser.push("<b>");
}
startIndex = word[2] + word[0].length;
teaser.push(body.substring(word[2], startIndex));
if (word[1] === TERM_WEIGHT) {
teaser.push("</b>");
}
}
teaser.push("…");
return teaser.join("");
}
function formatSearchResultItem(item, terms) {
return '<div class="search-results__item box">' +
`<a href="${item.ref}">${item.doc.title}</a>` +
`<div>${makeTeaser(item.doc.body, terms)}</div>` +
'</div>';
}
function initSearch() {
var $searchInput = document.getElementById("search");
var $searchResults = document.querySelector(".search-results");
var $searchResultsItems = document.querySelector(".search-results__items");
var MAX_ITEMS = 10;
var options = {
bool: "AND",
fields: {
title: {
boost: 2
},
body: {
boost: 1
},
}
};
var currentTerm = "";
var index = elasticlunr.Index.load(window.searchIndex);
$searchInput.addEventListener("keyup", debounce(function () {
var term = $searchInput.value.trim();
if (!index) {
return;
}
$searchResults.style.display = term === "" ? "none" : "block";
$searchResultsItems.innerHTML = "";
if (term === "") {
return;
}
var results = index.search(term, options);
if (results.length === 0) {
$searchResults.style.display = "none";
return;
}
currentTerm = term;
for (var i = 0; i < Math.min(results.length, MAX_ITEMS); i++) {
var item = document.createElement("li");
item.innerHTML = formatSearchResultItem(results[i], term.split(" "));
$searchResultsItems.appendChild(item);
}
}, 150));
}
if (document.readyState === "complete" ||
(document.readyState !== "loading" && !document.documentElement.doScroll)
) {
initSearch();
} else {
document.addEventListener("DOMContentLoaded", initSearch);
}

View file

@ -1,105 +0,0 @@
(function (switch_css) {
//Constants
const THEME_KEY = "ZULMA_THEME";
const STOP_LINK_CSS_ID = "stop-blink";
const STYLESHEET_CLASSNAME = "stylesheet"
//Variables
let link = null;
let theme = localStorage.getItem(THEME_KEY);
//Private Methods
/* Called when the theme is changed */
function changeTheme(themeName, firstLoad) {
//create the css link element
var fileref = document.createElement("link");
fileref.rel = "stylesheet";
fileref.type = "text/css";
fileref.href = `/${themeName}.css`;
//append it to the head
link = document.getElementsByTagName("head")[0].appendChild(fileref);
//when it's loaded, call onLinkLoad
link.addEventListener('load', onLinkLoad);
//if this is the first load of the page, remove the current stylesheet early to avoid flash of wrongly styled content
if (firstLoad) {
removeStylesheets();
}
saveTheme(themeName);
};
function removeStylesheets() {
document.querySelectorAll(`.${STYLESHEET_CLASSNAME}`).forEach((el) => {
el.remove();
});
}
/* The function called when the css has finished loading */
function onLinkLoad() {
link.removeEventListener('load', onLinkLoad);
//remove the previous stylesheet(s)
removeStylesheets();
//add stylesheet class
link.className += STYLESHEET_CLASSNAME;
//make body visible again if it was hidden
showBody();
};
/* Saves the current theme in localstorage */
function saveTheme(themeName) {
localStorage.setItem(THEME_KEY, themeName);
};
/* Hides the body of the page */
function hideBody() {
var head = document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.id = STOP_LINK_CSS_ID;
style.setAttribute('type', 'text/css');
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode('body{visibility:hidden;}'));
}
head.appendChild(style);
};
/* Shows the body of the page */
function showBody() {
let css = document.getElementById(STOP_LINK_CSS_ID);
if (css)
css.remove();
};
//Public Methods
switch_css.init = function () {
//if user has selected and theme and it is not the current theme
if (theme && !document.getElementById(theme)) {
//hide the body to stop FOUC
hideBody();
//change the theme
changeTheme(theme, true);
//when the DOM is loaded, change the select to their current choice
window.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('#theme-select>option').forEach(element => {
if (element.value === theme) {
element.selected = 'selected';
}
});
});
}
//when the DOM is loaded, set the dropdown to trigger the theme change
window.addEventListener('DOMContentLoaded', () => {
document.getElementById('theme-select').onchange = function () {
changeTheme(this.value);
}
});
}
}(switch_css = window.switch_css || {}));
switch_css.init();

View file

@ -0,0 +1,2 @@
!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t){var r,n;r=document.querySelector(".burger"),n=document.querySelector("#"+r.dataset.target),r.addEventListener("click",function(){r.classList.toggle("is-active"),n.classList.toggle("is-active")})}]);
//# sourceMappingURL=zulma_navbar.js.map

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,2 @@
!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=1)}([,function(e,t){function n(e,t){return'<div class="search-results__item box">'+'<a href="'.concat(e.ref,'">').concat(e.doc.title,"</a>")+"<div>".concat(function(e,t){var n=t.map(function(e){return elasticlunr.stemmer(e.toLowerCase())}),r=!1,o=0,u=[],i=e.toLowerCase().split(". ");for(var l in i){var a=i[l].split(" "),c=8;for(var s in a){if((g=a[s]).length>0){for(var d in n)elasticlunr.stemmer(g).startsWith(n[d])&&(c=40,r=!0);u.push([g,c,o]),c=2}o+=g.length,o+=1}o+=1}if(0===u.length)return e;var f=[],p=Math.min(u.length,30),m=0;for(l=0;l<p;l++)m+=u[l][1];for(f.push(m),l=0;l<u.length-p;l++)m-=u[l][1],m+=u[l+p][1],f.push(m);var v=0;if(r){var h=0;for(l=f.length-1;l>=0;l--)f[l]>h&&(h=f[l],v=l)}var y=[],b=u[v][2];for(l=v;l<v+p;l++){var g;b<(g=u[l])[2]&&(y.push(e.substring(b,g[2])),b=g[2]),40===g[1]&&y.push("<b>"),b=g[2]+g[0].length,y.push(e.substring(g[2],b)),40===g[1]&&y.push("</b>")}return y.push("…"),y.join("")}(e.doc.body,t),"</div>")+"</div>"}function r(){var e,t,r,o=document.getElementById("search"),u=document.querySelector(".search-results"),i=document.querySelector(".search-results__items"),l={bool:"AND",fields:{title:{boost:2},body:{boost:1}}},a=elasticlunr.Index.load(window.searchIndex);o.addEventListener("keyup",(e=function(){var e=o.value.trim();if(a&&(u.style.display=""===e?"none":"block",i.innerHTML="",""!==e)){var t=a.search(e,l);if(0!==t.length){e;for(var r=0;r<Math.min(t.length,10);r++){var c=document.createElement("li");c.innerHTML=n(t[r],e.split(" ")),i.appendChild(c)}}else u.style.display="none"}},t=150,function(){var n=this,o=arguments;clearTimeout(r),r=setTimeout(function(){r=null,e.apply(n,o)},t)}))}"complete"===document.readyState||"loading"!==document.readyState&&!document.documentElement.doScroll?r():document.addEventListener("DOMContentLoaded",r)}]);
//# sourceMappingURL=zulma_search.js.map

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,2 @@
!function(e){var t={};function n(o){if(t[o])return t[o].exports;var r=t[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,n),r.l=!0,r.exports}n.m=e,n.c=t,n.d=function(e,t,o){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(n.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var r in e)n.d(o,r,function(t){return e[t]}.bind(null,r));return o},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=2)}({2:function(e,t){!function(e){var t="ZULMA_THEME",n="stop-blink",o="stylesheet",r=null,c=localStorage.getItem(t);function i(e,n){var o=document.createElement("link");o.rel="stylesheet",o.type="text/css",o.href="/".concat(e,".css"),(r=document.getElementsByTagName("head")[0].appendChild(o)).addEventListener("load",u),n&&l(),function(e){localStorage.setItem(t,e)}(e)}function l(){document.querySelectorAll(".".concat(o)).forEach(function(e){e.remove()})}function u(){var e;r.removeEventListener("load",u),l(),r.className+=o,(e=document.getElementById(n))&&e.remove()}e.init=function(){var e,t;c&&!document.getElementById(c)&&(e=document.getElementsByTagName("head")[0],(t=document.createElement("style")).id=n,t.setAttribute("type","text/css"),t.styleSheet?t.styleSheet.cssText=css:t.appendChild(document.createTextNode("body{visibility:hidden;}")),e.appendChild(t),i(c,!0),window.addEventListener("DOMContentLoaded",function(){document.querySelectorAll("#theme-select>option").forEach(function(e){e.value===c&&(e.selected="selected")})})),window.addEventListener("DOMContentLoaded",function(){document.getElementById("theme-select").onchange=function(){i(this.value)}})}}(switch_css=window.switch_css||{}),switch_css.init()}});
//# sourceMappingURL=zulma_switchcss.js.map

File diff suppressed because one or more lines are too long