Skip to main content

Crate mdwire

Crate mdwire 

Source
Expand description

mdwire — safely deliver agent-generated Markdown to chat channels.

It does three things in one pipeline. The order is the design.

  1. Normalize — LLM output is not valid CommonMark. Unbalanced emphasis, emphasis that spans lines, and unclosed code fences are routine. Repair them first.
  2. Render for the channel — translate into the syntax the target accepts. Syntax the target cannot accept is not worth parsing in the first place (see the Channel docs below).
  3. Split safely — at channel limits and streaming boundaries, never cut through markup.

§Why no dependencies

Existing parsers are all batch parsers — they take the whole document, build an AST, then render. That does not fit a problem where output must go out as tokens stream in. And the CJK-adjacent emphasis policy is baked into the parser, so with someone else’s parser you cannot change it. That is exactly the problem this library sets out to fix.

Modules§

width
Display width — the number of cells a character takes in a monospace font.

Structs§

HtmlOptions
The browser channel’s policy. The defaults are the most conservative — <br> line breaks, images as links only, and only http, https, and mailto links.
Options
Conversion options — the part limit and the browser channel’s policy.
Rendered
The result of render_with — the parts and the repairs.
Repairs
Counts of what normalization repaired and what was changed to fit the channel. The first four (repairs) measure how often the model breaks formatting; the last six (changes) measure what a channel changes, before adopting it — to ask about each separately, use Repairs::any and Repairs::changed.
Streamer
Streaming converter.

Enums§

Channel
Target channel. Each channel accepts a different syntax, and the narrower output decides the parsing scope.
Images
How to emit an image ![alt](url).
LineBreaks
How to emit line breaks inside a block.

Constants§

MIN_LIMIT
The smallest part limit a caller can set.

Functions§

render
Converts a finished document in one go. Splits at safe points when it exceeds the limit.
render_with
render with options; also returns what normalization repaired.