
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
svelte-highlight
Advanced tools
Syntax highlighting for Svelte using highlight.js.
Try it in the Svelte REPL.
# Yarn
yarn add -D svelte-highlight
# npm
npm i -D svelte-highlight
# pnpm
pnpm i -D svelte-highlight highlight.js
Note that pnpm users must also install highlight.js
as a development dependency.
To use this library with SvelteKit or vite, instruct vite to optimize highlight.js
and highlight.js/lib/core
:
# svelte.config.js
export default {
kit: {
+ vite: {
+ optimizeDeps: {
+ include: ["highlight.js", "highlight.js/lib/core"],
+ },
+ },
},
};
Refer to examples/sveltekit for an example set-up.
There are two ways to apply highlight.js
styles:
svelte:head
This component exports highlight.js
themes in JavaScript. Import the theme from svelte-highlight/styles
and inject it using the svelte:head API.
<script>
import Highlight from "svelte-highlight";
import typescript from "svelte-highlight/languages/typescript";
import github from "svelte-highlight/styles/github";
const code = "const add = (a: number, b: number) => a + b;";
</script>
<svelte:head>
{@html github}
</svelte:head>
<Highlight language={typescript} {code} />
Depending on your set-up, importing a CSS StyleSheet in Svelte may require a CSS file loader. Refer to examples/webpack for a sample set-up.
<script>
import { Highlight } from "svelte-highlight";
import typescript from "svelte-highlight/languages/typescript";
import "svelte-highlight/styles/github.css";
const code = "const add = (a: number, b: number) => a + b;";
</script>
<Highlight language={typescript} {code} />
Use the HighlightSvelte
component for Svelte syntax highlighting.
<script>
import { HighlightSvelte } from "svelte-highlight";
import github from "svelte-highlight/styles/github";
$: code = `<button on:click={() => { console.log(0); }}>Increment {count}</button>`;
</script>
<svelte:head>
{@html github}
</svelte:head>
<HighlightSvelte {code} />
The HighlightAuto
component invokes the highlightAuto
API from highlight.js
.
Note that auto-highlighting will result in a larger bundle size in order to infer a language.
Prefer to specify a language if possible.
<script>
import { HighlightAuto } from "svelte-highlight";
import github from "svelte-highlight/styles/github";
$: code = `body {\n padding: 0;\n color: red;\n}`;
</script>
<svelte:head>
{@html github}
</svelte:head>
<HighlightAuto {code} />
All Highlight
components apply a data-language
attribute on the codeblock containing the language name.
This is also compatible with custom languages.
See the Languages page for a list of supported languages.
pre[data-language="css"] {
/* custom style rules */
}
All Highlight
components allow for a tag to be added at the top-right of the codeblock displaying the language name.
The language tag can be given a custom background
, color
, and border-radius
through the custom properties shown.
This is also compatible with custom languages.
It is recommended that you set values for --hljs-background
and --hljs-foreground
to ensure the langtags remain readable on any theme.
See the Languages page for a list of supported languages.
Defaults:
--hljs-background: inherit
--hljs-foreground: inherit
--hljs-border-radius: 0
<script>
import { HighlightAuto } from "svelte-highlight";
$: code = `.body { padding: 0; margin: 0; }`;
</script>
<HighlightAuto {code} langtag />
pre[data-language="css"] {
--hljs-background: linear-gradient(135deg, #2996cf, 80%, white);
--hljs-foreground: #fff;
--hljs-radius: 8px;
}
For custom language highlighting, pass a name
and register
function to the language prop.
Refer to the highlight.js language definition guide for guidance.
<script>
import { Highlight } from "svelte-highlight";
import hljs from "highlight.js";
const language = {
name: "custom-language",
register: (hljs) => {
return {
/** custom language rules */
};
},
};
</script>
<Highlight {language} code="..." />
Name | Type | Default value |
---|---|---|
code | any | undefined |
language (only required for Highlight.svelte ) | { name: string ; register: hljs => object } | { name: undefined, register: undefined } |
langtag | boolean | false |
$$restProps
are forwarded to the pre
element<Highlight
language={typescript}
{code}
on:highlight={(e) => {
console.log(e.detail.highlighted); // "<span>...</span>"
}}
/>
Svelte version 3.31 or greater is required to use this component with TypeScript.
TypeScript definitions are auto-generated by SvelteKit.
6.1.0 - 2022-06-10
Features
Documentation
FAQs
Svelte component library for highlighting code using highlight.js
The npm package svelte-highlight receives a total of 9,464 weekly downloads. As such, svelte-highlight popularity was classified as popular.
We found that svelte-highlight demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.