lurq 0.48.2

A Rust UI toolkit with typed components, retained state, reactive signals, GPU rendering, and DevTools.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
use std::sync::Arc;

use crate::{
  app::{
    component::Component,
    ctx::Ctx,
    theme::{MarkdownBlockStyle, ThemeMarkdown},
  },
  components::{Column, Rect, Row, Text},
  core::{Memo, Signal},
  layout::{
    Alignment,
    layout_kind::Justify,
    quad::RichTextSpan,
    text_style::{FontWeight, TextAlign, TextStyle},
  },
  markdown::{
    MarkdownBlock, MarkdownCodeBlockKind, MarkdownDocument, MarkdownHeadingLevel, MarkdownInline, MarkdownListItem,
    MarkdownTableAlignment, MarkdownTableRow, markdown_html_text, markdown_inline_rich_text, parse_markdown,
  },
  node::{Element, Node, color::Color, dimension::Dimension},
};

mod bidi_paragraphs;
mod link_block;
mod links;
mod list_view;
mod paragraph;
mod sizing;
mod tooltip;

pub use links::{MarkdownLink, MarkdownLinkPresentation, MarkdownRun};
pub use list_view::MarkdownList;
use list_view::MarkdownListView;
use paragraph::{FlowItem, LinkSession, MarkdownLinkActivator, MarkdownLinkCallback, MarkdownLinkPresenter, Paragraph};
use sizing::{BlockWidth, FILL_WIDTH};
use tooltip::{LinkTooltips, LinkTooltipsProps, TooltipHover};

#[derive(Clone)]
pub struct MarkdownProps {
  source: Arc<str>,
  style: TextStyle,
  theme: Option<ThemeMarkdown>,
  selectable: bool,
  width: Option<Dimension>,
  fit_content: bool,
  on_link_click: Option<MarkdownLinkCallback>,
  on_link_activate: Option<MarkdownLinkActivator>,
  link_presentation: Option<MarkdownLinkPresenter>,
  list_view: Option<MarkdownListView>,
}

impl PartialEq for MarkdownProps {
  fn eq(&self, other: &Self) -> bool {
    self.source == other.source
      && self.style == other.style
      && self.theme == other.theme
      && self.selectable == other.selectable
      && self.width == other.width
      && self.fit_content == other.fit_content
      && same_callback(&self.on_link_click, &other.on_link_click)
      && same_callback(&self.on_link_activate, &other.on_link_activate)
      && same_callback(&self.link_presentation, &other.link_presentation)
      && same_callback(&self.list_view, &other.list_view)
  }
}

impl MarkdownProps {
  pub fn new(source: impl Into<Arc<str>>) -> Self {
    Self {
      source: source.into(),
      style: TextStyle::default(),
      theme: None,
      selectable: false,
      width: None,
      fit_content: false,
      on_link_click: None,
      on_link_activate: None,
      link_presentation: None,
      list_view: None,
    }
  }

  pub fn styled(source: impl Into<Arc<str>>, style: TextStyle) -> Self {
    Self::new(source).style(style)
  }

  pub fn style(mut self, style: TextStyle) -> Self {
    self.style = style;
    self
  }

  pub fn theme(mut self, theme: ThemeMarkdown) -> Self {
    self.theme = Some(theme);
    self
  }

  pub fn selectable(mut self, selectable: bool) -> Self {
    self.selectable = selectable;
    self
  }

  pub fn width(mut self, width: impl Into<Dimension>) -> Self {
    self.width = Some(width.into());
    self
  }

  /// Sizes the document to its content instead of filling its container,
  /// like CSS `width: fit-content; max-width: 100%`: the document is as wide
  /// as its widest block, and never wider than its container, where its
  /// text wraps. Off by default: the document and every block in it fill the
  /// container's width.
  ///
  /// A short message is as wide as its longest line, so a chat bubble around
  /// it hugs its text. Every block is then stretched to the document's
  /// width, as CSS blocks are: a code block's box and a rule span the widest
  /// block. A code block does not wrap, so it is as wide as its longest line
  /// up to the container's width, and a longer line overflows its box as it
  /// does when filling. A table still fills the container's width, since its
  /// columns share that width and line up across its rows. An explicit
  /// [`width`](Self::width) sets the document's width as before.
  ///
  /// The container's width is the most a `Column` gives its children. A `Row`
  /// gives its children unbounded width, so a bubble in a row needs a
  /// `max_width` of `Dimension::Pct(100.0)` (or a `flex_shrink` factor) for
  /// its text to wrap at the row's width.
  pub fn fit_content(mut self, fit_content: bool) -> Self {
    self.fit_content = fit_content;
    self
  }

