refactor(news-keyword-search): adapt styling from projects
LocationBasedServices/smartguide/pipeline/head This commit looks good
LocationBasedServices/smartguide/pipeline/pr-dev Build queued...

This commit is contained in:
2026-08-24 16:32:47 +02:00
parent 84073854bf
commit 50ff313a57
4 changed files with 118 additions and 110 deletions
@@ -65,26 +65,46 @@
class="title"
[routerLink]="['/news', article.id]"
(click)="$event.stopPropagation()">
{{ article.maintitle || 'Kein Titel' }}
<app-highlighted-text
[content]="article.maintitle || 'Kein Titel'"
[highlights]="titleHighlights().get(article.id) ?? []" />
</a>
</div>
</ng-template>
<ng-template #snippetTmp let-article>
<div class="snippet-detail">
@if (snippetHighlights().get(article.id); as snippet) {
<span class="snippet-label">Fundstelle im Artikel</span>
<app-highlighted-text
class="snippet"
[content]="snippet.content"
[highlights]="snippet.terms" />
} @else {
<span class="empty-text">
Für diesen Artikel liegt keine Textstelle zu den gesuchten Keywords
vor.
</span>
}
</div>
<app-table-detail-row>
<div table-detail-header>
<h4 class="panel-field__label">Fundstelle im Artikel</h4>
</div>
<ng-container table-detail-body>
@if (titleHighlights().get(article.id); as titleTerms) {
<article class="panel-field panel-field--full">
<h4 class="panel-field__label">Titel</h4>
<app-highlighted-text
class="panel-field__text"
[content]="article.maintitle"
[highlights]="titleTerms" />
</article>
}
@if (snippetHighlights().get(article.id); as snippet) {
<article class="panel-field panel-field--full">
<h4 class="panel-field__label">Artikeltext</h4>
<app-highlighted-text
class="panel-field__text"
[content]="snippet.content"
[highlights]="snippet.terms" />
</article>
} @else {
<span class="empty-text">
Für diesen Artikel liegt keine Textstelle zu den gesuchten Keywords
vor.
</span>
}
</ng-container>
</app-table-detail-row>
</ng-template>
<ng-template #dateTmp let-article>
@@ -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;
}
@@ -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<string, HighlightTerm[]>();
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<string, MatchedTopic>();
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(/<em>([\s\S]*?)<\/em>/g)].map((match) =>
match[1].trim(),
);
}
private toHighlightTerms(terms: string[]): HighlightTerm[] {
const seen = new Set<string>();
const searchMatch = /<em>([\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<string, MatchedTopic>,
): 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 {
@@ -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 = '(?<![\\w\\u00C0-\\u024F])';
const alternatives = terms.map(({ term }) => {
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, '&gt;')
.replace(/"/g, '&quot;');
}
}
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(
`(?<!${WORD_CHAR})(?:${terms.map(termPattern).join('|')})`,
flags,
);
}
export function containsHighlightTerm(
text: string | null | undefined,
terms: readonly string[],
): boolean {
const usable = terms.map((term) => 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, '\\$&');
}