Restructured project

This commit is contained in:
squidfunk
2019-12-22 16:52:28 +01:00
parent d1928cc31f
commit e04387902c
45 changed files with 533 additions and 270 deletions

View File

@@ -55,25 +55,25 @@ export interface SearchIndexDocument {
}
/**
* Search index options
* Search index pipeline
*/
export interface SearchIndexOptions {
pipeline: {
trimmer: boolean /* Add trimmer to pipeline */
stopwords: boolean /* Add stopword filter to pipeline */
}
export interface SearchIndexPipeline {
trimmer: boolean /* Add trimmer to pipeline */
stopwords: boolean /* Add stopword filter to pipeline */
}
/* ------------------------------------------------------------------------- */
/**
* Search index
* Search index options
*
* This interfaces describes the format of the `search_index.json` file which
* is automatically built by the MkDocs search plugin.
*/
export interface SearchIndex {
export interface SearchIndexOptions {
config: SearchIndexConfig /* Search index configuration */
docs: SearchIndexDocument[] /* Search index documents */
options?: SearchIndexOptions /* Search index options */
pipeline?: SearchIndexPipeline /* Search index pipeline */
index?: object | string /* Prebuilt or serialized index */
}
@@ -87,25 +87,11 @@ export interface SearchResult {
sections: SectionDocument[] /* Section documents */
}
/* ----------------------------------------------------------------------------
* Data
* ------------------------------------------------------------------------- */
/**
* Default options
*/
const defaultOptions: SearchIndexOptions = {
pipeline: {
trimmer: true,
stopwords: true
}
}
/* ----------------------------------------------------------------------------
* Class
* ------------------------------------------------------------------------- */
export class Search {
export class SearchIndex {
/**
* Search document mapping
@@ -130,17 +116,19 @@ export class Search {
/**
* Create a search index
*
* @param index - Search index
* @param options - Options
*/
public constructor({ config, docs, options, index }: SearchIndex) {
public constructor({ config, docs, pipeline, index }: SearchIndexOptions) {
this.documents = setupSearchDocumentMap(docs)
this.highlight = setupSearchHighlighter(config)
/* If no index was given, create it */
if (typeof index === "undefined") {
this.index = lunr(function() {
const { pipeline } = options || defaultOptions
pipeline = pipeline || {
trimmer: true,
stopwords: true
}
/* Remove stemmer, as it cripples search experience */
this.pipeline.reset()
@@ -194,8 +182,8 @@ export class Search {
.reduce((results, result) => {
const document = this.documents.get(result.ref)
if (typeof document !== "undefined") {
if ("article" in document) {
const ref = document.article.location
if ("parent" in document) {
const ref = document.parent.location
results.set(ref, [...results.get(ref) || [], result])
} else {
const ref = document.location
@@ -228,7 +216,7 @@ export class Search {
}
/**
* Serialize index
* Serialize search index
*
* @return String representation
*/

View File

@@ -32,14 +32,14 @@ import { SearchIndexDocument } from "../_"
* A top-level article
*/
export interface ArticleDocument extends SearchIndexDocument {
section: boolean /* Whether the section was linked */
linked: boolean /* Whether the section was linked */
}
/**
* A section of an article
*/
export interface SectionDocument extends SearchIndexDocument {
article: ArticleDocument /* Parent article */
parent: ArticleDocument /* Parent article */
}
/* ------------------------------------------------------------------------- */
@@ -85,22 +85,22 @@ export function setupSearchDocumentMap(
/* Handle section */
if (hash) {
const article = documents.get(path) as ArticleDocument
const parent = documents.get(path) as ArticleDocument
/* Ignore first section, override article */
if (!article.section) {
article.title = doc.title
article.text = text
article.section = true
if (!parent.linked) {
parent.title = doc.title
parent.text = text
parent.linked = true
/* Add subsequent section */
} else {
documents.set(location, { location, title, text, article })
documents.set(location, { location, title, text, parent })
}
/* Add article */
} else {
documents.set(location, { location, title, text, section: false })
documents.set(location, { location, title, text, linked: false })
}
}
return documents

View File

@@ -21,4 +21,8 @@
*/
export * from "./_"
export * from "./document"
export {
ArticleDocument,
SearchDocument,
SectionDocument
} from "./document"