From 1cf80ae07444bb23996d29ab316cab34b78a5d3f Mon Sep 17 00:00:00 2001 From: genja Date: Mon, 6 Jul 2026 18:49:49 +0200 Subject: [PATCH] added highlighted source text of extracted keywords --- .../keywords-search.component.html | 6 ++- .../keywords-search.component.ts | 51 +++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/webapp/src/app/modules/keywords-search/keywords-search.component.html b/webapp/src/app/modules/keywords-search/keywords-search.component.html index 26a29eea..f7b27d62 100644 --- a/webapp/src/app/modules/keywords-search/keywords-search.component.html +++ b/webapp/src/app/modules/keywords-search/keywords-search.component.html @@ -344,14 +344,16 @@ Quellentext: {{ sourceUrl() }} -
{{ sourceContent() }}
+
} @if (isExtracting() && !keywordsExist()) { } diff --git a/webapp/src/app/modules/keywords-search/keywords-search.component.ts b/webapp/src/app/modules/keywords-search/keywords-search.component.ts index 4cd4f59e..f63bec41 100644 --- a/webapp/src/app/modules/keywords-search/keywords-search.component.ts +++ b/webapp/src/app/modules/keywords-search/keywords-search.component.ts @@ -45,6 +45,7 @@ import { } from '../../resolvers/utils/topic-legend'; import { MatTooltipModule } from '@angular/material/tooltip'; import { A11yModule } from '@angular/cdk/a11y'; +import { DomSanitizer } from '@angular/platform-browser'; type InputMode = 'url' | 'text'; type KeywordCategory = 'broad' | 'coarse' | 'fine' | 'manual'; type KeywordView = 'llm' | 'topic'; @@ -77,6 +78,7 @@ type KeywordView = 'llm' | 'topic'; }) export class KeywordsSearchComponent { private readonly formBuilder = inject(FormBuilder); + private readonly sanitizer = inject(DomSanitizer); private readonly route = inject(ActivatedRoute); private readonly router = inject(Router); private readonly destroyRef = inject(DestroyRef); @@ -128,6 +130,55 @@ export class KeywordsSearchComponent { readonly sourceContent = signal(''); readonly sourceUrl = signal(null); + readonly highlightedSourceContent = computed(() => { + const content = this.sourceContent(); + if (!content) return ''; + + const selectedKeywords = this.selectedKeywords(); + if (selectedKeywords.length === 0) return content; + + let highlightedContent = content; + + // replace the keyword with highlighted spans + for (const keyword of selectedKeywords) { + const normalizedKeyword = this.normalizeKeyword(keyword); + + // Find the first keyword entry that matches this normalized keyword + const entry = this.allKeywordEntries().find( + (e) => + this.normalizeKeyword(this.getKeywordLabel(e)) === normalizedKeyword, + ); + + if (entry) { + const topic = this.getTopic(entry); + if (topic) { + const color = this.getTopicColor(topic); + // Escape HTML special characters in the keyword before using it in HTML + const escapedKeyword = keyword + .replace(/&/g, '&') + .replace(//g, '>'); + + // Create the replacement span HTML + const replacement = `${escapedKeyword}`; + + const escapedRegexKeyword = keyword.replace( + /[.*+?^${}()|[\]\\]/g, + '\\$&', + ); + const regex = new RegExp(escapedRegexKeyword, 'g'); + + highlightedContent = highlightedContent.replace(regex, replacement); + } + } + } + console.log( + 'Highlighted content:', + this.sanitizer.bypassSecurityTrustHtml(highlightedContent), + ); + return this.sanitizer.bypassSecurityTrustHtml(highlightedContent); + }); + private isRestoringState = false; // Set right before we call router.navigate() ourselves (from