package hilite

  1. Overview
  2. Docs
Build time syntax highlighting

Install

dune-project
 Dependency

Authors

Maintainers

Sources

hilite-0.5.0.tbz
sha256=550c01abe4a95808553693499dcb75ef87bd86127db8f3db1f94a81689e13a15
sha512=b42375e1dd288fc3795c570be2b94486aa91c499c5b6a2cff936d530d6864b57f0269deb73a7f333339e41e89b4cd0452655ff7c40f58360cea13efaee645115

Description

A library for adding syntax highlighting to OCaml-related code and outputing to HTML

Tags

syntax highlighting

Published: 15 Jul 2025

README

Hilite 🎨

A small library that generates HTML ready for syntax-highlighting with CSS by parsing markdown documents.

Currently only some OCaml-related syntaxes are supported directly. For example the test.md file can be converted with:

# Cmarkit.Doc.of_string (read_file "test/test.md")
  |> Hilite_markdown.transform
  |> Cmarkit_html.of_doc ~safe:false;;
- : string =
"<p>&quot;Hello World!&quot; in OCaml looks like:</p>\n<pre><code><span class='ocaml-keyword-other'>let</span><span class='ocaml-source'> </span><span class='ocaml-constant-language-unit'>()</span><span class='ocaml-source'> </span><span class='ocaml-keyword-operator'>=</span><span class='ocaml-sourc"... (* string length 609; truncated *)

Customisation

Hilite supports users loading their own grammars so they can do custom, build-time syntax highlighting. Users most provided a collection of textmate grammars to the transformation function.

Here we will use a predefined grammar that comes along with the Hilite library.

# let tm =
    let t = TmLanguage.create () in
    let grammar = TmLanguage.of_yojson_exn Hilite.Grammars.diff in
    TmLanguage.add_grammar t grammar;
    t
val tm : TmLanguage.t = <abstr>

With this we can now highlight code blocks that start with diff.

# Cmarkit.Doc.of_string (read_file "test/test_diff.md")
  |> Hilite_markdown.transform ~lookup_method:`Filetype ~tm
  |> Cmarkit_html.of_doc ~safe:false;;
- : string =
"<p>A diff block!</p>\n<pre><code><span class='patch-punctuation-definition-deleted'>-</span><span class='patch-markup-deleted'>  module Sel = (val if is_osx () then (module Mac)\n</span><span class='patch-punctuation-definition-deleted'>-</span><span class='patch-markup-deleted'>                    e"... (* string length 1176; truncated *)

This example also shows a use off lookup_method to change how TmLanguage finds the grammar based on the code block infostring.

Dependencies (2)

  1. textmate-language >= "0.3.3"
  2. dune >= "3.8"

Dev Dependencies (3)

  1. odoc with-doc
  2. cmarkit >= "0.3.0" & with-test
  3. mdx >= "2.4.1" & with-test

Used by

None

Conflicts

None

OCaml

Innovation. Community. Security.