Skip to content

Commit

Permalink
Fixed highlighting of tags
Browse files Browse the repository at this point in the history
  • Loading branch information
squidfunk committed Dec 11, 2022
1 parent ee14964 commit 24a3be8
Show file tree
Hide file tree
Showing 15 changed files with 255 additions and 193 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion material/assets/stylesheets/extra.b3906f4e.min.css

This file was deleted.

1 change: 0 additions & 1 deletion material/assets/stylesheets/extra.b3906f4e.min.css.map

This file was deleted.

1 change: 1 addition & 0 deletions material/assets/stylesheets/extra.d35223bf.min.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions material/assets/stylesheets/extra.d35223bf.min.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions material/base.html
Expand Up @@ -211,7 +211,7 @@
"base": base_url,
"features": features,
"translations": {},
"search": "assets/javascripts/workers/search.208e55ea.min.js" | url
"search": "assets/javascripts/workers/search.f5389c75.min.js" | url
} -%}
{%- if config.extra.version -%}
{%- set _ = app.update({ "version": config.extra.version }) -%}
Expand Down Expand Up @@ -239,13 +239,13 @@
</script>
{% endblock %}
{% block scripts %}
<script src="{{ 'assets/javascripts/bundle.f1ef77e2.min.js' | url }}"></script>
<script src="{{ 'assets/javascripts/bundle.ce0331ff.min.js' | url }}"></script>
{% for path in config.extra_javascript %}
<script src="{{ path | url }}"></script>
{% endfor %}
{% endblock %}
{% if page.meta and page.meta.ᴴₒᴴₒᴴₒ %}
<link rel="stylesheet" href="{{ 'assets/stylesheets/extra.b3906f4e.min.css' | url }}">
<link rel="stylesheet" href="{{ 'assets/stylesheets/extra.d35223bf.min.css' | url }}">
<script src="{{ 'assets/javascripts/extra/bundle.f719a234.min.js' | url }}" defer></script>
{% endif %}
</body>
Expand Down
43 changes: 19 additions & 24 deletions src/assets/javascripts/integrations/search/_/index.ts
Expand Up @@ -30,6 +30,7 @@ import {
Position,
PositionTable,
highlight,
highlightAll,
tokenize
} from "../internal"
import {
Expand All @@ -46,7 +47,9 @@ import {
/**
* Search item
*/
export interface SearchItem extends SearchDocument {
export interface SearchItem
extends SearchDocument
{
score: number /* Score (relevance) */
terms: SearchQueryTerms /* Search query terms */
}
Expand Down Expand Up @@ -213,6 +216,8 @@ export class Search {
.reduce<SearchItem[]>((item, { ref, score, matchData }) => {
let doc = this.map.get(ref)
if (typeof doc !== "undefined") {

/* Shallow copy document */
doc = { ...doc }
if (doc.tags)
doc.tags = [...doc.tags]
Expand All @@ -223,39 +228,29 @@ export class Search {
Object.keys(matchData.metadata)
)

// we must collect all positions for each term!
// we now take the keys of the index
/* Highlight matches in fields */
for (const field of this.index.fields) {
if (!(field in doc))
if (typeof doc[field] === "undefined")
continue

/* Collect matches */
/* Collect positions from matches */
const positions: Position[] = []
for (const match of Object.values(matchData.metadata))
if (field in match)
if (typeof match[field] !== "undefined")
positions.push(...match[field].position)

/* Skip field, if no highlighting is necessary */
/* Skip highlighting, if no positions were collected */
if (!positions.length)
continue

// @ts-expect-error - @todo fix typings
if (Array.isArray(doc[field])) {
// @ts-expect-error - @todo fix typings
for (let i = 0; i < doc[field].length; i++) {
// @ts-expect-error - @todo fix typings
doc[field][i] = highlight(doc[field][i],
this.table.get([doc.location, field].join(":"))!,
positions
)
}
} else {
// @ts-expect-error - @todo fix typings
doc[field] = highlight(doc[field],
this.table.get([doc.location, field].join(":"))!,
positions
)
}
/* Load table and determine highlighting method */
const table = this.table.get([doc.location, field].join(":"))!
const fn = Array.isArray(doc[field])
? highlightAll
: highlight

// @ts-expect-error - stop moaning, TypeScript!
doc[field] = fn(doc[field], table, positions)
}

/* Highlight title and text and apply post-query boosts */
Expand Down
11 changes: 4 additions & 7 deletions src/assets/javascripts/integrations/search/internal/_/index.ts
Expand Up @@ -41,26 +41,23 @@ type VisitorFn = (
/**
* Split a string using the given separator
*
* 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 input - Input value
* @param separator - Separator
* @param fn - Visitor function
*/
export function split(
value: string, separator: RegExp, fn: VisitorFn
input: string, separator: RegExp, fn: VisitorFn
): void {
separator = new RegExp(separator, "g")

/* Split string using separator */
let match: RegExpExecArray | null
let index = 0
do {
match = separator.exec(value)
match = separator.exec(input)

/* Emit non-empty range */
const until = match?.index ?? value.length
const until = match?.index ?? input.length
if (index < until)
fn(index, until)

Expand Down

0 comments on commit 24a3be8

Please sign in to comment.