svelte-highlight 7.11.0 → 7.12.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 (88) hide show
  1. package/CopyButton.svelte +118 -0
  2. package/CopyButton.svelte.d.ts +138 -0
  3. package/HighlightStyle.svelte +25 -0
  4. package/HighlightStyle.svelte.d.ts +29 -0
  5. package/LangTag.svelte +10 -1
  6. package/LineNumbers.svelte +18 -6
  7. package/LineNumbers.svelte.d.ts +16 -0
  8. package/README.md +265 -7
  9. package/index.d.ts +2 -0
  10. package/index.js +2 -0
  11. package/langtag.css +1 -0
  12. package/languages/bibtex.d.ts +2 -0
  13. package/languages/bibtex.js +47 -0
  14. package/languages/bicep.d.ts +2 -0
  15. package/languages/bicep.js +72 -0
  16. package/languages/blade.d.ts +2 -0
  17. package/languages/blade.js +56 -0
  18. package/languages/caddy.d.ts +2 -0
  19. package/languages/caddy.js +59 -0
  20. package/languages/cairo.d.ts +2 -0
  21. package/languages/cairo.js +69 -0
  22. package/languages/clarity.d.ts +2 -0
  23. package/languages/clarity.js +55 -0
  24. package/languages/cue.d.ts +2 -0
  25. package/languages/cue.js +76 -0
  26. package/languages/cypher.d.ts +2 -0
  27. package/languages/cypher.js +61 -0
  28. package/languages/d2.d.ts +2 -0
  29. package/languages/d2.js +61 -0
  30. package/languages/dhall.d.ts +2 -0
  31. package/languages/dhall.js +74 -0
  32. package/languages/dotenv.d.ts +2 -0
  33. package/languages/dotenv.js +44 -0
  34. package/languages/fish.d.ts +2 -0
  35. package/languages/fish.js +68 -0
  36. package/languages/gleam.d.ts +2 -0
  37. package/languages/gleam.js +63 -0
  38. package/languages/groq.d.ts +2 -0
  39. package/languages/groq.js +69 -0
  40. package/languages/hcl.d.ts +2 -0
  41. package/languages/hcl.js +84 -0
  42. package/languages/index.d.ts +73 -1
  43. package/languages/index.js +36 -0
  44. package/languages/json5.d.ts +2 -0
  45. package/languages/json5.js +55 -0
  46. package/languages/jsonc.d.ts +2 -0
  47. package/languages/jsonc.js +36 -0
  48. package/languages/jsonnet.d.ts +2 -0
  49. package/languages/jsonnet.js +64 -0
  50. package/languages/liquid.d.ts +2 -0
  51. package/languages/liquid.js +55 -0
  52. package/languages/move.d.ts +2 -0
  53. package/languages/move.js +72 -0
  54. package/languages/nickel.d.ts +2 -0
  55. package/languages/nickel.js +65 -0
  56. package/languages/nushell.d.ts +2 -0
  57. package/languages/nushell.js +77 -0
  58. package/languages/odin.d.ts +2 -0
  59. package/languages/odin.js +78 -0
  60. package/languages/pkl.d.ts +2 -0
  61. package/languages/pkl.js +82 -0
  62. package/languages/prisma.d.ts +2 -0
  63. package/languages/prisma.js +70 -0
  64. package/languages/promql.d.ts +2 -0
  65. package/languages/promql.js +61 -0
  66. package/languages/pug.d.ts +2 -0
  67. package/languages/pug.js +50 -0
  68. package/languages/razor.d.ts +2 -0
  69. package/languages/razor.js +69 -0
  70. package/languages/rescript.d.ts +2 -0
  71. package/languages/rescript.js +71 -0
  72. package/languages/solidity.d.ts +2 -0
  73. package/languages/solidity.js +101 -0
  74. package/languages/starlark.d.ts +2 -0
  75. package/languages/starlark.js +54 -0
  76. package/languages/toml.d.ts +2 -0
  77. package/languages/toml.js +74 -0
  78. package/languages/v.d.ts +2 -0
  79. package/languages/v.js +90 -0
  80. package/languages/vyper.d.ts +2 -0
  81. package/languages/vyper.js +57 -0
  82. package/languages/wgsl.d.ts +2 -0
  83. package/languages/wgsl.js +56 -0
  84. package/languages/zig.d.ts +2 -0
  85. package/languages/zig.js +100 -0
  86. package/package.json +1 -1
  87. package/scoped.d.ts +11 -0
  88. package/scoped.js +276 -0
