svelte-highlight 7.16.2 → 7.18.0

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 (378) hide show
  1. package/AnsiOutput.svelte +2 -135
  2. package/HighlightStream.svelte +226 -17
  3. package/HighlightStream.svelte.d.ts +25 -0
  4. package/HighlightVirtual.svelte +13 -20
  5. package/LICENSE.highlight.txt +1 -1
  6. package/README.md +42 -17
  7. package/Typewriter.svelte +79 -56
  8. package/Typewriter.svelte.d.ts +10 -1
  9. package/ansi-color.js +201 -0
  10. package/editable-dom-paint.js +4 -3
  11. package/engine.js +61 -43
  12. package/index.d.ts +12 -0
  13. package/index.js +12 -0
  14. package/languages/1c.js +1 -1
  15. package/languages/abnf.js +1 -1
  16. package/languages/accesslog.js +1 -1
  17. package/languages/actionscript.js +1 -1
  18. package/languages/ada.js +7 -6
  19. package/languages/all.js +8 -0
  20. package/languages/angelscript.js +1 -1
  21. package/languages/apache.js +1 -1
  22. package/languages/applescript.js +1 -1
  23. package/languages/arcade.js +1 -1
  24. package/languages/arduino.js +91 -67
  25. package/languages/armasm.js +1 -1
  26. package/languages/asciidoc.js +1 -1
  27. package/languages/aspectj.js +1 -1
  28. package/languages/autohotkey.js +1 -1
  29. package/languages/autoit.js +1 -1
  30. package/languages/avrasm.js +1 -1
  31. package/languages/awk.js +1 -1
  32. package/languages/axapta.js +1 -1
  33. package/languages/bash.js +1 -1
  34. package/languages/basic.js +1 -1
  35. package/languages/bnf.js +1 -1
  36. package/languages/brainfuck.js +1 -1
  37. package/languages/c.js +91 -66
  38. package/languages/cal.js +1 -1
  39. package/languages/capnproto.js +1 -1
  40. package/languages/ceylon.js +1 -1
  41. package/languages/clean.js +1 -1
  42. package/languages/clojure-repl.js +1 -1
  43. package/languages/clojure.js +1 -1
  44. package/languages/cmake.js +4 -2
  45. package/languages/coffeescript.js +1 -1
  46. package/languages/coq.js +1 -1
  47. package/languages/cos.js +1 -1
  48. package/languages/cpp.js +90 -65
  49. package/languages/cql.d.ts +2 -0
  50. package/languages/cql.js +203 -0
  51. package/languages/crmsh.js +1 -1
  52. package/languages/crystal.js +27 -34
  53. package/languages/csharp.js +4 -4
  54. package/languages/csp.js +8 -1
  55. package/languages/css.js +20 -13
  56. package/languages/d.js +1 -1
  57. package/languages/dart.js +18 -3
  58. package/languages/delphi.js +1 -1
  59. package/languages/diff.js +2 -2
  60. package/languages/django.js +1 -1
  61. package/languages/dns.js +71 -43
  62. package/languages/dockerfile.js +1 -1
  63. package/languages/dos.js +3 -3
  64. package/languages/dsconfig.js +1 -1
  65. package/languages/dts.js +1 -1
  66. package/languages/dust.js +1 -1
  67. package/languages/ebnf.js +1 -1
  68. package/languages/elixir.js +59 -50
  69. package/languages/elm.js +1 -1
  70. package/languages/erb.js +1 -1
  71. package/languages/erlang-repl.js +1 -1
  72. package/languages/erlang.js +1 -1
  73. package/languages/excel.js +1 -1
  74. package/languages/fix.js +1 -1
  75. package/languages/flix.js +1 -1
  76. package/languages/flux.d.ts +2 -0
  77. package/languages/flux.js +272 -0
  78. package/languages/fortran.js +1 -1
  79. package/languages/freedesktop.d.ts +2 -0
  80. package/languages/freedesktop.js +45 -0
  81. package/languages/fsharp.js +1 -1
  82. package/languages/gams.js +1 -1
  83. package/languages/gauss.js +1 -1
  84. package/languages/gcode.js +1 -1
  85. package/languages/gherkin.js +36 -38
  86. package/languages/glsl.js +1 -1
  87. package/languages/gml.js +1 -1
  88. package/languages/go.js +10 -3
  89. package/languages/golo.js +1 -1
  90. package/languages/gradle.js +1 -1
  91. package/languages/graphql.js +1 -1
  92. package/languages/groovy.js +60 -5
  93. package/languages/haml.js +1 -1
  94. package/languages/handlebars.js +1 -1
  95. package/languages/haskell.js +2 -1
  96. package/languages/haxe.js +1 -1
  97. package/languages/hsp.js +1 -1
  98. package/languages/http.js +1 -1
  99. package/languages/hy.js +1 -1
  100. package/languages/index.d.ts +8 -0
  101. package/languages/index.js +4 -0
  102. package/languages/inform7.js +1 -1
  103. package/languages/ini.js +1 -1
  104. package/languages/irpf90.js +1 -1
  105. package/languages/isbl.js +1 -1
  106. package/languages/java.js +28 -25
  107. package/languages/javascript.js +9 -3
  108. package/languages/jboss-cli.js +1 -1
  109. package/languages/json.js +12 -9
  110. package/languages/julia-repl.js +1 -1
  111. package/languages/julia.js +1 -1
  112. package/languages/kotlin.js +3 -3
  113. package/languages/lasso.js +1 -1
  114. package/languages/latex.js +1 -1
  115. package/languages/ldif.js +1 -1
  116. package/languages/leaf.js +7 -2
  117. package/languages/less.js +60 -45
  118. package/languages/lisp.js +7 -2
  119. package/languages/livecodeserver.js +1 -1
  120. package/languages/livescript.js +1 -1
  121. package/languages/llvm.js +18 -3
  122. package/languages/lsl.js +1 -1
  123. package/languages/lua.js +2 -1
  124. package/languages/makefile.js +1 -1
  125. package/languages/markdown.js +8 -4
  126. package/languages/mathematica.js +1 -1
  127. package/languages/matlab.js +1 -1
  128. package/languages/maxima.js +1 -1
  129. package/languages/mel.js +1 -1
  130. package/languages/mercury.js +1 -1
  131. package/languages/mipsasm.js +1 -1
  132. package/languages/mizar.js +1 -1
  133. package/languages/mojolicious.js +1 -1
  134. package/languages/monkey.js +1 -1
  135. package/languages/moonscript.js +1 -1
  136. package/languages/n1ql.js +1 -1
  137. package/languages/nestedtext.js +1 -1
  138. package/languages/nginx.js +1 -1
  139. package/languages/nim.js +1 -1
  140. package/languages/nix.js +1 -1
  141. package/languages/node-repl.js +1 -1
  142. package/languages/nsis.js +20 -5
  143. package/languages/objectivec.js +1 -1
  144. package/languages/ocaml.js +1 -1
  145. package/languages/openscad.js +1 -1
  146. package/languages/oxygene.js +1 -1
  147. package/languages/parser3.js +1 -1
  148. package/languages/perl.js +1 -1
  149. package/languages/pf.js +1 -1
  150. package/languages/pgsql.js +1 -1
  151. package/languages/php-template.js +1 -1
  152. package/languages/php.js +150 -24
  153. package/languages/plaintext.js +1 -1
  154. package/languages/pony.js +1 -1
  155. package/languages/powershell.js +1 -1
  156. package/languages/processing.js +1 -1
  157. package/languages/profile.js +1 -1
  158. package/languages/prolog.js +1 -1
  159. package/languages/properties.js +1 -1
  160. package/languages/protobuf.js +1 -1
  161. package/languages/prql.d.ts +2 -0
  162. package/languages/prql.js +160 -0
  163. package/languages/puppet.js +1 -1
  164. package/languages/purebasic.js +1 -1
  165. package/languages/python-repl.js +1 -1
  166. package/languages/python.js +31 -11
  167. package/languages/q.js +1 -1
  168. package/languages/qml.js +1 -1
  169. package/languages/r.js +1 -1
  170. package/languages/reasonml.js +1 -1
  171. package/languages/rib.js +1 -1
  172. package/languages/roboconf.js +1 -1
  173. package/languages/routeros.js +1 -1
  174. package/languages/rsl.js +1 -1
  175. package/languages/ruby.js +2 -2
  176. package/languages/ruleslanguage.js +1 -1
  177. package/languages/rust.js +23 -11
  178. package/languages/sas.js +1 -1
  179. package/languages/scala.js +1 -1
  180. package/languages/scheme.js +1 -1
  181. package/languages/scilab.js +1 -1
  182. package/languages/scss.js +16 -9
  183. package/languages/shell.js +2 -2
  184. package/languages/smali.js +1 -1
  185. package/languages/smalltalk.js +1 -1
  186. package/languages/sml.js +1 -1
  187. package/languages/sqf.js +175 -10
  188. package/languages/sql.js +1 -1
  189. package/languages/stan.js +1 -1
  190. package/languages/stata.js +1 -1
  191. package/languages/step21.js +1 -1
  192. package/languages/stylus.js +18 -11
  193. package/languages/subunit.js +1 -1
  194. package/languages/swift.js +1 -1
  195. package/languages/taggerscript.js +1 -1
  196. package/languages/tap.js +1 -1
  197. package/languages/tcl.js +1 -1
  198. package/languages/thrift.js +1 -1
  199. package/languages/tp.js +1 -1
  200. package/languages/twig.js +1 -1
  201. package/languages/typescript.js +9 -3
  202. package/languages/vala.js +1 -1
  203. package/languages/vbnet.js +1 -1
  204. package/languages/vbscript-html.js +1 -1
  205. package/languages/vbscript.js +1 -1
  206. package/languages/verilog.js +1 -1
  207. package/languages/vhdl.js +1 -1
  208. package/languages/vim.js +1 -1
  209. package/languages/wasm.js +1 -1
  210. package/languages/wren.js +1 -1
  211. package/languages/x86asm.js +1 -1
  212. package/languages/xl.js +1 -1
  213. package/languages/xml.js +3 -3
  214. package/languages/xquery.js +1 -1
  215. package/languages/yaml.js +1 -1
  216. package/languages/zephir.js +1 -1
  217. package/package.json +1 -1
  218. package/stream-sealed-chunks.js +44 -0
  219. package/styles/a11y-dark.css +1 -1
  220. package/styles/a11y-dark.js +1 -1
  221. package/styles/a11y-light.css +1 -1
  222. package/styles/a11y-light.js +1 -1
  223. package/styles/an-old-hope.css +1 -1
  224. package/styles/an-old-hope.js +1 -1
  225. package/styles/arduino-light.css +1 -1
  226. package/styles/arduino-light.js +1 -1
  227. package/styles/arta.css +1 -1
  228. package/styles/arta.js +1 -1
  229. package/styles/brown-paper.css +1 -1
  230. package/styles/brown-paper.js +1 -1
  231. package/styles/codepen-embed.css +1 -1
  232. package/styles/codepen-embed.js +1 -1
  233. package/styles/cybertopia-cherry.css +10 -9
  234. package/styles/cybertopia-cherry.js +10 -9
  235. package/styles/cybertopia-dimmer.css +10 -9
  236. package/styles/cybertopia-dimmer.js +10 -9
  237. package/styles/cybertopia-icecap.css +10 -9
  238. package/styles/cybertopia-icecap.js +10 -9
  239. package/styles/cybertopia-saturated.css +10 -9
  240. package/styles/cybertopia-saturated.js +10 -9
  241. package/styles/dark.css +1 -1
  242. package/styles/dark.js +1 -1
  243. package/styles/docco.css +1 -1
  244. package/styles/docco.js +1 -1
  245. package/styles/equinox.css +25 -0
  246. package/styles/equinox.d.ts +1 -0
  247. package/styles/equinox.js +27 -0
  248. package/styles/far.css +1 -1
  249. package/styles/far.js +1 -1
  250. package/styles/gml.css +1 -1
  251. package/styles/gml.js +1 -1
  252. package/styles/googlecode.css +1 -1
  253. package/styles/googlecode.js +1 -1
  254. package/styles/hybrid.css +1 -1
  255. package/styles/hybrid.js +1 -1
  256. package/styles/index.d.ts +2 -0
  257. package/styles/index.js +2 -0
  258. package/styles/intellij-light.css +1 -1
  259. package/styles/intellij-light.js +1 -1
  260. package/styles/ir-black.css +1 -1
  261. package/styles/ir-black.js +1 -1
  262. package/styles/isbl-editor-dark.css +1 -1
  263. package/styles/isbl-editor-dark.js +1 -1
  264. package/styles/isbl-editor-light.css +1 -1
  265. package/styles/isbl-editor-light.js +1 -1
  266. package/styles/magula.css +1 -1
  267. package/styles/magula.js +1 -1
  268. package/styles/mono-blue.css +1 -1
  269. package/styles/mono-blue.js +1 -1
  270. package/styles/monokai-sublime.css +1 -1
  271. package/styles/monokai-sublime.js +1 -1
  272. package/styles/monokai.css +1 -1
  273. package/styles/monokai.js +1 -1
  274. package/styles/nnfx-dark.css +1 -1
  275. package/styles/nnfx-dark.js +1 -1
  276. package/styles/nnfx-light.css +1 -1
  277. package/styles/nnfx-light.js +1 -1
  278. package/styles/obsidian.css +1 -1
  279. package/styles/obsidian.js +1 -1
  280. package/styles/paraiso-dark.css +1 -1
  281. package/styles/paraiso-dark.js +1 -1
  282. package/styles/paraiso-light.css +1 -1
  283. package/styles/paraiso-light.js +1 -1
  284. package/styles/pojoaque.css +1 -1
  285. package/styles/pojoaque.js +1 -1
  286. package/styles/purebasic.css +1 -1
  287. package/styles/purebasic.js +1 -1
  288. package/styles/qtcreator-dark.css +1 -1
  289. package/styles/qtcreator-dark.js +1 -1
  290. package/styles/qtcreator-light.css +1 -1
  291. package/styles/qtcreator-light.js +1 -1
  292. package/styles/rainbow.css +1 -1
  293. package/styles/rainbow.js +1 -1
  294. package/styles/routeros.css +1 -1
  295. package/styles/routeros.js +1 -1
  296. package/styles/school-book.css +1 -1
  297. package/styles/school-book.js +1 -1
  298. package/styles/shades-of-purple.css +1 -1
  299. package/styles/shades-of-purple.js +1 -1
  300. package/styles/srcery.css +1 -1
  301. package/styles/srcery.js +1 -1
  302. package/styles/stackoverflow-dark.css +1 -1
  303. package/styles/stackoverflow-dark.js +1 -1
  304. package/styles/stackoverflow-light.css +1 -1
  305. package/styles/stackoverflow-light.js +1 -1
  306. package/styles/tomorrow-night-blue.css +1 -1
  307. package/styles/tomorrow-night-blue.js +1 -1
  308. package/styles/tomorrow-night-bright.css +1 -1
  309. package/styles/tomorrow-night-bright.js +1 -1
  310. package/styles/vs-dark.css +21 -0
  311. package/styles/vs-dark.d.ts +1 -0
  312. package/styles/vs-dark.js +23 -0
  313. package/styles/vs.css +1 -1
  314. package/styles/vs.js +1 -1
  315. package/styles/xcode.css +1 -1
  316. package/styles/xcode.js +1 -1
  317. package/styles/xt256.css +1 -1
  318. package/styles/xt256.js +1 -1
  319. package/text-diff.js +10 -2
  320. package/themes/a11y-dark.css +1 -1
  321. package/themes/a11y-dark.js +1 -1
  322. package/themes/a11y-light.css +1 -1
  323. package/themes/a11y-light.js +1 -1
  324. package/themes/an-old-hope.css +1 -1
  325. package/themes/an-old-hope.js +1 -1
  326. package/themes/arta.css +1 -1
  327. package/themes/arta.js +1 -1
  328. package/themes/brown-paper.css +1 -1
  329. package/themes/brown-paper.js +1 -1
  330. package/themes/codepen-embed.css +1 -1
  331. package/themes/codepen-embed.js +1 -1
  332. package/themes/cybertopia-cherry.css +1 -1
  333. package/themes/cybertopia-cherry.js +49 -48
  334. package/themes/cybertopia-dimmer.css +1 -1
  335. package/themes/cybertopia-dimmer.js +49 -48
  336. package/themes/cybertopia-icecap.css +1 -1
  337. package/themes/cybertopia-icecap.js +49 -48
  338. package/themes/cybertopia-saturated.css +1 -1
  339. package/themes/cybertopia-saturated.js +49 -48
  340. package/themes/dark.css +1 -1
  341. package/themes/dark.js +1 -1
  342. package/themes/docco.css +1 -1
  343. package/themes/docco.js +1 -1
  344. package/themes/equinox.css +1 -0
  345. package/themes/equinox.d.ts +1 -0
  346. package/themes/equinox.js +51 -0
  347. package/themes/far.css +1 -1
  348. package/themes/far.js +1 -1
  349. package/themes/googlecode.css +1 -1
  350. package/themes/googlecode.js +1 -1
  351. package/themes/index.d.ts +2 -0
  352. package/themes/index.js +2 -0
  353. package/themes/ir-black.css +1 -1
  354. package/themes/ir-black.js +1 -1
  355. package/themes/paraiso-dark.css +1 -1
  356. package/themes/paraiso-dark.js +1 -1
  357. package/themes/paraiso-light.css +1 -1
  358. package/themes/paraiso-light.js +1 -1
  359. package/themes/pojoaque.css +1 -1
  360. package/themes/pojoaque.js +1 -1
  361. package/themes/rainbow.css +1 -1
  362. package/themes/rainbow.js +1 -1
  363. package/themes/school-book.css +1 -1
  364. package/themes/school-book.js +1 -1
  365. package/themes/tomorrow-night-blue.css +1 -1
  366. package/themes/tomorrow-night-blue.js +1 -1
  367. package/themes/tomorrow-night-bright.css +1 -1
  368. package/themes/tomorrow-night-bright.js +1 -1
  369. package/themes/vs-dark.css +1 -0
  370. package/themes/vs-dark.d.ts +1 -0
  371. package/themes/vs-dark.js +45 -0
  372. package/themes/vs.css +1 -1
  373. package/themes/vs.js +1 -1
  374. package/typewriter-easing.d.ts +14 -0
  375. package/typewriter-easing.js +57 -0
  376. package/typewriter-units.d.ts +6 -0
  377. package/typewriter-units.js +27 -4
  378. package/virtual-window.js +67 -0
