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/AnsiOutput.svelte CHANGED
@@ -9,147 +9,14 @@
9
9
  export let autoContrast = true;
10
10
 
11
11
  import { parseAnsi } from "./ansi.js";
12
-
13
- // Default foreground and minimum WCAG contrast ratio against backgrounds.
14
- const FOREGROUND_FALLBACK = "#d4d4d4";
15
- const CONTRAST_TARGET = 4.5;
16
- /** @type {[number, number, number]} */
17
- const BLACK = [0, 0, 0];
18
- /** @type {[number, number, number]} */
19
- const WHITE = [255, 255, 255];
20
-
21
- // 16-color xterm defaults, each overridable via `--ansi-<name>`.
22
- const 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 (6×6×6).
42
- const CUBE = [0, 95, 135, 175, 215, 255];
43
-
44
- /** 256-color index → hex. */
45
- function indexedHex(index) {
46
- if (index >= 232) {
47
- const value = (index - 232) * 10 + 8;
48
- const hex = value.toString(16).padStart(2, "0");
49
- return `#${hex}${hex}${hex}`;
50
- }
51
- const n = index - 16;
52
- const r = CUBE[Math.floor(n / 36) % 6];
53
- const g = CUBE[Math.floor(n / 6) % 6];
54
- const b = CUBE[n % 6];
55
- return `#${[r, g, b].map((c) => c.toString(16).padStart(2, "0")).join("")}`;
56
- }
57
-
58
- /** Parsed color → CSS (named colors use `--ansi-*` vars). */
59
- function cssColor(color) {
60
- if ("name" in color) {
61
- return `var(--ansi-${color.name}, ${DEFAULTS[color.name]})`;
62
- }
63
- if ("rgb" in color) {
64
- return `rgb(${color.rgb[0]}, ${color.rgb[1]}, ${color.rgb[2]})`;
65
- }
66
- return indexedHex(color.index);
67
- }
68
-
69
- /**
70
- * @param {string} hex A `#rrggbb` string.
71
- * @returns {[number, number, number]}
72
- */
73
- function hexToRgb(hex) {
74
- const value = hex.replace("#", "");
75
- return [
76
- Number.parseInt(value.slice(0, 2), 16),
77
- Number.parseInt(value.slice(2, 4), 16),
78
- Number.parseInt(value.slice(4, 6), 16),
79
- ];
80
- }
81
-
82
- /** Named colors use the default palette (theme overrides aren't known here). */
83
- function colorToRgb(color) {
84
- if ("name" in color) return hexToRgb(DEFAULTS[color.name] ?? "#000000");
85
- if ("rgb" in color) return color.rgb;
86
- return hexToRgb(indexedHex(color.index));
87
- }
88
-
89
- /** WCAG relative luminance. */
90
- function luminance([r, g, b]) {
91
- const channels = [r, g, b].map((c) => {
92
- const v = c / 255;
93
- return v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
94
- });
95
- return 0.2126 * channels[0] + 0.7152 * channels[1] + 0.0722 * channels[2];
96
- }
97
-
98
- /** WCAG contrast ratio. */
99
- function contrastRatio(a, b) {
100
- const la = luminance(a);
101
- const lb = luminance(b);
102
- return (Math.max(la, lb) + 0.05) / (Math.min(la, lb) + 0.05);
103
- }
104
-
105
- /** Black or white, whichever contrasts more with `bg`. */
106
- function readableForeground(bg) {
107
- return contrastRatio(BLACK, bg) >= contrastRatio(WHITE, bg)
108
- ? "#000000"
109
- : "#ffffff";
110
- }
111
-
112
- /** Foreground CSS, with auto-contrast override when needed. */
113
- function foregroundCss(segment) {
114
- // Concealed text is rendered transparent (layout and copy text stay).
115
- if (segment.conceal) return "transparent";
116
- if (autoContrast && segment.bg) {
117
- const bg = colorToRgb(segment.bg);
118
- const fg = segment.fg
119
- ? colorToRgb(segment.fg)
120
- : hexToRgb(FOREGROUND_FALLBACK);
121
- if (contrastRatio(fg, bg) < CONTRAST_TARGET)
122
- return readableForeground(bg);
123
- }
124
- return segment.fg ? cssColor(segment.fg) : undefined;
125
- }
126
-
127
- /** @param {import("./ansi").AnsiSegment} segment */
128
- function classNames(segment) {
129
- const names = [];
130
- if (segment.bold) names.push("bold");
131
- if (segment.dim) names.push("dim");
132
- if (segment.italic) names.push("italic");
133
- if (segment.underline) names.push("underline");
134
- if (segment.strikethrough) names.push("strikethrough");
135
- return names.length ? names.join(" ") : undefined;
136
- }
137
-
138
- /** @param {import("./ansi").AnsiSegment} segment */
139
- function inlineStyle(segment) {
140
- let style = "";
141
- if (segment.bg) style += `background-color:${cssColor(segment.bg)};`;
142
- const fg = foregroundCss(segment);
143
- if (fg) style += `color:${fg};`;
144
- return style || undefined;
145
- }
12
+ import { classNames, inlineStyle } from "./ansi-color.js";
146
13
 
147
14
  // Precompute class/style per segment. Parse only when `text` changes.
148
15
  $: parsed = parseAnsi(text);
149
16
  $: segments = parsed.map((segment) => ({
150
17
  text: segment.text,
151
18
  class: classNames(segment),
152
- style: inlineStyle(segment),
19
+ style: inlineStyle(segment, autoContrast),
153
20
  link: segment.link,
154
21
  }));
155
22
  </script>
@@ -28,10 +28,43 @@
28
28
  */
29
29
  export let autoScroll = false;
30
30
 
31
+ /**
32
+ * Render only the lines within the scrolled viewport (plus `overscan`)
33
+ * instead of the whole growing buffer, so a long-running stream costs a
34
+ * bounded number of DOM nodes instead of one per line. Backed by
35
+ * `createTokenizedDocument` rather than the default sealed-chunk session,
36
+ * so output always reflects the streaming (non-canonicalized) parse, even
37
+ * once `done` - the same tradeoff `HighlightVirtual` makes. `on:highlight`
38
+ * is not dispatched in this mode, since materializing the full HTML on
39
+ * every repaint would defeat the point of windowing.
40
+ * @type {boolean}
41
+ */
42
+ export let virtualize = false;
43
+
44
+ /**
45
+ * Extra lines rendered above and below the viewport when `virtualize` is
46
+ * set.
47
+ * @type {number}
48
+ */
49
+ export let overscan = 12;
50
+
51
+ /**
52
+ * Lines between engine checkpoints when `virtualize` is set (forwarded to
53
+ * `createTokenizedDocument`).
54
+ * @type {number}
55
+ */
56
+ export let checkpointInterval = 100;
57
+
31
58
  import { createEventDispatcher, onMount, tick } from "svelte";
32
59
  import { extendLines } from "./engine.js";
33
60
  import { ensureRegistered, registry } from "./registry.js";
34
61
  import { createCompletedHtmlBuffer } from "./stream-highlighted.js";
62
+ import {
63
+ buildSealedChunkHtml,
64
+ pushSealedChunk,
65
+ } from "./stream-sealed-chunks.js";
66
+ import { createTokenizedDocument } from "./tokenized-document.js";
67
+ import { watchLineHeight, windowRange } from "./virtual-window.js";
35
68
 
36
69
  // Lines between sealed chunks. Once a chunk fills, its line spans are
37
70
  // joined into one immutable HTML string and never touched again - keyed
@@ -60,6 +93,28 @@
60
93
  // Stick to bottom until the user scrolls away from it.
61
94
  let stickToBottom = true;
62
95
 
96
+ // `virtualize` state: a random-access tokenized document (rather than the
97
+ // sealed-chunk session above) windowed the same way `HighlightVirtual`
98
+ // windows a static document.
99
+ /** @type {HTMLElement} */
100
+ let probe;
101
+ let vLineHeight = 16;
102
+ let vScrollTop = 0;
103
+ let vClientHeight = 0;
104
+ /** @type {ReturnType<typeof requestAnimationFrame> | undefined} */
105
+ let vFrame;
106
+ /** @type {ResizeObserver | undefined} */
107
+ let resizeObserver;
108
+ /** @type {ReturnType<typeof createTokenizedDocument> | undefined} */
109
+ let vdoc;
110
+ let vdocLanguageName = "";
111
+ let vdocCheckpointInterval;
112
+ let vLineCount = 0;
113
+ let vStart = 0;
114
+ let vEnd = 0;
115
+ /** @type {string[]} */
116
+ let vVisibleLines = [];
117
+
63
118
  /** @type {ReturnType<typeof registry.createSession> | undefined} */
