mirror of
https://github.com/huhu/rust-search-extension
synced 2024-11-16 00:17:57 +00:00
Show parent chapter's title when perform book search
This commit is contained in:
parent
28dcf4c822
commit
442d39c078
2 changed files with 13 additions and 12 deletions
|
@ -1,21 +1,21 @@
|
||||||
function BookSearch(bookIndex) {
|
function BookSearch(bookIndex) {
|
||||||
this.chapters = {};
|
this.pages = {};
|
||||||
bookIndex.forEach(({name, url, chapters}) => {
|
bookIndex.forEach(({name, url, pages}) => {
|
||||||
chapters.forEach(([title, path]) => {
|
pages.forEach(([title, path, parentTitles]) => {
|
||||||
let cleanedTitle = cleanChapterTitle(title);
|
let cleanedTitle = cleanChapterTitle(title);
|
||||||
if (!this.chapters.hasOwnProperty(cleanedTitle)) {
|
if (!this.pages.hasOwnProperty(cleanedTitle)) {
|
||||||
this.chapters[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) {
|
BookSearch.prototype.search = function(query) {
|
||||||
query = query.replace(/[%\s]/ig, "").toLowerCase();
|
query = query.replace(/[%\s]/ig, "").toLowerCase();
|
||||||
let results = [];
|
let results = [];
|
||||||
for (let title of this.chapterTitles) {
|
for (let title of this.titles) {
|
||||||
if (title.length < query.length) continue;
|
if (title.length < query.length) continue;
|
||||||
let index = title.indexOf(query);
|
let index = title.indexOf(query);
|
||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
|
@ -24,7 +24,7 @@ BookSearch.prototype.search = function(query) {
|
||||||
}
|
}
|
||||||
return results.sort((a, b) => a.title.length - b.title.length)
|
return results.sort((a, b) => a.title.length - b.title.length)
|
||||||
.flatMap(item => {
|
.flatMap(item => {
|
||||||
return this.chapters[item.title];
|
return this.pages[item.title];
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -94,10 +94,11 @@ omnibox.addPrefixQueryEvent("%", {
|
||||||
onSearch: (query) => {
|
onSearch: (query) => {
|
||||||
return bookSearcher.search(query);
|
return bookSearcher.search(query);
|
||||||
},
|
},
|
||||||
onFormat: (index, chapter) => {
|
onFormat: (index, page) => {
|
||||||
|
let parentTitles = page.parentTitles || [];
|
||||||
return {
|
return {
|
||||||
content: chapter.url,
|
content: page.url,
|
||||||
description: `${chapter.title} - ${compat.dim(chapter.name)}`
|
description: `${ [...parentTitles, compat.match(page.title)].join(" > ") } - ${compat.dim(page.name)}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue