From 13ffcb490fdd1740a4a498c1133f74c3d649ff69 Mon Sep 17 00:00:00 2001 From: squidfunk Date: Wed, 18 Dec 2019 16:18:09 +0100 Subject: [PATCH] Added search templates + filtered rogue quantifiers --- .../javascripts/modules/search/_/index.ts | 16 ++--- .../modules/search/document/index.ts | 12 ++-- src/assets/javascripts/templates/index.ts | 23 +++++++ .../javascripts/templates/search/_/index.tsx | 59 ++++++++++++++++++ .../templates/search/article/index.tsx | 62 +++++++++++++++++++ .../javascripts/templates/search/index.ts | 25 ++++++++ .../templates/search/section/index.tsx | 62 +++++++++++++++++++ 7 files changed, 246 insertions(+), 13 deletions(-) create mode 100644 src/assets/javascripts/templates/index.ts create mode 100644 src/assets/javascripts/templates/search/_/index.tsx create mode 100644 src/assets/javascripts/templates/search/article/index.tsx create mode 100644 src/assets/javascripts/templates/search/index.ts create mode 100644 src/assets/javascripts/templates/search/section/index.tsx diff --git a/src/assets/javascripts/modules/search/_/index.ts b/src/assets/javascripts/modules/search/_/index.ts index 3d1455195..647b29344 100644 --- a/src/assets/javascripts/modules/search/_/index.ts +++ b/src/assets/javascripts/modules/search/_/index.ts @@ -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 }) })) } diff --git a/src/assets/javascripts/modules/search/document/index.ts b/src/assets/javascripts/modules/search/document/index.ts index bd363f2c6..2c462dfaf 100644 --- a/src/assets/javascripts/modules/search/document/index.ts +++ b/src/assets/javascripts/modules/search/document/index.ts @@ -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) { diff --git a/src/assets/javascripts/templates/index.ts b/src/assets/javascripts/templates/index.ts new file mode 100644 index 000000000..fbb1fee4e --- /dev/null +++ b/src/assets/javascripts/templates/index.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2016-2019 Martin Donath + * + * 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" diff --git a/src/assets/javascripts/templates/search/_/index.tsx b/src/assets/javascripts/templates/search/_/index.tsx new file mode 100644 index 000000000..abd6fdfde --- /dev/null +++ b/src/assets/javascripts/templates/search/_/index.tsx @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2016-2019 Martin Donath + * + * 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 ( +
  • + {renderArticleDocument(article)} + {...sections.map(renderSectionDocument)} +
  • + ) +} diff --git a/src/assets/javascripts/templates/search/article/index.tsx b/src/assets/javascripts/templates/search/article/index.tsx new file mode 100644 index 000000000..445731f3d --- /dev/null +++ b/src/assets/javascripts/templates/search/article/index.tsx @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2016-2019 Martin Donath + * + * 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 ( + +
    +

    {title}

    + {text.length ?

    {text}

    : undefined} +
    +
    + ) +} diff --git a/src/assets/javascripts/templates/search/index.ts b/src/assets/javascripts/templates/search/index.ts new file mode 100644 index 000000000..90cd73a21 --- /dev/null +++ b/src/assets/javascripts/templates/search/index.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2016-2019 Martin Donath + * + * 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" diff --git a/src/assets/javascripts/templates/search/section/index.tsx b/src/assets/javascripts/templates/search/section/index.tsx new file mode 100644 index 000000000..e6c809c26 --- /dev/null +++ b/src/assets/javascripts/templates/search/section/index.tsx @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2016-2019 Martin Donath + * + * 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 ( + +
    +

    {title}

    + {text.length ?

    {text}

    : undefined} +
    +
    + ) +}