Skip to content

Markdown widget in a new eggshell-ui-extras crate (#254, #134) - #268

Merged
samitbasu merged 5 commits into
mainfrom
markdown-254
Nov 16, 2026
Merged

samitbasu merged 5 commits into
mainfrom
markdown-254

Conversation

@samitbasu

@samitbasu samitbasu commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

The Markdown widget, in the new eggshell-ui-extras crate. Stacked on #264 (rich text, #249): the base is richtext-249; once #264 merges I'll merge main in and retarget to main.

Crate (#134)

crates/eggshell-ui-extras (eggshell_ui_extras), on eggshell-ui's public API only. Widgets are opt-in features; markdown brings pulldown-cmark (no default features). eggshell-ui-shadcn gains an optional extras feature that fills the extras' styles from its tokens; extras never depends on shadcn.

API

use eggshell_ui_extras::markdown::{Directives, MarkdownExt};
b.markdown(key, &source)
    .on_link(|app, href| ..)   // relative links; "#anchor" naming a heading scrolls to it; URL schemes open through ctx.open_url (#264)
    .directives(&registry)     // Directives<A>: fence word -> fn(&mut Scope<A>, &Directive)
    .add();
markdown::headings(&source)    // level, anchor, text: for an outline / "On this page"

MarkdownStyle::from_style(&Style), MarkdownStyle::of(ctx), install_for(ctx, theme); shadcn: Shadcn::markdown(&Style), installed by apply under extras.

Shape

  • parse (pure, unit-tested in extras): pulldown-cmark events become blocks through a container stack (root, quote, list, item); inline events become Spans, text of one style merged into one span. Headings get GitHub-style anchors (an explicit {#id} wins; duplicates get -1, -2). Images are inline in Markdown and blocks here, splitting their paragraph.
  • Cache: egui's FrameCache in the context's memory, keyed by the source. Sound because blocks are a function of the source alone; links, directives and style are applied at build (stated in a comment). Agreed with the coordinator instead of per-layout state.
  • build: a paragraph is b.spans; a heading is b.spans keyed by its anchor (ids survive edits above); a list is (marker, content) rows; a quote is a column with a left border; a code fence is a monospace block, or a mounted directive when its first info word is registered; an image loads through egui's loaders and shows at its texture size (alt text while pending, a "not shown" box on error); --- is a rule.
  • Degrading: tables (until Table: rows and columns with per-column alignment, row rules and a rounded container #20) and HTML blocks show their source in a box labelled "Not shown: table/HTML"; inline HTML shows as inline code; extensions left off (strikethrough, math, footnotes) read as their literal text.
  • List markers are painted, not written: egui's fonts have no ◦ ▪ ☐ ☑ (the first run panicked on a missing glyph). Each marker is a one-line span whose draw closure paints a disc, circle, square or task box on that line.

Visual defaults (the user's to revisit)

After shadcn/ui's typography page:

  • h1 text-4xl 36/40 bold (shadcn: extrabold, which Weight lacks); h2 text-3xl 30/36 semibold with border-b pb-2; h3 24/32, h4 20/28 semibold; h5 18/28, h6 16/24 semibold (shadcn has none); tracking-tight on all. Top margins: h2 mt-10, h3 mt-8, others mt-6.
  • Blocks mt-6 apart; inside list items, mt-2 (as items are).
  • Lists ml-6 (a 6-unit marker column, 2-unit gap), items mt-2, markers muted-foreground; nested bullets disc → circle → square, as browsers do.
  • Quote border-l-2 pl-6 italic, border colour, foreground text.
  • Code block bg-muted rounded-lg p-4, monospace at 0.875 em, no border.
  • Rule and h2 line: border. Unsupported box: border, 1 pt, rounded-lg, p-3, label muted-foreground.
  • Paragraph text: the app's base text (shadcn's leading-7 is not imposed).

Follow-ups

Tests

  • extras unit tests (11): inline styles to spans; breaks; heading levels, anchors, dedupe, {#id}; nested and ordered lists with tasks; quotes holding blocks; code info and text; images splitting paragraphs; rules; tables and HTML keeping their source; disabled extensions reading as text; headings through quotes and lists. Each was seen failing first against a todo!().
  • tests/it/markdown.rs (14), through the rig on the test page: every text block drawn; heading sizes and anchor keys; the outline; the h2 rule; markers one line tall on the first line with a painted disc; ordered numbering; the quote border; the code fill and monospace; the image at its texture size; an internal link scrolls its heading to the top and skips on_link; a relative link calls on_link and the page shows it; an external link opens and skips on_link; a registered directive mounts a live counter; the shadcn preset installs its MarkdownStyle. Each was checked to fail with its piece removed (items_start, on_link pass-through, heading ids, directive lookup, heading rule).
  • Goldens markdown_light and markdown_dark (720×1540).

cargo xtask ci: exit 0, run in the foreground on fresh main (5fbc2c2) plus #264.

Needs a manual trial (UI): cargo xtask example markdown, via the coordinator on 8851.

Fixes #254
After the trial: a #anchor link naming a heading in the document now scrolls to it, as in a browser (Shell::scroll_into_view, Align::Min); it is no longer passed to on_link, while an anchor naming no heading still is. The test page's sentence links internal (#directives), relative (guide.md, shown in the status line) and external; the goldens change in that line only.

Fixes #134

🤖 Generated with Claude Code

https://claude.ai/code/session_01PWiKKf74MNWrhkW1CvKVUP

Apps (the docs site first) need to show Markdown as eggshell nodes. The
new `eggshell-ui-extras` crate holds composites written on the public
API alone; each widget is an opt-in feature, and `markdown` brings
pulldown-cmark.

- Two stages. `parse` walks pulldown-cmark's events with a container
  stack into blocks whose text is `Spans` (#249); `build` turns blocks
  into nodes. The parse is cached in egui's FrameCache keyed by the
  source: the blocks are a function of the source alone, as links,
  directives and style apply at build.
- `b.markdown(key, src).on_link(..).directives(&reg).add()`, through
  the `MarkdownExt` trait. External links open; internal ones go to
  on_link. A fence whose first info word names a registered directive
  mounts it in place of the code.
- Headings are keyed by their GitHub-style anchor; `headings(src)` lists
  them for an outline. Lists are (marker, content) rows with painted
  markers (disc, circle, square, task box) and numbers; quotes are a
  bordered column; code is a monospace block; images load through
  egui's loaders at their texture size. Tables and HTML show their
  source in a labelled box; disabled extensions read as their text.
- `MarkdownStyle` (extras) defaults from core's `Style`; the shadcn
  crate's new `extras` feature fills it from tokens on `apply`.
- examples/markdown.rs is the test page, with light/dark goldens.
- The text-test helpers (galley_of, part_rect, section_of,
  opened_urls) move into tests/it/support for both suites.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PWiKKf74MNWrhkW1CvKVUP
@samitbasu

Copy link
Copy Markdown
Owner Author

Manual trial (user, 2026-11-15): the internal link doesn't seem to do anything; otherwise it looks good, apart from tables (waiting on #20).

@samitbasu

Copy link
Copy Markdown
Owner Author

Gate (my run, the branch merged with current main): cargo xtask ci (with fuzz) exits 0.

samitbasu and others added 2 commits November 15, 2026 21:47
The trial found the test page's internal link did nothing: on_link got
"#lists" and the page only recorded it. Following an anchor within the
document is the widget's job, as it is a browser's, and only the widget
knows its headings' node ids (they sit under its private keys, nested
in quotes and lists). So the widget now records each heading's id as it
builds, and a link "#anchor" naming one of them scrolls every container
holding it to put the heading at the top (Shell::scroll_into_view,
Align::Min). Other links go to on_link as before; external ones still
open.

Behavior change: an anchor that names a heading in the document is no
longer passed to on_link. One naming no heading still is.

The test page's sentence now has an internal link to "#directives" (far
enough down to scroll), a relative link "guide.md" that the status line
shows, and the external link; the goldens change in that line only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PWiKKf74MNWrhkW1CvKVUP
@samitbasu

Copy link
Copy Markdown
Owner Author

Gate (my run on the fixed head, merged with main): cargo xtask ci (with fuzz) exits 0. The fixed build is on the trial site for re-check.

samitbasu and others added 2 commits November 16, 2026 10:54
The library must not panic in any build (#279).

- The document's top-level blocks are the walk's own list, not a Root on
  the container stack, so "the root is never closed" holds by
  construction and its expect and unreachable!s are gone.
- pulldown-cmark balances what it opens; should it not, a close with
  nothing open is ignored, an item outside a list keeps its blocks, and
  containers still open at the end are closed (TODO(#279) to report).
  Tested by feeding the walk unbalanced events.
- Emphasis and strong counts saturate; an ordered list's numbers
  saturate.
- A table or HTML block's source is read with `get`.
- Heading styles are destructured by level and the bullet shape is a
  match, not runtime indexing.

Nothing drawn changes; no golden moves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PWiKKf74MNWrhkW1CvKVUP
@samitbasu
samitbasu changed the base branch from richtext-249 to main November 16, 2026 20:16
@samitbasu
samitbasu merged commit 772e4f9 into main Nov 16, 2026
4 checks passed
samitbasu added a commit that referenced this pull request Nov 16, 2026
No textual conflicts. The new table (src/owned/table.rs) called the old
`div_id_with(id, key, ..)`; it now passes a Slot (the row's own, and
`slot_of` for each cell's id), so a repeated key there gets its own id
like everywhere else. The merged code adds no debug-misuse panics in
WP1's scope (the markdown builder's `shell()` is the builder's, which
cannot panic).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PWiKKe8zfqGfVMCSC5L3up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant