Added search templates + filtered rogue quantifiers

This commit is contained in:
squidfunk
2019-12-18 16:18:09 +01:00
parent cff9983af9
commit 13ffcb490f
7 changed files with 246 additions and 13 deletions

View File

@@ -23,9 +23,9 @@
import * as lunr from "lunr"
import {
SearchArticle,
ArticleDocument,
SearchDocumentMap,
SearchSection,
SectionDocument,
setupSearchDocumentMap
} from "../document"
@@ -79,8 +79,8 @@ export interface SearchIndex {
* Search result
*/
export interface SearchResult {
article: SearchArticle /* Relevant article */
sections: SearchSection[] /* Relevant sections */
article: ArticleDocument /* Article document */
sections: SectionDocument[] /* Section documents */
}
/* ----------------------------------------------------------------------------
@@ -176,7 +176,9 @@ export class Search {
* @return Search results
*/
public search(query: string): SearchResult[] {
const groups = this.index.search(query)
const groups = this.index.search(query
.replace(/\s+[+-](?:\s+|$)/, "") /* Filter rogue quantifiers */
)
/* Group sections by containing article */
.reduce((results, result) => {
@@ -195,9 +197,9 @@ export class Search {
/* Map groups to search documents */
return [...groups].map(([ref, sections]) => ({
article: this.documents.get(ref) as SearchArticle,
article: this.documents.get(ref) as ArticleDocument,
sections: sections.map(section => {
return this.documents.get(section.ref) as SearchSection
return this.documents.get(section.ref) as SectionDocument
})
}))
}

View File

@@ -31,15 +31,15 @@ import { SearchIndexDocument } from "../_"
/**
* A top-level article
*/
export interface SearchArticle extends SearchIndexDocument {
export interface ArticleDocument extends SearchIndexDocument {
section: boolean /* Whether the section was linked */
}
/**
* A section of an article
*/
export interface SearchSection extends SearchIndexDocument {
article: SearchArticle /* Parent article */
export interface SectionDocument extends SearchIndexDocument {
article: ArticleDocument /* Parent article */
}
/* ------------------------------------------------------------------------- */
@@ -48,8 +48,8 @@ export interface SearchSection extends SearchIndexDocument {
* Search document
*/
export type SearchDocument =
| SearchArticle
| SearchSection
| ArticleDocument
| SectionDocument
/**
* Search document mapping
@@ -85,7 +85,7 @@ export function setupSearchDocumentMap(
/* Handle section */
if (hash) {
const article = documents.get(path) as SearchArticle
const article = documents.get(path) as ArticleDocument
/* Ignore first section, override article */
if (!article.section) {