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 fa8728fe..b054c67b 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 @@ -39,13 +39,47 @@ } + + @if (article.snippet) { + @let expanded = isSnippetExpanded(article); + + } + +
- + {{ article.maintitle || 'Kein Titel' }} - @if (article.snippet) { - +
+
+ + +
+ @if (snippetHtml().get(article.id); as snippet) { + Fundstelle im Artikel + + } @else { + + Für diesen Artikel liegt keine Textstelle zu den gesuchten Keywords + vor. + }
@@ -86,7 +120,10 @@ @let visibleTopics = topicsExpanded ? topics : topics.slice(0, topicPreviewCount); -
+
@for (topic of visibleTopics; track topic.id ?? topic.display_name) { + matTooltip="Details anzeigen" + (click)="$event.stopPropagation()"> open_in_new @@ -141,7 +179,10 @@
} @else {
- +
(); @@ -136,13 +143,22 @@ export class KeywordsToNewsResultsComponent { return baseTopicIds.length > 0 || this.selectedTopicIds().length > 0; }); + expandTmp = viewChild.required>('expandTmp'); titleTmp = viewChild.required>('titleTmp'); dateTmp = viewChild.required>('dateTmp'); relevanceTmp = viewChild.required>('relevanceTmp'); topicsTmp = viewChild.required>('topicsTmp'); actionsTmp = viewChild.required>('actionsTmp'); + private readonly dataTable = viewChild(DataTableComponent); + readonly columns = computed[]>(() => [ + { + id: 'expand', + header: '', + type: 'display', + cellTemplate: this.expandTmp(), + }, { id: 'maintitle', header: 'Titel', @@ -222,6 +238,63 @@ export class KeywordsToNewsResultsComponent { return !!topic.id && this.selectedTopicIds().includes(topic.id); } + isSnippetExpanded(article: NewsResult): boolean { + return this.dataTable()?.expandedElement() === article; + } + + readonly snippetHtml = computed(() => { + const byArticleId = new Map(); + + for (const article of this.elements()) { + const html = this.buildSnippetHtml(article); + if (html !== null) { + byArticleId.set( + article.id, + this.sanitizer.bypassSecurityTrustHtml(html), + ); + } + } + + return byArticleId; + }); + + private buildSnippetHtml(article: NewsResult): string | null { + const snippet = article.snippet; + if (!snippet) { + return null; + } + + const keywordColors = topicKeywordColors(article.topics); + const searchMatch = /([\s\S]*?)<\/em>/g; + + let html = ''; + let lastIndex = 0; + let match: RegExpExecArray | null; + + while ((match = searchMatch.exec(snippet)) !== null) { + html += escapeHtml( + this.decodeHtml(snippet.slice(lastIndex, match.index)), + ); + html += `${highlightKeywords( + this.decodeHtml(match[1]), + keywordColors, + )}`; + + lastIndex = searchMatch.lastIndex; + } + + return html + escapeHtml(this.decodeHtml(snippet.slice(lastIndex))); + } + + private decodeHtml(value: string): string { + return value + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/"/g, '"') + .replace(/&#(?:39|x27);/g, "'") + .replace(/&/g, '&'); + } + isTopicsExpanded(article: NewsResult): boolean { return this.expandedTopicsIds().has(article.id); } diff --git a/webapp/src/app/modules/news-detail/news-detail.component.scss b/webapp/src/app/modules/news-detail/news-detail.component.scss index 1d31ab79..a3aaa6c7 100644 --- a/webapp/src/app/modules/news-detail/news-detail.component.scss +++ b/webapp/src/app/modules/news-detail/news-detail.component.scss @@ -586,39 +586,3 @@ main { --topic-bg: color-mix(in srgb, var(--sg-error) 8%, var(--sg-color-mix-base)); --topic-color: var(--sg-error); } - -// ── Keyword highlights in article text ─────────────────────────────────────── - -@keyframes kw-flash { - 0% { - background: color-mix(in srgb, var(--t) 45%, var(--sg-color-mix-base)); - } - 40% { - background: color-mix(in srgb, var(--t) 45%, var(--sg-color-mix-base)); - } - 100% { - background: transparent; - } -} - -:host ::ng-deep .kw-mark { - background: transparent; - color: var(--t); - text-decoration: underline; - text-decoration-color: var(--t); - text-decoration-style: solid; - text-decoration-thickness: 2px; - text-underline-offset: 2px; - font-weight: inherit; - border-radius: 2px; - padding: 0 1px; - transition: background 0.15s; - - &:hover { - background: color-mix(in srgb, var(--t) 15%, var(--sg-color-mix-base)); - } - - &.kw-flash { - animation: kw-flash 2.4s ease-out forwards; - } -} 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 fdbb74f9..039c7feb 100644 --- a/webapp/src/app/modules/news-detail/news-detail.component.ts +++ b/webapp/src/app/modules/news-detail/news-detail.component.ts @@ -16,6 +16,10 @@ import { DomSanitizer, SafeHtml } from '@angular/platform-browser'; 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'; interface MentionedPerson { @@ -187,53 +191,12 @@ export class NewsDetailComponent implements OnInit { private buildHighlightedContent(article: NewsArticleDetail): SafeHtml { const text = article.full_content; if (!text) return this.sanitizer.bypassSecurityTrustHtml(''); - const keywords = article.keywords ?? []; - const keywordColorMap = new Map(); - for (const topic of article.topics ?? []) { - if (topic.source_keyword) { - keywordColorMap.set( - topic.source_keyword.toLowerCase(), - this.getTopicColor(topic), - ); - } - } - - let html = this.escapeHtml(text); - [...keywords] - .sort((a, b) => b.length - a.length) - .forEach((kw) => { - const color = keywordColorMap.get(kw.toLowerCase()); - if (!color) return; - const kwHtml = this.escapeHtml(kw); - const escaped = this.escapeRegex(kwHtml); - const before = '(?$1`, - ); - }); - return this.sanitizer.bypassSecurityTrustHtml(html); - } - - private escapeHtml(text: string): string { - return text - .replace(/&/g, '&') - .replace(//g, '>') - .replace(/"/g, '"'); - } - - private escapeRegex(text: string): string { - return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + return this.sanitizer.bypassSecurityTrustHtml( + highlightKeywords( + text, + topicKeywordColors(article.topics, article.keywords), + ), + ); } } diff --git a/webapp/src/app/resolvers/utils/keyword-highlight.ts b/webapp/src/app/resolvers/utils/keyword-highlight.ts new file mode 100644 index 00000000..e261e496 --- /dev/null +++ b/webapp/src/app/resolvers/utils/keyword-highlight.ts @@ -0,0 +1,126 @@ +import { MatchedTopic } from '../../api'; +import { topicHierarchyColor } from './topic-colors'; + +/** A keyword together with the colour of the topic it resolved to. */ +export interface KeywordColor { + keyword: string; + color: string; +} + +/** Pairs every keyword that resolved to a topic with that topic's colour. */ +export function topicKeywordColors( + topics: readonly MatchedTopic[] | null | undefined, + keywords?: readonly string[] | null, +): KeywordColor[] { + const colorByKeyword = new Map(); + + for (const topic of topics ?? []) { + if (!topic.source_keyword) { + continue; + } + + colorByKeyword.set( + topic.source_keyword.toLowerCase(), + topicHierarchyColor({ + id: topic.id, + name: topic.display_name, + field_name: topic.field_name, + domain_name: topic.domain_name, + }), + ); + } + + const source = + keywords ?? + (topics ?? []) + .map((topic) => topic.source_keyword) + .filter((keyword): keyword is string => !!keyword); + + const seen = new Set(); + const result: KeywordColor[] = []; + + for (const keyword of source) { + const key = keyword.toLowerCase(); + const color = colorByKeyword.get(key); + if (!color || seen.has(key)) { + continue; + } + seen.add(key); + result.push({ keyword, color }); + } + + return result; +} + +const WORD_CHAR = '[\\w\\u00C0-\\u024F]'; + +/** + * Escapes `text` and wraps every occurrence of a keyword in a `` carrying + * the keyword's topic colour in `--t`. + */ +export function highlightKeywords( + text: string, + keywordColors: readonly KeywordColor[], +): string { + const sorted = [...keywordColors] + .filter(({ keyword }) => keyword.trim()) + .sort((a, b) => b.keyword.length - a.keyword.length); + + if (!sorted.length) { + return escapeHtml(text); + } + + // Acronyms are matched exactly, everything else may carry an inflection + // ("Gründung" also marks "Gründungen"). + const alternatives = sorted.map(({ keyword }) => + isAcronymLike(keyword) + ? `${escapeRegex(keyword)}(?!${WORD_CHAR})` + : `${escapeRegex(keyword)}${WORD_CHAR}*`, + ); + const pattern = new RegExp( + `(? + matched.toLowerCase().startsWith(keyword.toLowerCase()), + ); + if (!entry) { + continue; + } + + html += escapeHtml(text.slice(lastIndex, match.index)); + html += + `${escapeHtml(matched)}`; + + lastIndex = match.index + matched.length; + } + + return html + escapeHtml(text.slice(lastIndex)); +} + +export function escapeHtml(text: string): string { + return text + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"'); +} + +function escapeRegex(text: string): string { + return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +} + +function isAcronymLike(keyword: string): boolean { + return ( + keyword.length <= 3 || + (keyword === keyword.toUpperCase() && keyword !== keyword.toLowerCase()) + ); +} diff --git a/webapp/src/styles.scss b/webapp/src/styles.scss index 6d39dd4b..1655ff86 100644 --- a/webapp/src/styles.scss +++ b/webapp/src/styles.scss @@ -627,3 +627,44 @@ html.dark-theme { flex-shrink: 0; transition: transform 0.3s ease; } + +/* Keyword highlight */ +@keyframes kw-flash { + 0% { + background: color-mix(in srgb, var(--t) 45%, var(--sg-color-mix-base)); + } + 40% { + background: color-mix(in srgb, var(--t) 45%, var(--sg-color-mix-base)); + } + 100% { + background: transparent; + } +} + +.kw-mark { + background: transparent; + color: var(--t); + text-decoration: underline; + text-decoration-color: var(--t); + text-decoration-style: solid; + text-decoration-thickness: 2px; + text-underline-offset: 2px; + font-weight: inherit; + border-radius: 2px; + padding: 0 1px; + transition: background 0.15s; + + &:hover { + background: color-mix(in srgb, var(--t) 15%, var(--sg-color-mix-base)); + } + + &.kw-flash { + animation: kw-flash 2.4s ease-out forwards; + } +} + +.search-mark { + background: transparent; + color: var(--sg-blue-highlight); + font-weight: 700; +}