64
119
  let session;
65
120
  let sessionLanguageName = "";
@@ -121,14 +176,10 @@
121
176
  // as many full chunks as are ready.
122
177
  function sealChunk() {
123
178
  const chunkLines = unsealedLines.slice(0, SEAL_CHUNK_LINES);
124
- let domHtml = "";
125
- for (let li = 0; li < chunkLines.length; li++) {
126
- const i = sealedLineCount + li;
127
- const sep = i > 0 ? "\n" : "";
128
- domHtml += `${sep}<span class="highlight-stream-line" data-line="${i}">${chunkLines[li]}</span>`;
129
- }
130
- sealedChunks.push(domHtml);
131
- sealedChunks = sealedChunks;
179
+ sealedChunks = pushSealedChunk(
180
+ sealedChunks,
181
+ buildSealedChunkHtml(chunkLines, sealedLineCount),
182
+ );
132
183
  sealedLineCount += chunkLines.length;
133
184
  unsealedLines = unsealedLines.slice(SEAL_CHUNK_LINES);
134
185
  }
@@ -204,6 +255,7 @@
204
255
  const gap =
205
256
  container.scrollHeight - container.scrollTop - container.clientHeight;
206
257
  stickToBottom = gap <= 4;
258
+ if (virtualize) scheduleVirtualRepaint();
207
259
  }
208
260
 
209
261
  function cancelFrame() {
@@ -222,10 +274,94 @@
222
274
  });
223
275
  }
224
276
 
