Fixed incomplete search highlighting for code blocks in titles

This commit is contained in:
squidfunk 2023-10-05 16:45:36 +02:00
parent 790a446dd7
commit 57e598b96e
No known key found for this signature in database
GPG Key ID: 5ED40BC4F9C436DF
5 changed files with 29 additions and 15 deletions

View File

@ -221,7 +221,7 @@
"base": base_url,
"features": features,
"translations": {},
"search": "assets/javascripts/workers/search.b4c2f2ff.min.js" | url
"search": "assets/javascripts/workers/search.f886a092.min.js" | url
} -%}
{%- if config.extra.version -%}
{%- set _ = app.update({ "version": config.extra.version }) -%}

View File

@ -264,7 +264,7 @@ export class Search {
: highlight
// @ts-expect-error - stop moaning, TypeScript!
doc[field] = fn(doc[field], table, positions)
doc[field] = fn(doc[field], table, positions, field !== "text")
}
/* Highlight title and text and apply post-query boosts */

View File

@ -49,13 +49,14 @@ export type Position = number
* @param input - Input value
* @param table - Table for indexing
* @param positions - Occurrences
* @param full - Full results
*
* @returns Highlighted string value
*/
export function highlight(
input: string, table: PositionTable, positions: Position[]
input: string, table: PositionTable, positions: Position[], full = false
): string {
return highlightAll([input], table, positions).pop()!
return highlightAll([input], table, positions, full).pop()!
}
/**
@ -64,11 +65,12 @@ export function highlight(
* @param inputs - Input values
* @param table - Table for indexing
* @param positions - Occurrences
* @param full - Full results
*
* @returns Highlighted string values
*/
export function highlightAll(
inputs: string[], table: PositionTable, positions: Position[]
inputs: string[], table: PositionTable, positions: Position[], full = false
): string[] {
/* Map blocks to input values */
@ -87,6 +89,7 @@ export function highlightAll(
/* Highlight strings one after another */
return inputs.map((input, i) => {
let cursor = 0
/* Map occurrences to blocks */
const blocks = new Map<number, number[]>()
@ -119,6 +122,10 @@ export function highlightAll(
const end = t[t.length - 1] >>> 12
const length = t[t.length - 1] >>> 2 & 0x3FF
/* Add prefix, if full results are desired */
if (full && start > cursor)
slices.push(input.slice(cursor, start))
/* Extract and highlight slice */
let slice = input.slice(start, end + length)
for (const j of indexes.sort((a, b) => b - a)) {
@ -137,11 +144,18 @@ export function highlightAll(
].join("")
}
/* Update cursor */
cursor = end + length
/* Append slice and abort if we have two */
if (slices.push(slice) === 2)
break
}
/* Add suffix, if full results are desired */
if (full && cursor < input.length)
slices.push(input.slice(cursor))
/* Return highlighted slices */
return slices.join("")
})