Syntax highlighting
fractalpop
Lightweight, Svelte-first syntax highlighting with first-class indented Sass. Returns an HTML string without a DOM, with adapters for Svelte, mdsvex and remark.
Install
pnpm add @fractaldesign/pop@fractaldesign/pop — The engine: code in, HTML string out.
Quick start
import { highlight } from '@fractaldesign/pop';
const html = highlight('const ready = true', { lang: 'ts' });<script lang="ts">
import { Code } from '@fractaldesign/pop-svelte';
</script>
<Code code={source} lang="ts" title="example.ts" lineNumbers />import { mdsvex } from 'mdsvex';
import { fractalpopHighlighter } from '@fractaldesign/pop-mdsvex';
export default {
extensions: ['.svelte', '.svx', '.md'],
preprocess: [mdsvex({ extensions: ['.svx', '.md'], highlight: { highlighter: fractalpopHighlighter } })]
};Features
No DOM required
highlight() returns an HTML string, so it runs on the server, at build time, in a worker or on the edge.
Indented Sass, properly
First-class sass with no braces or semicolons, and a composite svelte grammar for script, markup and style.
Bring your own colours
Nine stable token types, each a CSS variable. A theme is one block of --fp-* custom properties.
Tree-shakeable languages
The default entry registers TypeScript and plaintext. Register more, or import /full for all 32.
A fast path by default
With no customization hooks, rendering concatenates strings with no per-token allocation.
Every pipeline covered
Svelte components and an action, an mdsvex highlighter, and a remark plugin for Markdown and MDX.
Packages
README
@fractaldesign/pop@0.1.0 README, from npm.
@fractaldesign/pop is the engine. It highlights a string of source code and returns
an HTML string. No DOM, no runtime dependencies. Use it directly on any
platform, or through one of the adapter packages.
Install
npm install @fractaldesign/popQuick start
Call highlight() with your code and a language. The result is a string of
nested <span> elements that you drop into a <pre><code> block.
import { highlight } from '@fractaldesign/pop'
const html = highlight('const ready = true', { lang: 'ts' })
// <span class="fp__line"><span class="fp__token--keyword" style="color:var(--fp-keyword)">const</span> …The output is HTML-encoded, so it is safe to inject with innerHTML, {@html}, or dangerouslySetInnerHTML.
highlight(code, options)
highlight takes the source string and an options object. It returns an HTML string.
| Option | Type | Default | Purpose |
|---|---|---|---|
lang | string | 'typescript' | Language id, extension, or alias (see Languages). |
cx | Partial<Record<TokenType, string>> | — | Extra class name to add per token type. |
mark | (token: MarkToken) => void | — | Mutate a single token before it renders. |
markLine | (line: MarkLine) => void | — | Mutate a whole line before it renders. |
cx, mark, and markLine are the only customization hooks, and they control
performance. When you pass none of them, render takes a fast path: it
concatenates strings with no per-token object allocation. Passing any hook
switches to the slower path that builds mutable token and line objects so the
hooks can run. If you do not need customization, pass no hooks.
Emphasis with cx
cx appends a class to every token of a given type. This is the Tailwind-
friendly way to emphasize tokens without writing selectors.
highlight(code, {
lang: 'sass',
cx: { keyword: 'font-bold', comment: 'italic opacity-70' },
})Line highlighting with markLine
Each line exposes its zero-based index. Add a class when the line is one you
want to highlight.
const active = new Set([2, 3]) // 1-based line numbers
highlight(code, {
lang: 'ts',
markLine(line) {
if (active.has(line.index + 1)) line.className += ' fp__line--highlighted'
},
})Theming
fractalpop does not ship colors. Every token renders with class="fp__token--<type>" and an inline style="color:var(--fp-<type>)", so a
theme is a block of CSS variables you set on any ancestor.
:root {
--fp-identifier: #354150;
--fp-keyword: #f47067;
--fp-string: #00a99a;
--fp-class: #8d85ff;
--fp-property: #4e8fdf;
--fp-entity: #665ac7;
--fp-jsxliterals: #bf7db6;
--fp-sign: #8996a3;
--fp-comment: #a19595;
}
.fp__line--highlighted {
background: #fff8c5;
}For dark mode, redefine the same variables under a selector or media query — for
example :root[data-theme='dark'] or @media (prefers-color-scheme: dark).
Token types
Token types are a small, fixed list. Keeping it stable keeps themes portable.
| Type | CSS variable | Typical meaning |
|---|---|---|
identifier | --fp-identifier | Plain identifiers and default text. |
keyword | --fp-keyword | Language keywords. |
string | --fp-string | Strings, regex literals, template text. |
class | --fp-class | Capitalized names, numbers, null, types. |
property | --fp-property | Object/CSS properties, Sass $variables. |
entity | --fp-entity | Function and mixin names. |
jsxliterals | --fp-jsxliterals | JSX text between tags. |
sign | --fp-sign | Punctuation and operators. |
comment | --fp-comment | Comments. |
Two more types, break (newlines) and space (horizontal whitespace), are
preserved for faithful output but carry no color.
Languages
fractalpop ships metadata and configs for 32 languages, but the default
entry only registers TypeScript and plaintext — the rest stay tree-
shakeable. Register what you need yourself, or import @fractaldesign/pop/full to
register all 32 up front. Resolve a name, file extension, or alias to a
canonical id with lang().
import { lang, findLanguage, allLanguages } from '@fractaldesign/pop/lang'
lang('tsx') // 'typescript'
lang('.scss') // 'scss' (metadata is known; register the config to highlight)
lang('yml') // 'yaml'
lang('nope') // undefinedFractalpop especially supports-
sass— indented Sass (no braces, no semicolons).svelte— the composite:<script lang="ts">+ markup +<style lang="sass">.typescript/javascript— a full JS/JSX/TS runtime (regex literals, template interpolation, JSX, and the TS-generic-vs-JSX distinction).
The rest is the general pack: css, html, markdown, diff, plaintext, c, cpp, csharp, go, java, rust, json, shell, sql, yaml, toml, python, ruby, php, kotlin, swift, lua, graphql, hcl, dockerfile, powershell, zig.
Subpath exports
The package exposes several entries so you only bundle what you need.
| Import | Exposes |
|---|---|
@fractaldesign/pop | highlight, registry utilities, and the option/token types. Only TypeScript and plaintext are registered by default. |
@fractaldesign/pop/full | Everything from @fractaldesign/pop, with all 32 languages registered. |
@fractaldesign/pop/core | parse, tokenize, render (→ string), generate (→ AST). |
@fractaldesign/pop/lang | lang, findLanguage, allLanguages. |
fractalpop/lang/<id> | A single language config (tree-shakeable). |
@fractaldesign/pop/gpu | Async WebGPU highlighting via optional peer gpu-lexer. |
generate returns a hast-like AST, which is what a unified/rehype pipeline
consumes. render returns the HTML string that highlight wraps.
import { parse, generate } from '@fractaldesign/pop/core'
import { lang, getLanguageConfig } from '@fractaldesign/pop/full'
const parsed = parse(code, getLanguageConfig(lang('sass')!))
const tree = generate(parsed) // element/text nodes, one per line and tokenRegistry utilities
The default @fractaldesign/pop entry only registers TypeScript and plaintext. Use
these utilities to control which languages are available at runtime.
The registry is a single module-level singleton shared by every entry point
(@fractaldesign/pop/full re-exports and mutates the same core as @fractaldesign/pop), so a
registration made through one import path is visible to all others in the same
app.
import {
registerLanguage,
setDefaults,
importDefaults,
canonicalizeLang,
getRegisteredLanguages,
} from '@fractaldesign/pop'
registerLanguage({ id: 'python', extension: 'py', aliases: ['python3'] }, pythonConfig)
setDefaults({ python: pythonConfig, rust: rustConfig })
await importDefaults(['python', 'rust'])
canonicalizeLang('tsx') // 'typescript'| Function | Purpose |
|---|---|
registerLanguage(language, config?) | Register one language, optionally with its parse config. |
setDefaults(languagesOrConfigs) | Replace the default set. Accepts Language[] or Record<string, ParseOptions>. |
importDefaults(ids) | Asynchronously import and register languages by id. |
canonicalizeLang(name) | Resolve a name/alias/extension to the canonical id. |
getRegisteredLanguages() | List currently registered languages. |
Related Packages
- @fractaldesign/pop-svelte — Svelte 5 components and action
- @fractaldesign/pop-mdsvex — Highlighter hook for MDSveX in SvelteKit
- @fractaldesign/pop-remark — Remark plugin for Markdown & MDX
Benchmarks
fractalpop, Sugar High, PrismJS, highlight.js, and Shiki highlighting the same generated TypeScript files:
Measured 2026-09-13 with Node v24.19.0, darwin arm64, Apple M3 Pro.
| TypeScript | fractalpop 0.1.0 | Sugar High 2.4.0 | PrismJS 1.30.0 | highlight.js 11.12.0 | Shiki 4.4.3 |
|---|---|---|---|---|---|
| Minified (KiB) | 12.35 | 27.29 | 14.57 | 29.49 | 364.20 |
| Gzip (KiB) | 5.28 | 10.09 | 5.57 | 11.28 | 72.79 |
| 11 KiB | 1.96 | 1.97 | 1.47 | 2.55 | 18.55 |
| 100 KiB | 19.75 | 20.50 | 14.68 | 24.11 | 163.34 |
| 500 KiB | 97.70 | 101.81 | 100.31 | 127.61 | 858.15 |
Median milliseconds per file; lower is better. 5 timed samples after warmup. Sizes are TypeScript-only browser bundles, minified with Bun; gzip uses level 9. Theme CSS is excluded. Loading and initialization are excluded. Each library highlights the same generated TypeScript into HTML using an explicit language. Grammars and HTML output differ; this is not a measure of highlighting quality or browser rendering speed. Results vary by machine and workload.