From c62d3050ad1adfcb765cfa4f5bfe102bf228ea3d Mon Sep 17 00:00:00 2001 From: squidfunk Date: Wed, 18 Dec 2019 10:06:03 +0100 Subject: [PATCH] Restructured project --- .../{theme/action => actions}/header/index.ts | 0 .../{theme/action => actions}/hidden/index.ts | 0 .../{theme/action => actions}/index.ts | 0 .../action => actions}/sidebar/index.ts | 0 .../Material/Source/Adapter/GitHub.js | 95 -------- .../component => components}/_/index.ts | 10 +- .../header/_/index.ts | 2 +- .../component => components}/header/index.ts | 0 .../header/offset/index.ts | 2 +- .../header/shadow/index.ts | 5 +- .../component => components}/hidden/index.ts | 4 +- .../{theme/component => components}/index.ts | 0 .../component => components}/main/index.ts | 2 +- .../component => components}/search/index.ts | 0 .../search/query/index.ts | 0 .../search/reset/index.ts | 0 .../component => components}/sidebar/index.ts | 4 +- .../{theme => extensions}/index.ts | 5 +- .../{config => extensions/rxjs/_}/index.ts | 29 ++- .../element => extensions/rxjs}/index.ts | 37 +--- .../rxjs/operators}/index.ts | 19 -- src/assets/javascripts/modules/index.ts | 23 ++ .../javascripts/modules/search/_/index.ts | 202 ++++++++++++++++++ .../modules/search/document/index.ts | 107 ++++++++++ .../javascripts/modules/search/index.ts | 24 +++ .../{ => utilities}/agent/document/index.ts | 0 .../{ => utilities}/agent/element/index.ts | 0 .../{ => utilities}/agent/index.ts | 0 .../{ => utilities}/agent/location/index.ts | 0 .../{ => utilities}/agent/media/index.ts | 0 .../{ => utilities}/agent/viewport/index.ts | 0 .../{ => utilities}/agent/worker/index.ts | 0 src/assets/javascripts/utilities/index.ts | 4 +- .../{theme => utilities}/toggle/index.ts | 0 34 files changed, 386 insertions(+), 188 deletions(-) rename src/assets/javascripts/{theme/action => actions}/header/index.ts (100%) rename src/assets/javascripts/{theme/action => actions}/hidden/index.ts (100%) rename src/assets/javascripts/{theme/action => actions}/index.ts (100%) rename src/assets/javascripts/{theme/action => actions}/sidebar/index.ts (100%) delete mode 100644 src/assets/javascripts/components/Material/Source/Adapter/GitHub.js rename src/assets/javascripts/{theme/component => components}/_/index.ts (94%) rename src/assets/javascripts/{theme/component => components}/header/_/index.ts (98%) rename src/assets/javascripts/{theme/component => components}/header/index.ts (100%) rename src/assets/javascripts/{theme/component => components}/header/offset/index.ts (98%) rename src/assets/javascripts/{theme/component => components}/header/shadow/index.ts (96%) rename src/assets/javascripts/{theme/component => components}/hidden/index.ts (95%) rename src/assets/javascripts/{theme/component => components}/index.ts (100%) rename src/assets/javascripts/{theme/component => components}/main/index.ts (98%) rename src/assets/javascripts/{theme/component => components}/search/index.ts (100%) rename src/assets/javascripts/{theme/component => components}/search/query/index.ts (100%) rename src/assets/javascripts/{theme/component => components}/search/reset/index.ts (100%) rename src/assets/javascripts/{theme/component => components}/sidebar/index.ts (98%) rename src/assets/javascripts/{theme => extensions}/index.ts (91%) rename src/assets/javascripts/{config => extensions/rxjs/_}/index.ts (69%) rename src/assets/javascripts/{utilities/element => extensions/rxjs}/index.ts (56%) rename src/assets/javascripts/{utilities/operator => extensions/rxjs/operators}/index.ts (85%) create mode 100644 src/assets/javascripts/modules/index.ts create mode 100644 src/assets/javascripts/modules/search/_/index.ts create mode 100644 src/assets/javascripts/modules/search/document/index.ts create mode 100644 src/assets/javascripts/modules/search/index.ts rename src/assets/javascripts/{ => utilities}/agent/document/index.ts (100%) rename src/assets/javascripts/{ => utilities}/agent/element/index.ts (100%) rename src/assets/javascripts/{ => utilities}/agent/index.ts (100%) rename src/assets/javascripts/{ => utilities}/agent/location/index.ts (100%) rename src/assets/javascripts/{ => utilities}/agent/media/index.ts (100%) rename src/assets/javascripts/{ => utilities}/agent/viewport/index.ts (100%) rename src/assets/javascripts/{ => utilities}/agent/worker/index.ts (100%) rename src/assets/javascripts/{theme => utilities}/toggle/index.ts (100%) diff --git a/src/assets/javascripts/theme/action/header/index.ts b/src/assets/javascripts/actions/header/index.ts similarity index 100% rename from src/assets/javascripts/theme/action/header/index.ts rename to src/assets/javascripts/actions/header/index.ts diff --git a/src/assets/javascripts/theme/action/hidden/index.ts b/src/assets/javascripts/actions/hidden/index.ts similarity index 100% rename from src/assets/javascripts/theme/action/hidden/index.ts rename to src/assets/javascripts/actions/hidden/index.ts diff --git a/src/assets/javascripts/theme/action/index.ts b/src/assets/javascripts/actions/index.ts similarity index 100% rename from src/assets/javascripts/theme/action/index.ts rename to src/assets/javascripts/actions/index.ts diff --git a/src/assets/javascripts/theme/action/sidebar/index.ts b/src/assets/javascripts/actions/sidebar/index.ts similarity index 100% rename from src/assets/javascripts/theme/action/sidebar/index.ts rename to src/assets/javascripts/actions/sidebar/index.ts diff --git a/src/assets/javascripts/components/Material/Source/Adapter/GitHub.js b/src/assets/javascripts/components/Material/Source/Adapter/GitHub.js deleted file mode 100644 index cca7a21c1..000000000 --- a/src/assets/javascripts/components/Material/Source/Adapter/GitHub.js +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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 Abstract from "./Abstract" - -/* ---------------------------------------------------------------------------- - * Class - * ------------------------------------------------------------------------- */ - -export default class GitHub extends Abstract { - - /** - * Retrieve repository information from GitHub - * - * @constructor - * - * @property {string} name_ - Name of the repository - * - * @param {(string|HTMLAnchorElement)} el - Selector or HTML element - */ - constructor(el) { - super(el) - - /* Extract user (and repository name) from URL, as we have to query for all - repositories, to omit 404 errors for private repositories */ - const matches = /^.+github\.com\/([^/]+)\/?([^/]+)?.*$/ - .exec(this.base_) - if (matches && matches.length === 3) { - const [, user, name] = matches - - /* Initialize base URL and repository name */ - this.base_ = `https://api.github.com/users/${user}/repos` - this.name_ = name - } - } - - /** - * Fetch relevant repository information from GitHub - * - * @return {Promise>} Promise returning an array of facts - */ - fetch_() { - const paginate = (page = 0) => ( - fetch(`${this.base_}?per_page=100&sort=updated&page=${page}`) - .then(response => response.json()) - .then(data => { - if (!(data instanceof Array)) - return [] - - /* Display number of stars and forks, if repository is given */ - if (this.name_) { - const repo = data.find(item => item.name === this.name_) - if (!repo && data.length === 30) - return paginate(page + 1) - - /* If we found a repo, extract the facts */ - return repo - ? [ - `${this.format_(repo.stargazers_count)} Stars`, - `${this.format_(repo.forks_count)} Forks` - ] - : [] - - /* Display number of repositories, otherwise */ - } else { - return [ - `${data.length} Repositories` - ] - } - }) - ) - - /* Paginate through repos */ - return paginate() - } -} diff --git a/src/assets/javascripts/theme/component/_/index.ts b/src/assets/javascripts/components/_/index.ts similarity index 94% rename from src/assets/javascripts/theme/component/_/index.ts rename to src/assets/javascripts/components/_/index.ts index 490ec4360..a50552ab1 100644 --- a/src/assets/javascripts/theme/component/_/index.ts +++ b/src/assets/javascripts/components/_/index.ts @@ -21,10 +21,10 @@ */ import { keys } from "ramda" -import { NEVER, Observable, OperatorFunction, merge, of, pipe } from "rxjs" +import { NEVER, Observable, OperatorFunction, of, pipe } from "rxjs" import { map, scan, shareReplay, switchMap } from "rxjs/operators" -import { getElement } from "../../../utilities" +import { getElement } from "../../utilities" /* ---------------------------------------------------------------------------- * Types @@ -62,7 +62,7 @@ export type ComponentMap = { * Options */ interface Options { - load$: Observable /* Document observable */ + document$: Observable /* Document observable */ } /* ---------------------------------------------------------------------------- @@ -81,9 +81,9 @@ interface Options { * @return Component map observable */ export function watchComponentMap( - names: Component[], { load$ }: Options + names: Component[], { document$ }: Options ): Observable { - const components$ = load$ + const components$ = document$ .pipe( /* Build component map */ diff --git a/src/assets/javascripts/theme/component/header/_/index.ts b/src/assets/javascripts/components/header/_/index.ts similarity index 98% rename from src/assets/javascripts/theme/component/header/_/index.ts rename to src/assets/javascripts/components/header/_/index.ts index 21ec46220..7a3848018 100644 --- a/src/assets/javascripts/theme/component/header/_/index.ts +++ b/src/assets/javascripts/components/header/_/index.ts @@ -55,7 +55,7 @@ export function watchHeader( const sticky = getComputedStyle(el) .getPropertyValue("position") === "fixed" - /* Return header as hot observable */ + /* Return header as hot observable */ return of({ sticky, height: sticky ? el.offsetHeight : 0 diff --git a/src/assets/javascripts/theme/component/header/index.ts b/src/assets/javascripts/components/header/index.ts similarity index 100% rename from src/assets/javascripts/theme/component/header/index.ts rename to src/assets/javascripts/components/header/index.ts diff --git a/src/assets/javascripts/theme/component/header/offset/index.ts b/src/assets/javascripts/components/header/offset/index.ts similarity index 98% rename from src/assets/javascripts/theme/component/header/offset/index.ts rename to src/assets/javascripts/components/header/offset/index.ts index 2d4ccb1f1..354ab5bc1 100644 --- a/src/assets/javascripts/theme/component/header/offset/index.ts +++ b/src/assets/javascripts/components/header/offset/index.ts @@ -28,7 +28,7 @@ import { switchMapTo } from "rxjs/operators" -import { ViewportOffset, ViewportSize } from "../../../../ui" +import { ViewportOffset, ViewportSize } from "../../../utilities" import { Header } from "../_" /* ---------------------------------------------------------------------------- diff --git a/src/assets/javascripts/theme/component/header/shadow/index.ts b/src/assets/javascripts/components/header/shadow/index.ts similarity index 96% rename from src/assets/javascripts/theme/component/header/shadow/index.ts rename to src/assets/javascripts/components/header/shadow/index.ts index d3e5d02ea..aeb105493 100644 --- a/src/assets/javascripts/theme/component/header/shadow/index.ts +++ b/src/assets/javascripts/components/header/shadow/index.ts @@ -32,10 +32,7 @@ import { tap } from "rxjs/operators" -import { - resetHeaderShadow, - setHeaderShadow -} from "../../../action" +import { resetHeaderShadow, setHeaderShadow } from "../../../actions" import { Main } from "../../main" /* ---------------------------------------------------------------------------- diff --git a/src/assets/javascripts/theme/component/hidden/index.ts b/src/assets/javascripts/components/hidden/index.ts similarity index 95% rename from src/assets/javascripts/theme/component/hidden/index.ts rename to src/assets/javascripts/components/hidden/index.ts index b9ebe5c07..f0dce55dc 100644 --- a/src/assets/javascripts/theme/component/hidden/index.ts +++ b/src/assets/javascripts/components/hidden/index.ts @@ -29,8 +29,8 @@ import { tap } from "rxjs/operators" -import { ViewportOffset } from "../../../ui" -import { resetHidden, setHidden } from "../../action" +import { resetHidden, setHidden } from "../../actions" +import { ViewportOffset } from "../../utilities" /* ---------------------------------------------------------------------------- * Functions diff --git a/src/assets/javascripts/theme/component/index.ts b/src/assets/javascripts/components/index.ts similarity index 100% rename from src/assets/javascripts/theme/component/index.ts rename to src/assets/javascripts/components/index.ts diff --git a/src/assets/javascripts/theme/component/main/index.ts b/src/assets/javascripts/components/main/index.ts similarity index 98% rename from src/assets/javascripts/theme/component/main/index.ts rename to src/assets/javascripts/components/main/index.ts index 961e91883..994229304 100644 --- a/src/assets/javascripts/theme/component/main/index.ts +++ b/src/assets/javascripts/components/main/index.ts @@ -28,7 +28,7 @@ import { shareReplay } from "rxjs/operators" -import { ViewportOffset, ViewportSize } from "../../../ui" +import { ViewportOffset, ViewportSize } from "../../utilities" import { Header } from "../header" /* ---------------------------------------------------------------------------- diff --git a/src/assets/javascripts/theme/component/search/index.ts b/src/assets/javascripts/components/search/index.ts similarity index 100% rename from src/assets/javascripts/theme/component/search/index.ts rename to src/assets/javascripts/components/search/index.ts diff --git a/src/assets/javascripts/theme/component/search/query/index.ts b/src/assets/javascripts/components/search/query/index.ts similarity index 100% rename from src/assets/javascripts/theme/component/search/query/index.ts rename to src/assets/javascripts/components/search/query/index.ts diff --git a/src/assets/javascripts/theme/component/search/reset/index.ts b/src/assets/javascripts/components/search/reset/index.ts similarity index 100% rename from src/assets/javascripts/theme/component/search/reset/index.ts rename to src/assets/javascripts/components/search/reset/index.ts diff --git a/src/assets/javascripts/theme/component/sidebar/index.ts b/src/assets/javascripts/components/sidebar/index.ts similarity index 98% rename from src/assets/javascripts/theme/component/sidebar/index.ts rename to src/assets/javascripts/components/sidebar/index.ts index e2a4c80b1..babf519bd 100644 --- a/src/assets/javascripts/theme/component/sidebar/index.ts +++ b/src/assets/javascripts/components/sidebar/index.ts @@ -37,13 +37,13 @@ import { tap } from "rxjs/operators" -import { ViewportOffset } from "../../../ui" import { resetSidebarHeight, resetSidebarLock, setSidebarHeight, setSidebarLock -} from "../../action" +} from "../../actions" +import { ViewportOffset } from "../../utilities" import { Main } from "../main" /* ---------------------------------------------------------------------------- diff --git a/src/assets/javascripts/theme/index.ts b/src/assets/javascripts/extensions/index.ts similarity index 91% rename from src/assets/javascripts/theme/index.ts rename to src/assets/javascripts/extensions/index.ts index acf22a88c..de4aa7bca 100644 --- a/src/assets/javascripts/theme/index.ts +++ b/src/assets/javascripts/extensions/index.ts @@ -20,7 +20,4 @@ * IN THE SOFTWARE. */ -export * from "./action" -export * from "./component" -export * from "./toggle" -// export * from "./worker" +export * from "./rxjs" diff --git a/src/assets/javascripts/config/index.ts b/src/assets/javascripts/extensions/rxjs/_/index.ts similarity index 69% rename from src/assets/javascripts/config/index.ts rename to src/assets/javascripts/extensions/rxjs/_/index.ts index d89f18ef0..7e67daf43 100644 --- a/src/assets/javascripts/config/index.ts +++ b/src/assets/javascripts/extensions/rxjs/_/index.ts @@ -20,30 +20,25 @@ * IN THE SOFTWARE. */ -/* ---------------------------------------------------------------------------- - * Types - * ------------------------------------------------------------------------- */ - -/** - * Configuration - */ -export interface Config { - base: string /* Base URL */ - search: string /* Web worker URL */ -} +import { Observable } from "rxjs" +import { map } from "rxjs/operators" /* ---------------------------------------------------------------------------- * Functions * ------------------------------------------------------------------------- */ /** - * Ensure that the given value is a valid configuration + * Invert boolean value of source observable * - * @param config - Configuration + * @param toggle$ - Toggle observable * - * @return Test result + * @return Inverted toggle observable */ -export function isConfig(config: any): config is Config { - return typeof config === "object" - && typeof config.base === "string" +export function not( + toggle$: Observable +): Observable { + return toggle$ + .pipe( + map(active => !active) + ) } diff --git a/src/assets/javascripts/utilities/element/index.ts b/src/assets/javascripts/extensions/rxjs/index.ts similarity index 56% rename from src/assets/javascripts/utilities/element/index.ts rename to src/assets/javascripts/extensions/rxjs/index.ts index 2b549dfb1..ddd7e8a66 100644 --- a/src/assets/javascripts/utilities/element/index.ts +++ b/src/assets/javascripts/extensions/rxjs/index.ts @@ -20,38 +20,5 @@ * IN THE SOFTWARE. */ -/* ---------------------------------------------------------------------------- - * Functions - * ------------------------------------------------------------------------- */ - -/** - * Retrieve an element matching the query selector - * - * @template T - Element type - * - * @param selector - Query selector - * @param node - Node of reference - * - * @return Element - */ -export function getElement( - selector: string, node: ParentNode = document -): T | undefined { - return node.querySelector(selector) || undefined -} - -/** - * Retrieve all elements matching the query selector - * - * @template T - Element type - * - * @param selector - Query selector - * @param node - Node of reference - * - * @return Elements - */ -export function getElements( - selector: string, node: ParentNode = document -): T[] { - return Array.from(node.querySelectorAll(selector)) -} +export * from "./_" +export * from "./operators" diff --git a/src/assets/javascripts/utilities/operator/index.ts b/src/assets/javascripts/extensions/rxjs/operators/index.ts similarity index 85% rename from src/assets/javascripts/utilities/operator/index.ts rename to src/assets/javascripts/extensions/rxjs/operators/index.ts index dd8e4146a..373ecd01f 100644 --- a/src/assets/javascripts/utilities/operator/index.ts +++ b/src/assets/javascripts/extensions/rxjs/operators/index.ts @@ -30,7 +30,6 @@ import { } from "rxjs" import { filter, - map, switchMap, takeUntil } from "rxjs/operators" @@ -39,24 +38,6 @@ import { * Functions * ------------------------------------------------------------------------- */ -/** - * Invert boolean value of source observable - * - * @param toggle$ - Toggle observable - * - * @return Inverted toggle observable - */ -export function not( - toggle$: Observable -): Observable { - return toggle$ - .pipe( - map(active => !active) - ) -} - -/* ------------------------------------------------------------------------- */ - /** * Toggle switch map with another observable * diff --git a/src/assets/javascripts/modules/index.ts b/src/assets/javascripts/modules/index.ts new file mode 100644 index 000000000..fbb1fee4e --- /dev/null +++ b/src/assets/javascripts/modules/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/modules/search/_/index.ts b/src/assets/javascripts/modules/search/_/index.ts new file mode 100644 index 000000000..9a656bafa --- /dev/null +++ b/src/assets/javascripts/modules/search/_/index.ts @@ -0,0 +1,202 @@ +/* + * 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 * as lunr from "lunr" +import { compress, decompress } from "lz-string" + +import { + SearchArticle, + SearchDocumentMap, + SearchSection, + setupSearchDocumentMap +} from "../document" + +/* ---------------------------------------------------------------------------- + * Types + * ------------------------------------------------------------------------- */ + +/** + * Search index configuration + */ +export interface SearchIndexConfig { + lang: string[] /* Search languages */ + separator: string /* Search separator */ +} + +/** + * Search index document + */ +export interface SearchIndexDocument { + location: string /* Document location */ + title: string /* Document title */ + text: string /* Document text */ +} + +/** + * Search index + * + * This interfaces describes the format of the `search_index.json` file which + * is automatically built by the MkDocs search plugin. + */ +export interface SearchIndex { + config: SearchIndexConfig /* Search index configuration */ + docs: SearchIndexDocument[] /* Search index documents */ + index?: object | string /* Pre-built or serialized index */ +} + +/* ------------------------------------------------------------------------- */ + +/** + * Search result + */ +export interface SearchResult { + article: SearchArticle /* Relevant article */ + sections: SearchSection[] /* Relevant sections */ +} + +/* ---------------------------------------------------------------------------- + * Function types + * ------------------------------------------------------------------------- */ + +/** + * Options + */ +interface Options { + pipeline: { + trimmer: boolean /* Add trimmer to pipeline */ + stopwords: boolean /* Add stopword filter to pipeline */ + } +} + +/* ---------------------------------------------------------------------------- + * Class + * ------------------------------------------------------------------------- */ + +export class Search { + + /** + * Search document mapping + * + * A mapping of URLs (including hash fragments) to the actual articles and + * sections of the documentation. The search document mapping must be created + * regardless of whether the index was prebuilt or not, as lunr itself will + * only store the actual index. + */ + protected documents: SearchDocumentMap + + /** + * The lunr search index + */ + protected index: lunr.Index + + /** + * Create a search index + * + * @param index - Search index + * @param options - Options + */ + public constructor({ docs, index }: SearchIndex, options: Options) { + this.documents = setupSearchDocumentMap(docs) + + /* If no index was given, create it */ + if (typeof index === "undefined") { + this.index = lunr(function() { + + /* Remove stemmer, as it cripples search experience */ + this.pipeline.reset() + if (options.pipeline.trimmer) + this.pipeline.add(lunr.trimmer) + if (options.pipeline.stopwords) + this.pipeline.add(lunr.stopWordFilter) + + /* Setup fields and reference */ + this.field("title", { boost: 10 }) + this.field("text") + this.ref("location") + + /* Index documents */ + for (const doc of docs) + this.add(doc) + }) + + /* Serialized and compressed index */ + } else if (typeof index === "string") { + this.index = lunr.Index.load(JSON.parse(decompress(index))) + + /* Prebuilt index */ + } else { + this.index = lunr.Index.load(index) + } + } + + /** + * Search for matching documents + * + * The search index which MkDocs provides is divided up into articles, which + * contain the whole content of the individual pages, and sections, which only + * contain the contents of the subsections obtained by breaking the individual + * pages up at `h1` ... `h6`. As there may be many sections on different pages + * with indentical titles (for example within this very project, e.g. "Usage" + * or "Installation"), they need to be put into the context of the containing + * page. For this reason, section results are grouped within their respective + * articles which are the top-level results that are returned. + * + * @param query - Query string + * + * @return Search results + */ + public search(query: string): SearchResult[] { + const groups = this.index.search(query) + + /* Group sections results by article */ + .reduce((results, result) => { + const document = this.documents.get(result.ref) + if (typeof document !== "undefined") { + if ("article" in document) { + const ref = document.article.location + results.set(ref, [...results.get(ref) || [], result]) + } else { + const ref = document.location + results.set(ref, results.get(ref) || []) + } + } + return results + }, new Map()) + + /* Map groups to search documents */ + return [...groups].map(([ref, sections]) => ({ + article: this.documents.get(ref) as SearchArticle, + sections: sections.map(section => { + return this.documents.get(section.ref) as SearchSection + }) + })) + } + + /** + * Serialize and compress the index + * + * @return Serialized and compressed index + */ + public toString(): string { + return compress(JSON.stringify(this.index)) + } +} diff --git a/src/assets/javascripts/modules/search/document/index.ts b/src/assets/javascripts/modules/search/document/index.ts new file mode 100644 index 000000000..bd363f2c6 --- /dev/null +++ b/src/assets/javascripts/modules/search/document/index.ts @@ -0,0 +1,107 @@ +/* + * 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 * as escapeHTML from "escape-html" + +import { SearchIndexDocument } from "../_" + +/* ---------------------------------------------------------------------------- + * Types + * ------------------------------------------------------------------------- */ + +/** + * A top-level article + */ +export interface SearchArticle extends SearchIndexDocument { + section: boolean /* Whether the section was linked */ +} + +/** + * A section of an article + */ +export interface SearchSection extends SearchIndexDocument { + article: SearchArticle /* Parent article */ +} + +/* ------------------------------------------------------------------------- */ + +/** + * Search document + */ +export type SearchDocument = + | SearchArticle + | SearchSection + +/** + * Search document mapping + */ +export type SearchDocumentMap = Map + +/* ---------------------------------------------------------------------------- + * Functions + * ------------------------------------------------------------------------- */ + +/** + * Build a search document mapping + * + * @param docs - Search index documents + * + * @return Search document map + */ +export function setupSearchDocumentMap( + docs: SearchIndexDocument[] +): SearchDocumentMap { + const documents = new Map() + for (const doc of docs) { + const [path, hash] = doc.location.split("#") + + /* Extract location and title */ + const location = doc.location + const title = doc.title + + /* Escape and cleanup text */ + const text = escapeHTML(doc.text) + .replace(/\s+(?=[,.:;!?])/g, "") + .replace(/\s+/g, " ") + + /* Handle section */ + if (hash) { + const article = documents.get(path) as SearchArticle + + /* Ignore first section, override article */ + if (!article.section) { + article.title = doc.title + article.text = text + article.section = true + + /* Add subsequent section */ + } else { + documents.set(location, { location, title, text, article }) + } + + /* Add article */ + } else { + documents.set(location, { location, title, text, section: false }) + } + } + return documents +} diff --git a/src/assets/javascripts/modules/search/index.ts b/src/assets/javascripts/modules/search/index.ts new file mode 100644 index 000000000..af5aeae1e --- /dev/null +++ b/src/assets/javascripts/modules/search/index.ts @@ -0,0 +1,24 @@ +/* + * 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 "./document" diff --git a/src/assets/javascripts/agent/document/index.ts b/src/assets/javascripts/utilities/agent/document/index.ts similarity index 100% rename from src/assets/javascripts/agent/document/index.ts rename to src/assets/javascripts/utilities/agent/document/index.ts diff --git a/src/assets/javascripts/agent/element/index.ts b/src/assets/javascripts/utilities/agent/element/index.ts similarity index 100% rename from src/assets/javascripts/agent/element/index.ts rename to src/assets/javascripts/utilities/agent/element/index.ts diff --git a/src/assets/javascripts/agent/index.ts b/src/assets/javascripts/utilities/agent/index.ts similarity index 100% rename from src/assets/javascripts/agent/index.ts rename to src/assets/javascripts/utilities/agent/index.ts diff --git a/src/assets/javascripts/agent/location/index.ts b/src/assets/javascripts/utilities/agent/location/index.ts similarity index 100% rename from src/assets/javascripts/agent/location/index.ts rename to src/assets/javascripts/utilities/agent/location/index.ts diff --git a/src/assets/javascripts/agent/media/index.ts b/src/assets/javascripts/utilities/agent/media/index.ts similarity index 100% rename from src/assets/javascripts/agent/media/index.ts rename to src/assets/javascripts/utilities/agent/media/index.ts diff --git a/src/assets/javascripts/agent/viewport/index.ts b/src/assets/javascripts/utilities/agent/viewport/index.ts similarity index 100% rename from src/assets/javascripts/agent/viewport/index.ts rename to src/assets/javascripts/utilities/agent/viewport/index.ts diff --git a/src/assets/javascripts/agent/worker/index.ts b/src/assets/javascripts/utilities/agent/worker/index.ts similarity index 100% rename from src/assets/javascripts/agent/worker/index.ts rename to src/assets/javascripts/utilities/agent/worker/index.ts diff --git a/src/assets/javascripts/utilities/index.ts b/src/assets/javascripts/utilities/index.ts index 4e50c4bec..7086e784e 100644 --- a/src/assets/javascripts/utilities/index.ts +++ b/src/assets/javascripts/utilities/index.ts @@ -20,5 +20,5 @@ * IN THE SOFTWARE. */ -export * from "./element" -export * from "./operator" +export * from "./agent" +export * from "./toggle" diff --git a/src/assets/javascripts/theme/toggle/index.ts b/src/assets/javascripts/utilities/toggle/index.ts similarity index 100% rename from src/assets/javascripts/theme/toggle/index.ts rename to src/assets/javascripts/utilities/toggle/index.ts