glimra

glimra is a zero runtime syntax highlighter for lustre/ssg.

In Swedish, glimra describes the brilliant gleam or lustre that comes from a polished, reflective surface, capturing the essence of light shining off something smooth and glossy.

glimra uses NIFs to extract syntax highlighting events provided by the tree-sitter and tree-sitter-highlight crates. This allows glimra to provide syntax highlighting for a wide range of languages with minimal effort.

Types

A configuration for the syntax highlighter with a theme.

  • line_numbers: Whether to display line numbers.
  • trim_source: Whether to trim whitespace from the source code.
  • theme: A theme to style the highlighted code.
pub opaque type Config(has_theme)

A phantom type representing a configuration with a theme.

pub type HasTheme

A phantom type representing a configuration without a theme.

pub type NoTheme

Possible errors that can occur during syntax highlighting.

pub type SyntaxHighlightingError {
  UnsupportedLanguage(language: String)
  TreeSitterError
  UnmatchedHighlightEvents
}

Constructors

  • UnsupportedLanguage(language: String)

    The specified language is not supported.

  • TreeSitterError

    An error occurred with the Tree-sitter syntax highlighting library.

  • UnmatchedHighlightEvents

    There were unmatched highlight events during the syntax highlighting process.

Functions

pub fn add_static_stylesheet(
  ssg_config ssg_config: Config(a, b, c),
  syntax_highlighter syntax_highlighter: glimra.Config(HasTheme),
) -> Config(a, b, c)

Add a static stylesheet for syntax highlighting to a lustre/ssg configuration.

  • ssg_config: The static site generator configuration.
  • syntax_highlighter: The syntax highlighter configuration with a theme.
pub fn codeblock_renderer(
  syntax_highlighter syntax_highlighter: Config(HasTheme),
) -> fn(Dict(String, String), Option(String), String) ->
  Element(Nil)

Create a renderer function for code blocks that uses the syntax highlighter.

  • syntax_highlighter: The syntax highlighter configuration with a theme.
pub fn enable_line_numbers(config config: Config(a)) -> Config(a)

Enable line numbers in the syntax highlighter configuration.

pub fn link_static_stylesheet() -> Element(a)

Generate a link element to include the static syntax highlighting stylesheet.

pub fn new_syntax_highlighter() -> Config(NoTheme)

Create a new syntax highlighter configuration with default settings.

The default configuration has line numbers disabled and source trimming enabled, with no theme applied.

pub fn set_theme(
  config config: Config(NoTheme),
  theme theme: Theme,
) -> Config(HasTheme)

Apply a theme to the syntax highlighter configuration.

pub fn set_trim_source(
  config config: Config(a),
  trim_source trim_source: Bool,
) -> Config(a)

Set whether to trim the source code in the syntax highlighter configuration.

  • trim_source: Whether to trim the source code.
pub fn syntax_highlight(
  config config: Config(a),
  source source: String,
  language language: String,
) -> Result(Element(Nil), SyntaxHighlightingError)

Perform syntax highlighting on the provided source code using the given configuration and programming language.

Returns a Lustre Element on success, or a SyntaxHighlightingError on failure.

  • config: The syntax highlighter configuration.
  • source: The source code to highlight.
  • language: The programming language of the source code.
pub fn to_css(config config: Config(HasTheme)) -> String

Generate CSS for the syntax highlighter configuration.

The CSS includes styles for the highlighted code blocks based on the configuration’s theme.

Search Document