Switch search to use jsonp to load the index, fully works offline. #198

This commit is contained in:
Stéphane Goetz 2020-04-24 22:58:52 +02:00
parent 213b33b9b5
commit 22fd55e9f7
15 changed files with 44 additions and 36 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -76,8 +76,8 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator, LiveGenerator
if ($config->getHTML()->hasSearch()) { if ($config->getHTML()->hasSearch()) {
file_put_contents( file_put_contents(
$destination . DIRECTORY_SEPARATOR . 'daux_search_index.json', $destination . DIRECTORY_SEPARATOR . 'daux_search_index.js',
json_encode(['pages' => $this->indexed_pages]) 'load_search_index(' . json_encode(['pages' => $this->indexed_pages]) . ');'
); );
if (json_last_error()) { if (json_last_error()) {

View File

@ -15,6 +15,7 @@
"scripts": { "scripts": {
"build": "crafty run", "build": "crafty run",
"watch": "crafty watch", "watch": "crafty watch",
"lint:js": "crafty jsLint --fix src/js/**/*.js",
"lint:css": "crafty cssLint --fix --preset recommended themes/daux_singlepage/scss/*.scss themes/daux/scss/*.scss" "lint:css": "crafty cssLint --fix --preset recommended themes/daux_singlepage/scss/*.scss themes/daux/scss/*.scss"
} }
} }

View File

@ -30,7 +30,7 @@ class SearchEngine {
descriptiveWords: 25, descriptiveWords: 25,
highlightTerms: true, highlightTerms: true,
highlightEveryTerm: false, highlightEveryTerm: false,
contentLocation: "daux_search_index.json", contentLocation: "daux_search_index.js",
...options ...options
}; };
@ -41,30 +41,37 @@ class SearchEngine {
loadData() { loadData() {
if (!this.loadingPromise) { if (!this.loadingPromise) {
this.loadingPromise = fetch( // We do this as jsonp instead of an XHR or fetch request
this.settings.base_url + this.settings.contentLocation // to be compatible with usage from filesystem
) const po = document.createElement("script");
.then(data => data.json()) po.type = "text/javascript";
.then(json => { po.async = true;
this.searchIndex = new FlexSearch({ po.src = this.settings.base_url + this.settings.contentLocation;
doc: { const s = document.getElementsByTagName("script")[0];
id: "url", s.parentNode.insertBefore(po, s);
field: ["title", "text", "tags"]
}
});
let pages = json.pages; this.loadingPromise = new Promise(resolve => {
window.load_search_index = data => resolve(data);
// Only keep the pages related to the current language }).then(json => {
if (window.searchLanguage) { this.searchIndex = new FlexSearch({
const pagePrefix = `${window.searchLanguage}/`; doc: {
pages = pages.filter( id: "url",
item => item.url.indexOf(pagePrefix) === 0 field: ["title", "text", "tags"]
);
} }
this.searchIndex.add(pages);
}); });
let pages = json.pages;
// Only keep the pages related to the current language
if (window.searchLanguage) {
const pagePrefix = `${window.searchLanguage}/`;
pages = pages.filter(
item => item.url.indexOf(pagePrefix) === 0
);
}
this.searchIndex.add(pages);
});
} }
return this.loadingPromise; return this.loadingPromise;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,2 +1,2 @@
!function(){"use strict";function e(e){"loading"===document.readyState?document.addEventListener("DOMContentLoaded",e):e()}function t(e,t){for(var n=0;n<e.length;n++)e[n].classList.toggle("CodeToggler--hidden",t);try{localStorage.setItem("daux_code_blocks_hidden",t)}catch(e){}}function n(e){var t=void 0!==e.preventDefault;t&&e.preventDefault();var n=function(e){for(var t=e;(t=t.parentNode)&&9!==t.nodeType;)if(1===t.nodeType&&t.classList.contains("Nav__item"))return t;throw new Error("Could not find a NavItem...")}(e.target),o=n.querySelector("ul.Nav");t&&n.classList.contains("Nav__item--open")?(o.style.height="".concat(o.scrollHeight,"px"),o.style.transitionDuration="150ms",o.style.height="0px",n.classList.remove("Nav__item--open")):t?(o.style.transitionDuration="150ms",o.addEventListener("transitionend",(function e(t){"0px"!==t.target.style.height&&(t.target.style.height="auto"),t.target.removeEventListener("transitionend",e)})),o.style.height="".concat(o.scrollHeight,"px"),n.classList.add("Nav__item--open")):o.style.height="auto"}e((function(){var e=document.querySelectorAll(".s-content pre"),n=document.querySelector(".CodeToggler");n&&(e.length?function(e,n){var o=e.querySelector(".CodeToggler__button--main");o.addEventListener("change",(function(e){t(n,!e.target.checked)}),!1);var a=!1;try{"false"===(a=localStorage.getItem("daux_code_blocks_hidden"))?a=!1:"true"===a&&(a=!0),a&&(t(n,!!a),o.checked=!a)}catch(e){}}(n,e):n.classList.add("CodeToggler--hidden"))})),e((function(){var e=document.querySelector(".Collapsible__trigger");if(e){var t=document.querySelector(".Collapsible__content");e.addEventListener("click",(function(n){t.classList.contains("Collapsible__content--open")?(t.style.height=0,t.classList.remove("Collapsible__content--open"),e.setAttribute("aria-expanded","false")):(e.setAttribute("aria-expanded","true"),t.style.transitionDuration="150ms",t.style.height="".concat(t.scrollHeight,"px"),t.classList.add("Collapsible__content--open"))}))}})),e((function(){var e=document.querySelectorAll("pre > code:not(.hljs)");if(e.length){var t=document.getElementsByTagName("head")[0],n=document.createElement("script");n.type="text/javascript",n.async=!0,n.src="".concat(window.base_url,"daux_libraries/highlight.pack.js"),n.onload=function(t){[].forEach.call(e,window.hljs.highlightBlock)},t.appendChild(n)}})),e((function(){for(var e,t=document.querySelectorAll(".Nav__item.has-children i.Nav__arrow"),o=t.length-1;o>=0;o--)(e=t[o]).addEventListener("click",n),e.parentNode.parentNode.classList.contains("Nav__item--open")&&n({target:e});var a=document.querySelectorAll(".Nav__item__link--nopage"),r=!0,i=!1,l=void 0;try{for(var c,s=a[Symbol.iterator]();!(r=(c=s.next()).done);r=!0){c.value.addEventListener("click",n)}}catch(e){i=!0,l=e}finally{try{r||null==s.return||s.return()}finally{if(i)throw l}}}))}(); !function(){"use strict";function e(e){"loading"===document.readyState?document.addEventListener("DOMContentLoaded",e):e()}function t(e,t){for(var n=0;n<e.length;n++)e[n].classList.toggle("CodeToggler--hidden",t);try{localStorage.setItem("daux_code_blocks_hidden",t)}catch(e){}}function n(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function r(e){if("undefined"==typeof Symbol||null==e[Symbol.iterator]){if(Array.isArray(e)||(e=function(e,t){if(e){if("string"==typeof e)return n(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(r):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(e,t):void 0}}(e))){var t=0,r=function(){};return{s:r,n:function(){return t>=e.length?{done:!0}:{done:!1,value:e[t++]}},e:function(e){throw e},f:r}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,a,i=!0,l=!1;return{s:function(){o=e[Symbol.iterator]()},n:function(){var e=o.next();return i=e.done,e},e:function(e){l=!0,a=e},f:function(){try{i||null==o.return||o.return()}finally{if(l)throw a}}}}function o(e){var t=void 0!==e.preventDefault;t&&e.preventDefault();var n=function(e){for(var t=e;(t=t.parentNode)&&9!==t.nodeType;)if(1===t.nodeType&&t.classList.contains("Nav__item"))return t;throw new Error("Could not find a NavItem...")}(e.target),r=n.querySelector("ul.Nav");t&&n.classList.contains("Nav__item--open")?(r.style.height="".concat(r.scrollHeight,"px"),r.style.transitionDuration="150ms",r.style.height="0px",n.classList.remove("Nav__item--open")):t?(r.style.transitionDuration="150ms",r.addEventListener("transitionend",(function e(t){"0px"!==t.target.style.height&&(t.target.style.height="auto"),t.target.removeEventListener("transitionend",e)})),r.style.height="".concat(r.scrollHeight,"px"),n.classList.add("Nav__item--open")):r.style.height="auto"}e((function(){var e=document.querySelectorAll(".s-content pre"),n=document.querySelector(".CodeToggler");n&&(e.length?function(e,n){var r=e.querySelector(".CodeToggler__button--main");r.addEventListener("change",(function(e){t(n,!e.target.checked)}),!1);var o=!1;try{"false"===(o=localStorage.getItem("daux_code_blocks_hidden"))?o=!1:"true"===o&&(o=!0),o&&(t(n,!!o),r.checked=!o)}catch(e){}}(n,e):n.classList.add("CodeToggler--hidden"))})),e((function(){var e=document.querySelector(".Collapsible__trigger");if(e){var t=document.querySelector(".Collapsible__content");e.addEventListener("click",(function(n){t.classList.contains("Collapsible__content--open")?(t.style.height=0,t.classList.remove("Collapsible__content--open"),e.setAttribute("aria-expanded","false")):(e.setAttribute("aria-expanded","true"),t.style.transitionDuration="150ms",t.style.height="".concat(t.scrollHeight,"px"),t.classList.add("Collapsible__content--open"))}))}})),e((function(){var e=document.querySelectorAll("pre > code:not(.hljs)");if(e.length){var t=document.getElementsByTagName("head")[0],n=document.createElement("script");n.type="text/javascript",n.async=!0,n.src="".concat(window.base_url,"daux_libraries/highlight.pack.js"),n.onload=function(t){[].forEach.call(e,window.hljs.highlightBlock)},t.appendChild(n)}})),e((function(){for(var e,t=document.querySelectorAll(".Nav__item.has-children i.Nav__arrow"),n=t.length-1;n>=0;n--)(e=t[n]).addEventListener("click",o),e.parentNode.parentNode.classList.contains("Nav__item--open")&&o({target:e});var a,i=r(document.querySelectorAll(".Nav__item__link--nopage"));try{for(i.s();!(a=i.n()).done;){a.value.addEventListener("click",o)}}catch(e){i.e(e)}finally{i.f()}}))}();
//# sourceMappingURL=daux.min.js.map //# sourceMappingURL=daux.min.js.map

File diff suppressed because one or more lines are too long