additional-js

This commit is contained in:
Nebel 2024-02-06 14:33:31 +09:00
parent fca7481ef2
commit c8688c941e
Signed by: nebel
GPG key ID: 79807D08C6EF6460
3 changed files with 33 additions and 3 deletions

View file

@ -1,9 +1,10 @@
[book]
authors = ["Kohei Watanabe"]
language = "ja"
title = "Node.jsを使う"
authors = ["Kohei Watanabe"]
[output.html]
site-url = "/nodejs-hands-on/"
git-repository-url = "https://github.com/kou029w/nodejs-hands-on"
additional-js = ["external-links.js", "regexp-search.js"]
edit-url-template = "https://github.com/kou029w/nodejs-hands-on/edit/main/{path}"
git-repository-url = "https://github.com/kou029w/nodejs-hands-on"
site-url = "/nodejs-hands-on/"

13
external-links.js Normal file
View file

@ -0,0 +1,13 @@
"use strict";
// 外部リンクを新しいブラウザーコンテキストで開く
window.addEventListener("DOMContentLoaded", () => {
const selector = ["http:", "https:"]
.map((scheme) => `a[href^="${scheme}"]`)
.join(",");
document.querySelectorAll(selector).forEach((a) => {
a.target = "_blank";
a.rel = "noreferrer";
});
});

16
regexp-search.js Normal file
View file

@ -0,0 +1,16 @@
"use strict";
// 正規表現検索を有効化
window.elasticlunr.Index.load = (index) => ({
search(keyword) {
const regexp = new RegExp(keyword, "gi");
return [...Object.entries(index.documentStore.docs)].flatMap(
([ref, doc]) => {
const match = `${doc.title} ${doc.body}`.match(regexp);
return match ? [{ ref, doc, score: match.length }] : [];
},
);
},
});