nano-ui-markdown: Markdown rendering for nano-ui, parsed incrementally

[ graphics, library, mit, text ] [ Propose Tags ] [ Report a vulnerability ]

A Markdown widget for nano-ui, drawn with its rich text and layout, and documents parsed by the commonmark library that text can be appended to without parsing the whole document again, for chat messages that stream in.


[Skip to Readme]

Flags

Manual Flags

NameDescriptionDefault
sdl

Build the SDL3 window example. Off by default, so that depending on the library does not need nano-ui-sdl and SDL3.

Disabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.2.0.0
Change log CHANGELOG.md
Dependencies base (>=4.20 && <4.23), commonmark (>=0.3 && <0.4), commonmark-extensions (>=0.2.7 && <0.3), containers (>=0.7 && <0.9), deepseq (>=1.4 && <1.7), hashable (>=1.5 && <1.6), nano-ui (>=0.2 && <0.3), nano-ui-markdown, parsec (>=3.1 && <3.2), text (>=2.1 && <2.2), unicode-transforms (>=0.4 && <0.5) [details]
Tested with ghc ==9.10.3 || ==9.14.1
License MIT
Author goolord
Maintainer zacharyachurchill@gmail.com
Uploaded by goolord at 2026-12-03T05:00:39Z
Category Graphics, Text
Home page https://github.com/goolord/nano-ui
Bug tracker https://github.com/goolord/nano-ui/issues
Source repo head: git clone https://github.com/goolord/nano-ui.git(packages/nano-ui-markdown)
Distributions
Executables nano-ui-markdown-example
Downloads 0 total (0 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for nano-ui-markdown-0.2.0.0

[back to package description]

nano-ui-markdown

CommonMark rendering for nano-ui, backed by commonmark (with GitHub tables, task lists, strikethrough, and autolinks) and incremental appends for streamed text. Backend-independent.

{-# LANGUAGE OverloadedStrings #-}

import Control.Monad (unless)
import Data.Foldable (for_)
import Data.Text qualified as T
import NanoUI (NanoUI, label, readVar, useVar, writeVar)
import NanoUI.Markdown

readme :: MarkdownDoc
readme = parseMarkdown "# Hello\n\nSome *emphasis*, `code`, and [a link](https://example.com)."

-- Allocate cache <- newMarkdownCache once during setup.
view :: MarkdownCache -> NanoUI ()
view cache = do
  lastLinkVar <- useVar ""
  clicked <- markdown cache readme
  for_ clicked (writeVar lastLinkVar)
  lastLink <- readVar lastLinkVar
  unless (T.null lastLink) $ label ("Clicked " <> lastLink)

markdown returns Just url on the frame a link is clicked. Parse static Markdown once and hold the MarkdownDoc rather than reparsing every frame.

Incremental streaming

appendMarkdown chunk doc produces the same AST as reparsing from scratch while only reparsing the trailing block (plain words appended to a paragraph skip the parser altogether). Unchanged earlier blocks keep their widget IDs and skip re-measurement and repainting. Concatenate chunks arriving within a single frame into one appendMarkdown call.

-- Allocate stream <- newStream once during setup.
replyView :: MarkdownCache -> Stream Int MarkdownDoc -> Client -> Int -> NanoUI ()
replyView cache stream client replyId = do
  doc <- useStream stream replyId emptyMarkdown $ \update ->
    onToken client (\token -> update (appendMarkdown token))
  void (markdown cache doc)

Customization

Use markdownConfigured cache config doc to override fonts, colors, images, and block rendering (Nothing fields fall back to the active theme):

markdownConfigured
  cache
  defaultMarkdownConfig
    { mdText = fontSize 15
    , mdImage = \src -> lookup src loadedImages
    , mdInlineCodeBackground = Just (colorRGBA 60 64 72 255)
    , mdCodeBlock = background (colorRGBA 30 32 36 255)
    }
  doc

Use markdownImages to collect image URLs for preloading, and override mdBlock to replace specific block types (such as custom syntax highlighting or async image loading) while delegating everything else. See MarkdownConfig in NanoUI.Markdown for the full list of fields.

Documentation