UNPKG

@atlaskit/renderer

Version:
41 lines 1.12 kB
export function segmentText(text, highlighter) { if (!highlighter || !text) { return [{ type: 'plain', text: text !== null && text !== void 0 ? text : '' }]; } const segments = []; let pos = 0; try { const markTextSegmentMatches = text.matchAll(highlighter.pattern); for (const markTextMatch of markTextSegmentMatches) { if (markTextMatch.index !== 0) { segments.push({ type: 'plain', text: text.substring(pos, markTextMatch.index) }); } segments.push({ type: 'match', text: markTextMatch[0], groups: markTextMatch.groups && // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion Object.keys(markTextMatch.groups).filter(key => markTextMatch.groups[key]) }); pos = markTextMatch.index + markTextMatch[0].length; } if (pos < text.length) { segments.push({ type: 'plain', text: text.substring(pos) }); } return segments; } catch (_e) {} return [{ type: 'plain', text }]; }