adjusted logic for news highligting to use the generic highlight text component

This commit is contained in:
2026-08-15 22:21:38 +02:00
parent 2cb6d1f660
commit cbd2e902a7
3 changed files with 30 additions and 73 deletions
@@ -55,10 +55,11 @@
<div class="article-body">
@if (article.full_content) {
<p
<app-highlighted-text
#articleBody
class="content"
[innerHTML]="highlightedContent"></p>
[content]="article.full_content"
[highlights]="articleHighlights" />
} @else {
<p class="placeholder">Kein Inhalt verfügbar.</p>
}
@@ -600,25 +600,3 @@ main {
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;
}
}
@@ -12,8 +12,10 @@ import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatTooltipModule } from '@angular/material/tooltip';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
import {
HighlightedTextComponent,
HighlightTerm,
} from '../../shared/ui/highlighted-text/highlighted-text.component';
import { WorksService } from '../../api';
import { topicHierarchyColor } from '../../resolvers/utils/topic-colors';
import { LoadingComponent } from '../../shared/ui/loading/loading.component';
@@ -54,21 +56,21 @@ interface NewsArticleDetail {
MatTooltipModule,
RouterLink,
LoadingComponent,
HighlightedTextComponent,
],
templateUrl: './news-detail.component.html',
styleUrl: './news-detail.component.scss',
})
export class NewsDetailComponent implements OnInit {
@ViewChild('articleBody') articleBodyRef?: ElementRef<HTMLElement>;
@ViewChild('articleBody', { read: ElementRef })
articleBodyRef?: ElementRef<HTMLElement>;
private readonly router = inject(Router);
private readonly destroyRef = inject(DestroyRef);
private readonly route = inject(ActivatedRoute);
private readonly httpClient = inject(HttpClient);
private readonly worksService = inject(WorksService);
private readonly sanitizer = inject(DomSanitizer);
article: NewsArticleDetail | null = null;
highlightedContent: SafeHtml = this.sanitizer.bypassSecurityTrustHtml('');
loading = false;
notFound = false;
@@ -172,7 +174,6 @@ export class NewsDetailComponent implements OnInit {
.subscribe({
next: (article) => {
this.article = article;
this.highlightedContent = this.buildHighlightedContent(article);
this.expandedTopicId = null;
this.personsPage = 0;
this.loading = false;
@@ -184,56 +185,33 @@ 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 ?? [];
get articleHighlights(): HighlightTerm[] {
const article = this.article;
if (!article) return [];
const keywordColorMap = new Map<string, string>();
const topicByKeyword = new Map<string, MatchedTopic>();
for (const topic of article.topics ?? []) {
if (topic.source_keyword) {
keywordColorMap.set(
topic.source_keyword.toLowerCase(),
this.getTopicColor(topic),
);
topicByKeyword.set(topic.source_keyword.toLowerCase(), 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 = '(?<![\\w\\u00C0-\\u024F])';
const acronymLike =
kw.length <= 3 ||
(kw === kw.toUpperCase() && kw !== kw.toLowerCase());
const re = new RegExp(
acronymLike
? `${before}(${escaped})(?![\\w\\u00C0-\\u024F])`
: `${before}(${escaped}[\\w\\u00C0-\\u024F]*)`,
'gi',
);
html = html.replace(
re,
`<mark class="kw-mark" style="--t:${color}" data-kw="${kwHtml}">$1</mark>`,
);
const highlights: HighlightTerm[] = [];
for (const keyword of article.keywords ?? []) {
const topic = topicByKeyword.get(keyword.toLowerCase());
if (!topic) continue;
highlights.push({
term: keyword,
topic: {
id: topic.id,
name: topic.display_name,
field_name: topic.field_name,
domain_name: topic.domain_name,
},
});
return this.sanitizer.bypassSecurityTrustHtml(html);
}
}
private escapeHtml(text: string): string {
return text
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
}
private escapeRegex(text: string): string {
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
return highlights;
}
}