Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat!: remove dispatched highlight event #346

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 0 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,24 +382,6 @@ In the example below, the `HighlightAuto` component and injected styles are dyna

`$$restProps` are forwarded to the top-level `pre` element.

#### Dispatched Events

- **on:highlight**: fired after `code` is highlighted

```svelte
<Highlight
language={typescript}
{code}
on:highlight={(e) => {
/**
* The highlighted HTML as a string.
* @example "<span>...</span>"
*/
console.log(e.detail.highlighted);
}}
/>
```

### `LineNumbers`

#### Props
Expand All @@ -425,23 +407,6 @@ In the example below, the `HighlightAuto` component and injected styles are dyna

`$$restProps` are forwarded to the top-level `pre` element.

#### Dispatched Events

- **on:highlight**: fired after `code` is highlighted

```svelte
<HighlightSvelte
{code}
on:highlight={(e) => {
/**
* The highlighted HTML as a string.
* @example "<span>...</span>"
*/
console.log(e.detail.highlighted);
}}
/>
```

### `HighlightAuto`

#### Props
Expand All @@ -453,29 +418,6 @@ In the example below, the `HighlightAuto` component and injected styles are dyna

`$$restProps` are forwarded to the top-level `pre` element.

#### Dispatched Events

- **on:highlight**: fired after `code` is highlighted

```svelte
<HighlightAuto
{code}
on:highlight={(e) => {
/**
* The highlighted HTML as a string.
* @example "<span>...</span>"
*/
console.log(e.detail.highlighted);

/**
* The inferred language name
* @example "css"
*/
console.log(e.detail.language);
}}
/>
```

## [Supported Languages](SUPPORTED_LANGUAGES.md)

## [Supported Styles](SUPPORTED_STYLES.md)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"test:unit": "bun test tests/*.ts",
"test:e2e": "bunx playwright test",
"format": "bun --bun prettier --write . --cache",
"upgrade-examples": "bun scripts/upgrade-examples.ts",
"astro": "astro",
"playwright": "playwright"
},
Expand Down
11 changes: 11 additions & 0 deletions scripts/upgrade-examples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { $, Glob } from "bun";

const dirs = new Glob("*").scanSync({
cwd: "examples",
onlyFiles: false,
absolute: true,
});

for await (const dir of dirs) {
await $`cd ${dir} && bun install`;
}
7 changes: 0 additions & 7 deletions src/Highlight.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,11 @@
export let langtag = false;

import hljs from "highlight.js/lib/core";
import { createEventDispatcher, afterUpdate } from "svelte";
import LangTag from "./LangTag.svelte";

const dispatch = createEventDispatcher();

/** @type {string} */
let highlighted = "";

afterUpdate(() => {
if (highlighted) dispatch("highlight", { highlighted });
});

$: {
hljs.registerLanguage(language.name, language.register);
highlighted = hljs.highlight(code, { language: language.name }).value;
Expand Down
12 changes: 1 addition & 11 deletions src/Highlight.svelte.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,6 @@ export type HighlightProps = HTMLAttributes<HTMLPreElement> &
language: LanguageType<string>;
};

export type HighlightEvents = {
highlight: CustomEvent<{
/**
* The highlighted HTML as a string.
* @example "<span>...</span>"
*/
highlighted: string;
}>;
};

