This article documents a practical setup for writing AsciiDoc articles inside Leo Editor. It covers three pieces:

  • the @button ascii button — what it does and how it reconciles Leo’s output name with the Asciidoctor xref name to drive the live preview
    (from the design note below);

  • what options.rb injects globally into every rendered page;

  • how asciidoctor-default.css customizes the top-right TOC buttons.

writing asciidoc in leo 1
writing asciidoc in leo 2

The @button ascii button

@button ascii (bound to Ctrl-/) is the button that drives the whole workflow: it compiles the @adoc node under the cursor into HTML and opens (or refreshes) a live preview in the browser.

In Leo the button’s code lives in myLeoSettings.leo under @buttons.

Pressing Ctrl-/ runs the button; pressing it again updates the existing preview instead of spawning a second server. All logic — finding the @adoc node, invoking the adoc command, renaming the generated HTML, and starting the preview server — is in this button.

View Design for HTML output naming and live preview

Design for HTML output naming and live preview

A node headline may contain more than one extension, for example:

@adoc aaaa.js.adoc

The button uses the Asciidoctor xref name as the canonical HTML name. That is the name written into in-page links and the file that live-server opens and watches.

Two filename rules

Leo’s adoc command computes the output path with c.markupCommands.compute_opath(), which strips up to three extensions (leoMarkup.py:363-374; the source comment notes that neither Asciidoctor nor Pandoc handles extra extensions well). Leo therefore writes:

aaaa.js.adoc  -> aaaa.html
a.b.c.adoc    -> a.html

Asciidoctor’s xref replaces only the final .adoc with .html and keeps every intermediate extension:

xref:aaaa.js.adoc[...]

So the same source produces a link to aaaa.js.html. The two rules agree only when the headline has a single extension:

Node headline Leo output Asciidoctor xref target

aaaa.adoc

aaaa.html

aaaa.html

aaaa.js.adoc

aaaa.html

aaaa.js.html

a.b.c.adoc

a.html

a.b.c.html

Canonical name and rename step

The design preserves the semantics of the xref — its target remains the real .adoc source file — and makes the generated HTML match the name Asciidoctor links to. The button distinguishes two names:

  • o_html_filename — the name computed by Leo’s compute_opath() (up to three extensions stripped). This is the rename source.

  • xref_html_filename — obtained by replacing only .adoc with .html, keeping .js and friends. This is the rename target, and the name live-server opens and watches.

The flow:

# 1. Derive the canonical HTML name Asciidoctor's xref will link to.
xref_html_filename = os.path.splitext(adoc_file)[0] + ".html"

# 2. Let the adoc command write the file, then ask Leo for its real output path.
i_paths = c.executeMinibufferCommand("adoc") or []
i_path = next((x for x in i_paths if os.path.basename(x) == adoc_file), None)
if i_path is None and i_paths:
    i_path = i_paths[0]
o_path = c.markupCommands.compute_opath(i_path) if i_path else None

# 3. Rename Leo's output to the canonical name; os.replace keeps only one HTML file.
xref_path = os.path.join(target_dir, xref_html_filename)
if o_path and os.path.normpath(o_path) != os.path.normpath(xref_path):
    if os.path.exists(o_path):
        os.replace(o_path, xref_path)

Details that matter:

  • executeMinibufferCommand("adoc") returns a list of .adoc paths (it can process every @adoc root in the tree). next(…​, None) short-circuits at the first basename match; the explicit None default is essential, because next(gen) without it would raise and never reach the fallback to i_paths[0].

  • os.replace rather than a copy: only one HTML file remains on disk, and that is the one live-server watches, so auto-reload keeps working.

  • When the headline contains a path (for example @adoc sub/foo.adoc), the basename match fails by design and the fallback uses the first path.

Preview server lifecycle

Each preview is identified by a key of (target_dir, xref_html_filename):

  • first run — start npx live-server --entry-file=<name> --watch=<name> in the target directory;

  • later runs — if the process is still alive, report "Updated" and reuse the running server; the browser auto-reloads because the watched file was replaced. If it has died, drop the key and ask the user to run the button again;

  • on Leo exit — an atexit handler terminates every tracked server.

The key keeps one server per output file, so re-running the button updates the same browser tab instead of spawning a second server.

Global attributes and toolbar injection with options.rb

options.rb is loaded by Asciidoctor for every render. It does two jobs.

