packages feed

nano-ui-markdown-0.2.0.0: README.md

# nano-ui-markdown

CommonMark rendering for [nano-ui](https://github.com/goolord/nano-ui), backed by [commonmark](https://hackage.haskell.org/package/commonmark) (with GitHub tables, task lists, strikethrough, and autolinks) and incremental appends for streamed text. Backend-independent.

```haskell
{-# 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.

```haskell
-- 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):

```haskell
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

- [The nano-ui Book: Markdown](https://goolord.github.io/nano-ui/book/packages/index.html#markdown): Streaming, styling, and custom block renderer examples.