Skip to content

Expose prepared output parts separately from md rendering - #71

Closed
jackhogan wants to merge 2 commits into
mainfrom
notebook-cells
Closed

jackhogan wants to merge 2 commits into
mainfrom
notebook-cells

Conversation

@jackhogan

Copy link
Copy Markdown

This is the aidialog part of the dialog/document work in AnswerDotAI/mdhtml#91. The document importer needs to know whether a stored output is text, markdown, HTML or an image without first rendering it to markdown and then interpreting that again. The output selection and preparation already happened here inside render_md, so this makes those prepared parts available separately.

I also removed the ```output language label from plain-text output fences (mdhtml records its role as output in the document)

Implementation details and design decisions (AI-written)

Separate preparation from serialization

out_parts(outputs, html1st=True) is now public. It returns an ordered list of (kind, text) pairs using four kinds:

  • txt: plain text with ANSI escapes removed.
  • html: HTML, SVG, or JavaScript wrapped in a script element.
  • md: Markdown or LaTeX content.
  • img: a raster image represented as a data URI.

MIME selection still uses the existing preferred_msg_out behavior. HTML remains preferred by default; html1st=False prefers Markdown when available.

Consecutive text outputs are pooled into one txt part. Encountering a non-text part flushes that pool, preserving output order. Stream handling continues to use concat_streams.

This gives document consumers access to the prepared content before Markdown-specific fences and image syntax are added. No dependency on mdhtml is introduced.

One Markdown writer for prepared outputs

render_md_parts(parts) serializes prepared parts:

  • Text uses an unlabelled code fence.
  • HTML uses a Pandoc {=html} raw fence.
  • Markdown is inserted directly.
  • Images use Markdown image syntax with their data URI.

render_md(outputs, html1st=True) retains its existing entry point and becomes the composition of out_parts and render_md_parts. Callers that already prepared the outputs can use the same writer without repeating MIME selection or maintaining another renderer.

The previous private _render_md mixed MIME interpretation with Markdown serialization. Its preparation work now lives in _out_part and out_parts.

Compatibility

Plain-text output fences change from a fence labelled output to a bare fence. Consumers that identify output regions using that language label need adjusting. Other output-format conventions remain the same.

These APIs are required by the companion mdhtml changes. The release version and mdhtml’s minimum aidialog dependency need coordinating.

Documentation and validation

The implementation, examples and assertions are updated in nbs/01_dialog.ipynb; the Python module and symbol index are regenerated exports.

The notebook checks HTML/Markdown preference, prepared HTML and image parts, image serialization, error rendering, and text containing backticks.

nbdev-test nbs/01_dialog.ipynb passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant