packages feed

nano-ui-markdown-0.2.0.0: lib/NanoUI/Markdown.hs

-- |
-- Module      : NanoUI.Markdown
-- Description : Markdown documents drawn with nano-ui
-- Copyright   : (c) 2026 Zachary Churchill
-- License     : MIT
-- Maintainer  : zacharyachurchill@gmail.com
--
-- Markdown drawn with nano-ui's rich text and layout. Parse a document once,
-- keep it in your model, and draw it every frame. 'markdown' returns the
-- link clicked this frame, if any:
--
-- Allocate @cache <- newMarkdownCache@ during component setup.
--
-- > view :: MarkdownCache -> MarkdownDoc -> NanoUI ()
-- > view cache doc = do
-- >   lastLink <- useVar ""
-- >   clicked <- markdown cache doc
-- >   for_ clicked (writeVar lastLink)
-- >   link <- readVar lastLink
-- >   unless (T.null link) $ label ("Clicked " <> link)
--
-- Grow a streamed reply with 'appendMarkdown'. When tokens arrive on a
-- worker thread, nano-ui's @useStream@ can append each one there:
--
-- Allocate @stream <- newStream@ once during setup.
--
-- > doc <- useStream stream replyId emptyMarkdown $ \update ->
-- >   onToken client (\token -> update (appendMarkdown token))
--
-- On the UI thread, append a frame's tokens in one call:
--
-- > onTokens :: [Text] -> MarkdownDoc -> MarkdownDoc
-- > onTokens tokens = appendMarkdown (T.concat tokens)
--
-- The built-in renderer lets users select and copy text in paragraphs,
-- headings, table cells and code blocks. Select All and Copy act on the
-- focused text block.
--
-- Appending reparses only the last top-level block, or less when the text
-- ends inside a list, table, fenced code or block quote.
-- "NanoUI.Markdown.Document" describes when more is reparsed.
--
-- 'mdBlock' overrides block drawing at any depth, given the widget's own
-- drawing to fall back to or wrap:
--
-- > highlighted :: MarkdownConfig
-- > highlighted = defaultMarkdownConfig {mdBlock = \own -> \case
-- >   CodeBlock "haskell" code -> Nothing <$ panel (richText (highlight code))
-- >   b -> own b}
--
-- Parsing uses @commonmark@ with GitHub tables, task lists (in bullet
-- lists), strikethrough and autolinks from @commonmark-extensions@. Raw HTML
-- is shown as text, with comments removed.
module NanoUI.Markdown
  ( -- * Documents
    MarkdownDoc
  , emptyMarkdown
  , parseMarkdown
  , appendMarkdown
  , markdownBlocks
  , markdownSource
  , markdownImages
  , parseMarkdownBlocks

    -- * Drawing
  , markdown
  , markdownWith
  , MarkdownCache
  , newMarkdownCache
  , markdownCacheSize
  , markdownConfigured
  , MarkdownConfig (..)
  , defaultMarkdownConfig

    -- * Syntax
  , module NanoUI.Markdown.Syntax
  ) where

import NanoUI.Markdown.Document
import NanoUI.Markdown.Syntax
import NanoUI.Markdown.Widget