Global attributes via DEFAULT_ATTRIBUTES

Attributes such as stem, source-highlighter, icons, scripts, toc=right, toclevels=3 and the admonition captions are written once into Asciidoctor::DEFAULT_ATTRIBUTES.

This is equivalent to putting :xxx: definitions in the header of every .adoc file. A value defined inside a single document still overrides the default.

Postprocessor: math fix, timestamp, and TOC toolbar

A postprocessor rewrites the finished HTML:

output = output.gsub(
  /(MathJax\.js\?config=)TeX-MML-AM_HTMLorMML/,
  '\1TeX-MML-AM_CHTML'
)

The unofficial mathjax-url attribute had no effect, so a string replacement is used to fix misaligned math rendering.The pattern is anchored to the MathJax script URL so that a code sample quoting the old value is left untouched.

A second rewrite strips the numeric timezone offset from the Last updated line in the footer, keeping only local wall-clock time:

output = output.gsub(/(Last updated \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) [+-]\d{4}/, '\1')

Asciidoctor’s docdatetime is already formatted in the local timezone, so the trailing offset is redundant.

The same postprocessor injects a toolbar just before </body>:

<div id="toc-toolbar">
  <a id="homeButton" href="/" title="Back to home">Home</a>
  <button id="toggleButton" type="button">Open TOC</button>
</div>

The toolbar is injected globally, so individual documents no longer need to embed toolbar markup themselves. Injection is guarded so that it:

  • only runs for the html5 backend (pdf / github backends are skipped automatically);

  • skips env-github documents;

  • skips older documents that already contain id="toc-toolbar", preventing duplicate buttons and scripts.

A small inline script sets homeButton: on a root-domain deployment it points to /, while on a GitHub Pages project site it goes back to the project root. It also toggles the .active class on the native #toc and switches the label between Open TOC and Close TOC; if there is no native TOC at all, the toggle is hidden and only Home remains.

The file also optionally loads asciidoctor-diagram (with a warning if it is missing). asciidoctor-kroki is present but commented out, and data-uri is available as a one-line switch to embed images as base64.

Customizing the top-right button in asciidoctor-default.css

asciidoctor-default.css is a copy of the Asciidoctor default stylesheet with a custom block at the end. Its job is to make the injected toolbar behave like a compact, always-available control in the top-right corner.

#toc-toolbar {
    position: fixed;
    top: 10px;
    right: 10px;
    z-index: 1001;      /* above the native #toc.toc2 (1000) */
    display: flex;
    align-items: center;
    gap: 8px;
}

Key decisions:

  • z-index: 1001 keeps the toolbar on top of the native right-hand TOC when it expands.

  • The older rule set #toggleButton { position: fixed; …​ }. The toolbar rule cancels that with position: static !important so the button sits inside the toolbar instead of floating on its own.

  • The button uses width: 110px !important (not min-width) plus centered text: with box-sizing: border-box 110px is enough for Close TOC, and a fixed width prevents the toolbar from jittering when the label changes. !important overrides inline styles left in older documents.

  • The native #toc is hidden by default (max-width: 0, opacity: 0) and revealed by #toc.active (max-width: 530px), with a max-width / padding / opacity transition. Without this, clicking the button would toggle a class on an always-visible sidebar.

  • html { scroll-padding-top: 10px; scroll-behavior: smooth; } keeps anchored headings clear of the fixed toolbar and makes jumps smooth.

  • In @media print the toolbar is hidden and the TOC is fully expanded — an interactive control has no place on paper.

Putting it together

The whole loop is: write AsciiDoc in a Leo node tree → press Ctrl-/ → the button invokes the adoc command (asciidoctor -b html5 -r options); during that Asciidoctor render options.rb supplies global attributes and injects the toolbar, while asciidoctor-default.css keeps the toolbar pinned and the TOC collapsible, and the HTML is written to Leo’s output path → the button reconciles Leo’s output name with the Asciidoctor xref name via os.replace() → it starts or reuses live-server and opens the live preview in the browser tab. Editing and re-running the button repeats the flow and updates the same browser tab.

The following are three different flowchart layouts for creating .adoc files.

flowchart LR

mermaid adoc lr 2026 12 01 122941

flowchart TD

mermaid adoc td 2026 12 01 123003

flowchart TD only 2 subgraph

mermaid adoc td 2026 12 01 124030
Home