Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

svelte-highlight

metonym96.6kMIT7.9.0TypeScript support: included

Svelte component library for highlighting code using highlight.js

svelte, svelte components, highlight.js, syntax highlight

readme

svelte-highlight

NPM npm

Syntax highlighting for Svelte using highlight.js.

Try it in the Svelte REPL or StackBlitz.

Documentation

The Changelog is available on GitHub.

Installation

# npm
npm i svelte-highlight

# pnpm
pnpm i svelte-highlight

# Bun
bun i svelte-highlight

# Yarn
yarn add svelte-highlight

Basic Usage

The Highlight component requires two props:

  • code: text to highlight
  • language: language grammar used to highlight the text

Import languages from svelte-highlight/languages.

See SUPPORTED_LANGUAGES.md for a list of supported languages.

<script>
  import Highlight from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";

  const code = "const add = (a: number, b: number) => a + b;";
</script>

<Highlight language={typescript} {code} />

Styling

Import styles from svelte-highlight/styles. See SUPPORTED_STYLES.md for a list of supported styles.

There are two ways to apply highlight.js styles.

  1. Injected styles through svelte:head
  2. CSS StyleSheets

Injected Styles

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} />

CSS StyleSheet

Depending on your set-up, importing a CSS StyleSheet in Svelte may require a CSS file loader. SvelteKit/Vite automatically supports this. For Webpack, refer to examples/webpack.

<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} />

Linking from a CDN

CSS StyleSheets can also be externally linked from a Content Delivery Network (CDN) like unpkg.com.

[!WARNING] Using a CDN is best suited for prototyping and not recommended for production use.

HTML

<head>
  <link
    rel="stylesheet"
    href="https://unpkg.com/svelte-highlight/styles/github.css"
  />
</head>

svelte:head

<svelte:head>
  <link
    rel="stylesheet"
    href="https://unpkg.com/svelte-highlight/styles/github.css"
  />
</svelte:head>

Svelte Syntax Highlighting

Use the HighlightSvelte component for Svelte syntax highlighting.

<script>
  import { HighlightSvelte } from "svelte-highlight";
  import github from "svelte-highlight/styles/github";

  const code = `<button on:click={() => { console.log(0); }}>Increment {count}</button>`;
</script>

<svelte:head>
  {@html github}
</svelte:head>

<HighlightSvelte {code} />

Auto-highlighting

The HighlightAuto component uses the highlightAuto API and attempts to guess what grammar to use based on the provided code.

[!WARNING] Auto-highlighting will result in a larger bundle size. Specify a language if possible.

<script>
  import { HighlightAuto } from "svelte-highlight";
  import github from "svelte-highlight/styles/github";

  const code = `body {\n  padding: 0;\n  color: red;\n}`;
</script>

<svelte:head>
  {@html github}
</svelte:head>

<HighlightAuto {code} />

Limiting Language Detection

You can restrict language auto-detection to a subset using the languageNames prop. This can improve performance and accuracy.

<script>
  import { HighlightAuto } from "svelte-highlight";
  import github from "svelte-highlight/styles/github";

  const code = "const x = 42;";
</script>

<svelte:head>
  {@html github}
</svelte:head>

<HighlightAuto {code} languageNames={["javascript", "typescript"]} />

Line Numbers

Use the LineNumbers component to render the highlighted code with line numbers.

<script>
  import Highlight, { LineNumbers } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import atomOneDark from "svelte-highlight/styles/atom-one-dark";

  const code = "const add = (a: number, b: number) => a + b";
</script>

<svelte:head>
  {@html atomOneDark}
</svelte:head>

<Highlight language={typescript} {code} let:highlighted>
  <LineNumbers {highlighted} />
</Highlight>

Hidden Border

Set hideBorder to true to hide the border of the line numbers column.

<Highlight language={typescript} {code} let:highlighted>
  <LineNumbers {highlighted} hideBorder />
</Highlight>

Wrapped Lines

