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) {

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2016-2019 Martin Donath <martin.donath@squidfunk.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
export * from "./search"

View File

@ -0,0 +1,59 @@
/*
* Copyright (c) 2016-2019 Martin Donath <martin.donath@squidfunk.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
import { h } from "../../../extensions"
import { SearchResult } from "../../../modules"
import { renderArticleDocument } from "../article"
import { renderSectionDocument } from "../section"
/* ----------------------------------------------------------------------------
* Data
* ------------------------------------------------------------------------- */
/**
* CSS class references
*/
const css = {
item: "md-search-result__item"
}
/* ----------------------------------------------------------------------------
* Functions
* ------------------------------------------------------------------------- */
/**
* Render a search result
*
* @param article - Search result
*
* @return JSX element
*/
export function renderSearchResult(
{ article, sections }: SearchResult
) {
return (
<li class={css.item}>
{renderArticleDocument(article)}
{...sections.map(renderSectionDocument)}
</li>
)
}

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2016-2019 Martin Donath <martin.donath@squidfunk.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
import { h } from "../../../extensions"
import { ArticleDocument } from "../../../modules"
/* ----------------------------------------------------------------------------
* Data
* ------------------------------------------------------------------------- */
/**
* CSS class references
*/
const css = {
link: "md-search-result__link",
article: "md-search-result__article md-search-result__article--document",
title: "md-search-result__title",
teaser: "md-search-result__teaser"
}
/* ----------------------------------------------------------------------------
* Functions
* ------------------------------------------------------------------------- */
/**
* Render an article document
*
* @param article - Article document
*
* @return JSX element
*/
export function renderArticleDocument(
{ location, title, text }: ArticleDocument
) {
return (
<a href={location} title={title} class={css.link} tabIndex={-1}>
<article class={css.article}>
<h1 class={css.title}>{title}</h1>
{text.length ? <p class={css.teaser}>{text}</p> : undefined}
</article>
</a>
)
}

View File

@ -0,0 +1,25 @@
/*
* Copyright (c) 2016-2019 Martin Donath <martin.donath@squidfunk.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
export * from "./_"
export * from "./article"
export * from "./section"

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2016-2019 Martin Donath <martin.donath@squidfunk.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
import { h } from "../../../extensions"
import { SectionDocument } from "../../../modules"
/* ----------------------------------------------------------------------------
* Data
* ------------------------------------------------------------------------- */
/**
* CSS class references
*/
const css = {
link: "md-search-result__link",
article: "md-search-result__article",
title: "md-search-result__title",
teaser: "md-search-result__teaser"
}
/* ----------------------------------------------------------------------------
* Functions
* ------------------------------------------------------------------------- */
/**
* Render a section document
*
* @param section - Section document
*
* @return JSX element
*/
export function renderSectionDocument(
{ location, title, text }: SectionDocument
) {
return (
<a href={location} title={title} class={css.link} tabIndex={-1}>
<article class={css.article}>
<h1 class={css.title}>{title}</h1>
{text.length ? <p class={css.teaser}>{text}</p> : undefined}
</article>
</a>
)
}