  /// Called when a link is followed: clicked with the left button, or Enter
  /// while it has keyboard focus. It runs inside the input's dispatch, so
  /// [`is_synthetic_input`](crate::app::events::is_synthetic_input) tells a
  /// person's press from synthetic input (an MCP client's). It is given the
  /// same [`MarkdownLink`] the presentation hook saw, with its
  /// [`index`](MarkdownLink::index). Without it and without
  /// [`on_link_activate`](Self::on_link_activate), a link to an in-app path
  /// (`/`, `#` or `?`) navigates the router, when there is one.
  pub fn on_link_click(mut self, f: impl Fn(&MarkdownLink) + Send + Sync + 'static) -> Self {
    self.on_link_click = Some(Arc::new(f));
    self
  }

  /// Called when a link is activated, with the input that did it: a left
  /// click, a middle click, or Enter while the link has keyboard focus. The
  /// [`MouseEvent`](crate::app::events::MouseEvent) says which: its `button`
  /// (`Left` or `Middle`; `Left` for Enter), its modifiers (`shift`, `ctrl`,
  /// `alt`, and `meta`: Cmd on macOS, the Windows key on Windows) and
  /// `from_keyboard` for Enter. An app follows browser rules with it: a
  /// middle click or Ctrl+click (Cmd+click on macOS) opens the link in the
  /// background, Ctrl+Shift+click opens it in front.
  ///
  /// A middle click is a press and a release of the middle button on the
  /// same link, like a left click. Its press starts no text selection or
  /// drag. A press released off the link activates nothing, and neither
  /// does a press on one link released on another more than a few pixels
  /// away; closer, the link under the release activates, as with a left
  /// click.
  /// The callback runs inside the input's dispatch, so
  /// [`is_synthetic_input`](crate::app::events::is_synthetic_input) tells a
  /// person from an MCP client here too.
  ///
  /// It runs before [`on_link_click`](Self::on_link_click), which still
  /// gets left clicks and Enter when both are set. With it, a link to an
  /// in-app path no longer navigates the router by itself.
  pub fn on_link_activate(
    mut self,
    f: impl Fn(&MarkdownLink, &crate::app::events::MouseEvent) + Send + Sync + 'static,
  ) -> Self {
    self.on_link_activate = Some(Arc::new(f));
    self
  }

  /// Decides how each link is shown: as a link with its own looks, trailing
  /// runs and tooltip, or as text ([`MarkdownLinkPresentation`]). It is
  /// called once per link and render with the exact link the document
  /// follows, so what a link shows always comes from the destination it
  /// opens. Without it, every link is a link in the theme's looks.
  ///
  /// A link stays inside its paragraph's text, which wraps and selects as
  /// one. A link is a Tab stop (in document order), Enter and a click follow
  /// it, and the pointer over it shows a pointer cursor. Its hovered and
  /// focused looks recolor its words, underline and trailing runs and can
  /// fill behind its words; keyboard focus also draws a ring around them.
  ///
  /// Keep the props (or the closure's `Arc`) across renders where you can:
  /// props with a new closure compare unequal and render the document again.
  pub fn link_presentation(
    mut self,
    f: impl Fn(&MarkdownLink) -> MarkdownLinkPresentation + Send + Sync + 'static,
  ) -> Self {
    self.link_presentation = Some(Arc::new(f));
    self
  }

