Search full-text (blogData.text), related posts snippet, minify tool, generator event guard; README base+related
This commit is contained in:
@@ -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>
|
||||
Reference in New Issue
Block a user