vultus-core 0.5.7 → 0.5.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/README.md +191 -5
  2. package/dist/Components.d.ts +41 -0
  3. package/dist/Components.d.ts.map +1 -1
  4. package/dist/Components.js.map +1 -1
  5. package/dist/changes/ChangeMark.d.ts +85 -0
  6. package/dist/changes/ChangeMark.d.ts.map +1 -0
  7. package/dist/changes/ChangeMark.js +113 -0
  8. package/dist/changes/ChangeMark.js.map +1 -0
  9. package/dist/changes/ChangingText.d.ts +20 -0
  10. package/dist/changes/ChangingText.d.ts.map +1 -0
  11. package/dist/changes/ChangingText.js +31 -0
  12. package/dist/changes/ChangingText.js.map +1 -0
  13. package/dist/changes/FallbackMark.d.ts +20 -0
  14. package/dist/changes/FallbackMark.d.ts.map +1 -0
  15. package/dist/changes/FallbackMark.js +53 -0
  16. package/dist/changes/FallbackMark.js.map +1 -0
  17. package/dist/changes/blocks.d.ts +15 -0
  18. package/dist/changes/blocks.d.ts.map +1 -0
  19. package/dist/changes/blocks.js +24 -0
  20. package/dist/changes/blocks.js.map +1 -0
  21. package/dist/changes/changeDurations.d.ts +21 -0
  22. package/dist/changes/changeDurations.d.ts.map +1 -0
  23. package/dist/changes/changeDurations.js +18 -0
  24. package/dist/changes/changeDurations.js.map +1 -0
  25. package/dist/changes/changesIn.d.ts +88 -0
  26. package/dist/changes/changesIn.d.ts.map +1 -0
  27. package/dist/changes/changesIn.js +470 -0
  28. package/dist/changes/changesIn.js.map +1 -0
  29. package/dist/changes/readText.d.ts +45 -0
  30. package/dist/changes/readText.d.ts.map +1 -0
  31. package/dist/changes/readText.js +99 -0
  32. package/dist/changes/readText.js.map +1 -0
  33. package/dist/changes/rehypeChanges.d.ts +81 -0
  34. package/dist/changes/rehypeChanges.d.ts.map +1 -0
  35. package/dist/changes/rehypeChanges.js +268 -0
  36. package/dist/changes/rehypeChanges.js.map +1 -0
  37. package/dist/changes/useTextChanges.d.ts +93 -0
  38. package/dist/changes/useTextChanges.d.ts.map +1 -0
  39. package/dist/changes/useTextChanges.js +154 -0
  40. package/dist/changes/useTextChanges.js.map +1 -0
  41. package/dist/changes/visibleBlocks.d.ts +42 -0
  42. package/dist/changes/visibleBlocks.d.ts.map +1 -0
  43. package/dist/changes/visibleBlocks.js +104 -0
  44. package/dist/changes/visibleBlocks.js.map +1 -0
  45. package/dist/changes.d.ts +9 -0
  46. package/dist/changes.d.ts.map +1 -0
  47. package/dist/changes.js +10 -0
  48. package/dist/changes.js.map +1 -0
  49. package/dist/index.d.ts +3 -1
  50. package/dist/index.d.ts.map +1 -1
  51. package/dist/index.js.map +1 -1
  52. package/package.json +13 -1
  53. package/src/Components.tsx +45 -0
  54. package/src/changes/ChangeMark.tsx +165 -0
  55. package/src/changes/ChangingText.tsx +46 -0
  56. package/src/changes/FallbackMark.tsx +59 -0
  57. package/src/changes/blocks.ts +25 -0
  58. package/src/changes/changeDurations.ts +31 -0
  59. package/src/changes/changesIn.ts +479 -0
  60. package/src/changes/readText.ts +123 -0
  61. package/src/changes/rehypeChanges.ts +302 -0
  62. package/src/changes/useTextChanges.ts +239 -0
  63. package/src/changes/visibleBlocks.ts +121 -0
  64. package/src/changes.ts +14 -0
  65. package/src/index.ts +4 -0
