feat(util): add splitByMarkers for [unleserlich] and [...] text splitting

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Marcel
2026-04-07 11:00:23 +02:00
parent f38c384268
commit 3279342ea7
2 changed files with 85 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
export type TextSegment = { type: 'text' | 'marker'; text: string };
const MARKER_PATTERN = /(\[unleserlich\]|\[\.{3}\])/g;
export function splitByMarkers(input: string): TextSegment[] {
if (!input) return [];
const segments: TextSegment[] = [];
let lastIndex = 0;
for (const match of input.matchAll(MARKER_PATTERN)) {
const matchStart = match.index;
if (matchStart > lastIndex) {
segments.push({ type: 'text', text: input.slice(lastIndex, matchStart) });
}
segments.push({ type: 'marker', text: match[0] });
lastIndex = matchStart + match[0].length;
}
if (lastIndex < input.length) {
segments.push({ type: 'text', text: input.slice(lastIndex) });
}
return segments;
}