svelte-highlight 6.2.1 → 7.0.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.
package/README.md CHANGED
@@ -24,33 +24,31 @@ pnpm i -D svelte-highlight highlight.js
24
24
 
25
25
  Note that [pnpm](https://github.com/pnpm/pnpm) users must also install `highlight.js`.
26
26
 
27
- ## SvelteKit Set-up
27
+ ## Basic Usage
28
28
 
29
- To use this library with [SvelteKit](https://github.com/sveltejs/kit) or [Vite](https://github.com/sveltejs/vite-plugin-svelte), instruct Vite to [optimize](https://vitejs.dev/config/dep-optimization-options.html#optimizedeps-include) `highlight.js` and `highlight.js/lib/core`:
29
+ The `Highlight` component requires two props:
30
30
 
31
- ```diff
32
- + optimizeDeps: {
33
- + include: ["highlight.js", "highlight.js/lib/core"],
34
- + },
35
- ```
31
+ - `code`: text to highlight
32
+ - `language`: language grammar used to highlight the text
33
+
34
+ Import languages from `svelte-highlight/languages`.
35
+
36
+ See [SUPPORTED_LANGUAGES.md](SUPPORTED_LANGUAGES.md) for a list of supported languages.
36
37
 
37
- **vite.config.js**
38
+ ```svelte
39
+ <script>
40
+ import Highlight from "svelte-highlight";
41
+ import typescript from "svelte-highlight/languages/typescript";
38
42
 
39
- ```js
40
- import { sveltekit } from "@sveltejs/kit/vite";
43
+ const code = "const add = (a: number, b: number) => a + b;";
44
+ </script>
41
45
 
42
- /** @type {import('vite').UserConfig} */
43
- export default {
44
- plugins: [sveltekit()],
45
- optimizeDeps: {
46
- include: ["highlight.js", "highlight.js/lib/core"],
47
- },
48
- };
46
+ <Highlight language={typescript} {code} />
49
47
  ```
50
48
 
51
- Refer to [examples/sveltekit](examples/sveltekit) or [examples/vite](examples/vite).
49
+ ## Styling
52
50
 
53
- ## Usage
51
+ Import styles from `svelte-highlight/styles`. See [SUPPORTED_STYLES.md](SUPPORTED_STYLES.md) for a list of supported styles.
54
52
 
55
53
  There are two ways to apply `highlight.js` styles.
56
54
 
@@ -79,7 +77,7 @@ This component exports `highlight.js` themes in JavaScript. Import the theme fro
79
77
 
80
78
  ### CSS StyleSheet
81
79
 
82
- Depending on your set-up, importing a CSS StyleSheet in Svelte may require a CSS file loader. Refer to [examples/webpack](examples/webpack) for a sample set-up.
80
+ Depending on your set-up, importing a CSS StyleSheet in Svelte may require a CSS file loader. SvelteKit/Vite automatically supports this. For Webpack, refer to [examples/webpack](examples/webpack).
83
81
 
84
82
  ```svelte
85
83
  <script>
@@ -97,7 +95,8 @@ Depending on your set-up, importing a CSS StyleSheet in Svelte may require a CSS
97
95
 
98
96
  CSS StyleSheets can also be externally linked from a Content Delivery Network (CDN) like [unpkg.com](https://unpkg.com/).
99
97
 
100
- This is best suited for prototyping and not recommended for production use.
98
+ > **Warning**
99
+ > Using a CDN is best suited for prototyping and not recommended for production use.
101
100
 
102
101
  **HTML**
103
102
 
@@ -133,7 +132,7 @@ Use the `HighlightSvelte` component for Svelte syntax highlighting.
133
132
  import { HighlightSvelte } from "svelte-highlight";
134
133
  import github from "svelte-highlight/styles/github";
135
134
 
136
- $: code = `<button on:click={() => { console.log(0); }}>Increment {count}</button>`;
135
+ const code = `<button on:click={() => { console.log(0); }}>Increment {count}</button>`;
137
136
  </script>
138
137
 
139
138
  <svelte:head>
@@ -147,16 +146,15 @@ Use the `HighlightSvelte` component for Svelte syntax highlighting.
147
146
 
148
147
  The `HighlightAuto` component uses [highlightAuto](https://highlightjs.readthedocs.io/en/latest/api.html#highlightauto) API.
149
148
 
150
- **Note:** auto-highlighting will result in a larger bundle size in order to infer a language.
151
-
152
- Prefer to specify a language if possible.
149
+ > **Warning**
150
+ > Auto-highlighting will result in a larger bundle size. Specify a language if possible.
153
151
 
154
152
  ```svelte
155
153
  <script>
156
154
  import { HighlightAuto } from "svelte-highlight";
157
155
  import github from "svelte-highlight/styles/github";
158
156
 
159
- $: code = `body {\n padding: 0;\n color: red;\n}`;
157
+ const code = `body {\n padding: 0;\n color: red;\n}`;
160
158
  </script>
161
159
 
162
160
  <svelte:head>
@@ -166,11 +164,78 @@ Prefer to specify a language if possible.
166
164
  <HighlightAuto {code} />
167
165
  ```
168
166
 
167
+ ## Line Numbers
168
+
169
+ Use the `LineNumbers` component to render the highlighted code with line numbers.
170
+
171
+ ```svelte
172
+ <script>
173
+ import Hightlight, { LineNumbers } from "svelte-highlight";
174
+ import typescript from "svelte-highlight/languages/typescript";
175
+ import atomOneDark from "svelte-highlight/styles/atom-one-dark";
176
+
177
+ const code = "const add = (a: number, b: number) => a + b";
178
+ </script>
179
+
180
+ <svelte:head>
181
+ {@html atomOneDark}
182
+ </svelte:head>
183
+
184
+ <Highlight language={typescript} {code} let:highlighted>
185
+ <LineNumbers {highlighted} />
186
+ </Highlight>
187
+ ```
188
+
189
+ ### Hidden Border
190
+
191
+ Set `hideBorder` to `true` to hide the border of the line numbers column.
192
+
193
+ ```svelte
194
+ <Highlight language={typescript} {code} let:highlighted>
195
+ <LineNumbers {highlighted} hideBorder />
196
+ </Highlight>
197
+ ```
198
+
199
+ ### Wrapped Lines
200
+
201
+ By default, overflowing horizontal content is contained by a scrollbar.
202
+
203
+ Set `wrapLines` to `true` to hide the border of the line numbers column.
204
+
205
+ ```svelte
206
+ <Highlight language={typescript} {code} let:highlighted>
207
+ <LineNumbers {highlighted} wrapLines />
208
+ </Highlight>
209
+ ```
210
+
211
+ ### Custom Styles
212
+
213
+ Use `--style-props` to customize styles.
214
+
215
+ | Style prop | Description | Default value |
216
+ | :------------------ | :----------------------------------------- | :------------- |
217
+ | --line-number-color | Text color of the line numbers | `currentColor` |
218
+ | --border-color | Border color of the column of line numbers | `currentColor` |
219
+ | --padding-left | Left padding for `td` elements | `1em` |
220
+ | --padding-right | Rightt padding for `td` elements | `1em` |
221
+
222
+ ```svelte
223
+ <Highlight language={typescript} {code} let:highlighted>
224
+ <LineNumbers
225
+ {highlighted}
226
+ --line-number-color="pink"
227
+ --border-color="rgba(255, 255, 255, 0.2)"
228
+ --padding-left="0"
229
+ --padding-right="3em"
230
+ />
231
+ </Highlight>
232
+ ```
233
+
169
234
  ## Language Targeting
170
235
 
171
236
  All `Highlight` components apply a `data-language` attribute on the codeblock containing the language name.
172
237
 
173
- See the [Languages page](SUPPORTED_LANGUAGES.md) for a list of supported languages.
238
+ See [SUPPORTED_LANGUAGES.md](SUPPORTED_LANGUAGES.md) for a list of supported languages.
174
239
 
175
240
  ```css
176
241
  [data-language="css"] {
@@ -180,19 +245,17 @@ See the [Languages page](SUPPORTED_LANGUAGES.md) for a list of supported languag
180
245
 
181
246
  ## Language Tags
182
247
 
183
- All `Highlight` components allow for a tag to be added at the top-right of the codeblock displaying the language name.
184
-
185
- The language tag can be given a custom `background` , `color` , and `border-radius` through CSS custom properties.
248
+ Set `langtag` to `true` to display the language name in the top right corner of the code block.
186
249
 
187
- It is recommended that you set values for `--hljs-background` and `--hljs-foreground` to ensure the langtags are readable with your theme.
250
+ Customize the language tag `background`, `color`, and `border-radius` using style props.
188
251
 
189
252
  See the [Languages page](SUPPORTED_LANGUAGES.md) for a list of supported languages.
190
253
 
191
- Defaults:
192
-
193
- - `--hljs-background: inherit`
194
- - `--hljs-foreground: inherit`
195
- - `--hljs-border-radius: 0`
254
+ | Style prop | Description | Default value |
255
+ | :---------------------- | :------------------------------ | :------------ |
256
+ | --langtag-background | Background color of the langtag | `inherit` |
257
+ | --langtag-color | Text color of the langtag | `inherit` |
258
+ | --langtag-border-radius | Border radius of the langtag | `0` |
196
259
 
197
260
  ```svelte
198
261
  <script>
@@ -206,9 +269,9 @@ Defaults:
206
269
 
207
270
  ```css
208
271
  [data-language="css"] {
209
- --hljs-background: linear-gradient(135deg, #2996cf, 80%, white);
210
- --hljs-foreground: #fff;
211
- --hljs-radius: 8px;
272
+ --langtag-background: linear-gradient(135deg, #2996cf, 80%, white);
273
+ --langtag-color: #fff;
274
+ --langtag-border-radius: 8px;
212
275
  }
213
276
  ```
214
277
 
@@ -239,7 +302,7 @@ Refer to the highlight.js [language definition guide](https://highlightjs.readth
239
302
 
240
303
  ## Code-splitting
241
304
 
242
- You can use the `dynamic import` syntax to code-split code.
305
+ You can use the `await import` syntax for code-splitting.
243
306
 
244
307
  In the example below, the `HighlightAuto` component and injected styles are dynamically loaded.
245
308
 
@@ -269,37 +332,111 @@ In the example below, the `HighlightAuto` component and injected styles are dyna
269
332
  />
270
333
  ```
271
334
 
272
- ## API
335
+ ## Component API
336
+
337
+ ### `Highlight`
273
338
 
274
- ### Props
339
+ #### Props
275
340
 
276
- | Name | Type | Default value |
277
- | :---------------------------------------------- | :--------------------------------------------- | :--------------------------------------- |
278
- | code | `any` | `undefined` |
279
- | language (only required for `Highlight.svelte`) | { name: `string`; register: hljs => `object` } | { name: undefined, register: undefined } |
280
- | langtag | `boolean` | `false` |
341
+ | Name | Type | Default value |
342
+ | :------- | :--------------------------------------------- | :------------- |
343
+ | code | `any` | N/A (required) |
344
+ | language | { name: `string`; register: hljs => `object` } | N/A (required) |
345
+ | langtag | `boolean` | `false` |
281
346
 
282
- - `$$restProps` are forwarded to the `pre` element
347
+ `$$restProps` are forwarded to the top-level `pre` element.
283
348
 
284
- ### Dispatched Events
349
+ #### Dispatched Events
285
350
 
286
- - **on:highlight**: fired after code syntax is highlighted
351
+ - **on:highlight**: fired after `code` is highlighted
287
352
 
288
353
  ```svelte
289
354
  <Highlight
290
355
  language={typescript}
291
356
  {code}
292
357
  on:highlight={(e) => {
293
- console.log(e.detail.highlighted); // "<span>...</span>"
358
+ /**
359
+ * The highlighted HTML as a string.
360
+ * @example "<span>...</span>"
361
+ */
362
+ console.log(e.detail.highlighted);
363
+ }}
364
+ />
365
+ ```
366
+
367
+ ### `LineNumbers`
368
+
369
+ #### Props
370
+
371
+ | Name | Type | Default value |
372
+ | :---------- | :-------- | :------------- |
373
+ | highlighted | `string` | N/A (required) |
374
+ | hideBorder | `boolean` | `false` |
375
+ | wrapLines | `boolean` | `false` |
376
+
377
+ `$$restProps` are forwarded to the top-level `div` element.
378
+
379
+ ### `HighlightSvelte`
380
+
381
+ #### Props
382
+
383
+ | Name | Type | Default value |
384
+ | :------ | :-------- | :------------- |
385
+ | code | `any` | N/A (required) |
386
+ | langtag | `boolean` | `false` |
387
+
388
+ `$$restProps` are forwarded to the top-level `pre` element.
389
+
390
+ #### Dispatched Events
391
+
392
+ - **on:highlight**: fired after `code` is highlighted
393
+
394
+ ```svelte
395
+ <HighlightSvelte
396
+ {code}
397
+ on:highlight={(e) => {
398
+ /**
399
+ * The highlighted HTML as a string.
400
+ * @example "<span>...</span>"
401
+ */
402
+ console.log(e.detail.highlighted);
294
403
  }}
295
404
  />
296
405
  ```
297
406
 
298
- ## TypeScript
407
+ ### `HighlightAuto`
408
+
409
+ #### Props
299
410
 
300
- Svelte version 3.31 or greater is required to use this component with TypeScript.
411
+ | Name | Type | Default value |
412
+ | :------ | :-------- | :------------- |
413
+ | code | `any` | N/A (required) |
414
+ | langtag | `boolean` | `false` |
301
415
 
302
- TypeScript definitions are auto-generated by SvelteKit.
416
+ `$$restProps` are forwarded to the top-level `pre` element.
417
+
418
+ #### Dispatched Events
419
+
420
+ - **on:highlight**: fired after `code` is highlighted
421
+
422
+ ```svelte
423
+ <HighlightAuto
424
+ {code}
425
+ on:highlight={(e) => {
426
+ /**
427
+ * The highlighted HTML as a string.
428
+ * @example "<span>...</span>"
429
+ */
430
+ console.log(e.detail.highlighted);
431
+
432
+ /**
433
+ * The inferred language name
434
+ * @example "css"
435
+ */
436
+ console.log(e.detail.language);
437
+ }}
438
+ />
439
+ ```
303
440
 
304
441
  ## [Supported Languages](SUPPORTED_LANGUAGES.md)
305
442
 
package/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { default as default } from "./Highlight.svelte";
2
- export { default as Highlight } from "./Highlight.svelte";
1
+ export { default as default, default as Highlight } from "./Highlight.svelte";
3
2
  export { default as HighlightAuto } from "./HighlightAuto.svelte";
4
3
  export { default as HighlightSvelte } from "./HighlightSvelte.svelte";
4
+ export { default as LineNumbers } from "./LineNumbers.svelte";
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { default as default } from "./Highlight.svelte";
2
- export { default as Highlight } from "./Highlight.svelte";
1
+ export { default as default, default as Highlight } from "./Highlight.svelte";
3
2
  export { default as HighlightAuto } from "./HighlightAuto.svelte";
4
3
  export { default as HighlightSvelte } from "./HighlightSvelte.svelte";
4
+ export { default as LineNumbers } from "./LineNumbers.svelte";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-highlight",
3
- "version": "6.2.1",
3
+ "version": "7.0.0",
4
4
  "license": "MIT",
5
5
  "description": "Syntax Highlighting for Svelte using highlight.js",
6
6
  "author": "Eric Liu (https://github.com/metonym)",
@@ -9,29 +9,30 @@
9
9
  ],
10
10
  "type": "module",
11
11
  "dependencies": {
12
- "highlight.js": "11.6.0"
12
+ "highlight.js": "11.7.0"
13
13
  },
14
14
  "devDependencies": {
15
- "@sveltejs/adapter-static": "1.0.0-next.39",
16
- "@sveltejs/kit": "1.0.0-next.405",
17
- "@testing-library/svelte": "^3.1.3",
18
- "@testing-library/user-event": "^14.4.2",
19
- "@types/prettier": "^2.7.0",
20
- "carbon-components-svelte": "^0.67.6",
21
- "carbon-icons-svelte": "^11.2.0",
15
+ "@sveltejs/adapter-static": "1.0.0",
16
+ "@sveltejs/kit": "1.0.1",
17
+ "@sveltejs/package": "^1.0.1",
18
+ "@testing-library/svelte": "^3.2.2",
19
+ "@testing-library/user-event": "^14.4.3",
20
+ "@types/prettier": "^2.7.2",
21
+ "carbon-components-svelte": "^0.70.13",
22
+ "carbon-icons-svelte": "^11.4.0",
22
23
  "carbon-preprocess-svelte": "^0.9.1",
23
- "jsdom": "^20.0.0",
24
- "prettier": "^2.7.1",
25
- "prettier-plugin-svelte": "^2.7.0",
26
- "svelte": "^3.49.0",
27
- "svelte-check": "^2.8.0",
24
+ "jsdom": "^20.0.3",
25
+ "prettier": "^2.8.1",
26
+ "prettier-plugin-svelte": "^2.9.0",
27
+ "svelte": "^3.55.0",
28
+ "svelte-check": "^3.0.1",
28
29
  "svelte-focus-key": "^0.3.2",
29
- "svelte-preprocess": "^4.10.7",
30
- "svelte2tsx": "^0.5.13",
30
+ "svelte-preprocess": "^5.0.0",
31
+ "svelte2tsx": "^0.6.0",
31
32
  "totalist": "^3.0.0",
32
- "typescript": "^4.7.4",
33
- "vite": "^3.0.4",
34
- "vitest": "^0.21.0"
33
+ "typescript": "^4.9.4",
34
+ "vite": "^4.0.3",
35
+ "vitest": "^0.26.2"
35
36
  },
36
37
  "repository": {
37
38
  "type": "git",
@@ -54,6 +55,7 @@
54
55
  "./Highlight.svelte": "./Highlight.svelte",
55
56
  "./HighlightAuto.svelte": "./HighlightAuto.svelte",
56
57
  "./HighlightSvelte.svelte": "./HighlightSvelte.svelte",
58
+ "./LineNumbers.svelte": "./LineNumbers.svelte",
57
59
  ".": "./index.js",
58
60
  "./languages/1c": "./languages/1c.js",
59
61
  "./languages/abnf": "./languages/abnf.js",