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,
"features": features,
"translations": {},
"search": "assets/javascripts/workers/search.7c75be7a.min.js" | url
"search": "assets/javascripts/workers/search.208e55ea.min.js" | url
} -%}
{%- if config.extra.version -%}
{%- set _ = app.update({ "version": config.extra.version }) -%}
@ -245,7 +245,7 @@
{% endfor %}
{% endblock %}
{% 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>
{% endif %}
</body>

View File

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

View File

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

View File

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

View File

@ -20,7 +20,19 @@
* IN THE SOFTWARE.
*/
import { Position, PositionTable } from "../tokenizer"
/* ----------------------------------------------------------------------------
* Types
* ------------------------------------------------------------------------- */
/**
* Table for indexing
*/
export type PositionTable = number[][]
/**
* Position
*/
export type Position = number
/* ----------------------------------------------------------------------------
* Functions
@ -29,17 +41,22 @@ import { Position, PositionTable } from "../tokenizer"
/**
* 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 table - Table for indexing
* @param positions - Occurrences
*
* @returns Highlighted string value
*/
export function highlighter(
export function highlight(
value: string, table: PositionTable, positions: Position[]
): string {
/* Map matches to blocks */
/* Map occurrences to blocks */
const blocks = new Map<number, number[]>()
for (const i of positions.sort((a, b) => a - b)) {
const block = i >>> 20
@ -59,7 +76,7 @@ export function highlighter(
for (const [block, indexes] of blocks) {
const t = table[block]
/* Extract start and end positions, and length */
/* Extract positions and length */
const start = t[0] >>> 12
const end = t[t.length - 1] >>> 12
const length = t[t.length - 1] >>> 2 & 0x3FF

View File

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

View File

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