  /// Lets the app draw a list block itself: called for every list of the
  /// document, it returns the element to draw instead, or `None` to draw the
  /// list as usual. Decide from [`MarkdownList::items`] before drawing
  /// anything with the list's `inlines` or `blocks`, since drawing numbers
  /// the links it draws.
  pub fn list_view(mut self, f: impl Fn(&MarkdownList<'_>) -> Option<Element> + Send + Sync + 'static) -> Self {
    self.list_view = Some(Arc::new(f));
    self
  }
}

fn same_callback<T: ?Sized>(left: &Option<Arc<T>>, right: &Option<Arc<T>>) -> bool {
  match (left, right) {
    (None, None) => true,
    (Some(left), Some(right)) => Arc::ptr_eq(left, right),
    _ => false,
  }
}

#[cfg(feature = "devtools")]
impl crate::app::component::DevtoolsInspectable for MarkdownProps {
  fn write_info(&self, buffer: &mut Vec<crate::app::component::ComponentInfo>) {
    buffer.push(crate::app::component::ComponentInfo::with_value(
      "source",
      std::any::type_name::<Arc<str>>(),
      format!("len={}", self.source.len()),
    ));
  }
}

pub struct Markdown {
  source: Signal<Arc<str>>,
  document: Memo<MarkdownDocument>,
  hover: TooltipHover,
}

struct MarkdownRenderContext<'a> {
  width: BlockWidth,
  links: &'a LinkSession,
  list_view: Option<&'a MarkdownListView>,
}

impl Markdown {
  pub fn mount(ctx: &mut Ctx, props: MarkdownProps) -> Element {
    ctx.mount::<Self>(props)
  }
}

impl Component for Markdown {
  type Props = MarkdownProps;

  fn create(ctx: &mut Ctx) -> Self {
    let source = ctx.signal(ctx.props::<Self::Props>().source.clone());
    let memo_source = source.clone();
    let document = ctx.memo(move || parse_markdown(&memo_source.get()));
    let hover = TooltipHover::new(|fire| ctx.create_timeout(std::time::Duration::ZERO, fire));
    Self {
      source,
      document,
      hover,
    }
  }

  fn render(&self, ctx: &mut Ctx) -> impl Into<Element> {
    let props = ctx.props::<Self::Props>().clone();
    if self.source.get_untracked() != props.source {
      self.source.set(props.source.clone());
    }
    let theme = props.theme.clone().unwrap_or_else(|| ctx.theme().markdown().clone());
    self.hover.set_delay(theme.link_tooltip_delay);
    let document = self.document.get();
    let links = LinkSession::new(
      props.link_presentation.clone(),
      props.on_link_click.clone(),
      props.on_link_activate.clone(),
      #[cfg(feature = "router")]
      ctx.use_context::<crate::router::Navigator>(),
      self.hover.clone(),
    );
    let render_ctx = MarkdownRenderContext {
      width: BlockWidth::new(props.fit_content),
      links: &links,
      list_view: props.list_view.as_ref(),
    };
    let mut root = render_document(&document, &props.style, &theme, &render_ctx);
    let tooltips = links.take_tooltips();
    if !tooltips.is_empty() {
      root = root.child(ctx.mount::<LinkTooltips>(LinkTooltipsProps {
        tooltips: tooltips.into(),
        shown: self.hover.shown(),
        style: props.style.clone(),
        text: theme.link_tooltip.clone(),
        panel: theme.link_tooltip_box.clone(),
      }));
    }
    let mut element: Element = root.into();

    if let Some(width) = props.width {
      element = element.map_node(|node| node.width(width));
    }
    if props.selectable {
      element.node.selectable_recursive(true);
    }
    element
  }
}

/// The document's own containers (the document, flows, lists, quotes,
/// footnotes) let what their blocks draw past them show, as CSS blocks do: a
/// link's focus ring reaches outside its words. Code blocks and tables keep
/// clipping to their boxes.
fn render_document(
  document: &MarkdownDocument,
  base_style: &TextStyle,
  theme: &ThemeMarkdown,
  render_ctx: &MarkdownRenderContext<'_>,
) -> Column {
  let mut root = render_ctx
    .width
    .blocks(Column::new().overflow_visible().spacing(theme.document_spacing));
  for block in &document.blocks {
    root = root.child(render_block(block, base_style, theme, render_ctx));
  }
  root
}

