Skip to content

Commit

Permalink
搜索换成fuse.js后可以正常使用了
Browse files Browse the repository at this point in the history
  • Loading branch information
rebron1900 committed Mar 26, 2024
1 parent 9b6b916 commit 853ddfe
Show file tree
Hide file tree
Showing 9 changed files with 1,923 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-terser": "^0.4.4",
"cheerio": "^1.0.0-rc.12",
"fuse.js": "^7.0.0",
"highlight.js": "^11.9.0",
"jsdom": "^24.0.0",
"medium-zoom": "^1.1.0",
Expand Down
6 changes: 5 additions & 1 deletion src/assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import '../sass/book.scss';

import Alpine from 'alpinejs'
import mediumZoom from 'medium-zoom';
import search from './search';


window.Alpine = Alpine
Expand All @@ -20,4 +21,7 @@ const images = document.querySelectorAll('.markdown img');
mediumZoom(images, {
background: 'rgba(0,0,0,0.75)',
container: '.medium-zoom-overlay'
});
});


search()
1,759 changes: 1,759 additions & 0 deletions src/assets/js/mermaid.min.js

Large diffs are not rendered by default.

123 changes: 123 additions & 0 deletions src/assets/js/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
const Fuse = require('fuse.js')


module.exports = () => {
const searchDataURL = "/assets/search-data.json";
const indexConfig = Object.assign(
{
encode: false,
tokenize: function (str) {
return str.replace(/[\x00-\x7F]/g, "").split("");
},
},
{
includeScore: true,
useExtendedSearch: true,
fieldNormWeight: 1.5,
threshold: 0.2,
ignoreLocation: true,
keys: [
{
name: "title",
weight: 0.7,
},
{
name: "content",
weight: 0.2,
},
{
name: "section",
weight: 0.1,
}
],
}
);

const input = document.querySelector("#book-search-input");
const results = document.querySelector("#book-search-results");

if (!input) {
return;
}

input.addEventListener("focus", init);
input.addEventListener("keyup", search);

document.addEventListener("keypress", focusSearchFieldOnKeyPress);

/**
* @param {Event} event
*/
function focusSearchFieldOnKeyPress(event) {
if (event.target.value !== undefined) {
return;
}

if (input === document.activeElement) {
return;
}

const characterPressed = String.fromCharCode(event.charCode);
if (!isHotkey(characterPressed)) {
return;
}

input.focus();
event.preventDefault();
}

/**
* @param {String} character
* @returns {Boolean}
*/
function isHotkey(character) {
const dataHotkeys = input.getAttribute("data-hotkeys") || "";
return dataHotkeys.indexOf(character) >= 0;
}

function init() {
input.removeEventListener("focus", init); // init once
input.required = true;

fetch(searchDataURL)
.then((pages) => pages.json())
.then((pages) => {
window.bookSearchIndex = new Fuse(pages, indexConfig);
})
.then(() => (input.required = false))
.then(search);
}

function search() {
while (results.firstChild) {
results.removeChild(results.firstChild);
}

if (!input.value) {
return;
}

const searchHits = window.bookSearchIndex.search(input.value).slice(0,10);
searchHits.forEach(function (page) {
const li = element("<li><a href></a><small></small></li>");
const a = li.querySelector("a"),
small = li.querySelector("small");

a.href = page.item.href;
a.textContent = page.item.title;
small.textContent = page.item.section;

results.appendChild(li);
});
}

/**
* @param {String} content
* @returns {Node}
*/
function element(content) {
const div = document.createElement("div");
div.innerHTML = content;
return div.firstChild;
}
};
1 change: 0 additions & 1 deletion src/site/_includes/partials/head.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
{% endcomment %}


{% comment %}
{{- if default true .Site.Params.BookSearch -}}
{{- $searchJSFile := printf "%s.search.js" .Language.Lang }}
Expand Down
2 changes: 1 addition & 1 deletion src/site/_includes/partials/list.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{% comment %} {{ partial "docs/post-meta" . }} {% endcomment %}
{% include 'partials/post-meta',img:img %}
<p>
{{ post.html | excerpt: 200 }}
{{ post.excerpt | strip_html | truncate: 150 }}
</p>
{% comment %} <a href="{{ post.slug | articleUrl }}">...</a> {% endcomment %}
</article>
Expand Down
18 changes: 16 additions & 2 deletions src/site/page.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ permalink: /{{ page.slug }}/
<h1>
<a href="{{ page.slug | articleUrl }}">{{ page.title }}</a>
</h1>
{% include "partials/post-meta" %}
{{ page.html }}
</article>
{% endblock %}
Expand All @@ -23,4 +22,19 @@ permalink: /{{ page.slug }}/
{% include 'partials/footer' %}
{% endblock %}

{% block h-toc %}{% render 'partials/toc',post:post %}{% endblock %}

{% block toc %}
{% render 'partials/toc',post:page %}
{% endblock %}


{% block h-toc %}
<aside class="hidden clearfix">
{% render 'partials/toc',post:page %}
</aside>
{% endblock %}



{% block pagination %}{% endblock %}

13 changes: 13 additions & 0 deletions src/site/search-data.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
permalink: /assets/search-data.json
---
[
{% for post in collections.posts %}
{
"id": {{ forloop.index }},
"href": "{{ post.slug | articleUrl }}",
"title": {{ post.title | json}},{% assign all_categories = post.tags | map: "name" %}
"section": "{{ all_categories | join: "," }}",
"content": {{ post.html | strip_html | strip_newlines | escape | json }}
}{% if forloop.last != true %},{% endif %}{% endfor %}
]
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2828,6 +2828,11 @@ functions-have-names@^1.2.3:
resolved "https://registry.yarnpkg.com/functions-have-names/-/functions-have-names-1.2.3.tgz#0404fe4ee2ba2f607f0e0ec3c80bae994133b834"
integrity sha512-xckBUXyTIqT97tq2x2AMb+g163b5JFysYk0x4qxNFwbfQkmNZoiRHb6sPzI9/QV33WeuvVYBUIiD4NzNIyqaRQ==

fuse.js@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/fuse.js/-/fuse.js-7.0.0.tgz#6573c9fcd4c8268e403b4fc7d7131ffcf99a9eb2"
integrity sha512-14F4hBIxqKvD4Zz/XjDc3y94mNZN6pRv3U13Udo0lNLCWRBUsrMv2xwcF/y/Z5sV6+FQW+/ow68cHpm4sunt8Q==

generic-names@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/generic-names/-/generic-names-4.0.0.tgz#0bd8a2fd23fe8ea16cbd0a279acd69c06933d9a3"
Expand Down

0 comments on commit 853ddfe

Please sign in to comment.