mirror of
https://github.com/squidfunk/mkdocs-material.git
synced 2026-07-22 05:53:03 -04:00
Merge
This commit is contained in:
@@ -52,9 +52,9 @@ type Child = Child[] | Element | Text | string | number
|
||||
*/
|
||||
function appendChild(el: Element, child: Child): void {
|
||||
|
||||
/* Handle primitive types */
|
||||
/* Handle primitive types (including raw HTML) */
|
||||
if (typeof child === "string" || typeof child === "number") {
|
||||
el.appendChild(new Text(child.toString()))
|
||||
el.innerHTML += child.toString()
|
||||
|
||||
/* Handle nodes */
|
||||
} else if (child instanceof Node) {
|
||||
|
||||
@@ -28,6 +28,10 @@ import {
|
||||
SectionDocument,
|
||||
setupSearchDocumentMap
|
||||
} from "../document"
|
||||
import {
|
||||
SearchHighlightFactoryFn,
|
||||
setupSearchHighlighter
|
||||
} from "../highlight"
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Types
|
||||
@@ -113,6 +117,11 @@ export class Search {
|
||||
*/
|
||||
protected documents: SearchDocumentMap
|
||||
|
||||
/**
|
||||
* Search highlight factory function
|
||||
*/
|
||||
protected highlight: SearchHighlightFactoryFn
|
||||
|
||||
/**
|
||||
* The lunr search index
|
||||
*/
|
||||
@@ -124,8 +133,9 @@ export class Search {
|
||||
* @param index - Search index
|
||||
* @param options - Options
|
||||
*/
|
||||
public constructor({ docs, options, index }: SearchIndex) {
|
||||
public constructor({ config, docs, options, index }: SearchIndex) {
|
||||
this.documents = setupSearchDocumentMap(docs)
|
||||
this.highlight = setupSearchHighlighter(config)
|
||||
|
||||
/* If no index was given, create it */
|
||||
if (typeof index === "undefined") {
|
||||
@@ -171,16 +181,24 @@ export class Search {
|
||||
* page. For this reason, section results are grouped within their respective
|
||||
* articles which are the top-level results that are returned.
|
||||
*
|
||||
* Rogue control characters must be filtered before handing the query to the
|
||||
* search index, as lunr will throw otherwise.
|
||||
*
|
||||
* @param query - Query string
|
||||
*
|
||||
* @return Search results
|
||||
*/
|
||||
public search(query: string): SearchResult[] {
|
||||
const groups = this.index.search(query
|
||||
.replace(/\s+[+-](?:\s+|$)/, "") /* Filter rogue quantifiers */
|
||||
)
|
||||
query = query
|
||||
.replace(/(?:^|\s+)[+-:~^]+(?=\s+|$)/g, "")
|
||||
.trim()
|
||||
|
||||
/* Group sections by containing article */
|
||||
/* Abort early, if query is empty */
|
||||
if (!query)
|
||||
return []
|
||||
|
||||
/* Group sections by containing article */
|
||||
const groups = this.index.search(query)
|
||||
.reduce((results, result) => {
|
||||
const document = this.documents.get(result.ref)
|
||||
if (typeof document !== "undefined") {
|
||||
@@ -195,11 +213,14 @@ export class Search {
|
||||
return results
|
||||
}, new Map<string, lunr.Index.Result[]>())
|
||||
|
||||
/* Create highlighter for query */
|
||||
const fn = this.highlight(query)
|
||||
|
||||
/* Map groups to search documents */
|
||||
return [...groups].map(([ref, sections]) => ({
|
||||
article: this.documents.get(ref) as ArticleDocument,
|
||||
article: fn(this.documents.get(ref) as ArticleDocument),
|
||||
sections: sections.map(section => {
|
||||
return this.documents.get(section.ref) as SectionDocument
|
||||
return fn(this.documents.get(section.ref) as SectionDocument)
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ export type SearchDocumentMap = Map<string, SearchDocument>
|
||||
* ------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Build a search document mapping
|
||||
* Create a search document mapping
|
||||
*
|
||||
* @param docs - Search index documents
|
||||
*
|
||||
|
||||
@@ -21,10 +21,45 @@
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { compress, decompress } from "lz-string"
|
||||
import {
|
||||
compress,
|
||||
compressToUTF16,
|
||||
decompress,
|
||||
decompressFromUTF16
|
||||
} from "lz-string"
|
||||
|
||||
import { PackerMessage, PackerMessageType } from "../_"
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Data
|
||||
* ------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Determine methods for packing and unpacking
|
||||
*
|
||||
* While all Webkit-based browsers can store invalid UTF-16 strings in local
|
||||
* storage, other browsers may only store valid UTF-16 strings.
|
||||
*
|
||||
* @see https://bit.ly/2Q1ArhU - LZ-String documentation
|
||||
*/
|
||||
const isWebkit = navigator.userAgent.includes("AppleWebKit")
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Method for packing
|
||||
*/
|
||||
const pack = isWebkit
|
||||
? compress
|
||||
: compressToUTF16
|
||||
|
||||
/**
|
||||
* Method for unpacking
|
||||
*/
|
||||
const unpack = isWebkit
|
||||
? decompress
|
||||
: decompressFromUTF16
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Functions
|
||||
* ------------------------------------------------------------------------- */
|
||||
@@ -43,14 +78,14 @@ export function handler(message: PackerMessage): PackerMessage {
|
||||
case PackerMessageType.STRING:
|
||||
return {
|
||||
type: PackerMessageType.PACKED,
|
||||
data: compress(message.data)
|
||||
data: pack(message.data)
|
||||
}
|
||||
|
||||
/* Unpack a packed string */
|
||||
case PackerMessageType.PACKED:
|
||||
return {
|
||||
type: PackerMessageType.STRING,
|
||||
data: decompress(message.data)
|
||||
data: unpack(message.data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,10 @@
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { SearchIndex, SearchResult } from "../../../modules"
|
||||
import { Subject } from "rxjs"
|
||||
|
||||
import { SearchIndex, SearchResult } from "modules"
|
||||
import { watchWorker } from "utilities"
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Types
|
||||
|
||||
Reference in New Issue
Block a user