# andreruffert/syntax-highlight-element architecture

> Architecture diagram of the GitHub repository andreruffert/syntax-highlight-element, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/andreruffert/syntax-highlight-element
- Repository: https://github.com/andreruffert/syntax-highlight-element

## Overview

#### 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: [`src/syntax-highlight-element.js`](https://github.com/andreruffert/syntax-highlight-element/blob/main/src/syntax-highlight-element.js) (SyntaxHighlightElement), [`src/index.js`](https://github.com/andreruffert/syntax-highlight-element/blob/main/src/index.js) (Library entry), [`src/tokenizer/prism.js`](https://github.com/andreruffert/syntax-highlight-element/blob/main/src/tokenizer/prism.js) (Prism adapter), [`vite.tokenizer.config.js`](https://github.com/andreruffert/syntax-highlight-element/blob/main/vite.tokenizer.config.js) (Tokenizer & theme builds)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Browser runtime"]
  node_entry["Library entry<br/>ESM entry<br/>[index.js]"]
  node_element["SyntaxHighlightElement<br/>custom element"]
  node_source["Source text boundary<br/>DOM content"]
  node_platform{{"Custom Highlight API<br/>browser platform"}}
  node_attributes["Element constants<br/>runtime contract<br/>[constants.js]"]
end

subgraph group_tokenization["Configuration &amp; tokenization"]
  node_config["Normalized config<br/>configuration<br/>[config.js]"]
  node_tokenizer{{"Tokenizer contract<br/>extension boundary<br/>[config.js]"}}
  node_prism["Prism adapter<br/>default tokenizer<br/>[prism.js]"]
  node_prism_lexer{{"Prism lexer<br/>runtime dependency<br/>[prism.js]"}}
end

subgraph group_delivery["Build &amp; delivery"]
  node_theme_defaults["Theme defaults<br/>CSS highlight rules<br/>[theme-defaults.css]"]
  node_themes["Selectable themes<br/>CSS themes<br/>[prettylights.css]"]
  node_theme_prism["Prism theme<br/>CSS theme<br/>[prism.css]"]
  node_library_build["Library build<br/>Vite build<br/>[vite.config.js]"]
  node_artifact_builds["Tokenizer &amp; theme builds<br/>Vite builds"]
  node_docs_build["Documentation demo<br/>static Vite site"]
  node_docs["Docs entry page<br/>HTML<br/>[index.html]"]
  node_npm{{"npm / CDN consumers<br/>distribution<br/>[package.json]"}}
end

node_entry -->|"initializes"| node_config
node_entry -->|"registers"| node_element
node_element -->|"uses"| node_attributes
node_element -->|"resolves and observes"| node_source
node_element -->|"reads"| node_config
node_element -->|"sends text and language"| node_tokenizer
node_config -->|"selects and configures"| node_tokenizer
node_tokenizer -->|"default implementation"| node_prism
node_prism -->|"tokenizes with"| node_prism_lexer
node_prism -->|"returns token ranges"| node_element
node_element -->|"registers and clears highlights"| node_platform
node_theme_defaults -->|"styles ::highlight rules"| node_platform
node_themes -->|"styles named highlights"| node_platform
node_theme_prism -->|"styles named highlights"| node_platform
node_library_build -->|"bundles"| node_entry
node_artifact_builds -->|"bundles tokenizer"| node_prism
node_artifact_builds -->|"bundles themes"| node_theme_defaults
node_docs_build -->|"builds"| node_docs
node_npm -.->|"publishes output"| node_library_build
node_npm -.->|"publishes artifacts"| node_artifact_builds
node_docs -.->|"demonstrates usage"| node_entry