@@ -0,0 +1,470 @@
1
+ import { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, diff_match_patch } from 'diff-match-patch';
2
+ /** diff-match-patch's time budget for one call, in seconds: the diff runs in the render that shows
3
+ * the new text, so a long text must not hold that render up. Past it the diff is coarser, not
4
+ * wrong. */
5
+ const DIFF_TIMEOUT_S = 0.2;
6
+ /**
7
+ * The share of letters past which something is replaced whole rather than letter by letter. A
8
+ * change is replaced whole, its old run removed whole before its new one, lit whole, when more than
9
+ * this share of the new run's letters were put in or of the old run's letters removed, both ways
10
+ * alike ("t. i." to "tudniillik": `[-t. i.][+tudniillik]`; back: `[-tudniillik][+t. i.]`). In a
11
+ * change that is not, a word more than this share of whose letters were put in is marked whole.
12
+ * This is the one place to change it.
13
+ */
14
+ const WHOLE_WORD_SHARE = 0.5;
15
+ /**
16
+ * The longest new run (UTF-16 units, first token to last) a change replaced whole is lit as: one
17
+ * piece of the lent mark's, which may not wrap, so no longer than fits a phone's line with room to
18
+ * spare. A longer one is lit word by word (each word with more than `WHOLE_WORD_SHARE` of its
19
+ * letters put in, whole).
20
+ */
21
+ const WHOLE_RUN_LENGTH = 24;
22
+ const WORD_RUN = /[\p{L}\p{M}\p{N}]+|\S/gu;
23
+ /**
24
+ * Splits a text into the units the comparison works on: a word is a run of letters, combining
25
+ * marks and digits, every other run of non-space characters is punctuation, one token per
26
+ * character, white space is no token. So "napról-napra" is a word, a hyphen and a word, and the
27
+ * hyphen's going is a change of its own that marks no word.
28
+ */
29
+ export function tokens(text) {
30
+ return Array.from(text.matchAll(WORD_RUN), (match) => ({
31
+ start: match.index,
32
+ end: match.index + match[0].length,
33
+ word: /^[\p{L}\p{M}\p{N}]/u.test(match[0]),
34
+ }));
35
+ }
36
+ /**
37
+ * Whether the character at an offset of `text` is white space that ends a line: each character of
38
+ * a white-space run holding '\n' or '\r' (a hard boundary, `changesIn`). Computed once per text.
39
+ */
40
+ export function lineEnds(text) {
41
+ const ends = new Set();
42
+ for (const run of text.matchAll(/\s+/g)) {
43
+ if (!/[\n\r]/.test(run[0]))
44
+ continue;
45
+ for (let at = run.index; at < run.index + run[0].length; at++)
46
+ ends.add(at);
47
+ }
48
+ return (at) => ends.has(at);
49
+ }
50
+ /**
51
+ * What changed between two versions of a text, as spans of the new one at two levels (GitHub-diff
52
+ * style): `word` spans, the changed words whole, never overlapping; and inside them `letters`
53
+ * spans, the changed letters. Compared word by word (each distinct token one character of an
54
+ * alphabet, so diff-match-patch's line-of-characters speed carries over to words), then, per
55
+ * change, letter by letter. A change that only inserts marks its words at both levels; one that
56
+ * replaces words is compared letter by letter, only the letters that differ marked ("Ferencz" to
57
+ * "Ferenc": the "z" removed and nothing lit).
58
+ * A change replaced whole (`WHOLE_WORD_SHARE` of the new run's letters put in, or of the old
59
+ * run's removed: "t. i." to "tudniillik" and back) is one span, its new run lit whole and the old
60
+ * one removed whole before it, when short (`WHOLE_RUN_LENGTH`); otherwise a word more than
61
+ * `WHOLE_WORD_SHARE` of whose letters are new is marked whole at the letter level, the old text it
62
+ * stands for removed before it. A split, one old word become several ("jóhirű" to "jó
63
+ * hírű": a space put in between two old letters), is one `word` span over the new run, the space
64
+ * among its letters, growing in as they do. Punctuation alone is never
65
+ * marked: a change of punctuation alone marks nothing ("napról-napra" to "napról napra").
66
+ *
67
+ * A changed word's `word` span runs from white space to white space, on every switch: the
68
+ * punctuation touching it (a comma, an opening quote)
69
+ * is inside its mark, lit lightly with it ("hivei," to "hívei,": "hívei,"), never at the letter
70
+ * level; a line may break around the lent mark's removal pieces, so a comma outside the mark could
71
+ * start the next line. The changed words of one run of non-space characters share one span. A new
72
+ * word in place of nothing (put in, or marked whole with no old text before it) holds one space
73
+ * beside it in both its spans, the one between it and the next token (at the end, the one between
74
+ * the token before and it: "a b" to "a b új", " új"), so that growing in from no width it leaves
75
+ * one space between its neighbours, not two.
76
+ *
77
+ * A line feed in `to` is a hard boundary (`lineEnds`: every character of a white-space run holding
78
+ * '\n' or '\r'). The reading (`readText`) puts one exactly where a line really ends: between
79
+ * blocks, at a `br`, at a code line's end. A line feed in a mark would be written as a space and
80
+ * join the lines, so a new word takes no such space, and neither a split nor a run replaced whole
81
+ * is one span across one (the run is lit word by word, as a long one is). Only hard boundaries
82
+ * moved (a paragraph split or joined) is no change: white space is no token.
83
+ *
84
+ * And what went: the old letters each change removed, as `removed` changes of no width at their
85
+ * place in the new text, carrying them (`text`, its white space collapsed: never a line feed,
86
+ * which the lent mark's pieces may keep as a break). Inside a changed word, before the letters
87
+ * that replaced them ("hivei" to "hívei": the "i" before the "í"; "katholikus" to "katolikus": the
88
+ * "h" between "t" and "o"), white space and punctuation too (a space joined away: "szent
89
+ * áldozásban" to "szentáldozásban"), never between a letter and its combining mark; at a word
90
+ * marked whole, the old text it replaced, whole, before it ("t. i." before "tudniillik"). What
91
+ * went outside every word's span is not shown. A change that removed
92
+ * words and put in none (a word removed altogether) gets a `word` span of no width at the start
93
+ * of the token after it (past the last one, at the end of the text), holding the run with the
94
+ * space that separated it: after it ("a b c" to "a c": "b "), or before it where nothing separates
95
+ * the place from the token before it (at the end: "a b c" to "a b", " c"; before punctuation:
96
+ * "mondta ő, hogy" to "mondta, hogy", " ő"), so it sits between its neighbours as it did. A run
97
+ * that touched the token before it (punctuation first) goes at that token's end instead, with no
98
+ * space after it ("Péter, a halász, mondta" to "Péter mondta": ", a halász,"). So does a run that
99
+ * ended its line, where the new text keeps that line end, with the space before it: a word removed
100
+ * at a paragraph's end stays in that paragraph ("a b\nc d" to "a\nc d": " b"); a line end only the
101
+ * old text had places nothing ("a b\nc d" to "a d": "b c "). More distinct tokens than there are
102
+ * code units to stand for them gives `[]`: no marks rather than a wrong or a slow diff.
103
+ */
104
+ export function changesIn(from, to) {
105
+ const fromTokens = tokens(from);
106
+ const toTokens = tokens(to);
107
+ const alphabet = new Map();
108
+ let next = 1;
109
+ const unitFor = (text) => {
110
+ const known = alphabet.get(text);
111
+ if (known !== undefined)
112
+ return known;
113
+ if (next === 0xd800)
114
+ next = 0xe000;
115
+ if (next > 0xffff)
116
+ return undefined;
117
+ const unit = String.fromCharCode(next++);
118
+ alphabet.set(text, unit);
119
+ return unit;
120
+ };
121
+ const encode = (source, list) => {
122
+ let encoded = '';
123
+ for (const token of list) {
124
+ const unit = unitFor(source.slice(token.start, token.end));
125
+ if (unit === undefined)
126
+ return undefined;
127
+ encoded += unit;
128
+ }
129
+ return encoded;
130
+ };
131
+ const a = encode(from, fromTokens);
132
+ const b = a === undefined ? undefined : encode(to, toTokens);
133
+ if (a === undefined || b === undefined)
134
+ return [];
135
+ const dmp = new diff_match_patch();
136
+ dmp.Diff_Timeout = DIFF_TIMEOUT_S;
137
+ // One deadline for the whole call, shared by every diff in it.
138
+ const deadline = Date.now() + DIFF_TIMEOUT_S * 1000;
139
+ const diffs = dmp.diff_main(a, b, false, deadline);
140
+ const lineEnd = lineEnds(to);
141
+ const fromLineEnd = lineEnds(from);
142
+ const changes = [];
143
+ let i = 0;
144
+ let j = 0;
145
+ let k = 0;
146
+ while (k < diffs.length) {
147
+ const [op, text] = diffs[k];
148
+ if (op === DIFF_EQUAL) {
149
+ i += text.length;
150
+ j += text.length;
151
+ k++;
152
+ continue;
153
+ }
154
+ const deletedFrom = i;
155
+ const insertedFrom = j;
156
+ while (k < diffs.length && diffs[k][0] !== DIFF_EQUAL) {
157
+ if (diffs[k][0] === DIFF_DELETE)
158
+ i += diffs[k][1].length;
159
+ else
160
+ j += diffs[k][1].length;
161
+ k++;
162
+ }
163
+ const deleted = fromTokens.slice(deletedFrom, i);
164
+ const inserted = toTokens.slice(insertedFrom, j);
165
+ if (inserted.some((token) => token.word))
166
+ addChange(changes, from, deleted, to, inserted, toTokens[j], lineEnd, dmp, deadline);
167
+ else if (deleted.some((token) => token.word)) {
168
+ addRemoval(changes, from, deleted, toTokens[insertedFrom - 1], toTokens[insertedFrom], lineEnd, fromLineEnd);
169
+ }
170
+ }
171
+ return toWhiteSpace(changes, to);
172
+ }
173
+ /**
174
+ * `changes` (in the order `addChange` and `addRemoval` give them, each level's in place order)
175
+ * with each word's span widened from white space to white space in `to` (`changesIn`), an edge
176
+ * that holds a space already left as it is, and the words of one run of non-space characters
177
+ * merged into one span; a word removed altogether inside one of them (beside a changed word) is
178
+ * no word of its own there, and what went outside every word's span is dropped.
179
+ */
180
+ function toWhiteSpace(changes, to) {
181
+ const spans = [];
182
+ for (const change of changes) {
183
+ if (change.level !== 'word' || change.start === change.end)
184
+ continue;
185
+ let { start, end } = change;
186
+ if (!/\s/.test(to.charAt(start)))
187
+ while (start > 0 && !/\s/.test(to.charAt(start - 1)))
188
+ start--;
189
+ if (!/\s/.test(to.charAt(end - 1)))
190
+ while (end < to.length && !/\s/.test(to.charAt(end)))
191
+ end++;
192
+ const last = spans[spans.length - 1];
193
+ if (last && start < last.end)
194
+ last.end = Math.max(last.end, end);
195
+ else
196
+ spans.push({ start, end, level: 'word' });
197
+ }
198
+ // The places come in order: one pointer finds the span each is in, at an edge too.
199
+ let s = 0;
200
+ const inSpan = (at) => {
201
+ while (s < spans.length && spans[s].end < at)
202
+ s++;
203
+ return s < spans.length && spans[s].start <= at;
204
+ };
205
+ const out = [];
206
+ let next = 0;
207
+ let lone;
208
+ for (const change of changes) {
209
+ if (change.level === 'letters')
210
+ out.push(change);
211
+ else if (change.level === 'word' && change.start < change.end) {
212
+ // Its widened span, put in at the first of its words.
213
+ if (next < spans.length && spans[next].start <= change.start)
214
+ out.push(spans[next++]);
215
+ }
216
+ else if (change.level === 'word') {
217
+ lone = inSpan(change.start) ? undefined : change.start;
218
+ if (lone !== undefined)
219
+ out.push(change);
220
+ }
221
+ else if (change.start === lone || inSpan(change.start))
222
+ out.push(change);
223
+ }
224
+ return out;
225
+ }
226
+ /**
227
+ * One change that put in a word: the old tokens it removed, the new ones it inserted (`after`, the
228
+ * token of `to` after them), appended to `changes` as `word` spans (on the words alone, widened
229
+ * later: `toWhiteSpace`), their `letters` and what went (`removed`, in place order, before the
230
+ * letters at the same place); `lineEnd`, `to`'s line ends (`lineEnds`). Appends one by one: a
231
+ * change of a hundred thousand words must not become a call's arguments.
232
+ */
233
+ function addChange(changes, from, deleted, to, inserted, after, lineEnd, dmp, deadline) {
234
+ // Whether a token comes after the inserted one at `n`, for the space a new word takes.
235
+ const followed = (n) => (inserted[n + 1] ?? after) !== undefined;
236
+ // Whether `to`'s `start`-`end` holds a line end that must stay one (`changesIn`).
237
+ const crossesLineEnd = (start, end) => {
238
+ for (let at = start; at < end; at++)
239
+ if (/\s/.test(to.charAt(at)) && lineEnd(at))
240
+ return true;
241
+ return false;
242
+ };
243
+ if (deleted.length === 0) {
244
+ let taken = 0;
245
+ for (let n = 0; n < inserted.length; n++) {
246
+ const word = inserted[n];
247
+ if (!word.word)
248
+ continue;
249
+ const { start, end } = withSpace(word, followed(n), to, taken, lineEnd);
250
+ changes.push({ start, end, level: 'word' });
251
+ changes.push({ start, end, level: 'letters' });
252
+ taken = end;
253
+ }
254
+ return;
255
+ }
256
+ // The old run's text as it stands, its white space collapsed, so what went reads as it was
257
+ // ("t. i.", not "t . i ."); the new run's tokens joined by single spaces, as the offsets below
258
+ // count them (where the new text joins a word and a comma, the diff sees a space put in, which
259
+ // marks nothing). For each character of the new side, whether the letter diff inserted it; for
260
+ // each place of it, where the old side is there, past what went at it; and what went at a place.
261
+ const oldText = from.slice(deleted[0].start, deleted[deleted.length - 1].end).replace(/\s+/g, ' ');
262
+ const newText = inserted.map((token) => to.slice(token.start, token.end)).join(' ');
263
+ const diffs = dmp.diff_main(oldText, newText, false, deadline);
264
+ const isNew = new Array(newText.length).fill(false);
265
+ const oldAt = [];
266
+ const went = new Map();
267
+ let position = 0;
268
+ let old = 0;
269
+ for (const [op, text] of diffs) {
270
+ if (op === DIFF_DELETE) {
271
+ const there = went.get(position);
272
+ if (there)
273
+ there.end += text.length;
274
+ else
275
+ went.set(position, { start: old, end: old + text.length });
276
+ old += text.length;
277
+ continue;
278
+ }
279
+ for (let c = 0; c < text.length; c++)
280
+ oldAt.push(op === DIFF_EQUAL ? old + c : old);
281
+ if (op === DIFF_INSERT)
282
+ isNew.fill(true, position, position + text.length);
283
+ else
284
+ old += text.length;
285
+ position += text.length;
286
+ }
287
+ oldAt.push(old);
288
+ // Replaced whole, a short run on one line: the old run removed whole before the new one, lit
289
+ // whole.
290
+ const first = inserted[0];
291
+ const last = inserted[inserted.length - 1];
292
+ if (last.end - first.start <= WHOLE_RUN_LENGTH && !crossesLineEnd(first.start, last.end) && replacedWhole(diffs)) {
293
+ changes.push({ start: first.start, end: last.end, level: 'word' });
294
+ changes.push({ start: first.start, end: first.start, level: 'removed', text: oldText });
295
+ changes.push({ start: first.start, end: last.end, level: 'letters' });
296
+ return;
297
+ }
298
+ // Letters lit, `start`-`end`, run on from the ones just before them, in one word's span (a word
299
+ // marked whole has letters of its own: one piece over several words could not wrap).
300
+ const addLetters = (start, end) => {
301
+ const last = changes[changes.length - 1];
302
+ if (last.level === 'letters' && last.end === start && span !== undefined && last.start >= span.start)
303
+ last.end = end;
304
+ else
305
+ changes.push({ start, end, level: 'letters' });
306
+ };
307
+ // A place of the joined new side belongs to the token it is in or at an edge of (the space
308
+ // joining two tokens has a place at each end).
309
+ let offset = 0;
310
+ let taken = 0;
311
+ let span;
312
+ for (let n = 0; n < inserted.length; n++) {
313
+ const token = inserted[n];
314
+ const length = token.end - token.start;
315
+ if (token.word) {
316
+ const word = to.slice(token.start, token.end);
317
+ const marked = wholeCharacters(word, isNew.slice(offset, offset + length));
318
+ const count = marked.filter(Boolean).length;
319
+ const whole = count > length * WHOLE_WORD_SHARE;
320
+ // A new word: the old text it stands for, from before what went at its start to past what
321
+ // went at its end, removed before it whole; with none, it holds the space beside it, as a
322
+ // word put in does.
323
+ const text = whole ? oldText.slice(went.get(offset)?.start ?? oldAt[offset], oldAt[offset + length]) : '';
324
+ const { start, end } = whole && !text ? withSpace(token, followed(n), to, taken, lineEnd) : token;
325
+ // A split ("jóhirű" to "jó hírű"): the space between this word and the one before was put in
326
+ // between two old letters (on one line); the span before goes on over both, the space among
327
+ // its letters.
328
+ const previous = inserted[n - 1];
329
+ const joint = offset - 1;
330
+ if (previous?.word && span?.end === previous.end && isNew[joint] && !isNew[joint - 1] && !isNew[offset]
331
+ && !crossesLineEnd(previous.end, token.start)) {
332
+ span.end = end;
333
+ addLetters(previous.end, token.start);
334
+ }
335
+ else
336
+ changes.push(span = { start, end, level: 'word' });
337
+ taken = end;
338
+ if (whole) {
339
+ if (text)
340
+ changes.push({ start, end: start, level: 'removed', text });
341
+ changes.push({ start, end, level: 'letters' });
342
+ }
343
+ else {
344
+ // What went inside the word, each at the start of the character it went in.
345
+ const removed = new Map();
346
+ for (let c = 0; c <= length; c++) {
347
+ const gone = went.get(offset + c);
348
+ if (!gone)
349
+ continue;
350
+ const at = characterStart(word, c);
351
+ removed.set(at, (removed.get(at) ?? '') + oldText.slice(gone.start, gone.end));
352
+ }
353
+ for (let c = 0; c <= length; c++) {
354
+ const lost = removed.get(c);
355
+ if (lost)
356
+ changes.push({ start: token.start + c, end: token.start + c, level: 'removed', text: lost });
357
+ if (c < length && marked[c])
358
+ addLetters(token.start + c, token.start + c + 1);
359
+ }
360
+ }
361
+ }
362
+ else {
363
+ // Punctuation: what went at it shows where it touches a changed word, inside the word's span.
364
+ for (let c = 0; c <= length; c++) {
365
+ const gone = went.get(offset + c);
366
+ if (gone)
367
+ changes.push({ start: token.start + c, end: token.start + c, level: 'removed', text: oldText.slice(gone.start, gone.end) });
368
+ }
369
+ }
370
+ offset += length + 1;
371
+ }
372
+ }
373
+ /**
374
+ * One change that removed words and put in none (punctuation at most), appended to `changes`: a
375
+ * `word` span of no width where it went, holding the run `removed` with the space that separated
376
+ * it, its white space collapsed (`changesIn`). `before` and `after` are the tokens of the new text
377
+ * around the place: the start of `after`, or at the end the end of `before`; the end of `before`
378
+ * too where the run touched it, or where it ended its line (`fromLineEnd` after it) and the new
379
+ * text keeps a line end after `before` (`toLineEnd`). The space after the run unless the place is
380
+ * at the end, the run touched `before` or ended its line; the space before it where no space
381
+ * separates the place from `before` (at the end, before punctuation, at a line's end).
382
+ */
383
+ function addRemoval(changes, from, deleted, before, after, toLineEnd, fromLineEnd) {
384
+ const first = deleted[0];
385
+ const last = deleted[deleted.length - 1];
386
+ // A run that touched the token before it in the old text (punctuation: "Péter, a halász,
387
+ // mondta" to "Péter mondta") goes at that token's end, the space after it left where it is.
388
+ const touching = before !== undefined && !first.word && first.start > 0 && !/\s/.test(from.charAt(first.start - 1));
389
+ // A run that ended its line stays on it where the new text keeps the line end: a word removed at
390
+ // a paragraph's end shows in that paragraph, not at the next one's start.
391
+ const endedLine = !touching && before !== undefined && after !== undefined
392
+ && toLineEnd(before.end) && fromLineEnd(last.end);
393
+ const place = touching || endedLine ? before.end : after?.start ?? before?.end ?? 0;
394
+ const glued = before !== undefined && before.end === place;
395
+ const spaceBefore = glued && /\s/.test(from.charAt(first.start - 1)) ? ' ' : '';
396
+ const spaceAfter = !touching && !endedLine && after !== undefined && /\s/.test(from.charAt(last.end)) ? ' ' : '';
397
+ const run = from.slice(first.start, last.end).replace(/\s+/g, ' ');
398
+ changes.push({ start: place, end: place, level: 'word' });
399
+ changes.push({ start: place, end: place, level: 'removed', text: spaceBefore + run + spaceAfter });
400
+ }
401
+ /** Whether a letter diff replaced its texts whole: more than `WHOLE_WORD_SHARE` of the new side's
402
+ * letters (letters, marks, digits) put in, or of the old side's removed. */
403
+ function replacedWhole(diffs) {
404
+ let old = 0;
405
+ let removed = 0;
406
+ let now = 0;
407
+ let put = 0;
408
+ for (const [op, text] of diffs) {
409
+ let letters = 0;
410
+ for (const character of text)
411
+ if (/[\p{L}\p{M}\p{N}]/u.test(character))
412
+ letters++;
413
+ if (op === DIFF_DELETE)
414
+ removed += letters;
415
+ else
416
+ now += letters;
417
+ if (op === DIFF_INSERT)
418
+ put += letters;
419
+ else
420
+ old += letters;
421
+ }
422
+ return put > now * WHOLE_WORD_SHARE || removed > old * WHOLE_WORD_SHARE;
423
+ }
424
+ /** `word`'s span with the space beside it (`changesIn`): the one between it and the next token when
425
+ * one comes after it (`followed`), else the one between the token before and it, unless a span
426
+ * before took that one (`taken`, the end of the last); none where the word touches punctuation,
427
+ * nor where that space is a line end that must stay one (`lineEnd`). */
428
+ function withSpace(word, followed, to, taken, lineEnd) {
429
+ const space = (at) => /\s/.test(to.charAt(at)) && !lineEnd(at);
430
+ if (followed)
431
+ return { start: word.start, end: space(word.end) ? word.end + 1 : word.end };
432
+ const before = word.start - 1 >= taken && space(word.start - 1);
433
+ return { start: before ? word.start - 1 : word.start, end: word.end };
434
+ }
435
+ /** The start of the character `word`'s offset `c` is in: `c` moved back past a combining mark or
436
+ * the second half of a surrogate pair, so a removal there never cuts a character. */
437
+ function characterStart(word, c) {
438
+ let at = c;
439
+ while (at > 0 && at < word.length) {
440
+ const unit = word.charCodeAt(at);
441
+ if (!(unit >= 0xdc00 && unit <= 0xdfff) && !/^\p{M}/u.test(word.slice(at, at + 2)))
442
+ break;
443
+ at--;
444
+ }
445
+ return at;
446
+ }
447
+ /**
448
+ * `marked` (one flag per UTF-16 unit of `word`) widened to whole characters: a surrogate pair is
449
+ * marked whole, and a combining mark goes with the letter before it, so a span never cuts a
450
+ * character ("o" + U+0308 is lit together).
451
+ */
452
+ function wholeCharacters(word, marked) {
453
+ const widened = marked.slice();
454
+ let clusterStart = 0;
455
+ let position = 0;
456
+ const close = (end) => {
457
+ if (marked.slice(clusterStart, end).some(Boolean))
458
+ widened.fill(true, clusterStart, end);
459
+ };
460
+ for (const character of word) {
461
+ if (position > 0 && !/^\p{M}/u.test(character)) {
462
+ close(position);
463
+ clusterStart = position;
464
+ }
465
+ position += character.length;
466
+ }
467
+ close(position);
468
+ return widened;
469
+ }
470
+ //# sourceMappingURL=changesIn.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"changesIn.js","sourceRoot":"","sources":["../../src/changes/changesIn.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAyB1F;;aAEa;AACb,MAAM,cAAc,GAAG,GAAG,CAAC;AAE3B;;;;;;;GAOG;AACH,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAE7B;;;;;GAKG;AACH,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAE5B,MAAM,QAAQ,GAAG,yBAAyB,CAAC;AAE3C;;;;;GAKG;AACH,MAAM,UAAU,MAAM,CAAC,IAAY;IACjC,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QACrD,KAAK,EAAE,KAAK,CAAC,KAAK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besides `data-vultus-change-skip`: the app's choice, since only it
3
+ * knows which elements hold no text of its own (a footnote's mark and back link, a notes heading
4
+ * for screen readers, rendered math). */
5
+ export interface ReadTextOptions {
6
+ /** Whether an element is left out of the reading, with all it holds. */
7
+ skip?: (element: Element) => boolean;
8
+ }
9
+ /** A text node read; `start`: where its value begins in the source (the nodes' values in document
10
+ * order, one '\n' per feed). */
11
+ export interface TextPiece {
12
+ node: Text;
13
+ parent: Element | Root;
14
+ start: number;
15
+ }
16
+ /** `offsets[i]`: the source offset of `text[i]` (a collapsed run's, its first); one more entry,
17
+ * the source offset after the last character. */
18
+ export interface TextPlaces {
19
+ pieces: TextPiece[];
20
+ offsets: number[];
21
+ }
22
+ /** A tree's text as `readText` reads it, and the map back to its text nodes. */
23
+ export interface Reading {
24
+ text: string;
25
+ places: TextPlaces;
26
+ }
27
+ /**
28
+ * The reading of a hast tree, which both sides of a diff use: its text nodes' values in
29
+ * document order, prose white space collapsed to one space (soft line breaks too, so a source line
30
+ * end inside "t. i." does not split it), and a line feed, a hard boundary for `changesIn`, exactly
31
+ * where a line really ends: before and after every block element (`BLOCK_ELEMENTS`), at a `br`, and
32
+ * at each line feed inside a `pre`. A run of white space becomes one '\n' where it holds a hard
33
+ * feed, else one ' '; the text is trimmed. An element `isSkipped` names is left out with all it
34
+ * holds, adding not even a feed. An element given as the tree reads as its children; a `pre`
35
+ * given as the tree keeps its line feeds hard.
36
+ *
37
+ * `places` maps the text back: `pieces`, the text nodes read, each with its parent and where its
38
+ * value begins in the source (the values joined in document order, one '\n' per feed); `offsets`,
39
+ * each character's source offset, then the one after the last.
40
+ */
41
+ export declare function readText(tree: Root | Element, options?: ReadTextOptions): Reading;
42
+ /** Whether `element` is left out of the reading, with all it holds: `data-vultus-change-skip`
43
+ * present (hast `dataVultusChangeSkip`, an empty string too), or `options.skip(element)`. */
44
+ export declare function isSkipped(element: Element, options?: ReadTextOptions): boolean;
45
+ //# sourceMappingURL=readText.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"readText.d.ts","sourceRoot":"","sources":["../../src/changes/readText.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAGhD;;0CAE0C;AAC1C,MAAM,WAAW,eAAe;IAC9B,wEAAwE;IACxE,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC;CACtC;AAED;iCACiC;AACjC,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,IAAI,CAAC;IACX,MAAM,EAAE,OAAO,GAAG,IAAI,CAAC;IACvB,KAAK,EAAE,MAAM,CAAC;CACf;AAED;kDACkD;AAClD,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,SAAS,EAAE,CAAC;IACpB,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB;AAED,gFAAgF;AAChF,MAAM,WAAW,OAAO;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,UAAU,CAAC;CACpB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,OAAO,CAoEjF;AAED;8FAC8F;AAC9F,wBAAgB,SAAS,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,OAAO,CAI9E"}
@@ -0,0 +1,99 @@
1
+ import { BLOCK_ELEMENTS } from './blocks.js';
2
+ /**
3
+ * The reading of a hast tree, which both sides of a diff use: its text nodes' values in
4
+ * document order, prose white space collapsed to one space (soft line breaks too, so a source line
5
+ * end inside "t. i." does not split it), and a line feed, a hard boundary for `changesIn`, exactly
6
+ * where a line really ends: before and after every block element (`BLOCK_ELEMENTS`), at a `br`, and
7
+ * at each line feed inside a `pre`. A run of white space becomes one '\n' where it holds a hard
8
+ * feed, else one ' '; the text is trimmed. An element `isSkipped` names is left out with all it
9
+ * holds, adding not even a feed. An element given as the tree reads as its children; a `pre`
10
+ * given as the tree keeps its line feeds hard.
11
+ *
12
+ * `places` maps the text back: `pieces`, the text nodes read, each with its parent and where its
13
+ * value begins in the source (the values joined in document order, one '\n' per feed); `offsets`,
14
+ * each character's source offset, then the one after the last.
15
+ */
16
+ export function readText(tree, options) {
17
+ const pieces = [];
18
+ const chunks = [];
19
+ // The source offsets of the hard feeds: those put in, and a `pre`'s line feeds.
20
+ const hard = new Set();
21
+ let length = 0;
22
+ const feed = () => {
23
+ hard.add(length);
24
+ chunks.push('\n');
25
+ length += 1;
26
+ };
27
+ const visit = (parent, inPre) => {
28
+ for (const child of parent.children) {
29
+ if (child.type === 'text') {
30
+ pieces.push({ node: child, parent, start: length });
31
+ if (inPre) {
32
+ for (let c = 0; c < child.value.length; c++) {
33
+ const unit = child.value.charAt(c);
34
+ if (unit === '\n' || unit === '\r')
35
+ hard.add(length + c);
36
+ }
37
+ }
38
+ chunks.push(child.value);
39
+ length += child.value.length;
40
+ }
41
+ else if (child.type === 'element') {
42
+ if (isSkipped(child, options))
43
+ continue;
44
+ if (child.tagName === 'br') {
45
+ feed();
46
+ continue;
47
+ }
48
+ const block = BLOCK_ELEMENTS.has(child.tagName);
49
+ if (block)
50
+ feed();
51
+ visit(child, inPre || child.tagName === 'pre');
52
+ if (block)
53
+ feed();
54
+ }
55
+ }
56
+ };
57
+ visit(tree, tree.type === 'element' && tree.tagName === 'pre');
58
+ const source = chunks.join('');
59
+ // Each white-space run one character, '\n' where it holds a hard feed; each character's source
60
+ // offset (a run's, its first), then the source's length.
61
+ let text = '';
62
+ const offsets = [];
63
+ let pos = 0;
64
+ for (const run of source.matchAll(/\s+/g)) {
65
+ for (let i = pos; i < run.index; i++)
66
+ offsets.push(i);
67
+ const end = run.index + run[0].length;
68
+ let feeds = false;
69
+ for (let at = run.index; at < end && !feeds; at++)
70
+ feeds = hard.has(at);
71
+ text += `${source.slice(pos, run.index)}${feeds ? '\n' : ' '}`;
72
+ offsets.push(run.index);
73
+ pos = end;
74
+ }
75
+ for (let i = pos; i < source.length; i++)
76
+ offsets.push(i);
77
+ text += source.slice(pos);
78
+ offsets.push(source.length);
79
+ // Trimmed: a run at the start goes with its offset; one at the end leaves its offset, its run's
80
+ // first, as the one after the last character kept.
81
+ if (/^\s/.test(text)) {
82
+ text = text.slice(1);
83
+ offsets.shift();
84
+ }
85
+ if (/\s$/.test(text)) {
86
+ text = text.slice(0, -1);
87
+ offsets.pop();
88
+ }
89
+ return { text, places: { pieces, offsets } };
90
+ }
91
+ /** Whether `element` is left out of the reading, with all it holds: `data-vultus-change-skip`
92
+ * present (hast `dataVultusChangeSkip`, an empty string too), or `options.skip(element)`. */
93
+ export function isSkipped(element, options) {
94
+ const flag = element.properties.dataVultusChangeSkip;
95
+ if (flag !== undefined && flag !== null && flag !== false)
96
+ return true;
97
+ return options?.skip?.(element) ?? false;
98
+ }
99
+ //# sourceMappingURL=readText.js.map