fn render_block(
  block: &MarkdownBlock,
  base_style: &TextStyle,
  theme: &ThemeMarkdown,
  render_ctx: &MarkdownRenderContext<'_>,
) -> Element {
  match block {
    MarkdownBlock::Paragraph(inlines) => render_inline_block(inlines, base_style.clone(), theme, render_ctx),
    MarkdownBlock::Heading { level, children } => {
      render_inline_block(children, heading_style(theme, *level, base_style), theme, render_ctx)
    }
    MarkdownBlock::BlockQuote(blocks) => render_blockquote(blocks, base_style, theme, render_ctx),
    MarkdownBlock::List { ordered, start, items } => {
      render_list(*ordered, *start, items, base_style, theme, render_ctx)
    }
    MarkdownBlock::Table { alignments, rows } => render_table(alignments, rows, base_style, theme, render_ctx),
    MarkdownBlock::CodeBlock { kind, text } => render_code_block(kind, text, base_style, theme, render_ctx.width),
    MarkdownBlock::Math { text } => render_math_block(text, base_style, theme, render_ctx.width),
    MarkdownBlock::FootnoteDefinition { label, blocks } => {
      render_footnote_definition(label, blocks, base_style, theme, render_ctx)
    }
    MarkdownBlock::Html(text) => render_ctx.width.leaf(Text::styled(
      &markdown_html_text(text.trim_end_matches('\n')),
      base_style.clone(),
    )),
    MarkdownBlock::ThematicBreak => render_ctx
      .width
      .leaf(Rect::new(Dimension::Auto, 1.0).background(theme.table_marker.apply(base_style).color)),
  }
}

/// A paragraph, a heading or a table cell: one rich-text node, links in it.
/// Images other than a lone one break it into a wrapping row of texts and
/// images.
fn render_inline_block(
  inlines: &[MarkdownInline],
  style: TextStyle,
  theme: &ThemeMarkdown,
  render_ctx: &MarkdownRenderContext<'_>,
) -> Element {
  if let Some(image) = single_image(inlines) {
    return render_markdown_image(image, &style, theme);
  }

  if let [MarkdownInline::Text(text)] = inlines {
    return render_ctx.width.leaf(Text::styled(text, style));
  }

  if has_image(inlines) {
    return render_inline_flow(inlines, style, theme, render_ctx);
  }

  let mut paragraph = Paragraph::new(inlines, false, theme, render_ctx.links);
  paragraph.push_inlines(inlines, &style, &mut None);
  render_ctx.width.leaf(Element::from_node(paragraph.take_node()))
}

fn render_inline_flow(
  inlines: &[MarkdownInline],
  style: TextStyle,
  theme: &ThemeMarkdown,
  render_ctx: &MarkdownRenderContext<'_>,
) -> Element {
  let mut hard_break_segments = inlines.split(|inline| matches!(inline, MarkdownInline::HardBreak));
  let Some(first_segment) = hard_break_segments.next() else {
    return render_inline_flow_row(inlines, &style, theme, render_ctx);
  };

  let remaining_segments = hard_break_segments.collect::<Vec<_>>();
  if remaining_segments.is_empty() {
    return render_inline_flow_row(first_segment, &style, theme, render_ctx);
  }

  let mut column = render_ctx
    .width
    .blocks(Column::new().overflow_visible().spacing(0.0).min_width(0.0));
  column = column.child(render_inline_flow_row(first_segment, &style, theme, render_ctx));
  for segment in remaining_segments {
    column = column.child(render_inline_flow_row(segment, &style, theme, render_ctx));
  }
  column.into()
}

/// A wrapping row of the texts between images, and the images.
fn render_inline_flow_row(
  inlines: &[MarkdownInline],
  style: &TextStyle,
  theme: &ThemeMarkdown,
  render_ctx: &MarkdownRenderContext<'_>,
) -> Element {
  let mut flow = Vec::new();
  let mut paragraph = Paragraph::new(inlines, true, theme, render_ctx.links);
  paragraph.push_inlines(inlines, style, &mut Some(&mut flow));
  if !paragraph.is_empty() {
    flow.push(FlowItem::Text(Box::new(paragraph.take_node())));
  }
  let mut row = render_ctx.width.row(
    Row::new()
      .overflow_visible()
      .wrap()
      .spacing(0.0)
      .align_items(Alignment::Center)
      .min_width(0.0),
  );
  for item in flow {
    row = row.child(match item {
      FlowItem::Text(node) => Element::from_node((*node).max_width(FILL_WIDTH).min_width(0.0)),
      FlowItem::Image(image, style) => render_markdown_image(image, &style, theme),
    });
  }
  row.into()
}

