mirror of
https://github.com/zadam/trilium.git
synced 2025-10-20 15:19:01 +02:00
feat(quick_search): format multi-line results better
This commit is contained in:
parent
318d504fad
commit
bbf8d757cd
@ -468,8 +468,13 @@ function extractContentSnippet(noteId: string, searchTokens: string[], maxLength
|
|||||||
content = striptags(content);
|
content = striptags(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Normalize whitespace
|
// Normalize whitespace while preserving paragraph breaks
|
||||||
content = content.replace(/\s+/g, " ").trim();
|
// First, normalize multiple newlines to double newlines (paragraph breaks)
|
||||||
|
content = content.replace(/\n\s*\n/g, "\n\n");
|
||||||
|
// Then normalize spaces within lines
|
||||||
|
content = content.split('\n').map(line => line.replace(/\s+/g, " ").trim()).join('\n');
|
||||||
|
// Finally trim the whole content
|
||||||
|
content = content.trim();
|
||||||
|
|
||||||
if (!content) {
|
if (!content) {
|
||||||
return "";
|
return "";
|
||||||
@ -495,21 +500,36 @@ function extractContentSnippet(noteId: string, searchTokens: string[], maxLength
|
|||||||
// Extract snippet
|
// Extract snippet
|
||||||
let snippet = content.substring(snippetStart, snippetStart + maxLength);
|
let snippet = content.substring(snippetStart, snippetStart + maxLength);
|
||||||
|
|
||||||
// Try to start/end at word boundaries
|
// If snippet contains linebreaks, limit to max 4 lines and override character limit
|
||||||
if (snippetStart > 0) {
|
const lines = snippet.split('\n');
|
||||||
const firstSpace = snippet.indexOf(" ");
|
if (lines.length > 4) {
|
||||||
if (firstSpace > 0 && firstSpace < 20) {
|
snippet = lines.slice(0, 4).join('\n');
|
||||||
snippet = snippet.substring(firstSpace + 1);
|
// Add ellipsis if we truncated lines
|
||||||
}
|
|
||||||
snippet = "..." + snippet;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (snippetStart + maxLength < content.length) {
|
|
||||||
const lastSpace = snippet.lastIndexOf(" ");
|
|
||||||
if (lastSpace > snippet.length - 20) {
|
|
||||||
snippet = snippet.substring(0, lastSpace);
|
|
||||||
}
|
|
||||||
snippet = snippet + "...";
|
snippet = snippet + "...";
|
||||||
|
} else if (lines.length > 1) {
|
||||||
|
// For multi-line snippets, just limit to 4 lines (keep existing snippet)
|
||||||
|
snippet = lines.slice(0, 4).join('\n');
|
||||||
|
if (lines.length > 4) {
|
||||||
|
snippet = snippet + "...";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Single line content - apply original word boundary logic
|
||||||
|
// Try to start/end at word boundaries
|
||||||
|
if (snippetStart > 0) {
|
||||||
|
const firstSpace = snippet.search(/\s/);
|
||||||
|
if (firstSpace > 0 && firstSpace < 20) {
|
||||||
|
snippet = snippet.substring(firstSpace + 1);
|
||||||
|
}
|
||||||
|
snippet = "..." + snippet;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (snippetStart + maxLength < content.length) {
|
||||||
|
const lastSpace = snippet.search(/\s[^\s]*$/);
|
||||||
|
if (lastSpace > snippet.length - 20 && lastSpace > 0) {
|
||||||
|
snippet = snippet.substring(0, lastSpace);
|
||||||
|
}
|
||||||
|
snippet = snippet + "...";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return snippet;
|
return snippet;
|
||||||
@ -574,7 +594,10 @@ function highlightSearchResults(searchResults: SearchResult[], highlightedTokens
|
|||||||
|
|
||||||
// Initialize highlighted content snippet
|
// Initialize highlighted content snippet
|
||||||
if (result.contentSnippet) {
|
if (result.contentSnippet) {
|
||||||
result.highlightedContentSnippet = escapeHtml(result.contentSnippet).replace(/[<{}]/g, "");
|
// Escape HTML but preserve newlines for later conversion to <br>
|
||||||
|
result.highlightedContentSnippet = escapeHtml(result.contentSnippet);
|
||||||
|
// Remove any stray < { } that might interfere with our highlighting markers
|
||||||
|
result.highlightedContentSnippet = result.highlightedContentSnippet.replace(/[<{}]/g, "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -621,7 +644,10 @@ function highlightSearchResults(searchResults: SearchResult[], highlightedTokens
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (result.highlightedContentSnippet) {
|
if (result.highlightedContentSnippet) {
|
||||||
|
// Replace highlighting markers with HTML tags
|
||||||
result.highlightedContentSnippet = result.highlightedContentSnippet.replace(/{/g, "<b>").replace(/}/g, "</b>");
|
result.highlightedContentSnippet = result.highlightedContentSnippet.replace(/{/g, "<b>").replace(/}/g, "</b>");
|
||||||
|
// Convert newlines to <br> tags for HTML display
|
||||||
|
result.highlightedContentSnippet = result.highlightedContentSnippet.replace(/\n/g, "<br>");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user