diff --git a/packages/carta-md/src/lib/internal/speculative.ts b/packages/carta-md/src/lib/internal/speculative.ts index fd1f5a07..4e48b831 100644 --- a/packages/carta-md/src/lib/internal/speculative.ts +++ b/packages/carta-md/src/lib/internal/speculative.ts @@ -14,6 +14,10 @@ const clonePosition = (position: Position): Position => { }; }; +/** Escape text so it is parsed as text, and not as markup, by the overlay. */ +const escapeHtml = (text: string): string => + text.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>'); + /** * Temporary updates the highlight overlay to reflect the changes between two text strings, * waiting for the actual update to be applied. This way, the user can immediately see the changes, @@ -32,7 +36,7 @@ export function speculativeHighlightUpdate(from: string, to: string, currentHTML let writingPosition: Position = { line: 0, span: 0, char: 0 }; let readingPosition: Position = { line: 0, span: 0, char: 0 }; - if (lines.length === 0) return to; + if (lines.length === 0) return escapeHtml(to); const advance = () => { writingPosition = clonePosition(readingPosition);