click node_entry "https://github.com/andreruffert/syntax-highlight-element/blob/main/src/index.js"
click node_element "https://github.com/andreruffert/syntax-highlight-element/blob/main/src/syntax-highlight-element.js"
click node_attributes "https://github.com/andreruffert/syntax-highlight-element/blob/main/src/constants.js"
click node_config "https://github.com/andreruffert/syntax-highlight-element/blob/main/src/config.js"
click node_tokenizer "https://github.com/andreruffert/syntax-highlight-element/blob/main/src/config.js"
click node_prism "https://github.com/andreruffert/syntax-highlight-element/blob/main/src/tokenizer/prism.js"
click node_prism_lexer "https://github.com/andreruffert/syntax-highlight-element/blob/main/src/tokenizer/prism.js"
click node_theme_defaults "https://github.com/andreruffert/syntax-highlight-element/blob/main/src/themes/theme-defaults.css"
click node_themes "https://github.com/andreruffert/syntax-highlight-element/blob/main/src/themes/prettylights.css"
click node_theme_prism "https://github.com/andreruffert/syntax-highlight-element/blob/main/src/themes/prism.css"
click node_library_build "https://github.com/andreruffert/syntax-highlight-element/blob/main/vite.config.js"
click node_artifact_builds "https://github.com/andreruffert/syntax-highlight-element/blob/main/vite.tokenizer.config.js"
click node_docs_build "https://github.com/andreruffert/syntax-highlight-element/blob/main/vite.docs.config.js"
click node_docs "https://github.com/andreruffert/syntax-highlight-element/blob/main/docs/index.html"
click node_npm "https://github.com/andreruffert/syntax-highlight-element/blob/main/package.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry,node_element,node_source,node_platform,node_attributes toneBlue
class node_config,node_tokenizer,node_prism,node_prism_lexer toneAmber
class node_theme_defaults,node_themes,node_theme_prism,node_library_build,node_artifact_builds,node_docs_build,node_docs,node_npm toneMint
```

## Components

### Browser runtime

Custom element lifecycle and platform highlight rendering.

- [Library entry](https://github.com/andreruffert/syntax-highlight-element/blob/main/src/index.js): ESM entry · Registers the public custom element. · `src/index.js`
- [SyntaxHighlightElement](https://github.com/andreruffert/syntax-highlight-element/blob/main/src/syntax-highlight-element.js): custom element · Owns lifecycle, observation, range creation, and cleanup. · `src/syntax-highlight-element.js`
- **Source text boundary**: DOM content · The host or content-selector descendant remains DOM-owned.
- **Custom Highlight API**: browser platform · Stores named range-backed highlights in CSS.highlights.
- [Element constants](https://github.com/andreruffert/syntax-highlight-element/blob/main/src/constants.js): runtime contract · Defines attribute and highlight naming behavior. · `src/constants.js`

### Configuration & tokenization

Shared configuration and replaceable tokenization boundary.

- [Normalized config](https://github.com/andreruffert/syntax-highlight-element/blob/main/src/config.js): configuration · Combines defaults with window.she.config extensions. · `src/config.js`
- [Tokenizer contract](https://github.com/andreruffert/syntax-highlight-element/blob/main/src/config.js): extension boundary · Receives normalized language and source text. · `src/config.js`
- [Prism adapter](https://github.com/andreruffert/syntax-highlight-element/blob/main/src/tokenizer/prism.js): default tokenizer · Flattens nested Prism tokens into token/range information. · `src/tokenizer/prism.js`
- [Prism lexer](https://github.com/andreruffert/syntax-highlight-element/blob/main/src/tokenizer/prism.js): runtime dependency · `src/tokenizer/prism.js`

### Build & delivery

Library, theme, tokenizer, and documentation outputs.

- [Theme defaults](https://github.com/andreruffert/syntax-highlight-element/blob/main/src/themes/theme-defaults.css): CSS highlight rules · Supplies base named-token styling behavior. · `src/themes/theme-defaults.css`
- [Selectable themes](https://github.com/andreruffert/syntax-highlight-element/blob/main/src/themes/prettylights.css): CSS themes · Alternative palettes target the same named highlights. · `src/themes/prettylights.css`
- [Prism theme](https://github.com/andreruffert/syntax-highlight-element/blob/main/src/themes/prism.css): CSS theme · `src/themes/prism.css`
- [Library build](https://github.com/andreruffert/syntax-highlight-element/blob/main/vite.config.js): Vite build · Produces the distributable ESM library. · `vite.config.js`
- [Tokenizer & theme builds](https://github.com/andreruffert/syntax-highlight-element/blob/main/vite.tokenizer.config.js): Vite builds · Produces separately consumable tokenizer and CSS artifacts. · `vite.tokenizer.config.js`
- [Documentation demo](https://github.com/andreruffert/syntax-highlight-element/blob/main/vite.docs.config.js): static Vite site · Demonstrates CDN-compatible browser usage. · `vite.docs.config.js`
- [Docs entry page](https://github.com/andreruffert/syntax-highlight-element/blob/main/docs/index.html): HTML · `docs/index.html`
- [npm / CDN consumers](https://github.com/andreruffert/syntax-highlight-element/blob/main/package.json): distribution · Consumers load ESM and independently import a theme. · `package.json`

## Connections

- 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
