Skip to main content

Module markdown_lines

Module markdown_lines 

Source
Expand description

A lean, line-level markdown styler shared by surfaces that want to style markdown without editing it (currently the Ask workspace’s answer body).

Design constraints that shape it:

  • Emphasis sigils are hidden; structural markers stay. Balanced **/__ (bold) and */_ (italic) delimiters are dropped from the rendered text (the run between them is styled instead), matching what a reader expects. Everything else stays visible: # headings, > quotes, fences, list markers, and citation [n] markers. Because hiding breaks the old 1:1 byte↔column identity, style_slice_mapped emits, alongside the styled line, a column map (rendered char index → source byte offset) so callers can still hit-test a click back to the right source byte. (This is why we don’t reuse the editor’s ParsedBuffer, which fully re-lays-out the visual line.)
  • Only same-line, balanced pairs are hidden. A sigil is hidden only when an opener and a closer of the same kind appear in the same wrapped slice (the per-slice approximation — we never look across the wrap boundary). A lone * (an unmatched sigil, a bullet, arithmetic) stays visible and does not emphasize anything. Sigils inside inline code are literal.
  • Intraword _ is not emphasis. Following CommonMark, a _/__ run may open only when the char before it is absent/non-alphanumeric and close only when the char after it is — so snake_case and foo_bar_baz identifiers render verbatim. */** keep the laxer rule (intraword * is legal).
  • Citations are the citations module’s job. [n] markers are found only through crate::ask::citations::scan; we merely style the ranges it reports. Code (fenced blocks and inline spans) is never citation-styled.

The unit of work is one logical source line, split across two layers:

  • Block identity is not ours to decide. classify_block_kinds hands the whole answer to the editor’s buffer-aware markdown model (crate::components::text_editor::markdown::ParsedBuffer, the real pulldown-cmark classifier) and maps each row’s result onto a LineKind. There is exactly one opinion about what a line is, and it is the editor’s — so answers and the note editor never disagree, and cross-line constructs the model resolves natively (unclosed fences, setext underlines, lazily-continued blockquotes) come along for free.
  • Inline styling stays here. style_slice_mapped styles one wrapped visual slice of a line (given its LineKind) and returns its column map — this is the answer-domain layer (emphasis hiding, citation styling, col_map) the editor’s fully-relaid ParsedBuffer render can’t provide.

Structs§

MdStyles
The semantic styles the answer body renders with, resolved from the theme once per render and reused across every line.

Enums§

LineKind
The block role of one logical source line.

Functions§

classify_block_kinds
Classify the block role of every logical (newline-free) source line in lines, in order, by delegating wholesale to the editor’s markdown model (ParsedBuffer::parse) — so there is a single, buffer-aware opinion about block identity shared with the note editor.
style_slice_mapped
Style one wrapped visual slice of a logical line whose block role is kind, returning the styled Line and its column map: map[k] is the source byte offset (into slice) of the k-th rendered character.