Skip to content

Commit

Permalink
feat(highlight-auto): include language in "on:highlight" event deta…
Browse files Browse the repository at this point in the history
…il (#252)

* feat(highlight-auto): include `language` in "on:highlight" event detail

* fix: `highlighted` in `e.detail` should be a string
  • Loading branch information
metonym authored Dec 29, 2022
1 parent eb402c3 commit 2c82ca3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,11 @@ In the example below, the `HighlightAuto` component and injected styles are dyna
language={typescript}
{code}
on:highlight={(e) => {
console.log(e.detail.highlighted); // "<span>...</span>"
/**
* The highlighted HTML as a string.
* @example "<span>...</span>"
*/
console.log(e.detail.highlighted);
}}
/>
```
Expand Down Expand Up @@ -391,7 +395,11 @@ In the example below, the `HighlightAuto` component and injected styles are dyna
<HighlightSvelte
{code}
on:highlight={(e) => {
console.log(e.detail.highlighted); // "<span>...</span>"
/**
* The highlighted HTML as a string.
* @example "<span>...</span>"
*/
console.log(e.detail.highlighted);
}}
/>
```
Expand All @@ -415,7 +423,17 @@ In the example below, the `HighlightAuto` component and injected styles are dyna
<HighlightAuto
{code}
on:highlight={(e) => {
console.log(e.detail.highlighted); // "<span>...</span>"
/**
* 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);
}}
/>
```
Expand Down
14 changes: 7 additions & 7 deletions src/Highlight.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<script context="module" lang="ts">
import type { LanguageFn } from "highlight.js";
export type HighlightedCode = undefined | string;
export interface Language {
name?: string;
register: LanguageFn;
Expand All @@ -11,18 +9,20 @@
export interface Slots {
default: {
/**
* The highlighted code.
* The highlighted HTML as a string.
* @example "<span>...</span>"
*/
highlighted: HighlightedCode;
highlighted: string;
};
}
export interface Events {
highlight: CustomEvent<{
/**
* The highlighted code.
* The highlighted HTML as a string.
* @example "<span>...</span>"
*/
highlighted?: HighlightedCode;
highlighted: string;
}>;
}
</script>
Expand Down Expand Up @@ -88,7 +88,7 @@
const dispatch = createEventDispatcher();
let highlighted: HighlightedCode = undefined;
let highlighted = "";
afterUpdate(() => {
if (highlighted) dispatch("highlight", { highlighted });
Expand Down
22 changes: 18 additions & 4 deletions src/HighlightAuto.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script context="module" lang="ts">
import type { HighlightedCode, Slots, Events } from "./Highlight.svelte";
import type { Slots } from "./Highlight.svelte";
</script>

<script lang="ts">
Expand Down Expand Up @@ -42,7 +42,21 @@
interface $$Slots extends Slots {}
interface $$Events extends Events {}
interface $$Events {
highlight: CustomEvent<{
/**
* The highlighted HTML as a string.
* @example "<span>...</span>"
*/
highlighted: string;
/**
* The inferred language name.
* @example "css"
*/
language?: string;
}>;
}
export let code;
Expand All @@ -53,11 +67,11 @@
const dispatch = createEventDispatcher();
let highlighted: HighlightedCode = undefined;
let highlighted = "";
let language = undefined;
afterUpdate(() => {
if (highlighted) dispatch("highlight", { highlighted });
if (highlighted) dispatch("highlight", { highlighted, language });
});
$: ({ value: highlighted, language } = hljs.highlightAuto(code));
Expand Down
6 changes: 6 additions & 0 deletions tests/SvelteHighlight.test.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
let toggled = true;
let highlighted = "";
let language = "";
$: code = "const add = (a: number, b: number) => a + b;";
$: codeSvelte = "<button on:click>Click me</button>";
Expand Down Expand Up @@ -39,8 +40,13 @@
id="highlight-auto-css"
code={`body {\n padding: 0;\n color: red;\n}`}
langtag={true}
on:highlight={(e) => {
language = e.detail.language;
}}
/>

<div id="inferred-language">{language}</div>

<Highlight
language={toggled ? javascript : typescript}
{code}
Expand Down
3 changes: 3 additions & 0 deletions tests/SvelteHighlight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ describe("SvelteHighlight", () => {
<span class=\\"hljs-attribute\\">color</span>: red;
}</code></pre>"
`);
expect(target.querySelector("#inferred-language")?.innerHTML).toEqual(
"css"
);

await userEvent.click(target.querySelector("button")!);

Expand Down

0 comments on commit 2c82ca3

Please sign in to comment.