jotdownload

blog · · Mohak Bajaj

Hiding Markdown syntax as you type, without moving the line

Jot 2.3 added a Markdown editor. Open a jot and the window grows into an editor beside the list. It styles Markdown as you type, and the **, # and link brackets disappear until you need them. Any .md file in Finder can open in it too.

I set two rules before writing any of it:

This post is how both rules shape the code. The styling is one Rust file of about 650 lines, tests included.

Styles are byte ranges, not a document model

Most rich editors parse Markdown into a tree, let you edit the tree, and write Markdown back out. That round trip is where your file gets rewritten: * becomes _, list markers get normalised, lines re-wrap. Jot's notes are also read by Obsidian, Vim, scripts and coding agents over MCP, so every rewrite would show up as a diff nobody made.

So the editor never holds a tree. The text field holds the plain text. On each frame, a pulldown-cmark parse (CommonMark with GitHub's tables, strikethrough, task lists, footnotes and autolinks, plus $math$) gives each element its exact byte range. Those ranges become styled spans laid over the text. Typing edits the text; the spans are thrown away and rebuilt. Saving writes the text. There is nothing to convert back.

Two kinds of syntax

Hiding all Markdown makes it unreadable. A list without bullets is just lines. So every piece of syntax is one of two kinds:

pub enum Kind {
    /// Syntax that stays visible, faint: `>`, `-`, `1.`, `[ ]`, fences, rules,
    /// table pipes, HTML, reference definitions.
    Marker,
    /// Syntax that Live mode hides away from the caret: heading hashes, `**`,
    /// `*`, `~~`, backticks, `$`, link brackets and URLs, escapes.
    Syntax,
    Heading(u8), Strong, Emphasis, Code, Quote, Done, Strike, Link, Highlight,
}

A Marker tells you the structure: quote bars, bullets, task boxes, fences, table pipes. It stays on screen, faint, and gets stronger on the caret's line. A Syntax range is decoration: the stars around bold, the hashes before a heading, the (https://…) after a link. It hides.

Hiding without deleting

You can't remove hidden syntax from the text, because then the text isn't what you typed. You can't leave it at full size in a transparent colour either, because then there are invisible gaps in every line. Jot sets it to a font size of 0.01:

/// The size hidden syntax is set at: wide enough to lay out, too small to see.
const HIDDEN: f32 = 0.01;

// Away from the caret the syntax takes no room and no ink; the caret still steps
// over it, and entering the element (or selecting over it) brings it back.
Kind::Syntax if !revealed(&m) => span.size(HIDDEN).color(Color::TRANSPARENT),
Kind::Syntax => span.color(look.muted_foreground),
Kind::Marker if on_caret_line(&m) => span.color(look.muted_foreground),
Kind::Marker => span.color(faint),

At 0.01 the characters still exist for layout and for the caret. Arrow keys step over them one by one, so you never lose your place in the real text. They just take no visible room. When the caret enters the element, the same range gets a normal size and a muted colour.

Only what you touch comes back

The obvious rule is "show all syntax on the caret's line". Try it and every click into a long line makes the whole line shift sideways as a dozen ** and []() appear at once.

Jot's rule is narrower: an element shows its syntax only when the caret or selection touches that element's range.

// Touching counts: a caret just after `**bold**` still shows its stars.
let revealed = |m: &Mark| {
    st.caret.is_some_and(|(a, b)| a <= m.scope.1 && b >= m.scope.0)
};

Put the caret in the middle of a bold word and its two pairs of stars appear. The link three words later stays a link. A selection shows the syntax of everything inside it, so what you copy is what you see. The caret's line only changes the Markers, from faint to a little stronger, and they never change size.

The edge cases are CommonMark's

A hand-written regex for **bold** gets snake_case_name and 2 * 3 = 6 wrong. Using the parser's ranges means Jot agrees with GitHub about what is emphasis, what is a heading and what is just text. The tests pin the cases people actually type:

for plain in ["snake_case_name", "2 * 3 = 6", "a * b *", "**", "**open"] {
    assert!(!kinds(plain).iter().any(|(_, k)| matches!(k, Kind::Strong | Kind::Emphasis)), "{plain}");
}
assert!(kinds("#tag").is_empty());                  // no space: not a heading
assert!(kinds("-not a list").is_empty());           // no space: not a list

One case came from the renderer, not Markdown. Rune, the GPU UI framework Jot uses, places a line's strike-through from the line's first run of text. A done task like - [x] ship it starts with hidden syntax, and a 0.01-size first run put the strike at the wrong height. The fix keeps that first character at full size, still transparent, so the line is struck where the words are.

Source mode is the same text

The status bar has Live and Source. Source is the same text with the same ranges, in a monospaced font, with all syntax shown and coloured and no size changes. Switching converts nothing, so the file and the caret stay where they are.

What I'd tell you before you build one