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 4b6f9497..bcb4777e 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 @@ -72,24 +72,12 @@
- @if (snippetSegments().get(article.id); as segments) { + @if (snippetHighlights().get(article.id); as snippet) { Fundstelle im Artikel -

- @for (segment of segments; track $index) { - @if (segment.isSearchMatch) { - {{ segment.text }} - } @else { - {{ segment.text }} - } - } -

+ } @else { Für diesen Artikel liegt keine Textstelle zu den gesuchten Keywords 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 117b1e3a..11f36e2c 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 @@ -153,21 +153,6 @@ overflow-wrap: anywhere; } -.snippet-mark { - background: transparent; - color: var(--sg-blue-highlight); - font-weight: 700; - cursor: help; - - &.has-topic { - color: var(--t); - text-decoration: underline; - text-decoration-color: var(--t); - text-decoration-thickness: 2px; - text-underline-offset: 2px; - } -} - .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 9de89e9e..b279388a 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,18 +42,15 @@ import { SORT_ICON, } from '../../../../shared/ui/icons'; import { - keywordTooltip, - splitByKeywords, - topicKeywordColors, -} from '../../../../resolvers/utils/keyword-highlight'; + HighlightedTextComponent, + HighlightTerm, +} from '../../../../shared/ui/highlighted-text/highlighted-text.component'; type NewsResult = NewsPage['results'][number]; -interface SnippetSegment { - text: string; - color: string | null; - tooltip: string | null; - isSearchMatch: boolean; +interface SnippetHighlight { + content: string; + terms: HighlightTerm[]; } @Component({ @@ -64,6 +61,7 @@ interface SnippetSegment { MatIconModule, MatTooltipModule, DataTableComponent, + HighlightedTextComponent, AppTopicChipComponent, ChipOverflowToggleComponent, FilterCardComponent, @@ -264,70 +262,84 @@ export class KeywordsToNewsResultsComponent { return this.dataTable()?.expandedElement() === article; } - readonly snippetSegments = computed(() => { - const byArticleId = new Map(); + readonly snippetHighlights = computed(() => { + const byArticleId = new Map(); for (const article of this.elements()) { - const segments = this.buildSnippetSegments(article); - if (segments.length) { - byArticleId.set(article.id, segments); + const snippet = this.buildSnippetHighlight(article); + if (snippet) { + byArticleId.set(article.id, snippet); } } return byArticleId; }); - private buildSnippetSegments(article: NewsResult): SnippetSegment[] { + private buildSnippetHighlight(article: NewsResult): SnippetHighlight | null { const snippet = article.snippet; if (!snippet) { - return []; + return null; } - const keywordColors = topicKeywordColors(article.topics); - const searchMatch = /([\s\S]*?)<\/em>/g; + 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); + } + } - const segments: SnippetSegment[] = []; - let lastIndex = 0; + const terms: HighlightTerm[] = []; + const seen = new Set(); + const searchMatch = /([\s\S]*?)<\/em>/g; let match: RegExpExecArray | null; - const pushPlain = (raw: string) => { - const text = this.decodeHtml(raw); - if (text) { - segments.push({ - text, - color: null, - tooltip: null, - isSearchMatch: false, - }); - } - }; - while ((match = searchMatch.exec(snippet)) !== null) { - pushPlain(snippet.slice(lastIndex, match.index)); - - const term = this.decodeHtml(match[1]); - const parts = splitByKeywords(term, keywordColors); - const hasTopic = parts.some((part) => part.match); - - for (const part of parts) { - segments.push({ - text: part.text, - color: part.match?.color ?? null, - tooltip: part.match - ? keywordTooltip(part.match.keyword, part.match.topicNames) - : hasTopic - ? null - : keywordTooltip(term, []), - isSearchMatch: true, - }); + const matched = this.decodeHtml(match[1]).trim(); + const topic = this.topicForTerm(matched, topicByKeyword); + const term = topic?.source_keyword ?? matched; + const key = term.toLowerCase(); + if (!term || seen.has(key)) { + continue; } + seen.add(key); - lastIndex = searchMatch.lastIndex; + terms.push({ + term, + topic: topic + ? { + id: topic.id, + name: topic.display_name, + field_name: topic.field_name, + domain_name: topic.domain_name, + } + : null, + }); } - pushPlain(snippet.slice(lastIndex)); + return { + content: this.decodeHtml(snippet.replace(/<\/?em>/g, '')), + terms, + }; + } - return segments; + 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; } private decodeHtml(value: string): string { diff --git a/webapp/src/app/modules/news-detail/news-detail.component.ts b/webapp/src/app/modules/news-detail/news-detail.component.ts index 702a37c8..c7d48c35 100644 --- a/webapp/src/app/modules/news-detail/news-detail.component.ts +++ b/webapp/src/app/modules/news-detail/news-detail.component.ts @@ -18,10 +18,6 @@ import { } from '../../shared/ui/highlighted-text/highlighted-text.component'; import { WorksService } from '../../api'; import { topicHierarchyColor } from '../../resolvers/utils/topic-colors'; -import { - highlightKeywords, - topicKeywordColors, -} from '../../resolvers/utils/keyword-highlight'; import { LoadingComponent } from '../../shared/ui/loading/loading.component'; import { ARROW_BACK_ICON,