/// Whether an image outside any link is among `inlines`: an image in a link
/// is part of the link's words (its alt text).
fn has_image(inlines: &[MarkdownInline]) -> bool {
  inlines.iter().any(|inline| match inline {
    MarkdownInline::Image { .. } => true,
    MarkdownInline::Emphasis(children) | MarkdownInline::Strong(children) | MarkdownInline::Strikethrough(children) => {
      has_image(children)
    }
    _ => false,
  })
}

fn single_image(inlines: &[MarkdownInline]) -> Option<&MarkdownInline> {
  let mut image = None;
  for inline in inlines {
    match inline {
      MarkdownInline::Image { .. } if image.is_none() => image = Some(inline),
      MarkdownInline::Text(text) if text.trim().is_empty() => {}
      MarkdownInline::SoftBreak | MarkdownInline::HardBreak => {}
      _ => return None,
    }
  }
  image
}

fn render_markdown_image(inline: &MarkdownInline, base_style: &TextStyle, theme: &ThemeMarkdown) -> Element {
  let MarkdownInline::Image {
    destination,
    title: _,
    alt,
  } = inline
  else {
    return Element::from_node(Node::rich_text(markdown_inline_rich_text(
      std::slice::from_ref(inline),
      base_style,
      theme,
    )));
  };
  let _ = destination;
  let _ = alt;

  #[cfg(all(feature = "image", feature = "resources"))]
  {
    return crate::components::Image::from_resource(destination)
      .max_width(FILL_WIDTH)
      .into();
  }

  #[cfg(not(all(feature = "image", feature = "resources")))]
  {
    let mut style = theme.inline_code.apply(base_style);
    if theme.inline_code.text.color.is_none() {
      style.color = Color::from_hex("#64748b");
    }
    Element::from_node(Node::rich_text(markdown_inline_rich_text(alt, &style, theme)))
  }
}

fn render_blockquote(
  blocks: &[MarkdownBlock],
  base_style: &TextStyle,
  theme: &ThemeMarkdown,
  render_ctx: &MarkdownRenderContext<'_>,
) -> Element {
  let mut body = render_ctx.width.row_body(
    Column::new()
      .overflow_visible()
      .spacing(theme.nested_block_spacing)
      .min_width(0.0),
  );
  for block in blocks {
    body = body.child(render_block(block, base_style, theme, render_ctx));
  }

  let bar_color = theme
    .blockquote_box
    .border_color
    .unwrap_or_else(|| theme.blockquote_marker.apply(base_style).color);
  let bar_width = theme.blockquote_box.border_width.unwrap_or(3.0);

  let mut row = render_ctx.width.row(
    Row::new()
      .overflow_visible()
      .spacing(theme.blockquote_gap)
      .align_items(Alignment::Stretch)
      .child(Rect::new(bar_width, FILL_WIDTH).background(bar_color))
      .child(body),
  );
  row = apply_row_box(row, &theme.blockquote_box, false);
  row.into()
}

fn render_list(
  ordered: bool,
  start: Option<u64>,
  items: &[MarkdownListItem],
  base_style: &TextStyle,
  theme: &ThemeMarkdown,
  render_ctx: &MarkdownRenderContext<'_>,
) -> Element {
  if let Some(list_view) = render_ctx.list_view {
    let list = MarkdownList {
      ordered,
      start,
      items,
      style: base_style,
      theme,
      render_ctx,
    };
    // A view that declines draws nothing that stays: forget the links it
    // drew to decide, so the list's own links are numbered as if it had not.
    let checkpoint = render_ctx.links.checkpoint();
    if let Some(element) = list_view(&list) {
      return element;
    }
    render_ctx.links.restore(checkpoint);
  }
  let mut list = render_ctx
    .width
    .blocks(Column::new().overflow_visible().spacing(theme.list_item_spacing));
  let start = start.unwrap_or(1);
  for (index, item) in items.iter().enumerate() {
    let marker = if ordered {
      format!("{}.", start + index as u64)
    } else {
      "-".to_owned()
    };
    let marker_style = theme.list_marker.apply(base_style);
    let marker_width = if ordered {
      theme.ordered_list_marker_width
    } else {
      theme.unordered_list_marker_width
    };
    let mut body = render_ctx.width.row_body(
      Column::new()
        .overflow_visible()
        .spacing(theme.nested_block_spacing)
        .min_width(0.0),
    );
    for block in &item.blocks {
      body = body.child(render_block(block, base_style, theme, render_ctx));
    }
    list = list.child(
      render_ctx.width.row(
        Row::new()
          .overflow_visible()
          .spacing(theme.list_marker_gap)
          .align_items(Alignment::Start)
          .child(Text::styled(&marker, marker_style).width(marker_width))
          .child(body),
      ),
    );
  }
  list.into()
}