@@ -0,0 +1,118 @@
1
+ <script>
2
+ /** @type {string} */
3
+ export let code;
4
+
5
+ /**
6
+ * Override the copy behavior. Receives the `code` to copy and may
7
+ * return a promise. When omitted, the native Clipboard API is used.
8
+ * @type {(code: string) => void | Promise<void>}
9
+ */
10
+ export let copy = (code) => navigator.clipboard.writeText(code);
11
+
12
+ /** @type {number} */
13
+ export let timeout = 2_000;
14
+
15
+ /** @type {string} */
16
+ export let text = "Copy";
17
+
18
+ /** @type {string} */
19
+ export let copiedText = "Copied!";
20
+
21
+ import { createEventDispatcher, onMount } from "svelte";
22
+
23
+ const dispatch = createEventDispatcher();
24
+
25
+ /** @type {boolean} */
26
+ let copied = false;
27
+
28
+ /** @type {boolean} */
29
+ let copying = false;
30
+
31
+ /** @type {ReturnType<typeof setTimeout> | undefined} */
32
+ let timeoutId;
33
+
34
+ async function handleClick() {
35
+ // Ignore clicks while a copy is in flight or the "copied" state is active,
36
+ // so no duplicate copy fires (even for slow async `copy` functions) and the
37
+ // "copied" icon stays visible until the timeout elapses.
38
+ if (copying || copied) return;
39
+
40
+ try {
41
+ copying = true;
42
+ await copy(code);
43
+ copied = true;
44
+ dispatch("copy", { code });
45
+ timeoutId = setTimeout(() => {
46
+ copied = false;
47
+ }, timeout);
48
+ } catch (error) {
49
+ dispatch("error", { error });
50
+ } finally {
51
+ copying = false;
52
+ }
53
+ }
54
+
55
+ onMount(() => () => clearTimeout(timeoutId));
56
+ </script>
57
+
58
+ <button
59
+ type="button"
60
+ aria-label={copied ? copiedText : text}
61
+ on:click={handleClick}
62
+ on:mouseenter
63
+ on:mouseleave
64
+ {...$$restProps}
65
+ >
66
+ <slot {copied} {copying}>
67
+ {#if copied}
68
+ <svg
69
+ xmlns="http://www.w3.org/2000/svg"
70
+ viewBox="0 0 32 32"
71
+ width="16"
72
+ height="16"
73
+ fill="currentColor"
74
+ preserveAspectRatio="xMidYMid meet"
75
+ aria-hidden="true"
76
+ >
77
+ <path
78
+ d="M13 24 4 15 5.414 13.586 13 21.171 26.586 7.586 28 9 13 24z"
79
+ ></path>
80
+ </svg>
81
+ {:else}
82
+ <svg
83
+ xmlns="http://www.w3.org/2000/svg"
84
+ viewBox="0 0 32 32"
85
+ width="16"
86
+ height="16"
87
+ fill="currentColor"
88
+ preserveAspectRatio="xMidYMid meet"
89
+ aria-hidden="true"
90
+ >
91
+ <path
92
+ d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z"
93
+ ></path>
94
+ <path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z"></path>
95
+ </svg>
96
+ {/if}
97
+ </slot>
98
+ </button>
99
+
100
+ <style>
101
+ button {
102
+ position: absolute;
103
+ top: var(--copy-top, 0.5em);
104
+ right: var(--copy-right, 0.5em);
105
+ display: flex;
106
+ align-items: center;
107
+ justify-content: center;
108
+ width: var(--copy-size, 2em);
109
+ height: var(--copy-size, 2em);
110
+ padding: var(--copy-padding, 0.5em);
111
+ background: var(--copy-background, inherit);
112
+ color: var(--copy-color, inherit);
113
+ border-radius: var(--copy-border-radius, 4px);
114
+ border: var(--copy-border, none);
115
+ z-index: var(--copy-z-index, 2);
116
+ cursor: pointer;
117
+ }
118
+ </style>
@@ -0,0 +1,138 @@
1
+ import type { SvelteComponentTyped } from "svelte";
2
+ import type { HTMLButtonAttributes } from "svelte/elements";
3
+
4
+ export type CopyButtonProps = HTMLButtonAttributes & {
5
+ /**
6
+ * Specify the text to copy to the clipboard.
7
+ */
8
+ code: string;
9
+
10
+ /**
11
+ * Override the copy behavior. Receives the `code` to copy and may
12
+ * return a promise. When omitted, the native Clipboard API is used.
13
+ * @default (code) => navigator.clipboard.writeText(code)
14
+ */
15
+ copy?: (code: string) => void | Promise<void>;
16
+
17
+ /**
18
+ * Set the duration in milliseconds to show the
19
+ * "copied" state before reverting.
20
+ * @default 2000
21
+ */
22
+ timeout?: number;
23
+
24
+ /**
25
+ * Specify the accessible label (`aria-label`) for the button.
26
+ * Used as the fallback text only if the default icon slot is overridden.
27
+ * @default "Copy"
28
+ */
29
+ text?: string;
30
+
31
+ /**
32
+ * Specify the accessible label (`aria-label`) shown after a successful copy.
33
+ * Used as the fallback text only if the default icon slot is overridden.
34
+ * @default "Copied!"
35
+ */
36
+ copiedText?: string;
37
+
38
+ /**
39
+ * Customize the top offset of the button.
40
+ * @default "0.5em"
41
+ */
42
+ "--copy-top"?: string;
43
+
44
+ /**
45
+ * Customize the right offset of the button.
46
+ * @default "0.5em"
47
+ */
48
+ "--copy-right"?: string;
49
+
50
+ /**
51
+ * Customize the width and height of the button.
52
+ * @default "2em"
53
+ */
54
+ "--copy-size"?: string;
55
+
56
+ /**
57
+ * Customize the inner padding of the button.
58
+ * @default "0.5em"
59
+ */
60
+ "--copy-padding"?: string;
61
+
62
+ /**
63
+ * Customize the background color of the button.
64
+ * @default "inherit"
65
+ */
66
+ "--copy-background"?: string;
67
+
68
+ /**
69
+ * Customize the foreground color of the button.
70
+ * @default "inherit"
71
+ */
72
+ "--copy-color"?: string;
73
+
74
+ /**
75
+ * Customize the border radius of the button.
76
+ * @default "4px"
77
+ */
78
+ "--copy-border-radius"?: string;
79
+
80
+ /**
81
+ * Customize the border of the button.
82
+ * @default "none"
83
+ */
84
+ "--copy-border"?: string;
85
+
86
+ /**
87
+ * Customize the stacking order of the button.
88
+ * @default 2
89
+ */
90
+ "--copy-z-index"?: string | number;
91
+ };
92
+
93
+ export type CopyButtonEvents = {
94
+ copy: CustomEvent<{
95
+ /**
96
+ * The code that was copied.
97
+ */
98
+ code: string;
99
+ }>;
100
+ error: CustomEvent<{
101
+ /**
102
+ * The error thrown by the copy behavior.
103
+ */
104
+ error: unknown;
105
+ }>;
106
+
107
+ /**
108
+ * Forwarded from the `button` element. Useful for prefetching
109
+ * an async copy on hover.
110
+ */
111
+ mouseenter: MouseEvent;
112
+
113
+ /**
114
+ * Forwarded from the `button` element.
115
+ */
116
+ mouseleave: MouseEvent;
117
+ };
118
+
119
+ export type CopyButtonSlots = {
120
+ default: {
121
+ /**
122
+ * `true` while the "copied" state is active.
123
+ */
124
+ copied: boolean;
125
+
126
+ /**
127
+ * `true` while an async `copy` is in flight (before it resolves).
128
+ * Useful for rendering a pending state for slow copy functions.
129
+ */
130
+ copying: boolean;
131
+ };
132
+ };
133
+
134
+ export default class CopyButton extends SvelteComponentTyped<
135
+ CopyButtonProps,
136
+ CopyButtonEvents,
137
+ CopyButtonSlots
138
+ > {}
@@ -0,0 +1,25 @@
1
+ <script>
2
+ import { scopeClassFor, scopeStyle } from "./scoped.js";
3
+
4
+ /**
5
+ * Theme CSS from `svelte-highlight/styles/<theme>`.
6
+ * @example
7
+ * import a11yDark from "svelte-highlight/styles/a11y-dark";
8
+ * @type {string}
9
+ */
10
+ export let theme;
11
+
12
+ /**
13
+ * Wrapper class the scoped selectors target.
14
+ * @type {string}
15
+ */
16
+ export let scopeClass = scopeClassFor(theme);
17
+
18
+ $: style = scopeStyle(theme, scopeClass);
19
+ </script>
20
+
21
+ <svelte:head> {@html style} </svelte:head>
22
+
23
+ <div class={scopeClass} {...$$restProps}>
24
+ <slot {scopeClass} />
25
+ </div>
@@ -0,0 +1,29 @@
1
+ import type { SvelteComponentTyped } from "svelte";
2
+ import type { HTMLAttributes } from "svelte/elements";
3
+
4
+ export type HighlightStyleProps = HTMLAttributes<HTMLDivElement> & {
5
+ /**
6
+ * Theme CSS from `svelte-highlight/styles/<theme>`.
7
+ * @example
8
+ * import a11yDark from "svelte-highlight/styles/a11y-dark";
9
+ */
10
+ theme: string;
11
+
12
+ /**
13
+ * Wrapper class the scoped selectors target.
14
+ * @default hash of `theme`
15
+ */
16
+ scopeClass?: string;
17
+ };
18
+
19
+ export type HighlightStyleSlots = {
20
+ default: {
21
+ scopeClass: string;
22
+ };
23
+ };
24
+
25
+ export default class HighlightStyle extends SvelteComponentTyped<
26
+ HighlightStyleProps,
27
+ Record<string, never>,
28
+ HighlightStyleSlots
29
+ > {}
package/LangTag.svelte CHANGED
@@ -12,7 +12,16 @@
12
12
  export let langtag = false;
13
13
  </script>
14
14
 
15
- <pre class:langtag={langtag} data-language={languageName} {...$$restProps}><code
15
+ <pre
16
+ class:langtag={langtag}
17
+ data-language={languageName}
18
+ style:overflow-x="var(--overflow-x, auto)"
19
+ style:overflow-y="var(--overflow-y, auto)"
20
+ style:border-radius="var(--border-radius, 0)"
21
+ style:width="var(--width, auto)"
22
+ style:max-width="var(--max-width, none)"
23
+ {...$$restProps}
24
+ ><code
16
25
  class:hljs={true}
17
26
  >{#if highlighted}{@html highlighted}{:else}{code}{/if}</code
18
27
  ></pre>
@@ -25,6 +25,7 @@
25
25
  const HIGHLIGHTED_BACKGROUND = "rgba(254, 241, 96, 0.2)";
26
26
 
27
27
  $: lines = highlighted.split("\n");
28
+ $: focusMode = highlightedLines.length > 0;
28
29
  $: len_digits = lines.length.toString().length;
29
30
  $: len = len_digits - MIN_DIGITS < 1 ? MIN_DIGITS : len_digits;
30
31
  $: width = len * DIGIT_WIDTH;
@@ -33,14 +34,19 @@
33
34
  <div
34
35
  class:langtag={langtag}
35
36
  data-language={languageName}
36
- style:overflow-x="auto"
37
+ style:overflow-x="var(--overflow-x, auto)"
38
+ style:overflow-y="var(--overflow-y, auto)"
39
+ style:border-radius="var(--border-radius, 0)"
40
+ style:width="var(--width, auto)"
41
+ style:max-width="var(--max-width, none)"
37
42
  {...$$restProps}
38
43
  >
39
44
  <table>
40
45
  <tbody class:hljs={true}>
41
46
  {#each lines as line, i}
42
47
  {@const lineNumber = i + startingLineNumber}
43
- <tr>
48
+ {@const isHighlighted = highlightedLines.includes(i)}
49
+ <tr class:dimmed={focusMode && !isHighlighted}>
44
50
  <td
45
51
  class:hljs={true}
46
52
  class:hideBorder
@@ -53,7 +59,7 @@
53
59
  <code style:color="var(--line-number-color, currentColor)">
54
60
  {lineNumber}
55
61
  </code>
56
- {#if highlightedLines.includes(i)}
62
+ {#if isHighlighted}
57
63
  <div
58
64
  class:line-background={true}
59
65
  style:background="var(--highlighted-background, {HIGHLIGHTED_BACKGROUND})"
@@ -62,7 +68,7 @@
62
68
  </td>
63
69
  <td>
64
70
  <pre class:wrapLines><code>{@html line || "\n"}</code></pre>
65
- {#if highlightedLines.includes(i)}
71
+ {#if isHighlighted}
66
72
  <div
67
73
  class:line-background={true}
68
74
  style:background="var(--highlighted-background, {HIGHLIGHTED_BACKGROUND})"
@@ -110,8 +116,8 @@
110
116
  }
111
117
 
112
118
  td {
113
- padding-left: var(--padding-left, 1em);
114
- padding-right: var(--padding-right, 1em);
119
+ padding-left: var(--padding-left, var(--padding, 1em));
120
+ padding-right: var(--padding-right, var(--padding, 1em));
115
121
  }
116
122
 
117
123
  td.hljs:not(.hideBorder):after {
@@ -160,4 +166,10 @@
160
166
  tr:last-of-type td .line-background {
161
167
  bottom: 1em;
162
168
  }
169
+
170
+ tr.dimmed td > code,
171
+ tr.dimmed pre {
172
+ opacity: var(--unhighlighted-opacity, 1);
173
+ filter: var(--unhighlighted-filter, none);
174
+ }
163
175
  </style>
@@ -81,6 +81,22 @@ export type LineNumbersProps = HTMLAttributes<HTMLDivElement> &
81
81
  * @example "#fff"
82
82
  */
83
83
  "--highlighted-background"?: string;
84
+
85
+ /**
86
+ * Specify the opacity of un-highlighted lines.
87
+ * Only applies when `highlightedLines` is non-empty.
88
+ * @default 1
89
+ * @example 0.4
90
+ */
91
+ "--unhighlighted-opacity"?: number | string;
92
+
93
+ /**
94
+ * Specify a CSS filter for un-highlighted lines.
95
+ * Only applies when `highlightedLines` is non-empty.
96
+ * @default none
97
+ * @example "blur(2px)"
98
+ */
99
+ "--unhighlighted-filter"?: string;
84
100
  };
85
101
 
86
102
  export type LineNumbersEvents = {};