How andreruffert/syntax-highlight-element works
Purpose and public entry point
This is an ESM browser library that registers a <syntax-highlight> custom element. It syntax-highlights text without injecting token s: token ranges are exposed through the CSS Custom Highlight API and styled by CSS themes. Package initialization starts at src/index.js; the element implementation is src/syntax-highlight-element.js.
Element lifecycle and DOM boundary
<syntax-highlight> accepts language and content-selector attributes. The element resolves either itself or a selected descendant as the source text boundary, then reacts to relevant content/attribute changes by regenerating highlights. DOM ownership remains with the host content; highlighting is represented externally as browser highlight ranges rather than rewritten markup.
• src/syntax-highlight-element.js
• src/constants.js
• src/utils.js
Configuration contract
Before registration, consumers may set window.she.config to extend supported languages, token types, language-specific token mappings, setup behavior, and tokenization. Defaults and config normalization are centralized so the custom element and tokenizer operate against one configuration contract.
• src/config.js
• src/constants.js
• src/index.js
Tokenization pipeline
The element sends content text and its normalized language to the configured tokenizer. The built-in Prism adapter converts Prism tokenization output into token/range information suitable for Custom Highlight registration, including nested token structures. This is the primary extensibility boundary: applications can replace config.tokenize for custom grammars or tokenizers.
• src/tokenizer/prism.js
• src/config.js
• src/syntax-highlight-element.js
Browser highlight rendering
For each recognized token type, the component creates range-backed named highlights and registers them through the platform CSS Custom Highlight API (CSS.highlights). Themes target those named highlights using CSS ::highlight(...) rules. Cleanup/re-render must remove prior registrations to avoid stale ranges when text or language changes.
• src/syntax-highlight-element.js
• src/constants.js
• src/themes/theme-defaults.css
Styling and theme distribution
Base token naming/default behavior is separated from selectable visual themes. Consumers import a CSS theme independently of the JavaScript element, allowing the same highlight registry to be rendered with different palettes.
• src/themes/theme-defaults.css
• src/themes/prettylights.css
• src/themes/prism.css
Build, docs, and delivery
Vite produces the library and separately configured tokenizer/theme artifacts; npm is the primary distribution channel, with CDN-compatible ESM usage documented. The repository also contains a static documentation/demo site built with its own Vite config.
• vite.config.js
• vite.tokenizer.config.js
• vite.themes.config.js
• vite.docs.config.js
• docs/index.html
Runtime dependencies and constraints
Runtime execution is browser-only: Custom Elements, DOM Ranges, and the CSS Custom Highlight API are required. Prism supplies the default lexer/tokenizer model. There is no server, database, or backend service; external delivery commonly uses npm or jsDelivr.
• package.json
• src/tokenizer/prism.js
• src/index.js
Main entry points: syntax-highlight-element.js SyntaxHighlightElement · index.js Library entry · prism.js Prism adapter · vite.tokenizer.config.js Tokenizer & theme builds
Components 17
Browser runtime
Custom element lifecycle and platform highlight rendering.
- Library entry · Registers the public custom element.
src/index.js - SyntaxHighlightElement · Owns lifecycle, observation, range creation, and cleanup.
src/syntax-highlight-element.js - Source text boundary · The host or content-selector descendant remains DOM-owned.
- Custom Highlight API · Stores named range-backed highlights in CSS.highlights.
- Element constants · Defines attribute and highlight naming behavior.
src/constants.js
Configuration & tokenization
Shared configuration and replaceable tokenization boundary.
- Normalized config · Combines defaults with window.she.config extensions.
src/config.js - Tokenizer contract · Receives normalized language and source text.
src/config.js - Prism adapter · Flattens nested Prism tokens into token/range information.
src/tokenizer/prism.js - Prism lexer
src/tokenizer/prism.js
Build & delivery
Library, theme, tokenizer, and documentation outputs.
- Theme defaults · Supplies base named-token styling behavior.
src/themes/theme-defaults.css - Selectable themes · Alternative palettes target the same named highlights.
src/themes/prettylights.css - Prism theme
src/themes/prism.css - Library build · Produces the distributable ESM library.
vite.config.js - Tokenizer & theme builds · Produces separately consumable tokenizer and CSS artifacts.
vite.tokenizer.config.js - Documentation demo · Demonstrates CDN-compatible browser usage.
vite.docs.config.js - Docs entry page
docs/index.html - npm / CDN consumers · Consumers load ESM and independently import a theme.
package.json
Connections 21
- Library entry → Normalized config: initializes
- Library entry → SyntaxHighlightElement: registers
- SyntaxHighlightElement → Element constants: uses
- SyntaxHighlightElement → Source text boundary: resolves and observes
- SyntaxHighlightElement → Normalized config: reads
- SyntaxHighlightElement → Tokenizer contract: sends text and language
- Normalized config → Tokenizer contract: selects and configures
- Tokenizer contract → Prism adapter: default implementation
- Prism adapter → Prism lexer: tokenizes with
- Prism adapter → SyntaxHighlightElement: returns token ranges
- SyntaxHighlightElement → Custom Highlight API: registers and clears highlights
- Theme defaults → Custom Highlight API: styles ::highlight rules
- Selectable themes → Custom Highlight API: styles named highlights
- Prism theme → Custom Highlight API: styles named highlights
- Library build → Library entry: bundles
- Tokenizer & theme builds → Prism adapter: bundles tokenizer
- Tokenizer & theme builds → Theme defaults: bundles themes
- Documentation demo → Docs entry page: builds
- npm / CDN consumers → Library build: publishes output
- npm / CDN consumers → Tokenizer & theme builds: publishes artifacts
- Docs entry page → Library entry: demonstrates usage