fn render_code_block(
  kind: &MarkdownCodeBlockKind,
  text: &str,
  base_style: &TextStyle,
  theme: &ThemeMarkdown,
  width: BlockWidth,
) -> Element {
  let mut code_style = theme.code_block.apply(base_style);
  if theme.code_block.text.color.is_none() {
    code_style.color = Color::from_hex("#e2e8f0");
  }

  let mut column = width.blocks(Column::new().spacing(theme.code_block_spacing));
  if let MarkdownCodeBlockKind::Fenced {
    language: Some(language),
  } = kind
  {
    let mut label_style = theme.code_block_label.apply(base_style);
    if theme.code_block_label.text.color.is_none() {
      label_style.color = Color::from_hex("#94a3b8");
    }
    column = column.child(width.leaf(Text::styled(language, label_style)));
  }
  column = column.child(width.leaf(Element::from_node(
    Node::rich_text(highlight_code_spans(text.trim_end_matches('\n'), kind, &code_style)).text_wrap(false),
  )));
  column = apply_column_box(column, &theme.code_block_box, true);
  column.into()
}

fn render_math_block(text: &str, base_style: &TextStyle, theme: &ThemeMarkdown, width: BlockWidth) -> Element {
  let kind = MarkdownCodeBlockKind::Fenced {
    language: Some("math".to_owned()),
  };
  render_code_block(&kind, text, base_style, theme, width)
}

fn render_footnote_definition(
  label: &str,
  blocks: &[MarkdownBlock],
  base_style: &TextStyle,
  theme: &ThemeMarkdown,
  render_ctx: &MarkdownRenderContext<'_>,
) -> Element {
  let marker_style = theme.link.apply(base_style);
  let mut body = render_ctx
    .width
    .row_body(Column::new().overflow_visible().spacing(theme.nested_block_spacing));
  for block in blocks {
    body = body.child(render_block(block, base_style, theme, render_ctx));
  }

  render_ctx
    .width
    .row(
      Row::new()
        .overflow_visible()
        .spacing(theme.list_marker_gap)
        .align_items(Alignment::Start)
        .child(Text::styled(&format!("[^{label}]"), marker_style).width(theme.ordered_list_marker_width))
        .child(body),
    )
    .into()
}

fn highlight_code_spans(text: &str, kind: &MarkdownCodeBlockKind, base_style: &TextStyle) -> Vec<RichTextSpan> {
  if !is_rust_like_code(kind) {
    return vec![RichTextSpan {
      text: text.to_owned(),
      style: base_style.clone(),
    }];
  }

  let keyword_style = code_style(base_style, Color::from_hex("#93c5fd"));
  let string_style = code_style(base_style, Color::from_hex("#86efac"));
  let comment_style = code_style(base_style, Color::from_hex("#94a3b8"));
  let number_style = code_style(base_style, Color::from_hex("#fca5a5"));
  let mut spans = Vec::new();
  let mut rest = text;
  while !rest.is_empty() {
    if let Some(comment) = rest.strip_prefix("//") {
      let line_len = comment.find('\n').map(|index| index + 2).unwrap_or(rest.len());
      push_code_span(&mut spans, &rest[..line_len], comment_style.clone());
      rest = &rest[line_len..];
      continue;
    }
    let Some(ch) = rest.chars().next() else {
      break;
    };
    if ch == '"' {
      let len = quoted_len(rest);
      push_code_span(&mut spans, &rest[..len], string_style.clone());
      rest = &rest[len..];
      continue;
    }
    if ch.is_ascii_digit() {
      let len = rest
        .char_indices()
        .find(|(_, ch)| !ch.is_ascii_digit() && *ch != '.')
        .map(|(index, _)| index)
        .unwrap_or(rest.len());
      push_code_span(&mut spans, &rest[..len], number_style.clone());
      rest = &rest[len..];
      continue;
    }
    if ch == '_' || ch.is_ascii_alphabetic() {
      let len = rest
        .char_indices()
        .find(|(_, ch)| *ch != '_' && !ch.is_ascii_alphanumeric())
        .map(|(index, _)| index)
        .unwrap_or(rest.len());
      let token = &rest[..len];
      let style = if is_rust_keyword(token) {
        keyword_style.clone()
      } else {
        base_style.clone()
      };
      push_code_span(&mut spans, token, style);
      rest = &rest[len..];
      continue;
    }
    push_code_span(&mut spans, &rest[..ch.len_utf8()], base_style.clone());
    rest = &rest[ch.len_utf8()..];
  }
  spans
}

