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.rbinjects globally into every rendered page; -
how
asciidoctor-default.csscustomizes the top-right TOC buttons.
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.
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
html5backend (pdf / github backends are skipped automatically); -
skips
env-githubdocuments; -
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: 1001keeps 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 withposition: static !importantso the button sits inside the toolbar instead of floating on its own. -
The button uses
width: 110px !important(notmin-width) plus centered text: withbox-sizing: border-box110px is enough forClose TOC, and a fixed width prevents the toolbar from jittering when the label changes.!importantoverrides inline styles left in older documents. -
The native
#tocis hidden by default (max-width: 0,opacity: 0) and revealed by#toc.active(max-width: 530px), with amax-width/padding/opacitytransition. 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 printthe 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.