From 50ff313a57a0b4abf449f9910cbe293b77355e6a Mon Sep 17 00:00:00 2001 From: "noemi.kallweit" Date: Mon, 24 Aug 2026 16:32:47 +0200 Subject: [PATCH] refactor(news-keyword-search): adapt styling from projects --- .../news-results/news-results.component.html | 50 +++++++---- .../news-results/news-results.component.scss | 31 +------ .../news-results/news-results.component.ts | 88 ++++++++----------- .../highlighted-text.component.ts | 59 +++++++++---- 4 files changed, 118 insertions(+), 110 deletions(-) diff --git a/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.html b/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.html index bcb4777e..7c21c0a7 100644 --- a/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.html +++ b/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.html @@ -65,26 +65,46 @@ class="title" [routerLink]="['/news', article.id]" (click)="$event.stopPropagation()"> - {{ article.maintitle || 'Kein Titel' }} + -
- @if (snippetHighlights().get(article.id); as snippet) { - Fundstelle im Artikel - - } @else { - - Für diesen Artikel liegt keine Textstelle zu den gesuchten Keywords - vor. - - } -
+ +
+

Fundstelle im Artikel

+
+ + + @if (titleHighlights().get(article.id); as titleTerms) { +
+

Titel

+ +
+ } + + @if (snippetHighlights().get(article.id); as snippet) { +
+

Artikeltext

+ +
+ } @else { + + Für diesen Artikel liegt keine Textstelle zu den gesuchten Keywords + vor. + + } +
+
diff --git a/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.scss b/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.scss index 11f36e2c..2e9112b4 100644 --- a/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.scss +++ b/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.scss @@ -115,6 +115,10 @@ color: var(--sg-text); text-decoration: underline; } + + app-highlighted-text { + display: inline; + } } .expand-icon { @@ -126,33 +130,6 @@ } } -/* ============================================================ - EXPANDED DETAIL ROW - ============================================================ */ -.snippet-detail { - display: flex; - flex-direction: column; - gap: 0.375rem; - padding: 0.875rem 1rem 1rem 3rem; - background: var(--sg-surface-soft); -} - -.snippet-label { - color: var(--sg-text-muted); - font-size: 11px; - font-weight: 700; - letter-spacing: 0.02em; - text-transform: uppercase; -} - -.snippet { - margin: 0; - font-size: 12.5px; - line-height: 1.6; - color: var(--sg-text-secondary); - overflow-wrap: anywhere; -} - .date-text { white-space: nowrap; } diff --git a/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.ts b/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.ts index 0d6c50a9..dbe251f1 100644 --- a/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.ts +++ b/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.ts @@ -42,9 +42,11 @@ import { SORT_ICON, } from '../../../../shared/ui/icons'; import { + containsHighlightTerm, HighlightedTextComponent, HighlightTerm, } from '../../../../shared/ui/highlighted-text/highlighted-text.component'; +import { TableDetailRowComponent } from '../../../../shared/ui/table-detail-row/table-detail-row.component'; type NewsResult = NewsPage['results'][number]; @@ -62,6 +64,7 @@ interface SnippetHighlight { MatTooltipModule, DataTableComponent, HighlightedTextComponent, + TableDetailRowComponent, AppTopicChipComponent, ChipOverflowToggleComponent, FilterCardComponent, @@ -275,71 +278,58 @@ export class KeywordsToNewsResultsComponent { return byArticleId; }); + readonly titleHighlights = computed(() => { + const byArticleId = new Map(); + const keywords = this.state.keywords(); + + for (const article of this.elements()) { + const found = keywords.filter((keyword) => + containsHighlightTerm(article.maintitle, [keyword]), + ); + if (found.length) { + byArticleId.set(article.id, this.toHighlightTerms(found)); + } + } + + return byArticleId; + }); + private buildSnippetHighlight(article: NewsResult): SnippetHighlight | null { const snippet = article.snippet; if (!snippet) { return null; } - const topicByKeyword = new Map(); - for (const topic of article.topics ?? []) { - const keyword = topic.source_keyword?.toLowerCase(); - if (keyword && !topicByKeyword.has(keyword)) { - topicByKeyword.set(keyword, topic); - } - } + return { + content: snippet.replace(/<\/?em>/g, ''), + terms: this.toHighlightTerms([ + ...this.state.keywords(), + ...this.markedTerms(snippet), + ]), + }; + } - const terms: HighlightTerm[] = []; + private markedTerms(snippet: string): string[] { + return [...snippet.matchAll(/([\s\S]*?)<\/em>/g)].map((match) => + match[1].trim(), + ); + } + + private toHighlightTerms(terms: string[]): HighlightTerm[] { const seen = new Set(); - const searchMatch = /([\s\S]*?)<\/em>/g; - let match: RegExpExecArray | null; + const result: HighlightTerm[] = []; - while ((match = searchMatch.exec(snippet)) !== null) { - const matched = match[1].trim(); - const topic = this.topicForTerm(matched, topicByKeyword); - const term = topic?.source_keyword ?? matched; + for (const raw of terms) { + const term = raw.trim(); const key = term.toLowerCase(); if (!term || seen.has(key)) { continue; } seen.add(key); - - terms.push({ - term, - topic: topic - ? { - id: topic.id, - name: topic.display_name, - field_name: topic.field_name, - domain_name: topic.domain_name, - } - : null, - }); + result.push({ term, topic: this.state.resolveTopicForTerm(term) }); } - return { - content: snippet.replace(/<\/?em>/g, ''), - terms, - }; - } - - private topicForTerm( - term: string, - topicByKeyword: ReadonlyMap, - ): MatchedTopic | null { - const lower = term.toLowerCase(); - const exact = topicByKeyword.get(lower); - if (exact) { - return exact; - } - - for (const [keyword, topic] of topicByKeyword) { - if (lower.startsWith(keyword)) { - return topic; - } - } - - return null; + return result; } isTopicsExpanded(article: NewsResult): boolean { diff --git a/webapp/src/app/shared/ui/highlighted-text/highlighted-text.component.ts b/webapp/src/app/shared/ui/highlighted-text/highlighted-text.component.ts index ece59f8e..327c566a 100644 --- a/webapp/src/app/shared/ui/highlighted-text/highlighted-text.component.ts +++ b/webapp/src/app/shared/ui/highlighted-text/highlighted-text.component.ts @@ -85,24 +85,11 @@ export class HighlightedTextComponent { ); } - /** - * Short/acronym-like terms (<=3 chars, or ALL CAPS) match exactly. - * Longer terms also swallow a trailing suffix (plural/case endings). - */ private buildMatchRegex(terms: HighlightTerm[]): RegExp { - const before = '(? { - const escaped = this.escapeRegex(this.escapeHtml(term)); - const acronymLike = - term.length <= 3 || - (term === term.toUpperCase() && term !== term.toLowerCase()); - - return acronymLike - ? `${escaped}(?![\\w\\u00C0-\\u024F])` - : `${escaped}[\\w\\u00C0-\\u024F]*`; - }); - - return new RegExp(`${before}(?:${alternatives.join('|')})`, 'gi'); + return termsRegex( + terms.map(({ term }) => this.escapeHtml(term)), + 'gi', + ); } private findOriginTerm( @@ -122,8 +109,42 @@ export class HighlightedTextComponent { .replace(/>/g, '>') .replace(/"/g, '"'); } +} - private escapeRegex(text: string): string { - return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +const WORD_CHAR = '[\\w\\u00C0-\\u024F]'; + +/** + * Short/acronym-like terms (<=3 chars, or ALL CAPS) match exactly. + * Longer terms also swallow a trailing suffix (plural/case endings). + */ +function termPattern(term: string): string { + const escaped = escapeRegex(term); + const acronymLike = + term.length <= 3 || + (term === term.toUpperCase() && term !== term.toLowerCase()); + + return acronymLike ? `${escaped}(?!${WORD_CHAR})` : `${escaped}${WORD_CHAR}*`; +} + +function termsRegex(terms: string[], flags: string): RegExp { + return new RegExp( + `(? term.trim()).filter(Boolean); + if (!text || !usable.length) { + return false; } + + return termsRegex(usable, 'i').test(text); +} + +function escapeRegex(text: string): string { + return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); }