fn is_rust_like_code(kind: &MarkdownCodeBlockKind) -> bool {
  matches!(
    kind,
    MarkdownCodeBlockKind::Fenced {
      language: Some(language)
    } if matches!(language.as_str(), "rust" | "rs")
  )
}

fn quoted_len(text: &str) -> usize {
  let mut escaped = false;
  for (index, ch) in text.char_indices().skip(1) {
    if escaped {
      escaped = false;
      continue;
    }
    if ch == '\\' {
      escaped = true;
      continue;
    }
    if ch == '"' {
      return index + ch.len_utf8();
    }
  }
  text.len()
}

fn is_rust_keyword(token: &str) -> bool {
  matches!(
    token,
    "as"
      | "async"
      | "await"
      | "break"
      | "const"
      | "continue"
      | "crate"
      | "else"
      | "enum"
      | "false"
      | "fn"
      | "for"
      | "if"
      | "impl"
      | "in"
      | "let"
      | "loop"
      | "match"
      | "mod"
      | "move"
      | "mut"
      | "pub"
      | "ref"
      | "return"
      | "self"
      | "Self"
      | "static"
      | "struct"
      | "super"
      | "trait"
      | "true"
      | "type"
      | "unsafe"
      | "use"
      | "where"
      | "while"
  )
}

fn code_style(base_style: &TextStyle, color: Color) -> TextStyle {
  let mut style = base_style.clone();
  style.color = color;
  style
}

fn push_code_span(spans: &mut Vec<RichTextSpan>, text: &str, style: TextStyle) {
  if text.is_empty() {
    return;
  }
  if let Some(last) = spans.last_mut()
    && last.style == style
  {
    last.text.push_str(text);
    return;
  }
  spans.push(RichTextSpan {
    text: text.to_owned(),
    style,
  });
}

fn render_table(
  alignments: &[MarkdownTableAlignment],
  rows: &[MarkdownTableRow],
  base_style: &TextStyle,
  theme: &ThemeMarkdown,
  render_ctx: &MarkdownRenderContext<'_>,
) -> Element {
  let mut table = Column::new().width(FILL_WIDTH);
  let line_color = table_line_color(theme);
  for (row_index, row) in rows.iter().enumerate() {
    if row_index > 0
      && theme.table_row_spacing > 0.0
      && let Some(line_color) = line_color
    {
      table = table.child(Rect::new(FILL_WIDTH, theme.table_row_spacing).background(line_color));
    }
    let mut row_node = Row::new().align_items(Alignment::Stretch).width(FILL_WIDTH);
    for (cell_index, cell) in row.cells.iter().enumerate() {
      if cell_index > 0
        && theme.table_column_spacing > 0.0
        && let Some(line_color) = line_color
      {
        row_node = row_node.child(Rect::new(theme.table_column_spacing, FILL_WIDTH).background(line_color));
      }
      let alignment = alignments.get(cell_index).copied().unwrap_or_default();
      row_node = row_node.child(render_table_cell(
        row.header, cell, alignment, base_style, theme, render_ctx,
      ));
    }
    table = table.child(row_node);
  }
  table = apply_column_box(table, &theme.table_box, true);
  table.into()
}

