in src/utils/highlightStemmed.ts [66:124]
export function splitIntoChunks(
content: string,
positions: MetadataPosition[],
tokens: string[]
): {
chunkIndex: number;
chunks: HighlightChunk[];
} {
const chunks: HighlightChunk[] = [];
let positionIndex = 0;
let cursor = 0;
let chunkIndex = -1;
while (positionIndex < positions.length) {
const [start, length] = positions[positionIndex];
positionIndex += 1;
if (start < cursor) {
continue;
}
if (start > cursor) {
const leadingChunks = looseTokenize(content.substring(cursor, start)).map(
(token) => ({
html: escapeHtml(token),
textLength: token.length,
})
);
for (const item of leadingChunks) {
chunks.push(item);
}
}
if (chunkIndex === -1) {
chunkIndex = chunks.length;
}
cursor = start + length;
chunks.push({
html: highlight(content.substring(start, cursor), tokens, true),
textLength: length,
});
}
if (cursor < content.length) {
const trailingChunks = looseTokenize(content.substring(cursor)).map(
(token) => ({
html: escapeHtml(token),
textLength: token.length,
})
);
for (const item of trailingChunks) {
chunks.push(item);
}
}
return {
chunkIndex,
chunks,
};
}