export type HighlightSlots = {
default: {
/**
Expand All @@ -94,6 +84,6 @@ export type HighlightSlots = {

export default class Highlight extends SvelteComponentTyped<
HighlightProps,
HighlightEvents,
{},
HighlightSlots
> {}
11 changes: 0 additions & 11 deletions src/HighlightAuto.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,13 @@
export let langtag = false;

import hljs from "highlight.js";
import { createEventDispatcher, afterUpdate } from "svelte";

/**
* @typedef {{ highlighted: string; language: string; }} HighlightEventDetail
* @type {import("svelte").EventDispatcher<{ highlight: HighlightEventDetail}>}
*/
const dispatch = createEventDispatcher();

/** @type {string} */
let highlighted = "";

/** @type {string} */
let language = "";

afterUpdate(() => {
if (highlighted) dispatch("highlight", { highlighted, language });
});

$: ({ value: highlighted, language = "" } = hljs.highlightAuto(code));
</script>

Expand Down
18 changes: 1 addition & 17 deletions src/HighlightAuto.svelte.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,6 @@ export type HighlightAutoProps = HTMLAttributes<HTMLPreElement> &
code: any;
};

export type HighlightAutoEvents = {
highlight: CustomEvent<{
/**
* The highlighted HTML as a string.
* @example "<span>...</span>"
*/
highlighted: string;

/**
* The language name inferred by `highlight.js`.
* @example "css"
*/
language: string;
}>;
};

export type HighlightAutoSlots = {
default: {
/**
Expand All @@ -38,6 +22,6 @@ export type HighlightAutoSlots = {

export default class HighlightAuto extends SvelteComponentTyped<
HighlightAutoProps,
HighlightAutoEvents,
{},
HighlightAutoSlots
> {}
7 changes: 0 additions & 7 deletions src/HighlightSvelte.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,11 @@
import xml from "highlight.js/lib/languages/xml";
import javascript from "highlight.js/lib/languages/javascript";
import css from "highlight.js/lib/languages/css";
import { createEventDispatcher, afterUpdate } from "svelte";

const dispatch = createEventDispatcher();

hljs.registerLanguage("xml", xml);
hljs.registerLanguage("javascript", javascript);
hljs.registerLanguage("css", css);

afterUpdate(() => {
if (highlighted) dispatch("highlight", { highlighted });
});

$: highlighted = hljs.highlightAuto(code).value;
</script>

Expand Down
12 changes: 1 addition & 11 deletions src/HighlightSvelte.svelte.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ export type HighlightSvelteProps = HTMLAttributes<HTMLPreElement> &
code: any;
};

export type HighlightSvelteEvents = {
highlight: CustomEvent<{
/**
* The highlighted HTML as a string.
* @example "<span>...</span>"
*/
highlighted: string;
}>;
};

export type HighlightSvelteSlots = {
default: {
/**
Expand All @@ -32,6 +22,6 @@ export type HighlightSvelteSlots = {

export default class HighlightSvelte extends SvelteComponentTyped<
HighlightSvelteProps,
HighlightSvelteEvents,
{},
HighlightSvelteSlots
> {}
10 changes: 1 addition & 9 deletions src/LangTag.svelte.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,4 @@ export type LangTagProps = HTMLAttributes<HTMLPreElement> &
languageName?: string;
};

export type LangTagEvents = {};

export type LangTagSlots = {};

export default class LangTag extends SvelteComponentTyped<
LangTagProps,
LangTagEvents,
LangTagSlots
> {}
export default class LangTag extends SvelteComponentTyped<LangTagProps> {}
10 changes: 1 addition & 9 deletions src/LineNumbers.svelte.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,4 @@ export type LineNumbersProps = HTMLAttributes<HTMLPreElement> & {
"--highlighted-background"?: string;
};

export type LineNumbersEvents = {};

export type LineNumbersSlots = {};

export default class LineNumbers extends SvelteComponentTyped<
LineNumbersProps,
LineNumbersEvents,
LineNumbersSlots
> {}
export default class LineNumbers extends SvelteComponentTyped<LineNumbersProps> {}
10 changes: 1 addition & 9 deletions tests/SvelteHighlightPackage.test.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
class=""
code=""
language={javascript || typescript || typescriptDefault || ts}
on:highlight={(e) => {
console.log(e.detail);
}}
let:highlighted
>
{@html highlighted}
Expand All @@ -41,12 +38,7 @@
/>
</Highlight>

<HighlightAuto
code=""
on:highlight={(e) => {
console.log(e.detail.language);
}}
/>
<HighlightAuto code="" />

<Highlight
code=""
Expand Down