Loheide-eu-mitarbeiterplatt.../resources/js/lib/highlight.js
Friederich Loheide b276e1c6d0
Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m24s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m26s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m41s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m46s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m31s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m16s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m18s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m14s
Globale Live-Suche, Kontaktkarten-Personensuche, neue Beitragsansicht, Termine mit Seitenleiste, Demo-Inhalte, Plugin-Upload-Fix
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 19:07:13 +00:00

17 lines
670 B
JavaScript

/**
* Text HTML-sicher ausgeben und Suchbegriffe mit <mark> hervorheben.
*/
const escape = (value) => String(value ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[c]);
export function highlight(text, query) {
const safe = escape(text);
const terms = String(query ?? '')
.trim()
.split(/\s+/)
.filter((t) => t.length >= 2)
.map((t) => escape(t).replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
if (!terms.length) return safe;
return safe.replace(new RegExp(`(${terms.join('|')})`, 'gi'), '<mark class="rounded-sm bg-brand-soft px-0.5 text-foreground">$1</mark>');
}