svelte-highlight 7.4.2 → 7.4.3

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/Highlight.svelte CHANGED
@@ -1,21 +1,32 @@
1
- <script context="module"></script>
2
-
3
- <script>export let language;
4
- export let code;
5
- export let langtag = false;
6
- import hljs from "highlight.js/lib/core";
7
- import { createEventDispatcher, afterUpdate } from "svelte";
8
- import LangTag from "./LangTag.svelte";
9
- const dispatch = createEventDispatcher();
10
- let highlighted = "";
11
- afterUpdate(() => {
12
- if (highlighted)
13
- dispatch("highlight", { highlighted });
14
- });
15
- $: {
16
- hljs.registerLanguage(language.name, language.register);
17
- highlighted = hljs.highlight(code, { language: language.name }).value;
18
- }
1
+ <script>
2
+ // @ts-check
3
+
4
+ /** @type {import("./languages").LanguageType<string>} */
5
+ export let language;
6
+
7
+ /** @type {any} */
8
+ export let code;
9
+
10
+ /** @type {boolean} */
11
+ export let langtag = false;
12
+
13
+ import hljs from "highlight.js/lib/core";
14
+ import { createEventDispatcher, afterUpdate } from "svelte";
15
+ import LangTag from "./LangTag.svelte";
16
+
17
+ const dispatch = createEventDispatcher();
18
+
19
+ /** @type {string} */
20
+ let highlighted = "";
21
+
22
+ afterUpdate(() => {
23
+ if (highlighted) dispatch("highlight", { highlighted });
24
+ });
25
+
26
+ $: {
27
+ hljs.registerLanguage(language.name, language.register);
28
+ highlighted = hljs.highlight(code, { language: language.name }).value;
29
+ }
19
30
  </script>
20
31
 
21
32
  <slot {highlighted}>
