Files
outline/app/editor/extensions/deburrWithMap.ts
T
Tom MoorandClaude Opus 4.8 3999f2d902 fix: Refactor find logic for non-ascii characters (#12813)
* fix: correct find-and-replace highlight positions for CJK/Hangul text

The in-document find feature searched over `deburr(text) + text` and assumed
the deburred prefix had the same length as the original, mapping match indices
with `m.index - text.length`. `deburr` normalizes to NFD form, which decomposes
precomposed CJK/Hangul syllables (e.g. "강" is one UTF-16 code unit but
decomposes into three jamo), so the deburred prefix is longer than the original.
This broke the boundary assumption, producing misaligned highlights, undercounted
matches, and out-of-bounds positions (the "array index out of bounds" console
error from expandCollapsedCodeBlockForCurrentMatch).

Replace the length-based offset math with an explicit index map built by
`deburrWithMap`, which records the original index of every emitted code unit.
The text is searched both deburred (diacritic-insensitive) and verbatim, with
each match translated back to its true position via the map.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKAYuiMFqpBNbCPirhe8yk

* refactor: extract deburrWithMap into its own module with unit tests

Move the diacritics-stripping/index-map helper out of the extension into a
dedicated module so it can be unit-tested in isolation without pulling in the
editor's React/ProseMirror component tree. Add tests covering ASCII, diacritics,
Hangul (NFD-expanded) text, ASCII matches surrounded by CJK, and surrogate pairs.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKAYuiMFqpBNbCPirhe8yk

* perf: add fast path to deburrWithMap to avoid per-character work

Only build the per-character index map when deburring actually changes the
string length (NFD-decomposed CJK/Hangul, expanded ligatures). For ASCII and
ordinary accented Latin text – where length is preserved and indices line up
1:1 – return an identity mapping with no per-character table, and skip the
redundant second search pass over deburred text when it equals the original.
Within the slow path, skip normalization of ASCII characters.

This keeps the common case as cheap as a single deburr() call (~0.4ms/keystroke
on a 100k-char document) and roughly halves the mixed-CJK cost.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKAYuiMFqpBNbCPirhe8yk

* test: cover interleaved ASCII and Hangul in deburrWithMap

Verify the index map stays consistent when ASCII characters (which take the
fast skip branch) are interleaved with Hangul characters (which decompose under
NFD), so every ASCII match still resolves to its exact original code unit.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKAYuiMFqpBNbCPirhe8yk

* fix: skip zero-length deburred matches and reduce slow-path allocations

Address review feedback on the CJK find-and-replace fix:

- A regex match against the deburred text can cover only part of a decomposed
  sequence (e.g. a single jamo of a Hangul syllable), which maps back to a
  zero-length range in the original text. Recording such a range made replace
  behave like an insert (from === to); skip matches where to <= from.

- Build the deburred string in the slow path by joining an array of parts once
  instead of repeated string concatenation in the loop, keeping it linear with
  less allocation churn.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKAYuiMFqpBNbCPirhe8yk

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-06-24 20:11:20 -04:00

64 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { deburr } from "es-toolkit/compat";
/**
* Build a diacritics-stripped version of a string together with a way to map
* any index in the stripped string back to the corresponding index in the
* original string.
*
* `deburr` normalizes to NFD form, which can change the length of the string –
* most notably it decomposes precomposed CJK/Hangul syllables (e.g. "강" is a
* single UTF-16 code unit but decomposes into three jamo). When that happens a
* naive offset no longer lines up with the original, so the source index of
* every emitted code unit is recorded instead.
*
* When deburring does not change the length – the common case for ASCII and
* ordinary accented Latin text – indices line up one-to-one and the cheap
* identity mapping is returned without building any per-character table.
*
* @param text The original text to deburr.
* @returns the deburred text and a `toOriginalIndex` function translating a
* deburred index (0…deburred.length) into the corresponding original index.
*/
export function deburrWithMap(text: string): {
deburred: string;
toOriginalIndex: (index: number) => number;
} {
const deburred = deburr(text);
// Fast path: deburring preserved the length, so indices line up one-to-one
// with the original string and no per-character table is needed.
if (deburred.length === text.length) {
return { deburred, toOriginalIndex: (index) => index };
}
// Slow path: deburring changed the length (e.g. NFD decomposed a CJK/Hangul
// syllable or expanded a ligature). Rebuild the deburred string per code
// point so it stays consistent with the index map, recording the original
// index of every emitted code unit plus a sentinel for the end position.
const parts: string[] = [];
const map: number[] = [];
let originalIndex = 0;
// Iterate by code point so surrogate pairs are deburred as a unit.
for (const char of text) {
// ASCII characters are never altered or decomposed by deburr, so emit them
// directly and skip the comparatively expensive normalization.
if (char.charCodeAt(0) < 0x80) {
map.push(originalIndex);
parts.push(char);
originalIndex += 1;
continue;
}
const stripped = deburr(char);
for (let i = 0; i < stripped.length; i++) {
map.push(originalIndex);
}
parts.push(stripped);
originalIndex += char.length;
}
map.push(text.length);
return { deburred: parts.join(""), toOriginalIndex: (index) => map[index] };
}