Search full-text (blogData.text), related posts snippet, minify tool, generator event guard; README base+related
This commit is contained in:
@@ -323,6 +323,8 @@
|
||||
}
|
||||
function showCanvas() {
|
||||
wrap.innerHTML = "";
|
||||
renderer.domElement.setAttribute("role", "img");
|
||||
renderer.domElement.setAttribute("aria-label", "Interactive 3D model preview");
|
||||
wrap.appendChild(renderer.domElement);
|
||||
var hint = document.createElement("div");
|
||||
hint.className = "model-viewer-hint";
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<script>
|
||||
(function () {
|
||||
var data = window.blogData;
|
||||
if (!data || !data.articles) return;
|
||||
var me = document.body.getAttribute("data-note-id");
|
||||
var all = data.articles;
|
||||
var cur = null;
|
||||
for (var i = 0; i < all.length; i++) { if (all[i].id === me) { cur = all[i]; break; } }
|
||||
if (!cur) return;
|
||||
var tags = cur.tags || [], cat = cur.category || "", scored = [];
|
||||
for (var j = 0; j < all.length; j++) {
|
||||
var a = all[j];
|
||||
if (a.id === me) continue;
|
||||
var s = 0;
|
||||
if (cat && a.category === cat) s += 2;
|
||||
for (var t = 0; t < tags.length; t++) { if ((a.tags || []).indexOf(tags[t]) >= 0) s += 1; }
|
||||
if (s > 0) scored.push({ a: a, score: s });
|
||||
}
|
||||
scored.sort(function (x, y) { return y.score - x.score; });
|
||||
var rel = scored.slice(0, 4);
|
||||
if (!rel.length) return;
|
||||
var host = document.getElementById("content");
|
||||
if (!host) return;
|
||||
var sec = document.createElement("section");
|
||||
sec.className = "blog-related";
|
||||
var h = document.createElement("h2"); h.textContent = "Related posts"; sec.appendChild(h);
|
||||
var ul = document.createElement("ul");
|
||||
rel.forEach(function (r) { var li = document.createElement("li"); var a = document.createElement("a"); a.href = r.a.url; a.textContent = r.a.title; li.appendChild(a); ul.appendChild(li); });
|
||||
sec.appendChild(ul); host.appendChild(sec);
|
||||
})();
|
||||
</script>
|
||||
+30
-30
@@ -3,46 +3,46 @@
|
||||
var old = document.querySelector(".search-item .search-input");
|
||||
if (!old) return;
|
||||
var box = old.closest(".search-item") || old.parentNode;
|
||||
|
||||
var input = document.createElement("input");
|
||||
input.type = "text";
|
||||
input.className = "blog-search-header-input";
|
||||
input.placeholder = old.placeholder || "Search...";
|
||||
old.replaceWith(input);
|
||||
|
||||
var overlay = document.createElement("div");
|
||||
overlay.className = "blog-search-overlay";
|
||||
box.appendChild(overlay);
|
||||
|
||||
function render(q) {
|
||||
q = (q || "").toLowerCase().trim();
|
||||
if (!q) { overlay.innerHTML = ""; overlay.classList.remove("open"); return; }
|
||||
var articles = (window.blogData && window.blogData.articles) || [];
|
||||
var hits = articles.filter(function (a) {
|
||||
var hay = (a.title + " " + (a.summary || "") + " " + (a.tags || []).join(" ")).toLowerCase();
|
||||
return hay.indexOf(q) !== -1;
|
||||
});
|
||||
if (!hits.length) {
|
||||
overlay.innerHTML = '<div class="blog-search-none">No results.</div>';
|
||||
} else {
|
||||
overlay.innerHTML = hits.slice(0, 8).map(function (a) {
|
||||
return '<a class="blog-search-overlay-hit" href="' + a.url + '"><span class="t">' + a.title + '</span>' +
|
||||
(a.summary ? '<span class="s">' + a.summary + '</span>' : '') + '</a>';
|
||||
}).join("");
|
||||
function esc(s){return String(s).replace(/[&<>"]/g,function(c){return({"&":"&","<":"<",">":">",'"':"""})[c];});}
|
||||
function snippet(text,word){if(!text)return"";var i=text.toLowerCase().indexOf(word);if(i<0)return"";var s=Math.max(0,i-32),e=Math.min(text.length,i+word.length+48);return(s>0?"\u2026":"")+esc(text.slice(s,e))+(e<text.length?"\u2026":"");}
|
||||
function render(q){
|
||||
q=(q||"").trim();
|
||||
if(!q){overlay.innerHTML="";overlay.classList.remove("open");return;}
|
||||
var words=q.toLowerCase().split(/\s+/).filter(Boolean);
|
||||
var articles=(window.blogData&&window.blogData.articles)||[];
|
||||
var scored=[];
|
||||
for(var i=0;i<articles.length;i++){
|
||||
var a=articles[i];
|
||||
var title=(a.title||"").toLowerCase(),sum=(a.summary||"").toLowerCase(),tag=(a.tags||[]).join(" ").toLowerCase(),body=(a.text||"").toLowerCase();
|
||||
var ok=true,score=0;
|
||||
for(var w=0;w<words.length;w++){
|
||||
var word=words[w],hit=0;
|
||||
if(title.indexOf(word)>=0){score+=6;hit++;}
|
||||
if(tag.indexOf(word)>=0){score+=4;hit++;}
|
||||
if(sum.indexOf(word)>=0){score+=3;hit++;}
|
||||
if(body.indexOf(word)>=0){score+=1;hit++;}
|
||||
if(!hit){ok=false;break;}
|
||||
}
|
||||
if(ok&&score>0)scored.push({a:a,score:score});
|
||||
}
|
||||
scored.sort(function(x,y){return y.score-x.score;});
|
||||
if(!scored.length){overlay.innerHTML='<div class="blog-search-none">No results.</div>';}
|
||||
else{var first=words[0];overlay.innerHTML=scored.slice(0,8).map(function(s){var a=s.a;var sub=snippet(a.text,first)||a.summary;return '<a class="blog-search-overlay-hit" href="'+esc(a.url)+'"><span class="t">'+esc(a.title)+'</span>'+(sub?'<span class="s">'+esc(sub)+'</span>':'')+'</a>';}).join("");}
|
||||
overlay.classList.add("open");
|
||||
}
|
||||
|
||||
var t;
|
||||
input.addEventListener("input", function () {
|
||||
clearTimeout(t);
|
||||
t = setTimeout(function () { render(input.value); }, 150);
|
||||
});
|
||||
input.addEventListener("focus", function () { if (input.value.trim()) render(input.value); });
|
||||
input.addEventListener("blur", function () { setTimeout(function () { overlay.classList.remove("open"); }, 150); });
|
||||
input.addEventListener("keydown", function (e) { if (e.key === "Escape") { overlay.classList.remove("open"); } });
|
||||
document.addEventListener("click", function (e) {
|
||||
if (!box.contains(e.target)) overlay.classList.remove("open");
|
||||
});
|
||||
input.addEventListener("input",function(){clearTimeout(t);t=setTimeout(function(){render(input.value);},150);});
|
||||
input.addEventListener("focus",function(){if(input.value.trim())render(input.value);});
|
||||
input.addEventListener("blur",function(){setTimeout(function(){overlay.classList.remove("open");},150);});
|
||||
input.addEventListener("keydown",function(e){if(e.key==="Escape")overlay.classList.remove("open");});
|
||||
document.addEventListener("click",function(e){if(!box.contains(e.target))overlay.classList.remove("open");});
|
||||
})();
|
||||
</script>
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user