@@ -1,13 +1,72 @@
1
- import { SvelteComponentTyped } from "svelte";
2
- import type { Props, Slots, Events } from "./shared";
3
- declare const __propDef: {
4
- props: Props;
5
- slots: Slots;
6
- events: Events<import("./shared").HighlightedCode>;
1
+ import type { SvelteComponentTyped } from "svelte";
2
+ import type { HTMLAttributes } from "svelte/elements";
3
+ import type { LanguageType } from "./languages";
4
+
5
+ export type HighlightProps = HTMLAttributes<HTMLPreElement> & {
6
+ /**
7
+ * Specify the text to highlight.
8
+ */
9
+ code: any;
10
+
11
+ /**
12
+ * Provide the language grammar used to highlight the code.
13
+ * Import languages from `svelte-highlight/languages/*`.
14
+ * @example
15
+ * import typescript from "svelte-highlight/languages/typescript";
16
+ */
17
+ language: LanguageType<string>;
18
+
19
+ /**
20
+ * Set to `true` for the language name to be
21
+ * displayed at the top right of the code block.
22
+ *
23
+ * Use style props to customize styles:
24
+ * - `--langtag-background`
25
+ * - `--langtag-color`
26
+ * - `--langtag-border-radius`
27
+ *
28
+ * @default false
29
+ */
30
+ langtag?: boolean;
31
+
32
+ /**
33
+ * Customize the background color of the langtag.
34
+ */
35
+ "--langtag-background"?: string;
36
+
37
+ /**
38
+ * Customize the text color of the langtag.
39
+ */
40
+ "--langtag-color"?: string;
41
+
42
+ /**
43
+ * Customize the border radius of the langtag.
44
+ */
45
+ "--langtag-border-radius"?: string;
7
46
  };
8
- export type HighlightProps = typeof __propDef.props;
9
- export type HighlightEvents = typeof __propDef.events;
10
- export type HighlightSlots = typeof __propDef.slots;
11
- export default class Highlight extends SvelteComponentTyped<HighlightProps, HighlightEvents, HighlightSlots> {
12
- }
13
- export {};
47
+
48
+ export type HighlightEvents = {
49
+ highlight: CustomEvent<{
50
+ /**
51
+ * The highlighted HTML as a string.
52
+ * @example "<span>...</span>"
53
+ */
54
+ highlighted: string;
55
+ }>;
56
+ };
57
+
58
+ export type HighlightSlots = {
59
+ default: {
60
+ /**
61
+ * The highlighted HTML as a string.
62
+ * @example "<span>...</span>"
63
+ */
64
+ highlighted: string;
65
+ };
66
+ };
67
+
68
+ export default class Highlight extends SvelteComponentTyped<
69
+ HighlightProps,
70
+ HighlightEvents,
71
+ HighlightSlots
72
+ > {}
@@ -1,19 +1,33 @@
1
- <script context="module"></script>
2
-
3
- <script>import LangTag from "./LangTag.svelte";
4
- export let code;
5
- export let langtag = false;
6
- import hljs from "highlight.js";
7
- import { createEventDispatcher, afterUpdate } from "svelte";
8
- const dispatch = createEventDispatcher();
9
- let highlighted = "";
10
- let language = void 0;
11
- afterUpdate(() => {
12
- if (highlighted)
13
- dispatch("highlight", { highlighted, language });
14
- });
15
- $:
16
- ({ value: highlighted, language } = hljs.highlightAuto(code));
1
+ <script>
2
+ // @ts-check
3
+ import LangTag from "./LangTag.svelte";
4
+
5
+ /** @type {any} */
6
+ export let code;
7
+
8
+ /** @type {boolean} */
9
+ export let langtag = false;
10
+
11
+ import hljs from "highlight.js";
12
+ import { createEventDispatcher, afterUpdate } from "svelte";
13
+
14
+ /**
15
+ * @typedef {{ highlighted: string; language: string; }} HighlightEventDetail
16
+ * @type {import("svelte").EventDispatcher<{ highlight: HighlightEventDetail}>}
17
+ */
18
+ const dispatch = createEventDispatcher();
19
+
20
+ /** @type {string} */
21
+ let highlighted = "";
22
+
23
+ /** @type {string} */
24
+ let language = "";
25
+
26
+ afterUpdate(() => {
27
+ if (highlighted) dispatch("highlight", { highlighted, language });
28
+ });
29
+
30
+ $: ({ value: highlighted, language = "" } = hljs.highlightAuto(code));
17
31
  </script>
18
32
 
19
33
  <slot {highlighted}>
@@ -1,19 +1,69 @@
1
- import { SvelteComponentTyped } from "svelte";
2
- import type { Props, Slots, Events, HighlightedCode } from "./shared";
3
- declare const __propDef: {
4
- props: Omit<Props, "language">;
5
- slots: Slots;
6
- events: Events<HighlightedCode & {
7
- /**
8
- * The inferred language name.
9
- * @example "css"
10
- */
11
- language?: string;
12
- }>;
1
+ import type { SvelteComponentTyped } from "svelte";
2
+ import type { HTMLAttributes } from "svelte/elements";
3
+
4
+ export type HighlightAutoProps = HTMLAttributes<HTMLPreElement> & {
5
+ /**
6
+ * Specify the text to highlight.
7
+ */
8
+ code: any;
9
+
10
+ /**
11
+ * Set to `true` for the language name to be
12
+ * displayed at the top right of the code block.
13
+ *
14
+ * Use style props to customize styles:
15
+ * - `--langtag-background`
16
+ * - `--langtag-color`
17
+ * - `--langtag-border-radius`
18
+ *
19
+ * @default false
20
+ */
21
+ langtag?: boolean;
22
+
23
+ /**
24
+ * Customize the background color of the langtag.
25
+ */
26
+ "--langtag-background"?: string;
27
+
28
+ /**
29
+ * Customize the text color of the langtag.
30
+ */
31
+ "--langtag-color"?: string;
32
+
33
+ /**
34
+ * Customize the border radius of the langtag.
35
+ */
36
+ "--langtag-border-radius"?: string;
13
37
  };
14
- export type HighlightAutoProps = typeof __propDef.props;
15
- export type HighlightAutoEvents = typeof __propDef.events;
16
- export type HighlightAutoSlots = typeof __propDef.slots;
17
- export default class HighlightAuto extends SvelteComponentTyped<HighlightAutoProps, HighlightAutoEvents, HighlightAutoSlots> {
18
- }
19
- export {};
38
+
39
+ export type HighlightAutoEvents = {
40
+ highlight: CustomEvent<{
41
+ /**
42
+ * The highlighted HTML as a string.
43
+ * @example "<span>...</span>"
44
+ */
45
+ highlighted: string;
46
+
47
+ /**
48
+ * The language name inferred by `highlight.js`.
49
+ * @example "css"
50
+ */
51
+ language: string;
52
+ }>;
53
+ };
54
+
55
+ export type HighlightAutoSlots = {
56
+ default: {
57
+ /**
58
+ * The highlighted HTML as a string.
59
+ * @example "<span>...</span>"
60
+ */
61
+ highlighted: string;
62
+ };
63
+ };
64
+
65
+ export default class HighlightAuto extends SvelteComponentTyped<
66
+ HighlightAutoProps,
67
+ HighlightAutoEvents,
68
+ HighlightAutoSlots
69
+ > {}
@@ -1,23 +1,30 @@
1
- <script context="module"></script>
2
-
3
- <script>import LangTag from "./LangTag.svelte";
4
- export let code;
5
- export let langtag = false;
6
- import hljs from "highlight.js/lib/core";
7
- import xml from "highlight.js/lib/languages/xml";
8
- import javascript from "highlight.js/lib/languages/javascript";
9
- import css from "highlight.js/lib/languages/css";
10
- import { createEventDispatcher, afterUpdate } from "svelte";
11
- const dispatch = createEventDispatcher();
12
- hljs.registerLanguage("xml", xml);
13
- hljs.registerLanguage("javascript", javascript);
14
- hljs.registerLanguage("css", css);
15
- afterUpdate(() => {
16
- if (highlighted)
17
- dispatch("highlight", { highlighted });
18
- });
19
- $:
20
- highlighted = hljs.highlightAuto(code).value;
1
+ <script>
2
+ // @ts-check
3
+ import LangTag from "./LangTag.svelte";
4
+
5
+ /** @type {any} */
6
+ export let code;
7
+
8
+ /** @type {boolean} */
9
+ export let langtag = false;
10
+
11
+ import hljs from "highlight.js/lib/core";
12
+ import xml from "highlight.js/lib/languages/xml";
13
+ import javascript from "highlight.js/lib/languages/javascript";
14
+ import css from "highlight.js/lib/languages/css";
15
+ import { createEventDispatcher, afterUpdate } from "svelte";
16
+
17
+ const dispatch = createEventDispatcher();
18
+
19
+ hljs.registerLanguage("xml", xml);
20
+ hljs.registerLanguage("javascript", javascript);
21
+ hljs.registerLanguage("css", css);
22
+
23
+ afterUpdate(() => {
24
+ if (highlighted) dispatch("highlight", { highlighted });
25
+ });
26
+
27
+ $: highlighted = hljs.highlightAuto(code).value;
21
28
  </script>
22
29
 
23
30
  <slot {highlighted}>
@@ -1,13 +1,63 @@
1
- import { SvelteComponentTyped } from "svelte";
2
- import type { Props, Slots, Events } from "./shared";
3
- declare const __propDef: {
4
- props: Omit<Props, "language">;
5
- slots: Slots;
6
- events: Events<import("./shared").HighlightedCode>;
1
+ import type { SvelteComponentTyped } from "svelte";
2
+ import type { HTMLAttributes } from "svelte/elements";
3
+
4
+ export type HighlightSvelteProps = HTMLAttributes<HTMLPreElement> & {
5
+ /**
6
+ * Specify the text to highlight.
7
+ */
8
+ code: any;
9
+
10
+ /**
11
+ * Set to `true` for the language name to be
12
+ * displayed at the top right of the code block.
13
+ *
14
+ * Use style props to customize styles:
15
+ * - `--langtag-background`
16
+ * - `--langtag-color`
17
+ * - `--langtag-border-radius`
18
+ *
19
+ * @default false
20
+ */
21
+ langtag?: boolean;
22
+
23
+ /**
24
+ * Customize the background color of the langtag.
25
+ */
26
+ "--langtag-background"?: string;
27
+
28
+ /**
29
+ * Customize the text color of the langtag.
30
+ */
31
+ "--langtag-color"?: string;
32
+
33
+ /**
34
+ * Customize the border radius of the langtag.
35
+ */
36
+ "--langtag-border-radius"?: string;
7
37
  };
8
- export type HighlightSvelteProps = typeof __propDef.props;
9
- export type HighlightSvelteEvents = typeof __propDef.events;
10
- export type HighlightSvelteSlots = typeof __propDef.slots;
11
- export default class HighlightSvelte extends SvelteComponentTyped<HighlightSvelteProps, HighlightSvelteEvents, HighlightSvelteSlots> {
12
- }
13
- export {};
38
+
39
+ export type HighlightSvelteEvents = {
40
+ highlight: CustomEvent<{
41
+ /**
42
+ * The highlighted HTML as a string.
43
+ * @example "<span>...</span>"
44
+ */
45
+ highlighted: string;
46
+ }>;
47
+ };
48
+
49
+ export type HighlightSvelteSlots = {
50
+ default: {
51
+ /**
52
+ * The highlighted HTML as a string.
53
+ * @example "<span>...</span>"
54
+ */
55
+ highlighted: string;
56
+ };
57
+ };
58
+
59
+ export default class HighlightSvelte extends SvelteComponentTyped<
60
+ HighlightSvelteProps,
61
+ HighlightSvelteEvents,
62
+ HighlightSvelteSlots
63
+ > {}
package/LangTag.svelte CHANGED
@@ -1,9 +1,17 @@
1
- <script context="module"></script>
1
+ <script>
2
+ // @ts-check
2
3
 
3
- <script>export let langtag = false;
4
- export let highlighted;
5
- export let code;
6
- export let languageName = "plaintext";
4
+ /** @type {any} */
5
+ export let code;
6
+
7
+ /** @type {string} */
8
+ export let highlighted;
9
+
10
+ /** @type {string} */
11
+ export let languageName = "plaintext";
12
+
13
+ /** @type {boolean} */
14
+ export let langtag = false;
7
15
  </script>
8
16
 
9
17
  <pre class:langtag data-language={languageName} {...$$restProps}><code
@@ -1,25 +1,43 @@
1
- import { SvelteComponentTyped } from "svelte";
2
- import type { Props } from "./shared";
3
- declare const __propDef: {
4
- props: Omit<Props, "language"> & {
5
- /**
6
- * Specify the highlighted code.
7
- */
8
- highlighted: string | undefined;
9
- /**
10
- * Specifiy the language name.
11
- * @default "plaintext"
12
- */
13
- languageName?: string;
14
- };
15
- events: {
16
- [evt: string]: CustomEvent<any>;
17
- };
18
- slots: {};
1
+ import type { SvelteComponentTyped } from "svelte";
2
+ import type { HTMLAttributes } from "svelte/elements";
3
+
4
+ export type LangTagProps = HTMLAttributes<HTMLPreElement> & {
5
+ /**
6
+ * Specify the text to highlight.
7
+ */
8
+ code: any;
9
+
10
+ /**
11
+ * Provide the highlighted code.
12
+ */
13
+ highlighted: string;
14
+
15
+ /**
16
+ * Provide the language name.
17
+ * @default "plaintext"
18
+ */
19
+ languageName?: string;
20
+
21
+ /**
22
+ * Set to `true` for the language name to be
23
+ * displayed at the top right of the code block.
24
+ *
25
+ * Use style props to customize styles:
26
+ * - `--langtag-background`
27
+ * - `--langtag-color`
28
+ * - `--langtag-border-radius`
29
+ *
30
+ * @default false
31
+ */
32
+ langtag?: boolean;
19
33
  };
20
- export type LangTagProps = typeof __propDef.props;
21
- export type LangTagEvents = typeof __propDef.events;
22
- export type LangTagSlots = typeof __propDef.slots;
23
- export default class LangTag extends SvelteComponentTyped<LangTagProps, LangTagEvents, LangTagSlots> {
24
- }
25
- export {};
34
+
35
+ export type LangTagEvents = {};
36
+
37
+ export type LangTagSlots = {};
38
+
39
+ export default class LangTag extends SvelteComponentTyped<
40
+ LangTagProps,
41
+ LangTagEvents,
42
+ LangTagSlots
43
+ > {}
@@ -1,19 +1,29 @@
1
- <script>export let highlighted;
2
- export let hideBorder = false;
3
- export let wrapLines = false;
4
- export let startingLineNumber = 1;
5
- export let highlightedLines = [];
6
- const DIGIT_WIDTH = 12;
7
- const MIN_DIGITS = 2;
8
- const HIGHLIGHTED_BACKGROUND = "rgba(254, 241, 96, 0.2)";
9
- $:
10
- lines = highlighted.split("\n");
11
- $:
12
- len_digits = lines.length.toString().length;
13
- $:
14
- len = len_digits - MIN_DIGITS < 1 ? MIN_DIGITS : len_digits;
15
- $:
16
- width = len * DIGIT_WIDTH;
1
+ <script>
2
+ // @ts-check
3
+
4
+ /** @type {string} */
5
+ export let highlighted;
6
+
7
+ /** @type {boolean} */
8
+ export let hideBorder = false;
9
+
10
+ /** @type {boolean} */
11
+ export let wrapLines = false;
12
+
13
+ /** @type {number} */
14
+ export let startingLineNumber = 1;
15
+
16
+ /** @type {number[]} */
17
+ export let highlightedLines = [];
18
+
19
+ const DIGIT_WIDTH = 12;
20
+ const MIN_DIGITS = 2;
21
+ const HIGHLIGHTED_BACKGROUND = "rgba(254, 241, 96, 0.2)";
22
+
23
+ $: lines = highlighted.split("\n");
24
+ $: len_digits = lines.length.toString().length;
25
+ $: len = len_digits - MIN_DIGITS < 1 ? MIN_DIGITS : len_digits;
26
+ $: width = len * DIGIT_WIDTH;
17
27
  </script>
18
28
 
19
29
  <div style:overflow-x="auto" {...$$restProps}>