package/Typewriter.svelte CHANGED
@@ -18,11 +18,23 @@
18
18
  export let play = true;
19
19
 
20
20
  import { createEventDispatcher, onMount } from "svelte";
21
+ import { linear } from "./typewriter-easing.js";
21
22
  import {
23
+ buildUnitMarkup,
22
24
  createTypewriterSplitter,
23
25
  tokenizeTypewriter as tokenize,
24
26
  } from "./typewriter-units.js";
25
27
 
28
+ /**
29
+ * Reveal-progress curve: maps elapsed-time fraction (0-1) to
30
+ * revealed-fraction (0-1). Total typing duration is always
31
+ * `speed * <visible character count>` regardless of curve -- only the
32
+ * pacing within that duration changes. Import a named curve
33
+ * (`easeOutQuad`, `easeInOutCubic`, ...) or pass your own function.
34
+ * @type {(t: number) => number}
35
+ */
36
+ export let easing = linear;
37
+
26
38
  const dispatch = createEventDispatcher();
27
39
 
28
40
  // Above this many visible units, per-unit spans (one element per char)
@@ -34,17 +46,20 @@
34
46
  /** @type {boolean} */
35
47
  let mounted = false;
36
48
 
37
- /** @type {boolean} */
38
- let reducedMotion = false;
39
-
40
49
  /** @type {boolean} */
