1
0
mirror of https://github.com/squidfunk/mkdocs-material.git synced 2025-01-31 11:53:53 +01:00

Refactored search integration structure

This commit is contained in:
squidfunk 2022-12-07 20:56:16 +01:00
parent 13680a5863
commit d7c6703020
13 changed files with 43 additions and 40 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -211,7 +211,7 @@
"base": base_url, "base": base_url,
"features": features, "features": features,
"translations": {}, "translations": {},
"search": "assets/javascripts/workers/search.7c75be7a.min.js" | url "search": "assets/javascripts/workers/search.208e55ea.min.js" | url
} -%} } -%}
{%- if config.extra.version -%} {%- if config.extra.version -%}
{%- set _ = app.update({ "version": config.extra.version }) -%} {%- set _ = app.update({ "version": config.extra.version }) -%}
@ -245,7 +245,7 @@
{% endfor %} {% endfor %}
{% endblock %} {% endblock %}
{% if page.meta and page.meta.ᴴₒᴴₒᴴₒ %} {% if page.meta and page.meta.ᴴₒᴴₒᴴₒ %}
<link rel="stylesheet" href="{{ 'assets/stylesheets/extra.300c463b.min.css' | url }}"> <link rel="stylesheet" href="{{ 'assets/stylesheets/extra.0d47dbba.min.css' | url }}">
<script src="{{ 'assets/javascripts/extra/bundle.f719a234.min.js' | url }}" defer></script> <script src="{{ 'assets/javascripts/extra/bundle.f719a234.min.js' | url }}" defer></script>
{% endif %} {% endif %}
</body> </body>

View File

@ -29,7 +29,7 @@ import {
import { import {
Position, Position,
PositionTable, PositionTable,
highlighter, highlight,
tokenize tokenize
} from "../internal" } from "../internal"
import { import {
@ -244,14 +244,14 @@ export class Search {
// @ts-expect-error - @todo fix typings // @ts-expect-error - @todo fix typings
for (let i = 0; i < doc[field].length; i++) { for (let i = 0; i < doc[field].length; i++) {
// @ts-expect-error - @todo fix typings // @ts-expect-error - @todo fix typings
doc[field][i] = highlighter(doc[field][i], doc[field][i] = highlight(doc[field][i],
this.table.get([doc.location, field].join(":"))!, this.table.get([doc.location, field].join(":"))!,
positions positions
) )
} }
} else { } else {
// @ts-expect-error - @todo fix typings // @ts-expect-error - @todo fix typings
doc[field] = highlighter(doc[field], doc[field] = highlight(doc[field],
this.table.get([doc.location, field].join(":"))!, this.table.get([doc.location, field].join(":"))!,
positions positions
) )

View File

@ -41,8 +41,8 @@ type VisitorFn = (
/** /**
* Split a string using the given separator * Split a string using the given separator
* *
* This function intentionally takes a visitor function contrary to collecting * This function intentionally expects a visitor function argument, as opposed
* and returning all ranges, as it's significantly more memory efficient. * to collecting and returning all sections, for better memory efficiency.
* *
* @param value - String value * @param value - String value
* @param separator - Separator * @param separator - Separator

View File

@ -43,10 +43,10 @@ type VisitorFn = (
/** /**
* Extract all non-HTML parts of a string * Extract all non-HTML parts of a string
* *
* This function preprocesses the given string by isolating all non-HTML parts * This function preprocesses the given string by isolating all non-HTML parts,
* of a string, in order to ensure that HTML tags are removed before indexing. * in order to ensure that HTML tags are removed before indexing. Note that it
* This function intentionally takes a visitor function contrary to collecting * intentionally expects a visitor function argument, as opposed to collecting
* and returning all sections, as it's significantly more memory efficient. * and returning all sections, for better memory efficiency.
* *
* @param value - String value * @param value - String value
* @param fn - Visitor function * @param fn - Visitor function

View File

@ -20,7 +20,19 @@
* IN THE SOFTWARE. * IN THE SOFTWARE.
*/ */
import { Position, PositionTable } from "../tokenizer" /* ----------------------------------------------------------------------------
* Types
* ------------------------------------------------------------------------- */
/**
* Table for indexing
*/
export type PositionTable = number[][]
/**
* Position
*/
export type Position = number
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
* Functions * Functions
@ -29,17 +41,22 @@ import { Position, PositionTable } from "../tokenizer"
/** /**
* Highlight all occurrences in a string * Highlight all occurrences in a string
* *
* This function receives a field's value (e.g. like `title` or `text`), it's
* position table that was generated during indexing, and the positions found
* when executing the query. It then highlights all occurrences, and returns
* their concatenation. In case of multiple blocks, two are returned.
*
* @param value - String value * @param value - String value
* @param table - Table for indexing * @param table - Table for indexing
* @param positions - Occurrences * @param positions - Occurrences
* *
* @returns Highlighted string value * @returns Highlighted string value
*/ */
export function highlighter( export function highlight(
value: string, table: PositionTable, positions: Position[] value: string, table: PositionTable, positions: Position[]
): string { ): string {
/* Map matches to blocks */ /* Map occurrences to blocks */
const blocks = new Map<number, number[]>() const blocks = new Map<number, number[]>()
for (const i of positions.sort((a, b) => a - b)) { for (const i of positions.sort((a, b) => a - b)) {
const block = i >>> 20 const block = i >>> 20
@ -59,7 +76,7 @@ export function highlighter(
for (const [block, indexes] of blocks) { for (const [block, indexes] of blocks) {
const t = table[block] const t = table[block]
/* Extract start and end positions, and length */ /* Extract positions and length */
const start = t[0] >>> 12 const start = t[0] >>> 12
const end = t[t.length - 1] >>> 12 const end = t[t.length - 1] >>> 12
const length = t[t.length - 1] >>> 2 & 0x3FF const length = t[t.length - 1] >>> 2 & 0x3FF

View File

@ -21,6 +21,6 @@
*/ */
export * from "./_" export * from "./_"
export * from "./extractor" export * from "./extract"
export * from "./highlighter" export * from "./highlight"
export * from "./tokenizer" export * from "./tokenize"

View File

@ -21,21 +21,7 @@
*/ */
import { split } from "../_" import { split } from "../_"
import { extract } from "../extractor" import { extract } from "../extract"
/* ----------------------------------------------------------------------------
* Types
* ------------------------------------------------------------------------- */
/**
* Table for indexing
*/
export type PositionTable = number[][]
/**
* Position
*/
export type Position = number
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
* Functions * Functions