svelte-highlight 7.17.0 → 7.18.1

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.
package/text-diff.js CHANGED
@@ -24,15 +24,23 @@ function isLowSurrogate(code) {
24
24
  export function diffText(before, after) {
25
25
  const minLength = Math.min(before.length, after.length);
26
26
 
27
+ // charCodeAt (not bracket indexing) so this loop - which walks the entire
28
+ // unchanged prefix on a pure-append edit, the common case while typing -
29
+ // doesn't allocate a 1-character string per position compared.
27
30
  let prefix = 0;
28
- while (prefix < minLength && before[prefix] === after[prefix]) prefix++;
31
+ while (
32
+ prefix < minLength &&
33
+ before.charCodeAt(prefix) === after.charCodeAt(prefix)
34
+ )
35
+ prefix++;
29
36
  if (isHighSurrogate(before.charCodeAt(prefix - 1))) prefix--;
30
37
 
31
38
  let suffix = 0;
32
39
  const maxSuffix = minLength - prefix;
33
40
  while (
34
41
  suffix < maxSuffix &&
35
- before[before.length - 1 - suffix] === after[after.length - 1 - suffix]
42
+ before.charCodeAt(before.length - 1 - suffix) ===
43
+ after.charCodeAt(after.length - 1 - suffix)
36
44
  ) {
37
45
  suffix++;
38
46
  }
@@ -0,0 +1,14 @@
1
+ /** Constant rate; `Typewriter`'s default. */
2
+ export declare function linear(t: number): number;
3
+
4
+ export declare function easeInQuad(t: number): number;
5
+ export declare function easeOutQuad(t: number): number;
6
+ export declare function easeInOutQuad(t: number): number;
7
+
8
+ export declare function easeInCubic(t: number): number;
9
+ export declare function easeOutCubic(t: number): number;
10
+ export declare function easeInOutCubic(t: number): number;
11
+
12
+ export declare function easeInSine(t: number): number;
13
+ export declare function easeOutSine(t: number): number;
14
+ export declare function easeInOutSine(t: number): number;
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Reveal-progress curves for `Typewriter`'s `easing` prop: each maps an
3
+ * elapsed-time fraction (0 to 1) to a revealed-fraction (0 to 1). Total
4
+ * typing duration is always `speed * <visible character count>` regardless
5
+ * of which curve is used -- only the pacing within that duration changes.
6
+ * Formulas from easings.net.
7
+ */
8
+
9
+ /** @param {number} t @returns {number} */
10
+ export function linear(t) {
11
+ return t;
12
+ }
13
+
14
+ /** @param {number} t @returns {number} */
15
+ export function easeInQuad(t) {
16
+ return t * t;
17
+ }
18
+
19
+ /** @param {number} t @returns {number} */
20
+ export function easeOutQuad(t) {
21
+ return 1 - (1 - t) * (1 - t);
22
+ }
23
+
24
+ /** @param {number} t @returns {number} */
25
+ export function easeInOutQuad(t) {
26
+ return t < 0.5 ? 2 * t * t : 1 - (-2 * t + 2) ** 2 / 2;
27
+ }
28
+
29
+ /** @param {number} t @returns {number} */
30
+ export function easeInCubic(t) {
31
+ return t * t * t;
32
+ }
33
+
34
+ /** @param {number} t @returns {number} */
35
+ export function easeOutCubic(t) {
36
+ return 1 - (1 - t) ** 3;
37
+ }
38
+
39
+ /** @param {number} t @returns {number} */
40
+ export function easeInOutCubic(t) {
41
+ return t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2;
42
+ }
43
+
44
+ /** @param {number} t @returns {number} */
45
+ export function easeInSine(t) {
46
+ return 1 - Math.cos((t * Math.PI) / 2);
47
+ }
48
+
49
+ /** @param {number} t @returns {number} */
50
+ export function easeOutSine(t) {
51
+ return Math.sin((t * Math.PI) / 2);
52
+ }
53
+
54
+ /** @param {number} t @returns {number} */
55
+ export function easeInOutSine(t) {
56
+ return -(Math.cos(Math.PI * t) - 1) / 2;
57
+ }
@@ -12,6 +12,12 @@ export interface TypewriterUnit {
12
12
  */
13
13
  export declare function tokenizeTypewriter(html: string): TypewriterUnit[];
14
14
 
15
+ /**
16
+ * Renders `units` into HTML once: tags pass through unchanged, each visible
17
+ * unit is wrapped in a `typewriter-unit typewriter-hidden` span.
18
+ */
19
+ export declare function buildUnitMarkup(units: TypewriterUnit[]): string;
20
+
15
21
  export interface TypewriterSplitter {
16
22
  splitAt(count: number): { head: string; tail: string };
17
23
  }
@@ -25,9 +25,13 @@ export function tokenizeTypewriter(html) {
25
25
  let i = 0;
26
26
 
27
27
  while (i < n) {
28
- const ch = html[i];
28
+ // charCodeAt, not html[i]: this runs once per iteration over the whole
29
+ // string, and bracket-indexing allocates a 1-character string for every
30
+ // position compared, even the (overwhelmingly common) plain-char branch
31
+ // below that doesn't otherwise need one.
32
+ const code = html.charCodeAt(i);
29
33
 
30
- if (ch === "<") {
34
+ if (code === 60 /* "<" */) {
31
35
  const end = html.indexOf(">", i);
32
36
  if (end === -1) {
33
37
  // Unclosed tag: treat as text.
@@ -49,14 +53,14 @@ export function tokenizeTypewriter(html) {
49
53
  name: match ? (match[1] ?? "") : "",
50
54
  });
51
55
  i = end + 1;
52
- } else if (ch === "&") {
56
+ } else if (code === 38 /* "&" */) {
53
57
  // One visible char per entity.
54
58
  const end = html.indexOf(";", i);
55
59
  if (end !== -1 && end - i <= 10) {
56
60
  units.push({ raw: html.slice(i, end + 1), visible: 1 });
57
61
  i = end + 1;
58
62
  } else {
59
- units.push({ raw: ch, visible: 1 });
63
+ units.push({ raw: html.charAt(i), visible: 1 });
60
64
  i += 1;
61
65
  }
62
66
  } else {
@@ -72,6 +76,25 @@ export function tokenizeTypewriter(html) {
72
76
  return units;
73
77
  }
74
78
 
79
+ /**
80
+ * Renders `units` into HTML once: tags pass through unchanged, each visible
81
+ * unit is wrapped in a `typewriter-unit typewriter-hidden` span so
82
+ * `Typewriter` can reveal them one at a time without touching the DOM tree
83
+ * shape again until `units` itself changes.
84
+ * @param {TypewriterUnit[]} units
85
+ * @returns {string}
86
+ */
87
+ export function buildUnitMarkup(units) {
88
+ let html = "";
89
+ for (const unit of units) {
90
+ html +=
91
+ unit.visible === 0
92
+ ? unit.raw
93
+ : `<span class="typewriter-unit typewriter-hidden">${unit.raw}</span>`;
94
+ }
95
+ return html;
96
+ }
97
+
75
98
  /**
76
99
  * @typedef {Object} TypewriterSplitter
77
100
  * @property {(count: number) => { head: string; tail: string }} splitAt