41
50
  let doneFired = false;
42
51
 
43
52
  /** Number of visible characters currently revealed. @type {number} */
44
53
  let revealed = 0;
45
54
 
46
- /** @type {ReturnType<typeof setInterval> | undefined} */
47
- let timerId;
55
+ /** @type {number | undefined} */
56
+ let rafId;
57
+
58
+ /** Active ms elapsed for the current run (excludes paused time). @type {number} */
59
+ let elapsedMs = 0;
60
+
61
+ /** `performance.now()` at the last tick, or `undefined` right after a (re)start. @type {number | undefined} */
62
+ let frameTime;
48
63
 
49
64
  /** @type {string | undefined} */
50
65
  let prevHighlighted;
@@ -64,18 +79,6 @@
64
79
  /** The unit currently marked with the caret, if any. @type {HTMLElement | undefined} */
65
80
  let caretMark;
66
81
 
67
- /** Render every unit once: tags pass through, each visible unit gets a span. */
68
- function buildUnitMarkup(list) {
69
- let html = "";
70
- for (const unit of list) {
71
- html +=
72
- unit.visible === 0
73
- ? unit.raw
74
- : `<span class="typewriter-unit typewriter-hidden">${unit.raw}</span>`;
75
- }
76
- return html;
77
- }
78
-
79
82
  /**
80
83
  * Bring `contentEl` in sync with `units`/`revealed` in O(1) amortized work:
81
84
  * a fresh `units` value triggers one full (re)paint; otherwise only the
@@ -90,6 +93,15 @@
90
93
  paintedUnits = units;
91
94
  revealedInDom = 0;
92
95
  caretMark = undefined;
96
+ // Resolve `currentColor` to a literal value now, while `contentEl`
97
+ // itself carries the theme's base foreground (nothing has recolored
98
+ // it yet). Stored as a custom property, it stays correct even once
99
+ // the caret mark lands inside a colored hljs token span, where a live
100
+ // `currentColor` would pick up that token's color instead.
101
+ contentEl.style.setProperty(
102
+ "--typewriter-caret-fg",
103
+ getComputedStyle(contentEl).color,
104
+ );
93
105
  }
94
106
 
95
107
  while (revealedInDom < revealed) {
@@ -110,9 +122,9 @@
110
122
  }
111
123
  }
112
124
 
113
- function clearTimer() {
114
- clearInterval(timerId);
115
- timerId = undefined;
125
+ function stopLoop() {
126
+ if (rafId !== undefined) cancelAnimationFrame(rafId);
127
+ rafId = undefined;
116
128
  }
117
129
 
118
130
  function fireDone() {
@@ -122,31 +134,50 @@
122
134
  }
123
135
  }
124
136
 
125
- function advance() {
126
- if (revealed < total) revealed += 1;
137
+ /**
138
+ * One animation frame: advance `elapsedMs` by the real time since the last
139
+ * frame (zero on the first frame after a start/resume, so pausing never
140
+ * counts the paused gap), then derive `revealed` from `easing` applied to
141
+ * the elapsed fraction of the total duration. Total duration is always
142
+ * `speed * total`, so only the pacing within it -- not its length --
143
+ * depends on `easing`. Clamped to `[0, total]` since a custom `easing` may
144
+ * overshoot outside `[0, 1]` (e.g. a "back"/"elastic" curve).
145
+ * @param {number} now
146
+ */
147
+ function tick(now) {
148
+ if (frameTime !== undefined) elapsedMs += now - frameTime;
149
+ frameTime = now;
150
+
151
+ const duration = Math.max(0, speed) * total;
152
+ const t = duration > 0 ? Math.min(1, elapsedMs / duration) : 1;
153
+ const target = Math.round(easing(t) * total);
154
+ const next = Math.min(total, Math.max(0, target));
155
+ if (next > revealed) revealed = next;
156
+
127
157
  if (useUnitReveal) syncUnitDom();
158
+
128
159
  if (revealed >= total) {
129
- clearTimer();
160
+ stopLoop();
130
161
  fireDone();
162
+ return;
131
163
  }
164
+ rafId = requestAnimationFrame(tick);
165
+ }
166
+
167
+ function startLoop() {
168
+ frameTime = undefined;
169
+ rafId = requestAnimationFrame(tick);
132
170
  }
