Show parent chapter's title when perform book search

This commit is contained in:
Folyd 2020-02-22 16:45:45 +08:00
parent 28dcf4c822
commit 442d39c078
2 changed files with 13 additions and 12 deletions

View file

@ -1,21 +1,21 @@
function BookSearch(bookIndex) {
this.chapters = {};
bookIndex.forEach(({name, url, chapters}) => {
chapters.forEach(([title, path]) => {
this.pages = {};
bookIndex.forEach(({name, url, pages}) => {
pages.forEach(([title, path, parentTitles]) => {
let cleanedTitle = cleanChapterTitle(title);
if (!this.chapters.hasOwnProperty(cleanedTitle)) {
this.chapters[cleanedTitle] = [];
if (!this.pages.hasOwnProperty(cleanedTitle)) {
this.pages[cleanedTitle] = [];
}
this.chapters[cleanedTitle].push({title, name, url: `${url}${path}.html`});
this.pages[cleanedTitle].push({title, name, url: `${url}${path}.html`, parentTitles});
});
});
this.chapterTitles = Object.keys(this.chapters);
this.titles = Object.keys(this.pages);
}
BookSearch.prototype.search = function(query) {
query = query.replace(/[%\s]/ig, "").toLowerCase();
let results = [];
for (let title of this.chapterTitles) {
for (let title of this.titles) {
if (title.length < query.length) continue;
let index = title.indexOf(query);
if (index > -1) {
@ -24,7 +24,7 @@ BookSearch.prototype.search = function(query) {
}
return results.sort((a, b) => a.title.length - b.title.length)
.flatMap(item => {
return this.chapters[item.title];
return this.pages[item.title];
});
};

View file

@ -94,10 +94,11 @@ omnibox.addPrefixQueryEvent("%", {
onSearch: (query) => {
return bookSearcher.search(query);
},
onFormat: (index, chapter) => {
onFormat: (index, page) => {
let parentTitles = page.parentTitles || [];
return {
content: chapter.url,
description: `${chapter.title} - ${compat.dim(chapter.name)}`
content: page.url,
description: `${ [...parentTitles, compat.match(page.title)].join(" > ") } - ${compat.dim(page.name)}`
}
}
});