fn table_line_color(theme: &ThemeMarkdown) -> Option<Color> {
  theme.table_box.border_color.or(theme.table_box.background)
}

fn render_table_cell(
  header: bool,
  cell: &[MarkdownInline],
  alignment: MarkdownTableAlignment,
  base_style: &TextStyle,
  theme: &ThemeMarkdown,
  render_ctx: &MarkdownRenderContext<'_>,
) -> Element {
  let mut style = if header {
    theme.table_header.apply(base_style)
  } else {
    theme.table_cell.apply(base_style)
  };
  if header {
    style.weight = FontWeight::Bold;
  }
  style.text_align = table_text_align(alignment);

  let box_style = if header {
    &theme.table_header_box
  } else {
    &theme.table_cell_box
  };
  if box_style.background.is_some() && style.color == base_style.color {
    style.color = Color::from_hex("#0f172a");
  }
  // The words are the cell's content, not the cell (see `apply_column_box`),
  // centered in its height as a stretched text leaf centers its words.
  let content = Column::new()
    .overflow_visible()
    .align_items(table_cell_alignment(alignment))
    .justify(Justify::Center)
    .child(render_inline_block(cell, style, theme, render_ctx));
  apply_column_box(content, box_style, true)
    .min_width(theme.table_cell_min_width)
    .flex(1.0)
    .into()
}

fn table_text_align(alignment: MarkdownTableAlignment) -> TextAlign {
  match alignment {
    MarkdownTableAlignment::None | MarkdownTableAlignment::Left => TextAlign::Left,
    MarkdownTableAlignment::Center => TextAlign::Center,
    MarkdownTableAlignment::Right => TextAlign::Right,
  }
}

/// Where a cell puts content narrower than the cell (an image, a fitting
/// text).
fn table_cell_alignment(alignment: MarkdownTableAlignment) -> Alignment {
  match alignment {
    MarkdownTableAlignment::None | MarkdownTableAlignment::Left => Alignment::Start,
    MarkdownTableAlignment::Center => Alignment::Center,
    MarkdownTableAlignment::Right => Alignment::End,
  }
}

/// Gives `column` the box `style`. A Markdown box is always a container
/// around its content, never the content itself: a `Text` or `RichText` leaf
/// with padding grows by it, but lurq paints the leaf's words over its whole
/// rect, padding included, so the words would start at the box's edge.
fn apply_column_box(mut column: Column, style: &MarkdownBlockStyle, include_border: bool) -> Column {
  if let Some(padding) = style.padding {
    column = column.padding(padding);
  }
  if let Some(background) = style.background {
    column = column.background(background);
  }
  if include_border && let (Some(width), Some(color)) = (style.border_width, style.border_color) {
    column = column.border_inside(width, color);
  }
  if let Some(radius) = style.radius {
    column = column.rounded(radius);
  }
  column
}

/// Gives `row` the box `style`; like [`apply_column_box`], never a text leaf.
fn apply_row_box(mut row: Row, style: &MarkdownBlockStyle, include_border: bool) -> Row {
  if let Some(padding) = style.padding {
    row = row.padding(padding);
  }
  if let Some(background) = style.background {
    row = row.background(background);
  }
  if include_border && let (Some(width), Some(color)) = (style.border_width, style.border_color) {
    row = row.border_inside(width, color);
  }
  if let Some(radius) = style.radius {
    row = row.rounded(radius);
  }
  row
}

fn heading_style(theme: &ThemeMarkdown, level: MarkdownHeadingLevel, base_style: &TextStyle) -> TextStyle {
  let shared = theme.heading.apply(base_style);
  match level {
    MarkdownHeadingLevel::H1 => theme.heading_1.apply(&shared),
    MarkdownHeadingLevel::H2 => theme.heading_2.apply(&shared),
    MarkdownHeadingLevel::H3 => theme.heading_3.apply(&shared),
    MarkdownHeadingLevel::H4 => theme.heading_4.apply(&shared),
    MarkdownHeadingLevel::H5 => theme.heading_5.apply(&shared),
    MarkdownHeadingLevel::H6 => theme.heading_6.apply(&shared),
  }
}