133
171
 
134
- /** Start/stop/retime the typing interval. */
172
+ /** Start/stop/retime the typing loop. */
135
173
  function sync() {
136
- clearTimer();
174
+ stopLoop();
137
175
  if (!mounted) return;
138
176
 
139
- if (reducedMotion) {
140
- // Reduced motion: show all at once.
141
- if (revealed !== total) revealed = total;
142
- fireDone();
143
- return;
144
- }
145
-
146
177
  if (useUnitReveal) syncUnitDom();
147
178
 
148
179
  if (play && revealed < total) {
149
- timerId = setInterval(advance, Math.max(0, speed));
180
+ startLoop();
150
181
  } else if (revealed >= total) {
151
182
  fireDone();
152
183
  }
@@ -159,20 +190,21 @@
159
190
  $: splitter = createTypewriterSplitter(units, highlighted);
160
191
  $: total = units.reduce((sum, unit) => sum + unit.visible, 0);
161
192
  $: bigInput = total > UNIT_THRESHOLD;
162
- $: useUnitReveal = mounted && !reducedMotion && !bigInput;
193
+ $: useUnitReveal = mounted && !bigInput;
163
194
 
164
195
  // Restart when `highlighted` changes.
165
196
  $: if (highlighted !== prevHighlighted) {
166
197
  prevHighlighted = highlighted;
167
198
  doneFired = false;
168
- revealed = reducedMotion ? total : 0;
199
+ elapsedMs = 0;
200
+ revealed = 0;
169
201
  void useUnitReveal;
170
202
  sync();
171
203
  }
172
204
 
173
- // Re-sync on play/speed/mount/motion. Skip `total` (handled above).
205
+ // Re-sync on play/speed/easing/mount. Skip `total` (handled above).
174
206
  $: {
175
- void [play, speed, mounted, reducedMotion, useUnitReveal];
207
+ void [play, speed, easing, mounted, useUnitReveal];
176
208
  sync();
177
209
  }
178
210
 
@@ -182,21 +214,13 @@
182
214
  : mounted
183
215
  ? splitter.splitAt(revealed)
184
216
  : { head: highlighted, tail: "" };
185
- $: showCaret = mounted && !reducedMotion && revealed < total;
217
+ $: showCaret = mounted && revealed < total;
186
218
 
187
219
  onMount(() => {
188
220
  mounted = true;
189
221
 
190
- const query = window.matchMedia("(prefers-reduced-motion: reduce)");
191
- reducedMotion = query.matches;
192
- const onChange = (event) => {
193
- reducedMotion = event.matches;
194
- };
195
- query.addEventListener?.("change", onChange);
196
-
197
222
  return () => {
198
- query.removeEventListener?.("change", onChange);
199
- clearTimer();
223
+ stopLoop();
200
224
  };
201
225
  });
202
226
  </script>
@@ -234,7 +258,13 @@
234
258
  }