By default, overflowing horizontal content is contained by a scrollbar.

Set wrapLines to true to hide the border of the line numbers column.

<Highlight language={typescript} {code} let:highlighted>
  <LineNumbers {highlighted} wrapLines />
</Highlight>

Custom Starting Line Number

The line number starts at 1. Customize this via the startingLineNumber prop.

<Highlight language={typescript} {code} let:highlighted>
  <LineNumbers {highlighted} startingLineNumber={42} />
</Highlight>

Highlighted Lines

Specify the lines to highlight using the highlightedLines prop. Indices start at zero.

Use --highlighted-background to customize the background color of highlighted lines.

<Highlight language={typescript} {code} let:highlighted>
  <LineNumbers
    {highlighted}
    highlightedLines={[0, 2, 3, 14]}
    --highlighted-background="#000"
  />
</Highlight>

Custom Styles

Use --style-props to customize styles.

Style prop Description Default value
--line-number-color Text color of the line numbers currentColor
--border-color Border color of the column of line numbers currentColor
--padding-left Left padding for td elements 1em
--padding-right Right padding for td elements 1em
--highlighted-background Background color of highlighted lines rgba(254, 241, 96, 0.2)
<Highlight language={typescript} {code} let:highlighted>
  <LineNumbers
    {highlighted}
    --line-number-color="pink"
    --border-color="rgba(255, 255, 255, 0.2)"
    --padding-left={0}
    --padding-right="3em"
    --highlighted-background="#000"
  />
</Highlight>

Language Targeting

All Highlight components apply a data-language attribute on the codeblock containing the language name.

See SUPPORTED_LANGUAGES.md for a list of supported languages.

[data-language="css"] {
  /* custom style rules */
}

Language Tags

Set langtag to true to display the language name in the top right corner of the code block.

Customize the language tag background, color, and border-radius using style props.

See the Languages page for a list of supported languages.

Style prop Description Default value
--langtag-top Top position of the langtag 0
--langtag-right Right position of the langtag 0
--langtag-background Background color of the langtag inherit
--langtag-color Text color of the langtag inherit
--langtag-border-radius Border radius of the langtag 0
--langtag-padding Padding of the langtag 1em
<script>
  import { HighlightAuto } from "svelte-highlight";

  $: code = `.body { padding: 0; margin: 0; }`;
</script>

<HighlightAuto {code} langtag />
<HighlightAuto
  {code}
  langtag
  --langtag-background="linear-gradient(135deg, #2996cf, 80%, white)"
  --langtag-color="#fff"
  --langtag-border-radius="6px"
  --langtag-padding="0.5rem"
/>

Custom Language

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 */
        contains: [],
      };
    },
  };
</script>

<Highlight {language} code="..." />

If you're using TypeScript, use the LanguageType interface to type the language.

import type { LanguageType } from "svelte-highlight";

const language: LanguageType<"custom-language"> = {
  name: "custom-language",
  register: (hljs) => {
    return {
      /** custom language rules */
      contains: [],
    };
  },
};

Custom Plugin

Additional plugin languages can be installed and used separately.

This example uses the cURL language plugin.

<script>
  import { Highlight } from "svelte-highlight";
  import curl from "highlightjs-curl";
  import github from "svelte-highlight/styles/github";

  const language = {
    name: "curl", // Language name displayed in the langtag
    register: curl,
  };

  const code = `curl -X POST "https://api.example.com/data" \\
     -H "Content-Type: application/json" \\
     -d '{"key": "value"}'`;
</script>

<svelte:head>
  {@html github}
</svelte:head>

<Highlight {language} {code} />

Code-splitting

You can use the await import syntax for code-splitting.

In the example below, the HighlightAuto component and injected styles are dynamically loaded.

<script>
  import { onMount } from "svelte";

  let component;
  let styles;

  onMount(async () => {
    component = (await import("svelte-highlight")).HighlightAuto;
    styles = (await import("svelte-highlight/styles/github")).default;
  });
