From 6c8776a4cc9447a2dcdd1af0779a334d09fc74a2 Mon Sep 17 00:00:00 2001 From: andwati Date: Wed, 11 Mar 2026 22:07:01 +0300 Subject: [PATCH] feat: Add client-side search, reading time estimates, and a reading progress bar, plus a clipboard copy fallback. --- TODO.md | 13 -- sass/_article.scss | 21 +++ sass/_search.scss | 175 +++++++++++++---------- static/js/copy-code.js | 20 ++- static/js/elasticlunr.min.js | 10 ++ static/js/progress.js | 34 +++++ static/js/search.js | 223 ++++++++++++++++++++++++++++++ templates/base.html | 26 +++- templates/page.html | 13 ++ templates/partials/post-card.html | 4 + templates/posts.html | 12 +- zola.toml | 4 +- 12 files changed, 457 insertions(+), 98 deletions(-) create mode 100644 static/js/elasticlunr.min.js create mode 100644 static/js/progress.js create mode 100644 static/js/search.js diff --git a/TODO.md b/TODO.md index 4fb1fcf..8f206d2 100644 --- a/TODO.md +++ b/TODO.md @@ -6,11 +6,8 @@ Feature backlog for the blog. Check off items as they're implemented. ## Discovery & Navigation -- [ ] **Client-side search** — enable `build_search_index = true` in `zola.toml`, add a search page/modal with Elasticlunr -- [ ] **Reading time estimate** — use `page.reading_time` to display "X min read" in post headers - [ ] **Related posts** — show 2–3 posts with overlapping tags at the bottom of each post - [ ] **Post series support** — `series` front-matter field; render a "Part N of X" callout with links to all parts in the series -- [ ] **Keyboard shortcuts** — `K` to open search, `T` to toggle theme; show a small hotkey hint overlay --- @@ -18,25 +15,15 @@ Feature backlog for the blog. Check off items as they're implemented. - [ ] **Callout / admonition shortcodes** — `{% note %}`, `{% warning %}`, `{% tip %}`, `{% danger %}` Tera shortcodes styled as terminal callout boxes -- [x] **Updated-at badge** — show "Last updated: " on posts that have `updated` front-matter set, distinct from the published date - --- ## Post List & Filtering -- [ ] **Tag filtering on posts page** — JS-powered tag chip filter; clicking a tag hides non-matching posts without a page reload -- [ ] **Reading progress bar** — thin accent-colored bar at the top of the viewport that fills as you scroll through a post -- [ ] **Post view count** — Cloudflare Worker + KV store to track and display view counts (fits existing `wrangler.toml` setup) -- [ ] **Sort / filter controls on posts page** — toggle between newest-first, oldest-first, and filter by tag or difficulty - --- ## UI / UX Polish - [ ] **Smooth page transitions** — CSS View Transitions API for navigation between pages -- [ ] **Footnote hover previews** — hover over a footnote ref to see its content in a tooltip without jumping to the bottom - ---- ## Distribution & Reach diff --git a/sass/_article.scss b/sass/_article.scss index 35b6759..90f68a7 100644 --- a/sass/_article.scss +++ b/sass/_article.scss @@ -1,3 +1,24 @@ +// Article details and content styling + +.reading-progress-container { + position: fixed; + top: 0; + left: 0; + width: 100%; + height: 3px; + background-color: transparent; + z-index: 1000; + pointer-events: none; +} + +.reading-progress-bar { + height: 100%; + width: 0%; + background-color: var(--accent-color); + box-shadow: 0 0 10px rgba(16, 185, 129, 0.5); /* Glowing emerald accent */ + transition: width 0.1s cubic-bezier(0.25, 0.8, 0.25, 1); +} + // Post Detail .post-detail { max-width: 800px; diff --git a/sass/_search.scss b/sass/_search.scss index 529038b..cbc9672 100644 --- a/sass/_search.scss +++ b/sass/_search.scss @@ -1,50 +1,68 @@ -// Search overlay +// Search Modal Styles .search-overlay { position: fixed; top: 0; left: 0; - right: 0; - bottom: 0; - background-color: rgba(0, 0, 0, 0.8); + width: 100%; + height: 100%; + background: rgba(0, 0, 0, 0.4); backdrop-filter: blur(4px); - z-index: 9999; - display: none; - align-items: flex-start; + -webkit-backdrop-filter: blur(4px); + z-index: 10000; + display: flex; justify-content: center; - padding: 2rem; - overflow-y: auto; + align-items: flex-start; + padding-top: 10vh; + opacity: 0; + visibility: hidden; + transition: + opacity 0.2s ease, + visibility 0.2s ease; - &.is-active { - display: flex; + &.active { + opacity: 1; + visibility: visible; + + .search-modal { + transform: translateY(0) scale(1); + } } } .search-modal { - background-color: var(--bg-color); + width: 90%; + max-width: 600px; + background: var(--bg-color); border: 1px solid var(--border-color); - border-radius: 0.5rem; - max-width: 42rem; - width: 100%; - margin-top: 10vh; - box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5); + border-radius: 12px; + box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5); + transform: translateY(-20px) scale(0.98); + transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275); + display: flex; + flex-direction: column; overflow: hidden; } .search-header { display: flex; align-items: center; - gap: 0.5rem; - padding: 1rem; + padding: 1rem 1.25rem; border-bottom: 1px solid var(--border-color); + background: var(--surface-color); + + svg { + color: var(--text-muted); + margin-right: 0.75rem; + } } -.search-input { +#search-input { flex: 1; - font-size: 1.125rem; - padding: 0.5rem; background: transparent; border: none; color: var(--text-color); + font-family: var(--font-sans); + font-size: 1.1rem; outline: none; &::placeholder { @@ -52,75 +70,82 @@ } } -.search-close { - background: transparent; - border: none; - color: var(--text-muted); - cursor: pointer; - padding: 0.25rem; +.search-footer { + padding: 0.5rem 1.25rem; + background: var(--surface-color); + border-top: 1px solid var(--border-color); display: flex; + justify-content: space-between; align-items: center; - justify-content: center; - border-radius: 0.25rem; - transition: background-color 0.2s ease; + font-size: 0.75rem; + color: var(--text-muted); - &:hover { - background-color: var(--border-color); - color: var(--text-color); + kbd { + background: var(--bg-color); + border: 1px solid var(--border-color); + border-radius: 4px; + padding: 0.1rem 0.3rem; + font-family: var(--font-mono); + font-size: 0.7rem; + margin: 0 0.2rem; } } .search-results { - max-height: 60vh; + max-height: 50vh; overflow-y: auto; padding: 0.5rem; -} -.search-result-item { - display: block; - padding: 1rem; - border-radius: 0.375rem; - text-decoration: none; - color: var(--text-color); - transition: background-color 0.2s ease; - - &:hover { + // Custom scrollbar + &::-webkit-scrollbar { + width: 8px; + } + &::-webkit-scrollbar-track { + background: transparent; + } + &::-webkit-scrollbar-thumb { background-color: var(--border-color); + border-radius: 4px; } -} -.search-result-title { - font-size: 1rem; - font-weight: 600; - margin: 0 0 0.5rem 0; - color: var(--accent-color); -} + .search-result-item { + display: flex; + flex-direction: column; + padding: 0.75rem 1rem; + border-radius: 8px; + text-decoration: none; + color: var(--text-color); + transition: background-color 0.15s ease; + border-left: 2px solid transparent; -.search-result-snippet { - font-size: 0.875rem; - color: var(--text-muted); - margin: 0; - line-height: 1.5; -} + &:hover, + &.selected { + background-color: var(--surface-color); + border-left-color: var(--accent-color); + } -.search-no-results { - padding: 2rem; - text-align: center; - color: var(--text-muted); -} + .result-title { + font-weight: 500; + color: var(--accent-color); + margin-bottom: 0.25rem; + } -.search-footer { - padding: 0.75rem 1rem; - border-top: 1px solid var(--border-color); - font-size: 0.75rem; - color: var(--text-muted); - text-align: center; + .result-summary { + font-size: 0.85rem; + color: var(--text-muted); + line-height: 1.4; + display: -webkit-box; + -webkit-line-clamp: 2; + line-clamp: 2; + -webkit-box-orient: vertical; + overflow: hidden; + } + } - kbd { - padding: 0.125rem 0.375rem; - background-color: var(--border-color); - border-radius: 0.25rem; - font-family: var(--font-mono); - font-size: 0.75rem; + .no-results { + padding: 2rem; + text-align: center; + color: var(--text-muted); + font-style: italic; } } diff --git a/static/js/copy-code.js b/static/js/copy-code.js index 24bc488..615a5a5 100644 --- a/static/js/copy-code.js +++ b/static/js/copy-code.js @@ -28,8 +28,24 @@ document.addEventListener('DOMContentLoaded', () => { const text = code.innerText; try { - // Wait for the clipboard API - await navigator.clipboard.writeText(text); + if (navigator.clipboard && window.isSecureContext) { + // Modern Async Clipboard API + await navigator.clipboard.writeText(text); + } else { + // Fallback for insecure contexts (like local network IPs via HTTP) + const textArea = document.createElement('textarea'); + textArea.value = text; + + // Move outside the screen to make it invisible + textArea.style.position = 'absolute'; + textArea.style.left = '-999999px'; + + document.body.appendChild(textArea); + textArea.select(); + + document.execCommand('copy'); + textArea.remove(); + } // Add visual feedback button.innerHTML = checkIcon; diff --git a/static/js/elasticlunr.min.js b/static/js/elasticlunr.min.js new file mode 100644 index 0000000..32cb1bc --- /dev/null +++ b/static/js/elasticlunr.min.js @@ -0,0 +1,10 @@ +/** + * elasticlunr - http://weixsong.github.io + * Lightweight full-text search engine in Javascript for browser search and offline search. - 0.9.5 + * + * Copyright (C) 2016 Oliver Nightingale + * Copyright (C) 2016 Wei Song + * MIT Licensed + * @license + */ +!function(){function e(e){if(null===e||"object"!=typeof e)return e;var t=e.constructor();for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n]);return t}var t=function(e){var n=new t.Index;return n.pipeline.add(t.trimmer,t.stopWordFilter,t.stemmer),e&&e.call(n,n),n};t.version="0.9.5",lunr=t,t.utils={},t.utils.warn=function(e){return function(t){e.console&&console.warn&&console.warn(t)}}(this),t.utils.toString=function(e){return void 0===e||null===e?"":e.toString()},t.EventEmitter=function(){this.events={}},t.EventEmitter.prototype.addListener=function(){var e=Array.prototype.slice.call(arguments),t=e.pop(),n=e;if("function"!=typeof t)throw new TypeError("last argument must be a function");n.forEach(function(e){this.hasHandler(e)||(this.events[e]=[]),this.events[e].push(t)},this)},t.EventEmitter.prototype.removeListener=function(e,t){if(this.hasHandler(e)){var n=this.events[e].indexOf(t);-1!==n&&(this.events[e].splice(n,1),0==this.events[e].length&&delete this.events[e])}},t.EventEmitter.prototype.emit=function(e){if(this.hasHandler(e)){var t=Array.prototype.slice.call(arguments,1);this.events[e].forEach(function(e){e.apply(void 0,t)},this)}},t.EventEmitter.prototype.hasHandler=function(e){return e in this.events},t.tokenizer=function(e){if(!arguments.length||null===e||void 0===e)return[];if(Array.isArray(e)){var n=e.filter(function(e){return null===e||void 0===e?!1:!0});n=n.map(function(e){return t.utils.toString(e).toLowerCase()});var i=[];return n.forEach(function(e){var n=e.split(t.tokenizer.seperator);i=i.concat(n)},this),i}return e.toString().trim().toLowerCase().split(t.tokenizer.seperator)},t.tokenizer.defaultSeperator=/[\s\-]+/,t.tokenizer.seperator=t.tokenizer.defaultSeperator,t.tokenizer.setSeperator=function(e){null!==e&&void 0!==e&&"object"==typeof e&&(t.tokenizer.seperator=e)},t.tokenizer.resetSeperator=function(){t.tokenizer.seperator=t.tokenizer.defaultSeperator},t.tokenizer.getSeperator=function(){return t.tokenizer.seperator},t.Pipeline=function(){this._queue=[]},t.Pipeline.registeredFunctions={},t.Pipeline.registerFunction=function(e,n){n in t.Pipeline.registeredFunctions&&t.utils.warn("Overwriting existing registered function: "+n),e.label=n,t.Pipeline.registeredFunctions[n]=e},t.Pipeline.getRegisteredFunction=function(e){return e in t.Pipeline.registeredFunctions!=!0?null:t.Pipeline.registeredFunctions[e]},t.Pipeline.warnIfFunctionNotRegistered=function(e){var n=e.label&&e.label in this.registeredFunctions;n||t.utils.warn("Function is not registered with pipeline. This may cause problems when serialising the index.\n",e)},t.Pipeline.load=function(e){var n=new t.Pipeline;return e.forEach(function(e){var i=t.Pipeline.getRegisteredFunction(e);if(!i)throw new Error("Cannot load un-registered function: "+e);n.add(i)}),n},t.Pipeline.prototype.add=function(){var e=Array.prototype.slice.call(arguments);e.forEach(function(e){t.Pipeline.warnIfFunctionNotRegistered(e),this._queue.push(e)},this)},t.Pipeline.prototype.after=function(e,n){t.Pipeline.warnIfFunctionNotRegistered(n);var i=this._queue.indexOf(e);if(-1===i)throw new Error("Cannot find existingFn");this._queue.splice(i+1,0,n)},t.Pipeline.prototype.before=function(e,n){t.Pipeline.warnIfFunctionNotRegistered(n);var i=this._queue.indexOf(e);if(-1===i)throw new Error("Cannot find existingFn");this._queue.splice(i,0,n)},t.Pipeline.prototype.remove=function(e){var t=this._queue.indexOf(e);-1!==t&&this._queue.splice(t,1)},t.Pipeline.prototype.run=function(e){for(var t=[],n=e.length,i=this._queue.length,o=0;n>o;o++){for(var r=e[o],s=0;i>s&&(r=this._queue[s](r,o,e),void 0!==r&&null!==r);s++);void 0!==r&&null!==r&&t.push(r)}return t},t.Pipeline.prototype.reset=function(){this._queue=[]},t.Pipeline.prototype.get=function(){return this._queue},t.Pipeline.prototype.toJSON=function(){return this._queue.map(function(e){return t.Pipeline.warnIfFunctionNotRegistered(e),e.label})},t.Index=function(){this._fields=[],this._ref="id",this.pipeline=new t.Pipeline,this.documentStore=new t.DocumentStore,this.index={},this.eventEmitter=new t.EventEmitter,this._idfCache={},this.on("add","remove","update",function(){this._idfCache={}}.bind(this))},t.Index.prototype.on=function(){var e=Array.prototype.slice.call(arguments);return this.eventEmitter.addListener.apply(this.eventEmitter,e)},t.Index.prototype.off=function(e,t){return this.eventEmitter.removeListener(e,t)},t.Index.load=function(e){e.version!==t.version&&t.utils.warn("version mismatch: current "+t.version+" importing "+e.version);var n=new this;n._fields=e.fields,n._ref=e.ref,n.documentStore=t.DocumentStore.load(e.documentStore),n.pipeline=t.Pipeline.load(e.pipeline),n.index={};for(var i in e.index)n.index[i]=t.InvertedIndex.load(e.index[i]);return n},t.Index.prototype.addField=function(e){return this._fields.push(e),this.index[e]=new t.InvertedIndex,this},t.Index.prototype.setRef=function(e){return this._ref=e,this},t.Index.prototype.saveDocument=function(e){return this.documentStore=new t.DocumentStore(e),this},t.Index.prototype.addDoc=function(e,n){if(e){var n=void 0===n?!0:n,i=e[this._ref];this.documentStore.addDoc(i,e),this._fields.forEach(function(n){var o=this.pipeline.run(t.tokenizer(e[n]));this.documentStore.addFieldLength(i,n,o.length);var r={};o.forEach(function(e){e in r?r[e]+=1:r[e]=1},this);for(var s in r){var u=r[s];u=Math.sqrt(u),this.index[n].addToken(s,{ref:i,tf:u})}},this),n&&this.eventEmitter.emit("add",e,this)}},t.Index.prototype.removeDocByRef=function(e){if(e&&this.documentStore.isDocStored()!==!1&&this.documentStore.hasDoc(e)){var t=this.documentStore.getDoc(e);this.removeDoc(t,!1)}},t.Index.prototype.removeDoc=function(e,n){if(e){var n=void 0===n?!0:n,i=e[this._ref];this.documentStore.hasDoc(i)&&(this.documentStore.removeDoc(i),this._fields.forEach(function(n){var o=this.pipeline.run(t.tokenizer(e[n]));o.forEach(function(e){this.index[n].removeToken(e,i)},this)},this),n&&this.eventEmitter.emit("remove",e,this))}},t.Index.prototype.updateDoc=function(e,t){var t=void 0===t?!0:t;this.removeDocByRef(e[this._ref],!1),this.addDoc(e,!1),t&&this.eventEmitter.emit("update",e,this)},t.Index.prototype.idf=function(e,t){var n="@"+t+"/"+e;if(Object.prototype.hasOwnProperty.call(this._idfCache,n))return this._idfCache[n];var i=this.index[t].getDocFreq(e),o=1+Math.log(this.documentStore.length/(i+1));return this._idfCache[n]=o,o},t.Index.prototype.getFields=function(){return this._fields.slice()},t.Index.prototype.search=function(e,n){if(!e)return[];var i=null;null!=n&&(i=JSON.stringify(n));var o=new t.Configuration(i,this.getFields()).get(),r=this.pipeline.run(t.tokenizer(e)),s={};for(var u in o){var a=this.fieldSearch(r,u,o),l=o[u].boost;for(var d in a)a[d]=a[d]*l;for(var d in a)d in s?s[d]+=a[d]:s[d]=a[d]}var c=[];for(var d in s)c.push({ref:d,score:s[d]});return c.sort(function(e,t){return t.score-e.score}),c},t.Index.prototype.fieldSearch=function(e,t,n){var i=n[t].bool,o=n[t].expand,r=n[t].boost,s=null,u={};return 0!==r?(e.forEach(function(e){var n=[e];1==o&&(n=this.index[t].expandToken(e));var r={};n.forEach(function(n){var o=this.index[t].getDocs(n),a=this.idf(n,t);if(s&&"AND"==i){var l={};for(var d in s)d in o&&(l[d]=o[d]);o=l}n==e&&this.fieldSearchStats(u,n,o);for(var d in o){var c=this.index[t].getTermFrequency(n,d),f=this.documentStore.getFieldLength(d,t),h=1;0!=f&&(h=1/Math.sqrt(f));var p=1;n!=e&&(p=.15*(1-(n.length-e.length)/n.length));var v=c*a*h*p;d in r?r[d]+=v:r[d]=v}},this),s=this.mergeScores(s,r,i)},this),s=this.coordNorm(s,u,e.length)):void 0},t.Index.prototype.mergeScores=function(e,t,n){if(!e)return t;if("AND"==n){var i={};for(var o in t)o in e&&(i[o]=e[o]+t[o]);return i}for(var o in t)o in e?e[o]+=t[o]:e[o]=t[o];return e},t.Index.prototype.fieldSearchStats=function(e,t,n){for(var i in n)i in e?e[i].push(t):e[i]=[t]},t.Index.prototype.coordNorm=function(e,t,n){for(var i in e)if(i in t){var o=t[i].length;e[i]=e[i]*o/n}return e},t.Index.prototype.toJSON=function(){var e={};return this._fields.forEach(function(t){e[t]=this.index[t].toJSON()},this),{version:t.version,fields:this._fields,ref:this._ref,documentStore:this.documentStore.toJSON(),index:e,pipeline:this.pipeline.toJSON()}},t.Index.prototype.use=function(e){var t=Array.prototype.slice.call(arguments,1);t.unshift(this),e.apply(this,t)},t.DocumentStore=function(e){this._save=null===e||void 0===e?!0:e,this.docs={},this.docInfo={},this.length=0},t.DocumentStore.load=function(e){var t=new this;return t.length=e.length,t.docs=e.docs,t.docInfo=e.docInfo,t._save=e.save,t},t.DocumentStore.prototype.isDocStored=function(){return this._save},t.DocumentStore.prototype.addDoc=function(t,n){this.hasDoc(t)||this.length++,this.docs[t]=this._save===!0?e(n):null},t.DocumentStore.prototype.getDoc=function(e){return this.hasDoc(e)===!1?null:this.docs[e]},t.DocumentStore.prototype.hasDoc=function(e){return e in this.docs},t.DocumentStore.prototype.removeDoc=function(e){this.hasDoc(e)&&(delete this.docs[e],delete this.docInfo[e],this.length--)},t.DocumentStore.prototype.addFieldLength=function(e,t,n){null!==e&&void 0!==e&&0!=this.hasDoc(e)&&(this.docInfo[e]||(this.docInfo[e]={}),this.docInfo[e][t]=n)},t.DocumentStore.prototype.updateFieldLength=function(e,t,n){null!==e&&void 0!==e&&0!=this.hasDoc(e)&&this.addFieldLength(e,t,n)},t.DocumentStore.prototype.getFieldLength=function(e,t){return null===e||void 0===e?0:e in this.docs&&t in this.docInfo[e]?this.docInfo[e][t]:0},t.DocumentStore.prototype.toJSON=function(){return{docs:this.docs,docInfo:this.docInfo,length:this.length,save:this._save}},t.stemmer=function(){var e={ational:"ate",tional:"tion",enci:"ence",anci:"ance",izer:"ize",bli:"ble",alli:"al",entli:"ent",eli:"e",ousli:"ous",ization:"ize",ation:"ate",ator:"ate",alism:"al",iveness:"ive",fulness:"ful",ousness:"ous",aliti:"al",iviti:"ive",biliti:"ble",logi:"log"},t={icate:"ic",ative:"",alize:"al",iciti:"ic",ical:"ic",ful:"",ness:""},n="[^aeiou]",i="[aeiouy]",o=n+"[^aeiouy]*",r=i+"[aeiou]*",s="^("+o+")?"+r+o,u="^("+o+")?"+r+o+"("+r+")?$",a="^("+o+")?"+r+o+r+o,l="^("+o+")?"+i,d=new RegExp(s),c=new RegExp(a),f=new RegExp(u),h=new RegExp(l),p=/^(.+?)(ss|i)es$/,v=/^(.+?)([^s])s$/,g=/^(.+?)eed$/,m=/^(.+?)(ed|ing)$/,y=/.$/,S=/(at|bl|iz)$/,x=new RegExp("([^aeiouylsz])\\1$"),w=new RegExp("^"+o+i+"[^aeiouwxy]$"),I=/^(.+?[^aeiou])y$/,b=/^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/,E=/^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/,D=/^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/,F=/^(.+?)(s|t)(ion)$/,_=/^(.+?)e$/,P=/ll$/,k=new RegExp("^"+o+i+"[^aeiouwxy]$"),z=function(n){var i,o,r,s,u,a,l;if(n.length<3)return n;if(r=n.substr(0,1),"y"==r&&(n=r.toUpperCase()+n.substr(1)),s=p,u=v,s.test(n)?n=n.replace(s,"$1$2"):u.test(n)&&(n=n.replace(u,"$1$2")),s=g,u=m,s.test(n)){var z=s.exec(n);s=d,s.test(z[1])&&(s=y,n=n.replace(s,""))}else if(u.test(n)){var z=u.exec(n);i=z[1],u=h,u.test(i)&&(n=i,u=S,a=x,l=w,u.test(n)?n+="e":a.test(n)?(s=y,n=n.replace(s,"")):l.test(n)&&(n+="e"))}if(s=I,s.test(n)){var z=s.exec(n);i=z[1],n=i+"i"}if(s=b,s.test(n)){var z=s.exec(n);i=z[1],o=z[2],s=d,s.test(i)&&(n=i+e[o])}if(s=E,s.test(n)){var z=s.exec(n);i=z[1],o=z[2],s=d,s.test(i)&&(n=i+t[o])}if(s=D,u=F,s.test(n)){var z=s.exec(n);i=z[1],s=c,s.test(i)&&(n=i)}else if(u.test(n)){var z=u.exec(n);i=z[1]+z[2],u=c,u.test(i)&&(n=i)}if(s=_,s.test(n)){var z=s.exec(n);i=z[1],s=c,u=f,a=k,(s.test(i)||u.test(i)&&!a.test(i))&&(n=i)}return s=P,u=c,s.test(n)&&u.test(n)&&(s=y,n=n.replace(s,"")),"y"==r&&(n=r.toLowerCase()+n.substr(1)),n};return z}(),t.Pipeline.registerFunction(t.stemmer,"stemmer"),t.stopWordFilter=function(e){return e&&t.stopWordFilter.stopWords[e]!==!0?e:void 0},t.clearStopWords=function(){t.stopWordFilter.stopWords={}},t.addStopWords=function(e){null!=e&&Array.isArray(e)!==!1&&e.forEach(function(e){t.stopWordFilter.stopWords[e]=!0},this)},t.resetStopWords=function(){t.stopWordFilter.stopWords=t.defaultStopWords},t.defaultStopWords={"":!0,a:!0,able:!0,about:!0,across:!0,after:!0,all:!0,almost:!0,also:!0,am:!0,among:!0,an:!0,and:!0,any:!0,are:!0,as:!0,at:!0,be:!0,because:!0,been:!0,but:!0,by:!0,can:!0,cannot:!0,could:!0,dear:!0,did:!0,"do":!0,does:!0,either:!0,"else":!0,ever:!0,every:!0,"for":!0,from:!0,get:!0,got:!0,had:!0,has:!0,have:!0,he:!0,her:!0,hers:!0,him:!0,his:!0,how:!0,however:!0,i:!0,"if":!0,"in":!0,into:!0,is:!0,it:!0,its:!0,just:!0,least:!0,let:!0,like:!0,likely:!0,may:!0,me:!0,might:!0,most:!0,must:!0,my:!0,neither:!0,no:!0,nor:!0,not:!0,of:!0,off:!0,often:!0,on:!0,only:!0,or:!0,other:!0,our:!0,own:!0,rather:!0,said:!0,say:!0,says:!0,she:!0,should:!0,since:!0,so:!0,some:!0,than:!0,that:!0,the:!0,their:!0,them:!0,then:!0,there:!0,these:!0,they:!0,"this":!0,tis:!0,to:!0,too:!0,twas:!0,us:!0,wants:!0,was:!0,we:!0,were:!0,what:!0,when:!0,where:!0,which:!0,"while":!0,who:!0,whom:!0,why:!0,will:!0,"with":!0,would:!0,yet:!0,you:!0,your:!0},t.stopWordFilter.stopWords=t.defaultStopWords,t.Pipeline.registerFunction(t.stopWordFilter,"stopWordFilter"),t.trimmer=function(e){if(null===e||void 0===e)throw new Error("token should not be undefined");return e.replace(/^\W+/,"").replace(/\W+$/,"")},t.Pipeline.registerFunction(t.trimmer,"trimmer"),t.InvertedIndex=function(){this.root={docs:{},df:0}},t.InvertedIndex.load=function(e){var t=new this;return t.root=e.root,t},t.InvertedIndex.prototype.addToken=function(e,t,n){for(var n=n||this.root,i=0;i<=e.length-1;){var o=e[i];o in n||(n[o]={docs:{},df:0}),i+=1,n=n[o]}var r=t.ref;n.docs[r]?n.docs[r]={tf:t.tf}:(n.docs[r]={tf:t.tf},n.df+=1)},t.InvertedIndex.prototype.hasToken=function(e){if(!e)return!1;for(var t=this.root,n=0;n0&&t.push(e);for(var i in n)"docs"!==i&&"df"!==i&&this.expandToken(e+i,t,n[i]);return t},t.InvertedIndex.prototype.toJSON=function(){return{root:this.root}},t.Configuration=function(e,n){var e=e||"";if(void 0==n||null==n)throw new Error("fields should not be null");this.config={};var i;try{i=JSON.parse(e),this.buildUserConfig(i,n)}catch(o){t.utils.warn("user configuration parse failed, will use default configuration"),this.buildDefaultConfig(n)}},t.Configuration.prototype.buildDefaultConfig=function(e){this.reset(),e.forEach(function(e){this.config[e]={boost:1,bool:"OR",expand:!1}},this)},t.Configuration.prototype.buildUserConfig=function(e,n){var i="OR",o=!1;if(this.reset(),"bool"in e&&(i=e.bool||i),"expand"in e&&(o=e.expand||o),"fields"in e)for(var r in e.fields)if(n.indexOf(r)>-1){var s=e.fields[r],u=o;void 0!=s.expand&&(u=s.expand),this.config[r]={boost:s.boost||0===s.boost?s.boost:1,bool:s.bool||i,expand:u}}else t.utils.warn("field name in user configuration not found in index instance fields");else this.addAllFields2UserConfig(i,o,n)},t.Configuration.prototype.addAllFields2UserConfig=function(e,t,n){n.forEach(function(n){this.config[n]={boost:1,bool:e,expand:t}},this)},t.Configuration.prototype.get=function(){return this.config},t.Configuration.prototype.reset=function(){this.config={}},lunr.SortedSet=function(){this.length=0,this.elements=[]},lunr.SortedSet.load=function(e){var t=new this;return t.elements=e,t.length=e.length,t},lunr.SortedSet.prototype.add=function(){var e,t;for(e=0;e1;){if(r===e)return o;e>r&&(t=o),r>e&&(n=o),i=n-t,o=t+Math.floor(i/2),r=this.elements[o]}return r===e?o:-1},lunr.SortedSet.prototype.locationFor=function(e){for(var t=0,n=this.elements.length,i=n-t,o=t+Math.floor(i/2),r=this.elements[o];i>1;)e>r&&(t=o),r>e&&(n=o),i=n-t,o=t+Math.floor(i/2),r=this.elements[o];return r>e?o:e>r?o+1:void 0},lunr.SortedSet.prototype.intersect=function(e){for(var t=new lunr.SortedSet,n=0,i=0,o=this.length,r=e.length,s=this.elements,u=e.elements;;){if(n>o-1||i>r-1)break;s[n]!==u[i]?s[n]u[i]&&i++:(t.add(s[n]),n++,i++)}return t},lunr.SortedSet.prototype.clone=function(){var e=new lunr.SortedSet;return e.elements=this.toArray(),e.length=e.elements.length,e},lunr.SortedSet.prototype.union=function(e){var t,n,i;this.length>=e.length?(t=this,n=e):(t=e,n=this),i=t.clone();for(var o=0,r=n.toArray();o { + const progressBar = document.getElementById('reading-progress-bar'); + if (!progressBar) return; + + const updateProgress = () => { + // Calculate how far the user has scrolled down the page + const scrollTop = document.documentElement.scrollTop || document.body.scrollTop; + + // Calculate the total scrollable height + const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight; + const clientHeight = document.documentElement.clientHeight || window.innerHeight; + + const windowHeight = scrollHeight - clientHeight; + + // Calculate percentage and update width + const scrollPercentage = windowHeight > 0 ? (scrollTop / windowHeight) * 100 : 0; + progressBar.style.width = scrollPercentage + '%'; + }; + + // Update initially in case the user refreshes midway down the page + updateProgress(); + + // Throttle the scroll event listener a tiny bit for performance + let isTicking = false; + window.addEventListener('scroll', () => { + if (!isTicking) { + window.requestAnimationFrame(() => { + updateProgress(); + isTicking = false; + }); + isTicking = true; + } + }); +}); diff --git a/static/js/search.js b/static/js/search.js new file mode 100644 index 0000000..e4cad9d --- /dev/null +++ b/static/js/search.js @@ -0,0 +1,223 @@ +document.addEventListener('DOMContentLoaded', () => { + const searchToggleBtn = document.querySelector('.search-toggle'); + if (!searchToggleBtn) return; + + // Check if elasticlunr is available + if (typeof elasticlunr === 'undefined') { + console.warn('Elasticlunr not loaded. Search will not work.'); + return; + } + + // Modal elements + const overlay = document.getElementById('search-overlay'); + const input = document.getElementById('search-input'); + const resultsContainer = document.getElementById('search-results'); + + if (!overlay || !input || !resultsContainer) return; + + let index = null; + let isIndexLoading = false; + let selectedIndex = -1; + let resultItems = []; + + // Initialize search index (loaded lazily on first open) + const initSearch = async () => { + if (index || isIndexLoading) return; + isIndexLoading = true; + + try { + // Fetch the Zola-generated index + const response = await fetch('/search_index.en.js'); + const jsText = await response.text(); + + // The file defines `window.searchIndex = {...}` + // We use a safe evaluation approach if it's purely assignment + // Or just append it as a script tag + const script = document.createElement('script'); + script.innerHTML = jsText; + document.body.appendChild(script); + + // Wait for it to parse + setTimeout(() => { + if (window.searchIndex) { + index = elasticlunr.Index.load(window.searchIndex); + // Search immediately if there's text + if (input.value.trim().length > 0) { + performSearch(); + } + } + }, 50); + } catch (e) { + console.error('Failed to load search index:', e); + } + }; + + // Open Modal + const openSearch = () => { + overlay.classList.add('active'); + + // Slight delay ensures the browser focuses the input after the display transition starts + setTimeout(() => { + input.focus(); + }, 50); + + initSearch(); + document.body.style.overflow = 'hidden'; // Prevent background scrolling + }; + + // Close Modal + const closeSearch = () => { + overlay.classList.remove('active'); + input.value = ''; + resultsContainer.innerHTML = ''; + document.body.style.overflow = ''; + selectedIndex = -1; + resultItems = []; + }; + + // Bind Toggle Button + searchToggleBtn.addEventListener('click', (e) => { + e.preventDefault(); + if (overlay.classList.contains('active')) { + closeSearch(); + } else { + openSearch(); + } + }); + + // Close on overlay click + overlay.addEventListener('click', (e) => { + if (e.target === overlay) closeSearch(); + }); + + // Handle Keyboard Shortcuts + document.addEventListener('keydown', (e) => { + // Ctrl+K -> Open Search + if ((e.ctrlKey || e.metaKey) && e.key === 'k') { + e.preventDefault(); + openSearch(); + } + + if (!overlay.classList.contains('active')) return; + + // Escape -> Close Search + if (e.key === 'Escape') { + closeSearch(); + } + + // Arrow Navigation + if (e.key === 'ArrowDown' || e.key === 'ArrowUp') { + e.preventDefault(); + if (resultItems.length === 0) return; + + if (e.key === 'ArrowDown') { + selectedIndex = (selectedIndex + 1) % resultItems.length; + } else { + selectedIndex = (selectedIndex - 1 + resultItems.length) % resultItems.length; + } + + updateSelection(); + } + + // Enter -> Navigate to selected + if (e.key === 'Enter') { + e.preventDefault(); + if (selectedIndex >= 0 && resultItems[selectedIndex]) { + window.location.href = resultItems[selectedIndex].href; + } else if (resultItems.length > 0) { + // Default to first item if none selected + window.location.href = resultItems[0].href; + } + } + }); + + const updateSelection = () => { + resultItems.forEach((el, i) => { + if (i === selectedIndex) { + el.classList.add('selected'); + el.scrollIntoView({ block: 'nearest' }); + } else { + el.classList.remove('selected'); + } + }); + }; + + // Perform the Search + const performSearch = () => { + if (!index) { + resultsContainer.innerHTML = '
Loading search index...
'; + return; + } + + const query = input.value.trim(); + if (query.length === 0) { + resultsContainer.innerHTML = ''; + selectedIndex = -1; + resultItems = []; + return; + } + + const options = { + bool: 'OR', + expand: true, + }; + + const results = index.search(query, options); + + if (results.length === 0) { + resultsContainer.innerHTML = `
No results found for "${query}"
`; + selectedIndex = -1; + resultItems = []; + return; + } + + // Build HTML + let html = ''; + results.forEach((res) => { + // The doc is stored inside the index.documentStore + // For Zola + elasticlunr, res.ref IS the URL. + const url = res.ref; + const doc = index.documentStore.docs[res.ref]; + + if (!doc) return; + + // Truncate summary if needed + let body = doc.body || ''; + if (body.length > 150) { + body = body.substring(0, 150) + '...'; + } + + const title = doc.title || url; + + html += ` + +
${title}
+
${body}
+
+ `; + }); + + resultsContainer.innerHTML = html; + + // Cache DOM elements for keyboard navigation + resultItems = Array.from(resultsContainer.querySelectorAll('.search-result-item')); + selectedIndex = -1; // Reset selection on new search + + // Mouse hover updates selection index + resultItems.forEach((item, i) => { + item.addEventListener('mouseenter', () => { + selectedIndex = i; + updateSelection(); + }); + }); + }; + + // Debounce the input + let debounceTimer; + input.addEventListener('input', () => { + clearTimeout(debounceTimer); + debounceTimer = setTimeout(() => { + performSearch(); + }, 150); + }); +}); diff --git a/templates/base.html b/templates/base.html index d77445f..f3b46bb 100644 --- a/templates/base.html +++ b/templates/base.html @@ -250,10 +250,30 @@ - - - + + + + + + + + + + {% block scripts %}{% endblock scripts %} diff --git a/templates/page.html b/templates/page.html index 9ca599d..3b43c7f 100644 --- a/templates/page.html +++ b/templates/page.html @@ -79,10 +79,17 @@ {% endif %} + + + {% endblock head %} {% block content %} + +

{{ page.title }}

@@ -100,6 +107,12 @@ {% endif %} + {% if page.reading_time %} + + + {% endif %}
diff --git a/templates/partials/post-card.html b/templates/partials/post-card.html index 2ea5cca..4f7ab32 100644 --- a/templates/partials/post-card.html +++ b/templates/partials/post-card.html @@ -18,6 +18,10 @@ " > {{ page.date | date(format="%Y-%m-%d") }} + {% if page.reading_time %} + · + {{ page.reading_time }} min read + {% endif %}