mirror of
https://github.com/dergigi/boris.git
synced 2025-12-28 03:54:51 +01:00
- Create textMatching module for text search utilities - Create domUtils module for DOM manipulation helpers - Create htmlMatching module for HTML highlight application - Reduce highlightMatching.tsx from 217 lines to 59 lines - All files now under 210 lines
47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import { Highlight } from '../../types/highlights'
|
|
|
|
export interface HighlightMatch {
|
|
highlight: Highlight
|
|
startIndex: number
|
|
endIndex: number
|
|
}
|
|
|
|
/**
|
|
* Normalize whitespace for flexible matching
|
|
*/
|
|
export const normalizeWhitespace = (str: string) => str.replace(/\s+/g, ' ').trim()
|
|
|
|
/**
|
|
* Find all occurrences of highlight text in the content
|
|
*/
|
|
export function findHighlightMatches(
|
|
content: string,
|
|
highlights: Highlight[]
|
|
): HighlightMatch[] {
|
|
const matches: HighlightMatch[] = []
|
|
|
|
for (const highlight of highlights) {
|
|
if (!highlight.content || highlight.content.trim().length === 0) {
|
|
continue
|
|
}
|
|
|
|
const searchText = highlight.content.trim()
|
|
let startIndex = 0
|
|
|
|
let index = content.indexOf(searchText, startIndex)
|
|
while (index !== -1) {
|
|
matches.push({
|
|
highlight,
|
|
startIndex: index,
|
|
endIndex: index + searchText.length
|
|
})
|
|
|
|
startIndex = index + searchText.length
|
|
index = content.indexOf(searchText, startIndex)
|
|
}
|
|
}
|
|
|
|
return matches.sort((a, b) => a.startIndex - b.startIndex)
|
|
}
|
|
|