mirror of
https://github.com/squidfunk/mkdocs-material.git
synced 2025-01-22 19:32:07 +01:00
Added search templates + filtered rogue quantifiers
This commit is contained in:
parent
cff9983af9
commit
13ffcb490f
@ -23,9 +23,9 @@
|
|||||||
import * as lunr from "lunr"
|
import * as lunr from "lunr"
|
||||||
|
|
||||||
import {
|
import {
|
||||||
SearchArticle,
|
ArticleDocument,
|
||||||
SearchDocumentMap,
|
SearchDocumentMap,
|
||||||
SearchSection,
|
SectionDocument,
|
||||||
setupSearchDocumentMap
|
setupSearchDocumentMap
|
||||||
} from "../document"
|
} from "../document"
|
||||||
|
|
||||||
@ -79,8 +79,8 @@ export interface SearchIndex {
|
|||||||
* Search result
|
* Search result
|
||||||
*/
|
*/
|
||||||
export interface SearchResult {
|
export interface SearchResult {
|
||||||
article: SearchArticle /* Relevant article */
|
article: ArticleDocument /* Article document */
|
||||||
sections: SearchSection[] /* Relevant sections */
|
sections: SectionDocument[] /* Section documents */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------------
|
/* ----------------------------------------------------------------------------
|
||||||
@ -176,7 +176,9 @@ export class Search {
|
|||||||
* @return Search results
|
* @return Search results
|
||||||
*/
|
*/
|
||||||
public search(query: string): SearchResult[] {
|
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 */
|
/* Group sections by containing article */
|
||||||
.reduce((results, result) => {
|
.reduce((results, result) => {
|
||||||
@ -195,9 +197,9 @@ export class Search {
|
|||||||
|
|
||||||
/* Map groups to search documents */
|
/* Map groups to search documents */
|
||||||
return [...groups].map(([ref, sections]) => ({
|
return [...groups].map(([ref, sections]) => ({
|
||||||
article: this.documents.get(ref) as SearchArticle,
|
article: this.documents.get(ref) as ArticleDocument,
|
||||||
sections: sections.map(section => {
|
sections: sections.map(section => {
|
||||||
return this.documents.get(section.ref) as SearchSection
|
return this.documents.get(section.ref) as SectionDocument
|
||||||
})
|
})
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
@ -31,15 +31,15 @@ import { SearchIndexDocument } from "../_"
|
|||||||
/**
|
/**
|
||||||
* A top-level article
|
* A top-level article
|
||||||
*/
|
*/
|
||||||
export interface SearchArticle extends SearchIndexDocument {
|
export interface ArticleDocument extends SearchIndexDocument {
|
||||||
section: boolean /* Whether the section was linked */
|
section: boolean /* Whether the section was linked */
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A section of an article
|
* A section of an article
|
||||||
*/
|
*/
|
||||||
export interface SearchSection extends SearchIndexDocument {
|
export interface SectionDocument extends SearchIndexDocument {
|
||||||
article: SearchArticle /* Parent article */
|
article: ArticleDocument /* Parent article */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------------------- */
|
/* ------------------------------------------------------------------------- */
|
||||||
@ -48,8 +48,8 @@ export interface SearchSection extends SearchIndexDocument {
|
|||||||
* Search document
|
* Search document
|
||||||
*/
|
*/
|
||||||
export type SearchDocument =
|
export type SearchDocument =
|
||||||
| SearchArticle
|
| ArticleDocument
|
||||||
| SearchSection
|
| SectionDocument
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search document mapping
|
* Search document mapping
|
||||||
@ -85,7 +85,7 @@ export function setupSearchDocumentMap(
|
|||||||
|
|
||||||
/* Handle section */
|
/* Handle section */
|
||||||
if (hash) {
|
if (hash) {
|
||||||
const article = documents.get(path) as SearchArticle
|
const article = documents.get(path) as ArticleDocument
|
||||||
|
|
||||||
/* Ignore first section, override article */
|
/* Ignore first section, override article */
|
||||||
if (!article.section) {
|
if (!article.section) {
|
||||||
|
23
src/assets/javascripts/templates/index.ts
Normal file
23
src/assets/javascripts/templates/index.ts
Normal 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"
|
59
src/assets/javascripts/templates/search/_/index.tsx
Normal file
59
src/assets/javascripts/templates/search/_/index.tsx
Normal 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>
|
||||||
|
)
|
||||||
|
}
|
62
src/assets/javascripts/templates/search/article/index.tsx
Normal file
62
src/assets/javascripts/templates/search/article/index.tsx
Normal 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>
|
||||||
|
)
|
||||||
|
}
|
25
src/assets/javascripts/templates/search/index.ts
Normal file
25
src/assets/javascripts/templates/search/index.ts
Normal 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"
|
62
src/assets/javascripts/templates/search/section/index.tsx
Normal file
62
src/assets/javascripts/templates/search/section/index.tsx
Normal 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>
|
||||||
|
)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user