Skip to content

Commit 7e8a347

Browse files
committed
feat(highlight-auto): support subset of languages
Closes #383
1 parent 28435b5 commit 7e8a347

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

src/HighlightAuto.svelte

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
/** @type {any} */
55
export let code;
66
7+
/** @type {string[] | undefined} */
8+
export let languages = undefined;
9+
710
/** @type {boolean} */
811
export let langtag = false;
912
@@ -26,7 +29,10 @@
2629
if (highlighted) dispatch("highlight", { highlighted, language });
2730
});
2831
29-
$: ({ value: highlighted, language = "" } = hljs.highlightAuto(code));
32+
$: ({ value: highlighted, language = "" } = hljs.highlightAuto(
33+
code,
34+
languages,
35+
));
3036
</script>
3137

3238
<slot {highlighted}>

src/HighlightAuto.svelte.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import type { SvelteComponentTyped } from "svelte";
22
import type { HTMLAttributes } from "svelte/elements";
33
import type { LangtagProps } from "./Highlight.svelte";
4+
import type { LanguageName } from "./languages";
45

56
export type HighlightAutoProps = HTMLAttributes<HTMLPreElement> &
67
LangtagProps & {
78
/**
89
* Specify the text to highlight.
910
*/
1011
code: any;
12+
13+
/**
14+
* Specify an array of language names and aliases to restrict language detection.
15+
* @example ["javascript", "typescript", "python"]
16+
*/
17+
languages?: LanguageName[];
1118
};
1219

1320
export type HighlightAutoEvents = {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script lang="ts">
2+
import { HighlightAuto } from "svelte-highlight";
3+
import atomOneDark from "svelte-highlight/styles/atom-one-dark";
4+
5+
let code = "const x = 42;";
6+
</script>
7+
8+
<svelte:head>
9+
{@html atomOneDark}
10+
</svelte:head>
11+
12+
<HighlightAuto {code} languages={["javascript", "typescript"]} langtag />

tests/e2e/e2e.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expect, test } from "@playwright/experimental-ct-svelte";
22
import Highlight from "./Highlight.test.svelte";
33
import HighlightAuto from "./HighlightAuto.test.svelte";
4+
import HighlightAutoLanguageRestriction from "./HighlightAuto.languageRestriction.test.svelte";
45
import LineNumbersCustomStartingLine from "./LineNumbers.customStartingLine.test.svelte";
56
import LineNumbersHideBorder from "./LineNumbers.hideBorder.test.svelte";
67
import LineNumbers from "./LineNumbers.test.svelte";
@@ -94,3 +95,17 @@ test("Auto-highlighting detects language", async ({ mount, page }) => {
9495
await expect(page.locator(".hljs-selector-tag")).toBeVisible();
9596
await expect(page.locator("pre")).toHaveAttribute("data-language", "css");
9697
});
98+
99+
test("Auto-highlighting with language restriction", async ({ mount, page }) => {
100+
await mount(HighlightAutoLanguageRestriction);
101+
102+
// Should detect as javascript since we restricted to ["javascript", "typescript"]
103+
await expect(page.locator("pre")).toHaveAttribute(
104+
"data-language",
105+
"javascript",
106+
);
107+
108+
// Should have JavaScript highlighting
109+
await expect(page.locator(".hljs-keyword")).toBeVisible();
110+
await expect(page.locator(".hljs-number")).toHaveText("42");
111+
});

0 commit comments

Comments
 (0)