generated from nhcarrigan/template
feat: add /search command to highlight matches in conversation
Closes #32
This commit is contained in:
@@ -6,9 +6,10 @@
|
||||
|
||||
interface Props {
|
||||
content: string;
|
||||
searchQuery?: string;
|
||||
}
|
||||
|
||||
let { content }: Props = $props();
|
||||
let { content, searchQuery = "" }: Props = $props();
|
||||
let containerElement: HTMLDivElement;
|
||||
|
||||
const renderer = new marked.Renderer();
|
||||
@@ -52,10 +53,47 @@
|
||||
return processed;
|
||||
}
|
||||
|
||||
function highlightSearchMatches(html: string, query: string): string {
|
||||
if (!query) return html;
|
||||
|
||||
const codeBlockPlaceholders: string[] = [];
|
||||
const tagPlaceholders: string[] = [];
|
||||
|
||||
// Temporarily replace code blocks with placeholders (don't highlight in code)
|
||||
let processed = html.replace(/<(pre|code)[^>]*>[\s\S]*?<\/\1>/gi, (match) => {
|
||||
codeBlockPlaceholders.push(match);
|
||||
return `__CODE_SEARCH_PLACEHOLDER_${codeBlockPlaceholders.length - 1}__`;
|
||||
});
|
||||
|
||||
// Temporarily replace all HTML tags with placeholders
|
||||
processed = processed.replace(/<[^>]+>/g, (match) => {
|
||||
tagPlaceholders.push(match);
|
||||
return `__TAG_PLACEHOLDER_${tagPlaceholders.length - 1}__`;
|
||||
});
|
||||
|
||||
// Apply search highlighting to text content
|
||||
const escapedQuery = query.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
const regex = new RegExp(`(${escapedQuery})`, "gi");
|
||||
processed = processed.replace(regex, '<mark class="search-highlight">$1</mark>');
|
||||
|
||||
// Restore HTML tags
|
||||
processed = processed.replace(/__TAG_PLACEHOLDER_(\d+)__/g, (_, index) => {
|
||||
return tagPlaceholders[parseInt(index)];
|
||||
});
|
||||
|
||||
// Restore code blocks
|
||||
processed = processed.replace(/__CODE_SEARCH_PLACEHOLDER_(\d+)__/g, (_, index) => {
|
||||
return codeBlockPlaceholders[parseInt(index)];
|
||||
});
|
||||
|
||||
return processed;
|
||||
}
|
||||
|
||||
function renderMarkdown(text: string): string {
|
||||
try {
|
||||
const html = marked.parse(text) as string;
|
||||
return processSpoilers(html);
|
||||
const withSpoilers = processSpoilers(html);
|
||||
return highlightSearchMatches(withSpoilers, searchQuery);
|
||||
} catch {
|
||||
return text;
|
||||
}
|
||||
@@ -304,4 +342,11 @@
|
||||
color: var(--text-primary);
|
||||
user-select: text;
|
||||
}
|
||||
|
||||
.markdown-content :global(.search-highlight) {
|
||||
background-color: var(--search-highlight, #fbbf24);
|
||||
color: var(--search-highlight-text, #000);
|
||||
border-radius: 2px;
|
||||
padding: 0 2px;
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user