Skip to content

HTML and CSS

Comfit renders source code as an escaped HTML fragment. The default structure is <pre><code>, with a span around each line. Choose how the fragment represents token styles:

ModeUse it whenWhat you supply
InlineYou want theme colors in the returned HTML.A bundled theme ID or theme object, depending on the package.
ClassesYou want a shared stylesheet.CSS rules for capture classes, or an @comfit/css theme.
VariablesYou want your application's CSS to define the colors.--comfit-* CSS variables.

The default is classes in the libraries and inline styles in the CLI. None of these modes sets your font, padding, or line-number layout.

Inline styles

Embed theme colors in the returned fragment.

JavaScript
JavaScript
import { renderHtml, RenderHtmlConfig, RenderHtmlStyle } from "@comfit/node";

const config = new RenderHtmlConfig();
config.style = RenderHtmlStyle.Inline;

const html = renderHtml(
  "const answer = 42;",
  "javascript",
  "catppuccin-mocha",
  config,
);

Classes

Use class-based output when several code blocks should share a stylesheet instead of embedding colors in every fragment. @comfit/css styles HTML generated by any Comfit package.

This example uses Catppuccin Mocha. The theme catalog lists the other stylesheets and their matching outer classes.

Install a stylesheet

Shell
Shell
pnpm add @comfit/css

Load a theme

Load the stylesheet on the web page that displays the generated HTML, regardless of which Comfit package renders it.

JavaScript

Import the stylesheet in your frontend entry point with a build tool that supports CSS imports.

JavaScript
import "@comfit/css/catppuccin-mocha.css";

Match the HTML class

Generate a fragment with the matching outer class:

JavaScript
JavaScript
import { renderHtmlClasses, RenderHtmlConfig } from "@comfit/node";

const config = new RenderHtmlConfig();
config.preClass = "comfit-catppuccin-mocha";

const html = renderHtmlClasses("const answer = 42;", "javascript", config);

The stylesheet's scope must match the outer pre class: catppuccin-mocha.css expects comfit-catppuccin-mocha. You can serve the stylesheet through an HTML link element instead of importing it.

A function.call capture produces both function and function-call classes. Lines have line and, when selected, line-highlight classes. Brackets can have rainbow and rainbow-N classes.

The class-prefix option changes these names. Bundled CSS expects no prefix. A custom prefix needs a matching stylesheet.

CSS variables

Render CSS variable references instead of theme colors.

JavaScript
JavaScript
import {
  renderHtmlClasses,
  RenderHtmlConfig,
  RenderHtmlStyle,
} from "@comfit/node";

const config = new RenderHtmlConfig();
config.style = RenderHtmlStyle.Variables;
config.preClass = "code-block";

const html = renderHtmlClasses("const answer = 42;", "javascript", config);

Define colors in your CSS:

CSS
.code-block {
  --comfit-fg: #cdd6f4;
  --comfit-bg: #1e1e2e;
  --comfit-keyword-fg: #cba6f7;
  --comfit-number-fg: #fab387;
}

Capture variables fall back from the full name to its parents, then to the default. For example, --comfit-function-call-fg falls back to --comfit-function-fg, then --comfit-fg. The class-prefix option does not rename these variables.

@comfit/css provides class rules, not variable-mode palettes. Set the variables yourself.

Add layout styles

Use your outer class to set the font, padding, and overflow. The generated .line elements do not add visible line numbers. Give them display: block if you want selected-line backgrounds to fill the available width.

Comfit escapes source text and generated attribute values. When building your own renderer from tokens, escape token text yourself. A syntax highlighter is not a sanitizer for arbitrary HTML documents.

Element tags

Choose the outer container and line wrapper elements. Their defaults are pre and span.

The inner code element and token span elements stay unchanged. Classes and inline styles remain on the selected wrappers.

JavaScript
JavaScript
import { RenderHtmlConfig } from "@comfit/node";

const config = new RenderHtmlConfig();
config.preTag = "div";
config.lineTag = "div";

Bundled @comfit/css stylesheets target pre.comfit-<theme>. A different outer tag needs a stylesheet with a matching selector.