277
+ function ensureVirtualDoc() {
278
+ if (
279
+ vdoc &&
280
+ vdocLanguageName === language.name &&
281
+ vdocCheckpointInterval === checkpointInterval
282
+ ) {
283
+ return;
284
+ }
285
+ vdoc = createTokenizedDocument({ language, checkpointInterval });
286
+ vdocLanguageName = language.name;
287
+ vdocCheckpointInterval = checkpointInterval;
288
+ }
289
+
290
+ function computeVirtualWindow() {
291
+ if (!vdoc) return;
292
+ const total = vdoc.lineCount();
293
+ vLineCount = total;
294
+ ({ start: vStart, end: vEnd } = windowRange({
295
+ scrollTop: vScrollTop,
296
+ clientHeight: vClientHeight,
297
+ lineHeight: vLineHeight,
298
+ overscan,
299
+ total,
300
+ }));
301
+ vVisibleLines = vdoc.lineRange(vStart, vEnd);
302
+ }
303
+
304
+ // Mirrors `scrollToBottom`/the shrink-clamp in `HighlightVirtual`, merged:
305
+ // while streaming with `autoScroll`, stick to the (growing) bottom; once
306
+ // the user scrolls away, just keep the scroll position in bounds.
307
+ async function syncVirtualFromContainer() {
308
+ await tick();
309
+ if (!container) return;
310
+ if (autoScroll && stickToBottom) {
311
+ container.scrollTop = container.scrollHeight;
312
+ } else {
313
+ const maxScrollTop = Math.max(
314
+ 0,
315
+ container.scrollHeight - container.clientHeight,
316
+ );
317
+ if (container.scrollTop > maxScrollTop) {
318
+ container.scrollTop = maxScrollTop;
319
+ }
320
+ }
321
+ vScrollTop = container.scrollTop;
322
+ vClientHeight = container.clientHeight;
323
+ }
324
+
325
+ function cancelVirtualFrame() {
326
+ if (vFrame != null) {
327
+ cancelAnimationFrame(vFrame);
328
+ vFrame = undefined;
329
+ }
330
+ }
331
+
332
+ // Coalesce scroll bursts into one window recompute per frame.
333
+ function scheduleVirtualRepaint() {
334
+ if (vFrame != null) return;
335
+ vFrame = requestAnimationFrame(() => {
336
+ vFrame = undefined;
337
+ if (container) vScrollTop = container.scrollTop;
338
+ });
339
+ }
340
+
341
+ function measureVirtualLineHeight() {
342
+ return watchLineHeight(
343
+ () => probe,
344
+ () => vLineHeight,
345
+ (height) => (vLineHeight = height),
346
+ );
347
+ }
348
+
225
349
  $: {
226
350
  void code;
227
351
  void language;
228
- if (mounted && !done) {
352
+ if (virtualize) {
353
+ // Content/window updates are handled by the virtualize-specific
354
+ // reactive blocks below; this block only tracks `done` dispatch so
355
+ // both modes share the same guard/reset semantics.
356
+ if (mounted && done) {
357
+ if (!doneDispatched) {
358
+ doneDispatched = true;
359
+ dispatch("done");
360
+ }
361
+ } else {
362
+ doneDispatched = false;
363
+ }
364
+ } else if (mounted && !done) {
229
365
  doneDispatched = false;
230
366
  scheduleRepaint();
231
367
  } else {
@@ -239,20 +375,99 @@
239
375
  }
240
376
  }
241
377
 
378
+ // Rebuilds/updates the virtualized document whenever its content or shape
379
+ // changes. Deliberately separate from the scroll-driven block below, same
380
+ // reasoning as `HighlightVirtual`.
381
+ $: if (virtualize && mounted) {
382
+ void code;
383
+ void language;
384
+ void checkpointInterval;
385
+ ensureVirtualDoc();
386
+ vdoc.setCode(code);
387
+ vLineCount = vdoc.lineCount();
388
+ computeVirtualWindow();
389
+ syncVirtualFromContainer();
390
+ }
391
+
392
+ // Scroll/resize/overscan/lineHeight-driven window recompute.
393
+ $: if (virtualize && mounted) {
394
+ void overscan;
395
+ void vLineHeight;
396
+ void vScrollTop;
397
+ void vClientHeight;
398
+ void vLineCount;
399
+ computeVirtualWindow();
400
+ }
401
+
242
402
  $: useSplitRendering = mounted && !done;
243
403
  $: showCaret = useSplitRendering && caret;
244
404
 
245
405
  onMount(() => {
246
406
  mounted = true;
247
- return cancelFrame;
407
+ if (virtualize) {
408
+ measureVirtualLineHeight();
409
+ syncVirtualFromContainer();
410
+ if (typeof ResizeObserver !== "undefined" && container) {
411
+ resizeObserver = new ResizeObserver(() => {
412
+ if (container) vClientHeight = container.clientHeight;
413
+ });
414
+ resizeObserver.observe(container);
415
+ }
416
+ }
417
+ return () => {
418
+ cancelFrame();
419
+ cancelVirtualFrame();
420
+ resizeObserver?.disconnect();
421
+ };
248
422
  });
249
423
  </script>
250
424
 
251
- <pre bind:this={container} on:scroll={onScroll} {...$$restProps}><code
425
+ {#if virtualize}
426
+ <pre
427
+ bind:this={container}
428
+ class:hljs={true}
429
+ class:shl-virtual={true}
430
+ on:scroll={onScroll}
431
+ {...$$restProps}
432
+ ><code>{#if !mounted}{code}{:else}<span class="shl-virtual-sizer" style="height: {vLineCount * vLineHeight}px;"><span class="shl-virtual-window" style="transform: translateY({vStart * vLineHeight}px);">{#each vVisibleLines as line, i (vStart + i)}<span class="highlight-stream-line" data-line={vStart + i}>{@html line}</span>{#if showCaret && vEnd === vLineCount && i === vVisibleLines.length - 1}<span class="highlight-stream-caret" aria-hidden="true"></span>{/if}{"\n"}{/each}</span></span>{/if}</code><span
433
+ bind:this={probe}
434
+ class="shl-virtual-probe highlight-stream-line"
435
+ aria-hidden="true"
436
+ >&nbsp;</span></pre>
437
+ {:else}
438
+ <pre bind:this={container} on:scroll={onScroll} {...$$restProps}><code
252
439
  class:hljs={true}
253
440
  >{#if useSplitRendering}{#each sealedChunks as chunk, c (c)}{@html chunk}{/each}{#each tailLines as line, li (sealedLineCount + li)}{#if sealedLineCount + li > 0}{"\n"}{/if}<span class="highlight-stream-line" data-line={sealedLineCount + li}>{@html line}</span>{/each}{#if showCaret}<span class="highlight-stream-caret" aria-hidden="true"></span>{/if}{:else}{@html highlighted}{/if}</code></pre>
441
+ {/if}
254
442
 
255
443
  <style>
444
+ .shl-virtual {
445
+ display: block;
446
+ position: relative;
447
+ overflow: auto;
448
+ white-space: pre;
449
+ margin: 0;
450
+ }
451
+
452
+ .shl-virtual-sizer {
453
+ display: block;
454
+ position: relative;
455
+ }
456
+
457
+ .shl-virtual-window {
458
+ display: block;
459
+ position: absolute;
460
+ top: 0;
461
+ left: 0;
462
+ right: 0;
463
+ }
464
+
465
+ .shl-virtual-probe {
466
+ position: absolute;
467
+ visibility: hidden;
468
+ pointer-events: none;
469
+ }
470
+
256
471
  .highlight-stream-caret {
257
472
  display: inline-block;
258
473
  width: var(--caret-width, 0.6em);
@@ -263,12 +478,6 @@
263
478
  animation: highlight-stream-blink var(--caret-blink, 1s) step-end infinite;
264
479
  }
265
480
 
266
- @media (prefers-reduced-motion: reduce) {
267
- .highlight-stream-caret {
268
- animation: none;
269
- }
270
- }
271
-
272
481
  @keyframes highlight-stream-blink {
273
482
  50% {
274
483
  opacity: 0;
@@ -37,6 +37,31 @@ export type HighlightStreamProps = HTMLAttributes<HTMLPreElement> & {
37
37
  */
38
38
  autoScroll?: boolean;
39
39
 
40
+ /**
41
+ * Render only the lines within the scrolled viewport (plus `overscan`)
42
+ * instead of the whole growing buffer, so a long-running stream costs a
43
+ * bounded number of DOM nodes instead of one per line. Backed by
44
+ * `createTokenizedDocument` rather than the sealed-chunk session used
45
+ * otherwise, so output always reflects the streaming (non-canonicalized)
46
+ * parse, even once `done` -- the same tradeoff `HighlightVirtual` makes.
47
+ * `on:highlight` is not dispatched in this mode.
48
+ * @default false
49
+ */
50
+ virtualize?: boolean;
51
+
52
+ /**
53
+ * Extra lines rendered above and below the viewport when `virtualize` is set.
54
+ * @default 12
55
+ */
56
+ overscan?: number;
57
+
58
+ /**
59
+ * Lines between engine checkpoints when `virtualize` is set (forwarded to
60
+ * `createTokenizedDocument`).
61
+ * @default 100
62
+ */
63
+ checkpointInterval?: number;
64
+
40
65
  /**
41
66
  * Width of the blinking caret.
42
67
  * @default "0.6em"
@@ -23,6 +23,7 @@
23
23
 
24
24
  import { onMount, tick } from "svelte";
25
25
  import { createTokenizedDocument } from "./tokenized-document.js";
26
+ import { watchLineHeight, windowRange } from "./virtual-window.js";
26
27
 
27
28
  /** @type {HTMLElement} */
28
29
  let container;
@@ -78,13 +79,13 @@
78
79
  if (!doc) return;
79
80
  const total = doc.lineCount();
80
81
  lineCount = total;
81
- const first = Math.max(0, Math.floor(scrollTop / lineHeight) - overscan);
82
- const last = Math.min(
82
+ ({ start, end } = windowRange({
83
+ scrollTop,
84
+ clientHeight,
85
+ lineHeight,
86
+ overscan,
83
87
  total,
84
- Math.ceil((scrollTop + clientHeight) / lineHeight) + overscan,
85
- );
86
- start = Math.min(first, total);
87
- end = Math.max(start, last);
88
+ }));
88
89
  visibleLines = doc.lineRange(start, end);
89
90
  }
90
91
 
@@ -124,20 +125,12 @@
124
125
  scheduleRepaint();
125
126
  }
126
127
 
127
- async function measureLineHeight() {
128
- await tick();
129
- if (probe) {
130
- const height = probe.getBoundingClientRect().height;
131
- if (height > 0) lineHeight = height;
132
- }
133
- if (typeof document !== "undefined" && document.fonts?.ready) {
134
- document.fonts.ready.then(async () => {
135
- await tick();
136
- if (!probe) return;
137
- const height = probe.getBoundingClientRect().height;
138
- if (height > 0 && height !== lineHeight) lineHeight = height;
139
- });
140
- }
128
+ function measureLineHeight() {
129
+ return watchLineHeight(
130
+ () => probe,
131
+ () => lineHeight,
132
+ (height) => (lineHeight = height),
133
+ );
141
134
  }
142
135
 
143
136
  // Rebuilds/updates the document whenever its content or shape changes.
@@ -2,7 +2,7 @@ Third-party notice
2
2
  ==================
3
3
 
4
4
  svelte-highlight ships grammar definitions derived from highlight.js
5
- 11.11.1 (https://github.com/highlightjs/highlight.js), used under the
5
+ 11.12.0 (https://github.com/highlightjs/highlight.js), used under the
6
6
  BSD-3-Clause license below. Each derived grammar module carries a banner
7
7
  pointing back to this file. The svelte-highlight engine itself and all
8
8
  custom (non-hljs-derived) grammars are original code under this package's
package/README.md CHANGED
@@ -1333,7 +1333,22 @@ Set `speed` (milliseconds per character) and pause with `play`. Turn `play` back
1333
1333
  </Highlight>
1334
1334
  ```
1335
1335
 
1336
- With `prefers-reduced-motion`, the full block shows immediately and `on:done` runs right away. Customize the caret with `--caret-width`, `--caret-height`, `--caret-gap`, `--caret-color`, and `--caret-blink`.
1336
+ Control the pacing curve with `easing`: a function mapping elapsed-time fraction (`0`-`1`) to revealed-fraction (`0`-`1`). It defaults to `linear`; import a named curve or pass your own. Total typing duration is always `speed` × character count, no matter the curve -- only the pacing within that duration changes.
1337
+
1338
+ ```svelte
1339
+ <script>
1340
+ import Highlight, { Typewriter, easeOutCubic } from "svelte-highlight";
1341
+ </script>
1342
+
1343
+ <Highlight language={typescript} {code} let:highlighted>
1344
+ <Typewriter {highlighted} easing={easeOutCubic} />
1345
+ </Highlight>
1346
+ ```
1347
+
1348
+ Named curves: `linear` (default), `easeInQuad`/`easeOutQuad`/`easeInOutQuad`, `easeInCubic`/`easeOutCubic`/`easeInOutCubic`, `easeInSine`/`easeOutSine`/`easeInOutSine`.
1349
+
1350
+ `Typewriter` doesn't gate itself on `prefers-reduced-motion` -- if you want to honor it, check `matchMedia("(prefers-reduced-motion: reduce)")` yourself and skip rendering `Typewriter` (or set a very low `speed`). Customize the caret with `--caret-width`, `--caret-height`, `--caret-gap`, `--caret-color`, and `--caret-blink`.
1351
+
1337
1352
 
1338
1353
  Each tick reveals one already-rendered unit rather than re-rendering the block, so animating stays smooth up to tens of thousands of characters; past that it falls back to the coarser whole-block reveal.
1339
1354
 
@@ -1379,7 +1394,13 @@ Set `autoScroll` to keep the container pinned to the bottom as output grows -- i
1379
1394
  <HighlightStream language={typescript} {code} {done} autoScroll style="max-height: 20em; overflow-y: auto;" />
1380
1395
  ```
1381
1396
 
1382
- Per-chunk work is O(tail), not O(stream length so far): finished output is sealed into immutable chunks the DOM never re-diffs, so a response that's ten times longer doesn't cost ten times more per repaint. DOM updates stay proportional to the changed lines -- fine for chat-sized output up to very long responses, not a substitute for `HighlightVirtual` below if you also need to *scroll* through a huge, already-complete document.
1397
+ Per-chunk work is O(tail), not O(stream length so far): finished output is sealed into immutable chunks the DOM never re-diffs, so a response that's ten times longer doesn't cost ten times more per repaint. DOM updates stay proportional to the changed lines -- fine for chat-sized output up to very long responses, but every line ever streamed still stays in the DOM; for a stream that runs long enough to *scroll* through, set `virtualize` to bound that too.
1398
+
1399
+ ```svelte
1400
+ <HighlightStream language={typescript} {code} {done} virtualize style="height: 20em;" />
1401
+ ```
1402
+
1403
+ `virtualize` renders only the lines within the scrolled viewport (plus `overscan`), the same windowing `HighlightVirtual` does for static documents -- a stream that runs to tens of thousands of lines still costs a couple dozen DOM nodes. It swaps the sealed-chunk session for `TokenizedDocument` (see [Large documents](#large-documents) below), so output always reflects the streaming (non-canonicalized) parse, even once `done` -- unlike the default mode, which upgrades to a canonical final render. `on:highlight` isn't dispatched in this mode, since materializing the full HTML on every repaint would defeat the point of windowing; `on:done`, the caret, and `autoScroll` all keep working.
1383
1404
 
1384
1405
  ## Large documents
1385
1406
 
@@ -1737,19 +1758,22 @@ Use `bind:this`, then call `undo()`, `redo()`, `focus()`, `selectAll()`, `insert
1737
1758
 
1738
1759
  #### Props
1739
1760
 
1740
- | Name | Type | Default value |
1741
- | :--------- | :--------------------------------------------- | :------------- |
1742
- | code | `string` | `""` |
1743
- | language | { name: `string`; register: `object` } | N/A (required) |
1744
- | done | `boolean` | `false` |
1745
- | caret | `boolean` | `true` |
1746
- | autoScroll | `boolean` | `false` |
1761
+ | Name | Type | Default value |
1762
+ | :----------------- | :-------------------------------------- | :------------- |
1763
+ | code | `string` | `""` |
1764
+ | language | { name: `string`; register: `object` } | N/A (required) |
1765
+ | done | `boolean` | `false` |
1766
+ | caret | `boolean` | `true` |
1767
+ | autoScroll | `boolean` | `false` |
1768
+ | virtualize | `boolean` | `false` |
1769
+ | overscan | `number` | `12` |
1770
+ | checkpointInterval | `number` | `100` |
1747
1771
 
1748
- `$$restProps` are forwarded to the top-level `pre` element.
1772
+ `$$restProps` are forwarded to the top-level `pre` element. `overscan` and `checkpointInterval` only apply when `virtualize` is set.
1749
1773
 
1750
1774
  #### Dispatched Events
1751
1775
 
1752
- - **on:highlight**: fired after each highlight pass, with `{ highlighted }`
1776
+ - **on:highlight**: fired after each highlight pass, with `{ highlighted }` -- not dispatched when `virtualize` is set
1753
1777
  - **on:done**: fired after the final full highlight once `done` is set
1754
1778
 
1755
1779
  ```svelte
@@ -1806,17 +1830,18 @@ See [Large documents](#large-documents) above.
1806
1830
 
1807
1831
  #### Props
1808
1832
 
1809
- | Name | Type | Default value |
1810
- | :---------- | :-------- | :------------ |
1811
- | highlighted | `string` | `""` |
1812
- | speed | `number` | `30` |
1813
- | play | `boolean` | `true` |
1833
+ | Name | Type | Default value |
1834
+ | :---------- | :----------------------- | :------------ |
1835
+ | highlighted | `string` | `""` |
1836
+ | speed | `number` | `30` |
1837
+ | play | `boolean` | `true` |
1838
+ | easing | `(t: number) => number` | `linear` |
1814
1839
 
1815
1840
  `$$restProps` are forwarded to the top-level `pre` element.
1816
1841
 
1817
1842
  #### Dispatched Events
1818
1843
 
1819
- - **on:done**: fires when typing finishes, or immediately when reduced motion is on
1844
+ - **on:done**: fires when typing finishes
1820
1845
 
1821
1846
  ```svelte
1822
1847
  <Highlight language={typescript} {code} let:highlighted>