</script>

<svelte:head>
  {#if styles}
    {@html styles}
  {/if}
</svelte:head>

<svelte:component
  this={component}
  langtag
  code={`body {\n  padding: 0;\n  color: red;\n}`}
/>

Component API

Highlight

Props

Name Type Default value
code any N/A (required)
language { name: string; register: hljs => object } N/A (required)
langtag boolean false

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

Dispatched Events

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

LineNumbers

Props

Name Type Default value
highlighted string N/A (required)
hideBorder boolean false
wrapLines boolean false
startingLineNumber number 1
highlightedLines number[] []

$$restProps are forwarded to the top-level div element.

HighlightSvelte

Props

Name Type Default value
code any N/A (required)
langtag boolean false

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

Dispatched Events

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

HighlightAuto

Props

Name Type Default value
code any N/A (required)
languages LanguageName[] undefined
langtag boolean false

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

Note: LanguageName is a union type of all supported language names, providing autocomplete and type safety. You can import it from svelte-highlight:

import type { LanguageName } from "svelte-highlight";

Dispatched Events

  • on:highlight: fired after code is highlighted
<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 Styles

Examples

By default, example set-ups use Svelte 5. The exception is examples/vite@svelte-4, which uses Svelte 4.

Changelog

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

7.9.0 - 2025-10-21

Features

  • add languageNames prop to HighlightAuto to allow a subset of languages
  • update languageName in LangTag to use LanguageName utility type
  • export LanguageName utility type

7.8.4 - 2025-08-31

Fixes

  • export LanguageType utility type

7.8.3 - 2025-04-01

Fixes

  • remove @ts-check comments

7.8.2 - 2024-12-27

Fixes

  • update docs on pnpm usage

7.8.1 - 2024-12-25

Fixes

  • upgrade highlight.js to version 11.11.1

7.8.0 - 2024-12-15

Features

  • upgrade highlight.js to version 11.11.0

Fixes

  • optimize preprocessed CSS StyleSheets

7.7.0 - 2024-07-15

Features

  • upgrade highlight.js to version 11.10.0

7.6.1 - 2024-05-15

Fixes

  • avoid self-closing tag in LineNumbers to address Svelte 5 warning

7.6.0 - 2024-02-05

Features

  • add --langtag-top, --langtag-right style props

7.5.0 - 2024-02-01

Features

  • add --langtag-padding style prop

7.4.8 - 2024-01-04

Fixes

  • add missing exports to allow importing from svelte-highlight/styles and svelte-highlight/languages

7.4.7 - 2023-12-28

Fixes

  • use declare const for languages and styles type definitions

7.4.6 - 2023-12-16

Fixes

  • fix ./*.svelte mapping; use import specifier instead of svelte

7.4.5 - 2023-12-16

Fixes

  • map ./*.svelte files correctly in package.json exports

7.4.4 - 2023-12-16

Fixes

  • add types to exports map in package.json
  • optimize generated CSS using cssnano

7.4.3 - 2023-12-16

Fixes

  • add svelte entry point to exports map in package.json to address Vite development warning

7.4.2 - 2023-11-25

Fixes

7.4.1 - 2023-10-13

Fixes

  • Re-publish package after correctly building it

7.4.0 - 2023-10-13

Features

  • upgrade highlight.js to version 11.9.0

7.3.0 - 2023-05-13

Features

  • upgrade highlight.js to version 11.8.0

7.2.1 - 2023-04-01

Fixes

  • line numbers should not be covered by a solid fill, custom line background color
  • line numbers column should not be covered by code if scrollable

7.2.0 - 2023-02-14

Features

  • support highlighted lines in LineNumbers through the highlightedLines prop

7.1.2 - 2023-01-01

Fixes

  • correctly set top/bottom padding in LineNumbers if code is updated

7.1.1 - 2022-12-31

Fixes

  • republish package

7.1.0 - 2022-12-31

Features

  • add startingLineNumber prop to LineNumbers

7.0.1 - 2022-12-30

Fixes

  • reset table styles in LineNumbers

7.0.0 - 2022-12-30

Breaking Changes

  • rename langtag style props
    • --hljs-background -> --langtag-background
    • --hljs-foreground -> --langtag-color
    • --hljs-radius -> --langtag-border-radius
  • mark code, language as required props

Features

  • include inferred language in on:highlight event detail in HighlightAuto
  • add LineNumbers component to support rendering line numbers
  • upgrade highlight.js to version 11.7.0

Fixes

  • use HTMLAttributes from svelte/elements to correctly type $$restProps

6.2.1 - 2022-08-07

Fixes

  • generate types for svelte-highlight/languages/*, svelte-highlight/styles/*

Documentation

  • add dynamic import example
  • document CDN styles usage
  • use TypeScript in all example set-ups
  • remove Snowpack example as it is no longer maintained
  • update set-up instructions for SvelteKit/Vite

6.2.0 - 2022-07-13

Features

  • upgrade highlight.js to version 11.6.0 (net +2 styles)

6.1.1 - 2022-07-02

Fixes

  • remove language prop type from HighlightAuto TypeScript definitions

6.1.1 - 2022-06-27

Refactor

  • remove unnecessary language null check when setting data-language attribute
  • reduce .langtag CSS specificity

6.1.0 - 2022-06-10

Features

  • port Svelte components to TypeScript

Documentation

  • fix prop table types and default values

6.0.1 - 2022-03-26

Fixes

  • update generated SUPPORTED_LANGUAGES.md and SUPPORTED_STYLES.md

6.0.0 - 2022-03-26

Breaking Changes

  • use SvelteKit to package the library and auto-generate TypeScript definitions
    • styles are moved from src/styles to styles
    • languages are moved from src/languages to languages

5.3.2 - 2022-03-16

  • specify in docs that Vite should optimize both "highlight.js" and "highlight.js/lib/core" if using SvelteKit or Vite

5.3.1 - 2022-03-16

  • use default highlight.js library for HighlightAuto

5.3.0 - 2022-03-12

  • upgrade highlight.js to version 11.5.0 (net +1 language, +3 styles)

5.2.2 - 2022-02-25

  • use default export from highlight.js/lib/core.js

5.2.1 - 2022-02-11

  • remove whitespace from pretags because Svelte v3.46.4 now preservespre whitespace

5.2.0 - 2022-01-06

  • upgrade highlight.js to version 11.4.0 (net +1 style)

5.1.4 - 2021-12-18

  • recommend installing highlight.js as a dependency for pnpm users

5.1.3 - 2021-11-13

  • patch highlight.js to version 11.3.1

5.1.2 - 2021-10-19

Fixes

  • move TypeScript definitions components to the src/ folder

5.1.1 - 2021-10-19

Fixes

  • emit TypeScript definitions for languages and styles to the src/ folder

5.1.0 - 2021-10-17

Features

  • upgrade highlight.js to v11.3.0

5.0.0 - 2021-10-08

Breaking Changes

  • use .svelte.d.ts extension for TypeScript definitions

4.0.0 - 2021-09-16

Breaking Changes

  • move .hljs class from pre element to code to align with the intended usage of highlight.js
  • change padding of pre.langtag::after to 1em to prevent overpadding

3.4.0 - 2021-09-12

Features

  • add data-language attribute to pre element to allow targeted styling
  • add langtag prop that displays the highlighted language if enabled

3.3.0 - 2021-09-01

Features

  • include named language export in src/languages/* files

3.2.1 - 2021-09-01

Documentation

  • update link to live demo

3.2.0 - 2021-08-02

Features

  • upgrade highlight.js to v11.2.0

3.1.0 - 2021-07-08

Features

  • upgrade highlight.js to v11.1.0

3.0.0 - 2021-05-30

Breaking Changes

  • github-gist style removed from highlight.js

Features

  • upgrade highlight.js to v11.0.0
    • +2 styles (github-dark, github-dark-dimmed)

2.1.0 - 2021-05-21

Features

  • upgrade highlight.js to v11.0.0-beta1
    • +1 language (wren)
    • +1 style (colors)

2.0.0 - 2021-04-30

Breaking Changes

  • upgrade highlight.js to the next major version (v11), which supports ESM
    • removed languages: c-like, htmlbars, sql_more

Features

  • added languages: nestedtext, wasm
  • 143 new styles

2.0.0-rc.1 - 2021-04-30

Fixes

  • omit .js extension when importing highlight.js language
  • sort types/styles/index.d.ts by name

2.0.0-rc.0 - 2021-04-30

Breaking Changes

  • upgrade highlight.js to the next major version (v11), which supports ESM
    • removed languages: c-like, htmlbars, sql_more

Features

  • added languages: nestedtext, wasm
  • 143 new styles

1.0.1 - 2021-04-23

Fixes

  • fix typos in README.md

1.0.0 - 2021-04-23

Breaking Changes

  • upgrade highlight.js to version 10.7
  • remove all forwarded events from Highlight, HighlightSvelte
  • remove legacy component; replace with Highlight.svelte as the default export
  • remove Rollup from build process; only ship Svelte source code and languages/styles as ESM
  • remove highlightjs-svelte dependency; use XML/JavaScript/CSS as sublanguages

Features

  • add HighlightAuto component that auto highlights code
  • major bundle size improvements by using only the core library

Fixes

  • correctly type languages/styles

Other

  • drop Node.js v12 from Travis CI build config

Documentation

  • use SvelteKit instead of Sapper for the documentation/live demo site
  • provide correct NPM install command

0.7.1 - 2021-02-13

Fixes

  • include types folder in published files

0.7.0 - 2021-02-13

Features

  • include highlighted markup in dispatched event detail
  • add TypeScript definitions

Fixes

  • mark CSS styles as side effects

0.6.2 - 2020-05-10

  • Fix "language is undefined" error by falling back to unhighlighted code

0.6.1 - 2020-05-10

  • Refactor build process to reduce bundled package size

0.6.0 - 2020-05-03

  • Export new Highlight, HighlightSvelte components; maintain backwards compatibility with Legacy component

  • Require svelte^3.20.x as a peer dependency due to usage of the $$restProps API

  • Support Svelte syntax highlighting by wrapping highlightjs-svelte

  • Dispatch highlight event

  • Forward the following events to the pre element (on:click, on:mouseover, on:mouseenter, on:mouseleave, on:focus, on:blur)

0.5.0 - 2020-03-27

  • Add editable support by exporting contenteditable, spellcheck props (#105)

0.4.1 - 2020-02-01

  • Bump highlight.js version from 9.17.1 to 9.18.1

0.4.0 - 2019-12-18

  • Pass id, style, class props to pre element (#48)

  • Deprecate darkula style (#49)

0.3.5 - 2019-12-14

  • Use gh-pages to publish demo

  • Move documentation for supported languages, styles to docs folder

0.3.4 - 2019-12-14

  • Bump highlight.js from 9.17.0 to 9.17.1

0.3.3 - 2019-12-12

  • Use consistent terminology when documenting usage

  • Add version number to demo

0.3.2 - 2019-12-12

  • Upgrade highlight.js to v9.17.0

0.3.1 - 2019-12-11

  • Move src/docs folder to demo (#36)

  • Document custom language highlighting in README (#35)

0.3.0 - 2019-11-18

  • Add code prop for dynamically updating code

0.2.2 - 2019-11-17

  • Add new live demo and remove storybook

0.2.1 - 2019-11-15

  • Document supported languages, styles

0.2.0 - 2019-11-10

  • Export highlight.js theme CSS files as JavaScript

0.1.1 - 2019-11-09

  • Fix broken svelte import by publishing src folder

0.1.0 - 2019-11-09

  • Initial release