Sugar High

Super lightweight syntax highlighter

/* super tiny syntax highlighter *//* super tiny syntax highlighter *//* super tiny syntax highlighter */
import { highlight } from 'sugar-high'import { highlight } from 'sugar-high'import { highlight } from 'sugar-high'
const code = highlight('const text = 1')const code = highlight('const text = 1')const code = highlight('const text = 1')
1export default function App() {2  return (3    <>4      <h1 id="title">5        Hello6        <span> world</span>7      </h1>8      <div style={styles.bar} />9    </>10  )11}1213

Click the dashed file name to switch light or dark preview for the code samples below.Theme:

import { highlight } from 'sugar-high'const html = highlight(code)

/* styles.css — sugar-high token colors *//* Light (default) */:root {  --sh-class: #8d85ff;  --sh-identifier: #354150;  --sh-sign: #8996a3;  --sh-string: #00a99a;  --sh-keyword: #f47067;  --sh-comment: #a19595;  --sh-jsxliterals: #bf7db6;  --sh-entity: #6eafad;  --sh-property: #4e8fdf;}/* Dark — e.g. <html data-theme="dark"> */:root[data-theme='dark'] {  --sh-class: #7eb5ff;  --sh-identifier: #d4d4d4;  --sh-sign: #8b949e;  --sh-string: #88bbb6;  --sh-keyword: #ffada8;  --sh-comment: #8b8b8b;  --sh-jsxliterals: #d2a8ff;  --sh-entity: #56d4dd;  --sh-property: #79c0ff;}

For CSS (and SCSS, Sass, Less), a preset treats /* */ comments and @-rules as CSS, not as JS regex or division. For Python, the preset uses # line comments and Python keywords instead of JS rules. C, Go, and Java use C-like preset profiles with language-specific keyword sets.

Pass that preset as the second argument to highlight. With Codice, built-in extension mapping covers CSS, Python, and Rust; for other languages call highlight with the preset and pass the HTML to Code using asMarkup (as below). In plain JS, map the path or extension yourself the same way.

import { highlight } from 'sugar-high'import * as presets from 'sugar-high/presets'// Use the file extension from a path or Codice `title` (e.g. "theme.css", "main.py")const presetForTitle = (title) =>  ({    c: presets.c,    go: presets.go,    java: presets.java,    css: presets.css,    py: presets.python,    rs: presets.rust,  })[    title.split('.').pop()  ]highlight('.card { color: red; }', presetForTitle('theme.css'))highlight('int main() { return 0; }', presetForTitle('main.c'))highlight('package main\nfunc main() {}', presetForTitle('main.go'))highlight('class App { public static void main(String[] a) {} }', presetForTitle('App.java'))highlight('def hi():\n    print("ok")', presetForTitle('main.py'))

SCSS, Sass, and Less use the same CSS preset. Rust uses presets.rust, and C-like languages use presets.c, presets.go, or presets.java.

#include <stdint.h> /* fixed-width ints */
#define MIX(x,y) (((x) & 0xffu) ^ ((y) >> 8) | (0xab00u)) // bit ops
typedef struct { uint16_t a; uint32_t b; } hdr_t; // packed header fields
static inline uint32_t rot(uint32_t x, int n) { return (x << n) | (x >> (32 - n)); } // rotate
package main
import "encoding/json"; import "fmt"; import "strings" // one line, three imports
type Row struct { ID string `json:"id"`; Tags []string `json:"tags,omitempty"` } // struct tags
func (r Row) Label() string { return fmt.Sprintf("%s [%s]", r.ID, strings.Join(r.Tags, ",")) } // Sprintf + Join
var _ json.Marshaler = (*Row)(nil) // interface satisfaction
// FQCN-heavy lines: generics, streams, method refs (no extra imports)
java.util.List<java.util.Map<String, Integer>> rows = java.util.List.of(java.util.Map.of("a", 1), java.util.Map.of("b", 2));
java.util.stream.Stream.of("x", "y").map(String::toUpperCase).filter(s -> !s.isEmpty()).forEach(System.out::println);
from __future__ import annotations  # postponed annotations
from itertools import chain, groupby  # stdlib
RE = r"(?x) ^\s* (?P<name> [A-Za-z_]\w* ) \s* = \s* (?P<val> .+ ) $ "  # verbose regex
def windows(xs: list[int], n: int) -> list[list[int]]: return [xs[i : i + n] for i in range(0, len(xs), n)]  # slices

Usage with remark.js

Remark.js is a powerful markdown processor, you can use the sugar-high remark plugin with remark.js to highlight code blocks in markdown.