235
259
 
236
260
  /* The caret rides the next-to-reveal (still hidden) unit's ::before, so it
237
- never requires moving a real DOM node -- one class add/remove per tick. */
261
+ never requires moving a real DOM node -- one class add/remove per tick.
262
+ That unit can land inside a still-open hljs token span (e.g. mid-
263
+ keyword), where `currentColor` would resolve to that token's color
264
+ instead of the theme's base foreground. `--typewriter-caret-fg` is set
265
+ on `contentEl` as a literal resolved color (not the `currentColor`
266
+ keyword) in `syncUnitDom`, so it survives that nesting unaffected by
267
+ descendant `color` overrides. */
238
268
  :global(.typewriter-unit.typewriter-caret)::before {
239
269
  content: "";
240
270
  visibility: visible;
@@ -243,17 +273,10 @@
243
273
  height: var(--caret-height, 1.1em);
244
274
  margin-left: var(--caret-gap, 1px);
245
275
  vertical-align: text-bottom;
246
- background: var(--caret-color, currentColor);
276
+ background: var(--caret-color, var(--typewriter-caret-fg, currentColor));
247
277
  animation: typewriter-blink var(--caret-blink, 1s) step-end infinite;
248
278
  }
249
279
 
250
- @media (prefers-reduced-motion: reduce) {
251
- .typewriter-caret,
252
- :global(.typewriter-unit.typewriter-caret)::before {
253
- animation: none;
254
- }
255
- }
256
-
257
280
  @keyframes typewriter-blink {
258
281
  50% {
259
282
  opacity: 0;
@@ -20,6 +20,15 @@ export type TypewriterProps = HTMLAttributes<HTMLPreElement> & {
20
20
  */
21
21
  play?: boolean;
22
22
 
23
+ /**
24
+ * Reveal-progress curve: maps elapsed-time fraction (0-1) to
25
+ * revealed-fraction (0-1). Total typing duration is always
26
+ * `speed * <visible character count>` regardless of curve. Import a named
27
+ * curve (`easeOutQuad`, `easeInOutCubic`, ...) or pass your own function.
28
+ * @default linear
29
+ */
30
+ easing?: (t: number) => number;
31
+
23
32
  /**
24
33
  * Width of the blinking caret.
25
34
  * @default "0.6em"
@@ -53,7 +62,7 @@ export type TypewriterProps = HTMLAttributes<HTMLPreElement> & {
53
62
 
54
63
  export type TypewriterEvents = {
55
64
  /**
56
- * Fires when typing finishes (or immediately with reduced motion).
65
+ * Fires when typing finishes.
57
66
  */
58
67
  done: CustomEvent<null>;
59
68
  };
package/ansi-color.js ADDED
@@ -0,0 +1,201 @@
1
+ /**
2
+ * Pure color math for AnsiOutput: SGR color to CSS, and WCAG contrast-based
3
+ * auto-foreground selection. Split out of the component (rather than left
4
+ * inline in its `<script>`) so it's directly testable and benchable without
5
+ * a DOM - .svelte files aren't type-checked by `tsgo`/covered by `bun test`.
6
+ */
7
+
8
+ /** @typedef {import("./ansi.d.ts").AnsiColor} AnsiColor */
9
+ /** @typedef {import("./ansi.d.ts").AnsiSegment} AnsiSegment */
10
+
11
+ export const FOREGROUND_FALLBACK = "#d4d4d4";
12
+ export const CONTRAST_TARGET = 4.5;
13
+ /** @type {[number, number, number]} */
14
+ const BLACK = [0, 0, 0];
15
+ /** @type {[number, number, number]} */
16
+ const WHITE = [255, 255, 255];
17
+
18
+ /**
19
+ * 16-color xterm defaults, each overridable via `--ansi-<name>`.
20
+ * @type {Record<string, string>}
21
+ */
22
+ export const ANSI_COLOR_DEFAULTS = {
23
+ black: "#000000",
24
+ red: "#cd0000",
25
+ green: "#00cd00",
26
+ yellow: "#cdcd00",
27
+ blue: "#0000ee",
28
+ magenta: "#cd00cd",
29
+ cyan: "#00cdcd",
30
+ white: "#e5e5e5",
31
+ "bright-black": "#7f7f7f",
32
+ "bright-red": "#ff0000",
33
+ "bright-green": "#00ff00",
34
+ "bright-yellow": "#ffff00",
35
+ "bright-blue": "#5c5cff",
36
+ "bright-magenta": "#ff00ff",
37
+ "bright-cyan": "#00ffff",
38
+ "bright-white": "#ffffff",
39
+ };
40
+
41
+ // 256-color cube steps (6x6x6).
42
+ const CUBE = [0, 95, 135, 175, 215, 255];
43
+
44
+ /**
45
+ * 256-color index to an RGB triple. Shared by `indexedHex` (stringifies it
46
+ * for CSS) and `colorToRgb` (used as-is) so the latter doesn't have to
47
+ * round-trip through a hex string just to parse the numbers back out.
48
+ * @param {number} index
49
+ * @returns {[number, number, number]}
50
+ */
51
+ function indexedRgb(index) {
52
+ if (index >= 232) {
53
+ const value = (index - 232) * 10 + 8;
54
+ return [value, value, value];
55
+ }
56
+ const n = index - 16;
57
+ // n is always in [0, 215] for index in [16, 231], so these three indices
58
+ // are always in CUBE's bounds ([0, 5]).
59
+ return [
60
+ /** @type {number} */ (CUBE[Math.floor(n / 36) % 6]),
61
+ /** @type {number} */ (CUBE[Math.floor(n / 6) % 6]),
62
+ /** @type {number} */ (CUBE[n % 6]),
63
+ ];
64
+ }
65
+
66
+ /**
67
+ * 256-color index to hex.
68
+ * @param {number} index
69
+ * @returns {string}
70
+ */
71
+ export function indexedHex(index) {
72
+ const [r, g, b] = indexedRgb(index);
73
+ return `#${[r, g, b].map((c) => c.toString(16).padStart(2, "0")).join("")}`;
74
+ }
75
+
76
+ /**
77
+ * Parsed color to CSS (named colors use `--ansi-*` vars).
78
+ * @param {AnsiColor} color
79
+ * @returns {string}
80
+ */
81
+ export function cssColor(color) {
82
+ if ("name" in color) {
83
+ return `var(--ansi-${color.name}, ${ANSI_COLOR_DEFAULTS[color.name]})`;
84
+ }
85
+ if ("rgb" in color) {
86
+ return `rgb(${color.rgb[0]}, ${color.rgb[1]}, ${color.rgb[2]})`;
87
+ }
88
+ return indexedHex(color.index);
89
+ }
90
+
91
+ /**
92
+ * @param {string} hex A `#rrggbb` string.
93
+ * @returns {[number, number, number]}
94
+ */
95
+ export function hexToRgb(hex) {
96
+ const value = hex.replace("#", "");
97
+ return [
98
+ Number.parseInt(value.slice(0, 2), 16),
99
+ Number.parseInt(value.slice(2, 4), 16),
100
+ Number.parseInt(value.slice(4, 6), 16),
101
+ ];
102
+ }
103
+
104
+ /**
105
+ * Named colors use the default palette (theme overrides aren't known here).
106
+ * @param {AnsiColor} color
107
+ * @returns {[number, number, number]}
108
+ */
109
+ export function colorToRgb(color) {
110
+ if ("name" in color) {
111
+ return hexToRgb(ANSI_COLOR_DEFAULTS[color.name] ?? "#000000");
112
+ }
113
+ if ("rgb" in color) return color.rgb;
114
+ return indexedRgb(color.index);
115
+ }
116
+
117
+ /**
118
+ * WCAG relative luminance.
119
+ * @param {[number, number, number]} rgb
120
+ * @returns {number}
121
+ */
122
+ export function luminance(rgb) {
123
+ const toLinear = (/** @type {number} */ c) => {
124
+ const v = c / 255;
125
+ return v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
126
+ };
127
+ return (
128
+ 0.2126 * toLinear(rgb[0]) +
129
+ 0.7152 * toLinear(rgb[1]) +
130
+ 0.0722 * toLinear(rgb[2])
131
+ );
132
+ }
133
+
134
+ /**
135
+ * WCAG contrast ratio.
136
+ * @param {[number, number, number]} a
137
+ * @param {[number, number, number]} b
138
+ * @returns {number}
139
+ */
140
+ export function contrastRatio(a, b) {
141
+ const la = luminance(a);
142
+ const lb = luminance(b);
143
+ return (Math.max(la, lb) + 0.05) / (Math.min(la, lb) + 0.05);
144
+ }
145
+
146
+ /**
147
+ * Black or white, whichever contrasts more with `bg`.
148
+ * @param {[number, number, number]} bg
149
+ * @returns {string}
150
+ */
151
+ export function readableForeground(bg) {
152
+ return contrastRatio(BLACK, bg) >= contrastRatio(WHITE, bg)
153
+ ? "#000000"
154
+ : "#ffffff";
155
+ }
156
+
157
+ /**
158
+ * Foreground CSS, with auto-contrast override when needed.
159
+ * @param {AnsiSegment} segment
160
+ * @param {boolean} autoContrast
161
+ * @returns {string | undefined}
162
+ */
163
+ export function foregroundCss(segment, autoContrast) {
164
+ // Concealed text is rendered transparent (layout and copy text stay).
165
+ if (segment.conceal) return "transparent";
166
+ if (autoContrast && segment.bg) {
167
+ const bg = colorToRgb(segment.bg);
168
+ const fg = segment.fg
169
+ ? colorToRgb(segment.fg)
170
+ : hexToRgb(FOREGROUND_FALLBACK);
171
+ if (contrastRatio(fg, bg) < CONTRAST_TARGET) return readableForeground(bg);
172
+ }
173
+ return segment.fg ? cssColor(segment.fg) : undefined;
174
+ }
175
+
176
+ /**
177
+ * @param {AnsiSegment} segment
178
+ * @returns {string | undefined}
179
+ */
180
+ export function classNames(segment) {
181
+ const names = [];
182
+ if (segment.bold) names.push("bold");
183
+ if (segment.dim) names.push("dim");
184
+ if (segment.italic) names.push("italic");
185
+ if (segment.underline) names.push("underline");
186
+ if (segment.strikethrough) names.push("strikethrough");
187
+ return names.length ? names.join(" ") : undefined;
188
+ }
189
+
190
+ /**
191
+ * @param {AnsiSegment} segment
192
+ * @param {boolean} autoContrast
193
+ * @returns {string | undefined}
194
+ */
195
+ export function inlineStyle(segment, autoContrast) {
196
+ let style = "";
197
+ if (segment.bg) style += `background-color:${cssColor(segment.bg)};`;
198
+ const fg = foregroundCss(segment, autoContrast);
199
+ if (fg) style += `color:${fg};`;
200
+ return style || undefined;
201
+ }
@@ -133,9 +133,10 @@ export function createDomLinePainter({ registry }) {
133
133
  openScopes,
134
134
  pendingHtml,
135
135
  );
136
- if (result.completedLines.length > 0) {
137
- completedLines = completedLines.concat(result.completedLines);
138
- }
136
+ // push, not concat: concat copies the whole (ever-growing) array on
137
+ // every call, which is O(lines) per completed line - push is O(1)
138
+ // amortized (same reasoning as HighlightStream's sealed-chunk append).
139
+ for (const line of result.completedLines) completedLines.push(line);
139
140
  openScopes = result.openScopes;
140
141
  pendingHtml = result.pendingHtml;
141
142
  renderedEventCount = committed.length;
package/engine.js CHANGED
@@ -61,13 +61,13 @@
61
61
  * Memoized result of scanning a single rule/frame's pattern: `match` is the
62
62
  * earliest guard-passing match at or after the position `this.pos` had when
63
63
  * it was computed (or `null` if none exists in `this.code` as of `codeLen`
64
- * characters). See `Tokenizer#cachedMatch`.
64
+ * characters). See `Tokenizer#nextMatch`'s cache-hit checks.
65
65
  * @typedef {{ codeLen: number, match: RegExpExecArray | null }} MatchCache
66
66
  */
67
67
 
68
68
  /**
69
69
  * One entry of the tokenizer's parse stack. `endCache` memoizes this
70
- * frame's end-pattern scan (see `cachedMatch`); it's per-frame rather than
70
+ * frame's end-pattern scan (see `nextMatch`); it's per-frame rather than
71
71
  * per-state because `endSameAsBegin`'s guard depends on this frame's own
72
72
  * `beginMatch`, and two frames can share the same state (recursive nesting).
73
73
  * `beginPos` is this frame's begin match's position in `this.code` (see
@@ -124,14 +124,26 @@ export const TEXT = 0;
124
124
  export const OPEN = 1;
125
125
  export const CLOSE = 2;
126
126
 
127
- /** @param {string} value */
127
+ const HTML_ESCAPE_RE = /[&<>"']/g;
128
+ /** @type {Record<string, string>} */
129
+ const HTML_ESCAPE_MAP = {
130
+ "&": "&amp;",
131
+ "<": "&lt;",
132
+ ">": "&gt;",
133
+ '"': "&quot;",
134
+ "'": "&#x27;",
135
+ };
136
+
137
+ /**
138
+ * Five sequential `.replace()` passes (one per character) previously cost
139
+ * ~87% of renderHtml's time, since every TEXT event runs through this. A
140
+ * single regex pass, skipped entirely when there's nothing to escape, cuts
141
+ * that to a fraction (see bench/render.bench.ts).
142
+ * @param {string} value
143
+ */
128
144
  export function escapeHtml(value) {
129
- return value
130
- .replace(/&/g, "&amp;")
131
- .replace(/</g, "&lt;")
132
- .replace(/>/g, "&gt;")
133
- .replace(/"/g, "&quot;")
134
- .replace(/'/g, "&#x27;");
145
+ if (!HTML_ESCAPE_RE.test(value)) return value;
146
+ return value.replace(HTML_ESCAPE_RE, (char) => HTML_ESCAPE_MAP[char] ?? char);
135
147
  }
136
148
 
137
149
  /**
@@ -205,7 +217,7 @@ class Tokenizer {
205
217
  this.subContinuations = Object.create(null);
206
218
  /**
207
219
  * Memoized begin-pattern scans, indexed by state index (see
208
- * `cachedMatch`). A rule's "next match" is independent of which parent
220
+ * `nextMatch`). A rule's "next match" is independent of which parent
209
221
  * state referenced it, so this is shared across all of them rather than
210
222
  * being per-frame like `Frame#endCache`.
211
223
  * @type {(MatchCache | undefined)[]}
@@ -416,27 +428,17 @@ class Tokenizer {
416
428
  }
417
429
 
418
430
  /**
419
- * Memoized `execValid(re, this.pos, guardFn())`. Without this, every token
431
+ * Whether `cache` is still valid at `this.pos`: without this, every token
420
432
  * boundary re-scans every rule's pattern from scratch. A cached match stays
421
433
  * valid until `this.pos` passes it; a cached miss stays valid until the
422
- * code grows (streaming append). `guardFn` is only called on a cache miss,
423
- * so guarded rules don't allocate a fresh guard closure on every call once
424
- * their match is cached.
425
- * @param {RegExp} re
426
- * @param {() => ((m: RegExpExecArray) => boolean) | null} guardFn
434
+ * code grows (streaming append).
427
435
  * @param {MatchCache | undefined} cache
428
- * @returns {MatchCache}
436
+ * @returns {cache is MatchCache}
429
437
  */
430
- cachedMatch(re, guardFn, cache) {
431
- if (cache) {
432
- if (cache.match !== null && cache.match.index >= this.pos) return cache;
433
- if (cache.match === null && cache.codeLen === this.code.length)
434
- return cache;
435
- }
436
- return {
437
- codeLen: this.code.length,
438
- match: this.execValid(re, this.pos, guardFn()),
439
- };
438
+ isCacheValid(cache) {
439
+ if (!cache) return false;
440
+ if (cache.match !== null && cache.match.index >= this.pos) return true;
441
+ return cache.match === null && cache.codeLen === this.code.length;
440
442
  }
441
443
 
442
444
  /**
@@ -547,13 +549,23 @@ class Tokenizer {
547
549
  // rules[i] is in-bounds by the loop condition.
548
550
  const ruleIdx = /** @type {number} */ (state.rules[i]);
549
551
  const child = /** @type {CompiledState} */ (this.program.states[ruleIdx]);
550
- const cache = this.cachedMatch(
551
- // Every state referenced from a `rules` list has a `begin` pattern.
552
- /** @type {RegExp} */ (child.beginRe),
553
- () => this.beginGuard(child),
554
- this.beginCache[ruleIdx],
555
- );
556
- this.beginCache[ruleIdx] = cache;
552
+ let cache = this.beginCache[ruleIdx];
553
+ // Guard closures (this.beginGuard(child)) are only built on a cache
554
+ // miss - passing them in as a pre-built thunk, as this used to via a
555
+ // shared cachedMatch() helper, allocated one on every call regardless
556
+ // of hit/miss, since JS evaluates argument expressions eagerly.
557
+ if (!this.isCacheValid(cache)) {
558
+ cache = {
559
+ codeLen: this.code.length,
560
+ match: this.execValid(
561
+ // Every state referenced from a `rules` list has a `begin` pattern.
562
+ /** @type {RegExp} */ (child.beginRe),
563
+ this.pos,
564
+ this.beginGuard(child),
565
+ ),
566
+ };
567
+ this.beginCache[ruleIdx] = cache;
568
+ }
557
569
  consider(cache.match, i, "begin", ruleIdx);
558
570
  }
559
571
  // End candidates walk outward while `endsWithParent` chains allow.
@@ -562,15 +574,21 @@ class Tokenizer {
562
574
  // d ranges over [1, frames.length - 1], always in bounds.
563
575
  const frame = /** @type {Frame} */ (this.frames[d]);
564
576
  if (frame.state.endRe) {
565
- const cache = this.cachedMatch(
566
- frame.state.endRe,
567
- () =>
568
- frame.state.endSameAsBegin
569
- ? (/** @type {RegExpExecArray} */ m) => m[1] === frame.beginMatch
570
- : null,
571
- frame.endCache,
572
- );
573
- frame.endCache = cache;
577
+ let cache = frame.endCache;
578
+ if (!this.isCacheValid(cache)) {
579
+ cache = {
580
+ codeLen: this.code.length,
581
+ match: this.execValid(
582
+ /** @type {RegExp} */ (frame.state.endRe),
583
+ this.pos,
584
+ frame.state.endSameAsBegin
585
+ ? (/** @type {RegExpExecArray} */ m) =>
586
+ m[1] === frame.beginMatch
587
+ : null,
588
+ ),
589
+ };
590
+ frame.endCache = cache;
591
+ }
574
592
  consider(cache.match, pri++, "end", d);
575
593
  }
576
594
  if (!frame.state.endsWithParent) break;
package/index.d.ts CHANGED
@@ -25,3 +25,15 @@ export {
25
25
  } from "./scoped";
26
26
  export { default as Typewriter } from "./Typewriter.svelte";
27
27
  export type { ThemePalette } from "./theme";
28
+ export {
29
+ easeInCubic,
30
+ easeInOutCubic,
31
+ easeInOutQuad,
32
+ easeInOutSine,
33
+ easeInQuad,
34
+ easeInSine,
35
+ easeOutCubic,
36
+ easeOutQuad,
37
+ easeOutSine,
38
+ linear,
39
+ } from "./typewriter-easing";
package/index.js CHANGED
@@ -21,3 +21,15 @@ export {
21
21
  scopeStyle,
22
22
  } from "./scoped.js";
23
23
  export { default as Typewriter } from "./Typewriter.svelte";
24
+ export {
25
+ easeInCubic,
26
+ easeInOutCubic,
27
+ easeInOutQuad,
28
+ easeInOutSine,
29
+ easeInQuad,
30
+ easeInSine,
31
+ easeOutCubic,
32
+ easeOutQuad,
33
+ easeOutSine,
34
+ linear,
35
+ } from "./typewriter-easing.js";
package/languages/1c.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Grammar "1c" derived from highlight.js 11.11.1 (BSD-3-Clause).
2
+ * Grammar "1c" derived from highlight.js 11.12.0 (BSD-3-Clause).
3
3
  * Copyright (c) 2006, Ivan Sagalaev. Full text: LICENSE.highlight.txt.
4
4
  */
5
5
  export const _1c = /** @type {import("./index.d.ts").LanguageType<"1c">} */ ({
package/languages/abnf.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Grammar "abnf" derived from highlight.js 11.11.1 (BSD-3-Clause).
2
+ * Grammar "abnf" derived from highlight.js 11.12.0 (BSD-3-Clause).
3
3
  * Copyright (c) 2006, Ivan Sagalaev. Full text: LICENSE.highlight.txt.
4
4
  */
5
5
  export const abnf =