documents: GitHub alerts, a [toc] list of headings, front matter in a box, HTML blocks as text, a visible rule and bullets by depth

At the root a quote marked [!NOTE] and the rest, as GitHub's API reads it,
is an alert; a [toc] paragraph lists the headings and a press reveals one;
an HTML block shows its text with the inline tags, white space as a browser
lays it, scripts and styles dropped. The rule is 2px; bullets turn by depth.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-13 23:08:51 -04:00
1 parent 9ff403df31
commit 2eec25140d
15 files changed
+810 -118

No files matched your search

+14 -2
View File
@@ -19,13 +19,18 @@ use gpui::{
ListState, Pixels, Subscription, Task, Window,
};
pub use crate::documents::tree::DocumentHeading;
pub use attach::Attachment;
pub use look::DocumentLook;
pub use parse::DocumentHeading;
use parse::{Parsed, alike, parsed};
pub use tables::{TableEdit, TableRefusal};
use super::{MarkdownMode, markdown::markdown_highlights, modes::joint, render::OnLink};
use super::{
MarkdownMode,
markdown::markdown_highlights,
modes::joint,
render::{OnHeading, OnLink},
};
use crate::{
editor::{CodeEditor, EditorEvent, LineNumbers},
forms::TextInput,
@@ -94,6 +99,7 @@ pub struct MarkdownDocument {
centered: Option<(usize, u64, u64, [u64; 2])>,
focus: FocusHandle,
on_link: Option<OnLink>,
on_heading: Option<OnHeading>,
_subscriptions: Vec<Subscription>,
}
@@ -132,6 +138,11 @@ impl MarkdownDocument {
cx.on_focus(&focus, window, Self::focused),
];
let overdraw = window.rem_size() * 40.0;
let this = cx.entity().downgrade();
let on_heading: OnHeading = Rc::new(move |at, window, cx| {
this.update(cx, |document, cx| document.reveal_at(at, window, cx))
.expect("a heading list lives in its document");
});
let mut document = Self {
editor,
mode,
@@ -151,6 +162,7 @@ impl MarkdownDocument {
room: Pixels::ZERO,
focus,
on_link: None,
on_heading: Some(on_heading),
_subscriptions: subscriptions,
};
document.reparse(true, cx);
+12 -37
View File
@@ -3,27 +3,18 @@ use std::ops::Range;
use markdown::mdast;
use crate::documents::{
markdown::{file_tree, span, walk},
tree::{Node, Reader},
markdown::{file_tree, span},
tree::{DocumentHeading, Node, Reader},
};
/// A top-level block: its source, and what it draws.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct Block {
pub range: Range<usize>,
/// None shows the source as written: definitions, HTML, front matter.
/// None shows a link definition as written.
pub nodes: Option<Vec<Node>>,
}
/// A heading anywhere in the file.
#[derive(Clone, Debug, PartialEq)]
pub struct DocumentHeading {
pub depth: u8,
pub title: String,
/// Where its source starts, in bytes.
pub at: usize,
}
/// A file read as blocks and headings.
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct Parsed {
@@ -44,16 +35,10 @@ pub(crate) fn parsed(text: &str) -> Parsed {
.map(|node| {
let range = span(node);
let kept = text[range.clone()].trim_end_matches(['\n', '\r']).len();
let raw = matches!(
node,
mdast::Node::Definition(_)
| mdast::Node::Html(_)
| mdast::Node::Yaml(_)
| mdast::Node::Toml(_)
);
let raw = matches!(node, mdast::Node::Definition(_));
let nodes = (!raw).then(|| {
let mut out = Vec::new();
reader.block(node, &mut out);
reader.top(node, &mut out);
out
});
Block {
@@ -62,23 +47,10 @@ pub(crate) fn parsed(text: &str) -> Parsed {
}
})
.collect();
let mut nodes = Vec::new();
walk(&root, &mut nodes);
let headings = nodes
.into_iter()
.filter_map(|node| match node {
mdast::Node::Heading(heading) => Some(DocumentHeading {
depth: heading.depth,
title: reader.inline(&heading.children).text,
at: span(node).start,
}),
_ => None,
})
.collect();
Parsed {
text: text.to_string(),
blocks,
headings,
headings: reader.headings,
}
}
@@ -111,7 +83,7 @@ mod tests {
use crate::documents::tree::Style;
#[test]
fn every_block_is_shown_raw_ones_as_written() {
fn every_block_is_shown_definitions_as_written() {
let text = "---\ntitle: a\n---\n\n# Lift\n\n[site]: https://ely.dev\n\n<div>x</div>\n\n> ## Quoted\n";
let found = parsed(text);
let shown: Vec<(&str, bool)> = found
@@ -122,13 +94,16 @@ mod tests {
assert_eq!(
shown,
[
("---\ntitle: a\n---", false),
("---\ntitle: a\n---", true),
("# Lift", true),
("[site]: https://ely.dev", false),
("<div>x</div>", false),
("<div>x</div>", true),
("> ## Quoted", true),
]
);
let drawn = |ix: usize| found.blocks[ix].nodes.clone().expect("drawn");
assert_eq!(drawn(0), [Node::FrontMatter("title: a".into())]);
assert!(matches!(&drawn(3)[..], [Node::Paragraph(line)] if line.text == "x"));
let headings: Vec<(u8, &str, usize)> = found
.headings
.iter()
+10
View File
@@ -189,6 +189,16 @@ impl MarkdownDocument {
.saturating_sub(1)
}
/// Reveals the line holding byte `at` of the text.
pub(super) fn reveal_at(&mut self, at: usize, window: &mut Window, cx: &mut Context<Self>) {
let editor = self.editor.read(cx);
if at > editor.text().len() {
return log::error!("markdown document: a heading past the text");
}
let line = editor.buffer.line_of(at);
self.reveal(line, window, cx);
}
/// Line `line` at top and focused, caret at start.
pub fn reveal(&mut self, line: usize, window: &mut Window, cx: &mut Context<Self>) {
self.set_caret(line, 0, cx);
+1
View File
@@ -370,6 +370,7 @@ mod history;
mod links;
mod look;
mod place;
mod toc;
mod tools;
#[gpui::test]
+36
View File
@@ -0,0 +1,36 @@
use gpui::{Modifiers, MouseButton, TestAppContext, VisualTestContext, point, px};
use super::{MarkdownMode, open};
const LISTED: &str = "[toc]\n\n# A\n\ntext\n\n## B\n";
/// Presses entry `ix` of the drawn heading list.
fn press_entry(ix: usize, modifiers: Modifiers, cx: &mut VisualTestContext) {
let bounds = cx
.debug_bounds(Box::leak(format!("markdown-toc-{ix}").into_boxed_str()))
.expect("the entry is drawn");
let at = point(bounds.left() + px(2.0), bounds.top() + px(2.0));
cx.simulate_mouse_down(at, MouseButton::Left, modifiers);
cx.simulate_mouse_up(at, MouseButton::Left, modifiers);
cx.run_until_parked();
}
#[gpui::test]
fn a_press_in_preview_reveals_the_heading(cx: &mut TestAppContext) {
let (document, cx) = open(LISTED, MarkdownMode::Preview, cx);
press_entry(1, Modifiers::none(), cx);
let caret = document.read_with(cx, |document, cx| document.caret(cx));
assert_eq!(caret, (6, 0));
}
#[gpui::test]
fn visual_reveals_on_a_secondary_press_and_edits_on_a_plain_one(cx: &mut TestAppContext) {
let (document, cx) = open(LISTED, MarkdownMode::Visual, cx);
press_entry(1, Modifiers::secondary_key(), cx);
let (caret, open) = document.read_with(cx, |document, cx| {
(document.caret(cx), document.open.is_some())
});
assert_eq!((caret, open), ((6, 0), false));
press_entry(0, Modifiers::none(), cx);
assert!(document.read_with(cx, |document, _| document.open.is_some()));
}
+1
View File
@@ -101,6 +101,7 @@ impl MarkdownDocument {
nodes,
&self.base,
&self.on_link,
&self.on_heading,
self.look.scale,
cx,
)),
+24 -57
View File
@@ -16,12 +16,16 @@ use super::{
use crate::{
editor::{code_colors, code_colors_with},
navigation::{Anchor, Sections},
primitives::{FocusRing as _, Icon, IconName},
theme::{ActiveTheme, IconSize, Radius, TextSize},
primitives::FocusRing as _,
theme::{ActiveTheme, Radius, TextSize},
typography::{Latex, failed},
};
mod blocks;
pub(crate) type OnLink = Rc<dyn Fn(&str, &mut Window, &mut App)>;
/// Reveals the heading whose source starts at a byte.
pub(crate) type OnHeading = Rc<dyn Fn(usize, &mut Window, &mut App)>;
type DrawCode = Rc<dyn Fn(ElementId, Option<SharedString>, SharedString) -> AnyElement>;
/// Markdown drawn as a document; links go to the host.
@@ -94,6 +98,9 @@ struct Draw<'a> {
base: &'a Option<Arc<Path>>,
/// Text size against the theme's.
scale: f32,
/// Lists open around the node drawn.
lists: std::cell::Cell<usize>,
on_heading: &'a Option<OnHeading>,
}
impl Draw<'_> {
@@ -263,58 +270,7 @@ impl Draw<'_> {
.child(StyledText::new(text.clone()).with_highlights(styles))
.into_any_element()
}
Node::List { start, items } => div()
.flex()
.flex_col()
.gap_1()
.children(items.iter().enumerate().map(|(ix, (task, body))| {
let marker: AnyElement = match (task, start) {
(Some(done), _) => Icon::new(if *done {
IconName::SquareCheck
} else {
IconName::Square
})
.size(IconSize::Sm)
.color(if *done {
colors.focus
} else {
colors.fg_subtle
})
.into_any_element(),
(None, Some(first)) => div()
.text_color(colors.fg_muted)
.child(format!("{}.", first + ix as u64))
.into_any_element(),
(None, None) => div()
.text_color(colors.fg_muted)
.child("•")
.into_any_element(),
};
div()
.flex()
.gap_2()
.child(
div()
.flex_none()
.min_w_5()
.flex()
.justify_end()
.child(marker),
)
.child(
div()
.flex_1()
.min_w_0()
.flex()
.flex_col()
.gap_1()
.when(*task == Some(true), |body| {
body.text_color(colors.fg_subtle)
})
.children(self.nodes(body, cx)),
)
}))
.into_any_element(),
Node::List { start, items } => self.list(start, items, cx),
Node::Table { aligns, head, rows } => {
let cell = |blocks: &[Node], column: usize, header: bool, cx: &App| {
div()
@@ -370,10 +326,10 @@ impl Draw<'_> {
.into_any_element()
}
Node::Rule => div()
.debug_selector(|| "markdown-rule".into())
.my_2()
.h_0()
.border_t_1()
.border_color(colors.border)
.border_t_2()
.border_color(colors.border_strong)
.into_any_element(),
Node::Math(tex) => div()
.flex()
@@ -416,6 +372,9 @@ impl Draw<'_> {
.children(self.nodes(body, cx)),
)
.into_any_element(),
Node::Alert { kind, body } => self.alert(*kind, body, cx),
Node::Toc(headings) => self.toc(headings, cx),
Node::FrontMatter(text) => self.front_matter(text, cx),
}
}
}
@@ -436,6 +395,7 @@ pub(crate) fn draw_nodes(
nodes: &[Node],
base: &Option<Arc<Path>>,
on_link: &Option<OnLink>,
on_heading: &Option<OnHeading>,
scale: f32,
cx: &App,
) -> Vec<AnyElement> {
@@ -448,6 +408,8 @@ pub(crate) fn draw_nodes(
code: &None,
base,
scale,
lists: std::cell::Cell::new(0),
on_heading,
};
draw.nodes(nodes, cx)
}
@@ -465,6 +427,8 @@ impl RenderOnce for MarkdownRenderer {
code: &self.code,
base: &self.base,
scale: 1.0,
lists: std::cell::Cell::new(0),
on_heading: &None,
};
div()
.flex()
@@ -475,3 +439,6 @@ impl RenderOnce for MarkdownRenderer {
.children(draw.nodes(&nodes, cx))
}
}
#[cfg(test)]
mod tests;
+172
View File
@@ -0,0 +1,172 @@
use gpui::{AnyElement, App, FontWeight, IntoElement, ParentElement, Styled, div, prelude::*};
use super::Draw;
use crate::{
documents::tree::{AlertKind, DocumentHeading, Node},
primitives::{FocusRing as _, Icon, IconName},
theme::{ActiveTheme, IconSize, Radius, TextSize},
};
impl Draw<'_> {
/// A list; bullets turn with depth, as browsers draw them.
pub(super) fn list(
&self,
start: &Option<u64>,
items: &[(Option<bool>, Vec<Node>)],
cx: &App,
) -> AnyElement {
let colors = cx.theme().colors.clone();
let depth = self.lists.get();
self.lists.set(depth + 1);
let bullet = match depth {
0 => "•",
1 => "◦",
_ => "▪",
};
let list = div()
.flex()
.flex_col()
.gap_1()
.children(items.iter().enumerate().map(|(ix, (task, body))| {
let marker: AnyElement = match (task, start) {
(Some(done), _) => Icon::new(if *done {
IconName::SquareCheck
} else {
IconName::Square
})
.size(IconSize::Sm)
.color(if *done {
colors.focus
} else {
colors.fg_subtle
})
.into_any_element(),
(None, Some(first)) => div()
.text_color(colors.fg_muted)
.child(format!("{}.", first + ix as u64))
.into_any_element(),
(None, None) => div()
.debug_selector(move || format!("markdown-bullet-{depth}"))
.text_color(colors.fg_muted)
.child(bullet)
.into_any_element(),
};
div()
.flex()
.gap_2()
.child(
div()
.flex_none()
.min_w_5()
.flex()
.justify_end()
.child(marker),
)
.child(
div()
.flex_1()
.min_w_0()
.flex()
.flex_col()
.gap_1()
.when(*task == Some(true), |body| {
body.text_color(colors.fg_subtle)
})
.children(self.nodes(body, cx)),
)
}))
.into_any_element();
self.lists.set(depth);
list
}
/// A GitHub alert: a colored rule, icon, title and body.
pub(super) fn alert(&self, kind: AlertKind, body: &[Node], cx: &App) -> AnyElement {
let colors = &cx.theme().colors;
let (tone, icon) = match kind {
AlertKind::Note => (colors.info, IconName::Info),
AlertKind::Tip => (colors.success, IconName::Lightbulb),
// The palette's violet, read at 4.5:1.
AlertKind::Important => (colors.syntax.keyword, IconName::MessageSquareWarning),
AlertKind::Warning => (colors.warning, IconName::TriangleAlert),
AlertKind::Caution => (colors.danger, IconName::OctagonAlert),
};
div()
.flex()
.flex_col()
.gap_1()
.py_1()
.pl_4()
.border_l_2()
.border_color(tone)
.child(
div()
.flex()
.items_center()
.gap_2()
.font_weight(FontWeight::MEDIUM)
.text_color(tone)
.child(Icon::new(icon).size(IconSize::Sm).color(tone))
.child(kind.title()),
)
.child(
div()
.flex()
.flex_col()
.gap_2()
.text_color(colors.fg)
.children(self.nodes(body, cx)),
)
.into_any_element()
}
/// `[toc]`: each heading by depth; a press reveals it.
pub(super) fn toc(&self, headings: &[DocumentHeading], cx: &App) -> AnyElement {
let colors = cx.theme().colors.clone();
let top = headings.iter().map(|heading| heading.depth).min();
div()
.flex()
.flex_col()
.gap_1()
.children(headings.iter().enumerate().map(|(ix, heading)| {
let entry = div()
.id(self.key("toc"))
.debug_selector(move || format!("markdown-toc-{ix}"))
.child(heading.title.clone());
let entry = match heading.depth - top.expect("a heading is listed") {
0 => entry,
1 => entry.pl_4(),
2 => entry.pl_8(),
3 => entry.pl_12(),
4 => entry.pl_16(),
_ => entry.pl_20(),
};
let Some(reveal) = self.on_heading.clone() else {
return entry.text_color(colors.fg_muted).into_any_element();
};
let at = heading.at;
entry
.text_color(colors.link)
.tab_index(0)
.focus_ring(cx)
.cursor_pointer()
.on_click(move |_, window, cx| reveal(at, window, cx))
.into_any_element()
}))
.into_any_element()
}
/// Front matter as written, inside its box.
pub(super) fn front_matter(&self, text: &str, cx: &App) -> AnyElement {
let theme = cx.theme();
div()
.p_3()
.rounded(theme.radius(Radius::Md))
.bg(theme.colors.sunken)
.font_family(theme.mono_family.clone())
.text_size(theme.text_size(TextSize::Sm) * self.scale)
.text_color(theme.colors.fg_muted)
.child(text.to_string())
.into_any_element()
}
}
+53
View File
@@ -0,0 +1,53 @@
use gpui::{Context, IntoElement, Render, TestAppContext, Window, div, prelude::*, px};
use super::MarkdownRenderer;
use crate::theme::Theme;
/// `source` drawn in a 400-point column.
struct Page(&'static str);
impl Render for Page {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div()
.w(px(400.0))
.child(MarkdownRenderer::new("page", self.0))
}
}
#[gpui::test]
fn a_rule_draws_a_line_two_pixels_high(cx: &mut TestAppContext) {
cx.update(Theme::init);
let (_, cx) = cx.add_window_view(|_, _| Page("above\n\n---\n\nbelow\n"));
cx.run_until_parked();
let rule = cx.debug_bounds("markdown-rule").expect("the rule is drawn");
assert_eq!(rule.size.height, px(2.0), "{rule:?}");
assert!(rule.size.width > px(300.0), "{rule:?}");
}
#[gpui::test]
fn bullets_turn_with_depth(cx: &mut TestAppContext) {
cx.update(Theme::init);
let (_, cx) = cx.add_window_view(|_, _| Page("- a\n - b\n 1. c\n - d\n"));
cx.run_until_parked();
for depth in [0, 1, 3] {
let selector = Box::leak(format!("markdown-bullet-{depth}").into_boxed_str());
assert!(cx.debug_bounds(selector).is_some(), "depth {depth}");
}
assert!(
cx.debug_bounds("markdown-bullet-2").is_none(),
"an ordered list"
);
}
#[gpui::test]
fn alerts_lists_of_headings_and_front_matter_draw(cx: &mut TestAppContext) {
cx.update(Theme::init);
let source = "> [!IMPORTANT]\n> read\n\n[toc]\n\n# One\n\n## Two\n";
let (_, cx) = cx.add_window_view(|_, _| Page(source));
cx.run_until_parked();
let (one, two) = (
cx.debug_bounds("markdown-toc-0").expect("drawn"),
cx.debug_bounds("markdown-toc-1").expect("drawn"),
);
assert!(two.top() > one.top(), "{one:?} {two:?}");
}
+326
View File
@@ -0,0 +1,326 @@
use markdown::mdast;
use super::{Node, Reader, Run, html, text_block};
use crate::documents::markdown::span;
/// A heading anywhere in the file.
#[derive(Clone, Debug, PartialEq)]
pub struct DocumentHeading {
pub depth: u8,
pub title: String,
/// Where its source starts, in bytes.
pub at: usize,
}
/// A GitHub alert's kind, named by its marker.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AlertKind {
Note,
Tip,
Important,
Warning,
Caution,
}
impl AlertKind {
/// The title GitHub shows.
pub fn title(self) -> &'static str {
match self {
Self::Note => "Note",
Self::Tip => "Tip",
Self::Important => "Important",
Self::Warning => "Warning",
Self::Caution => "Caution",
}
}
/// The kind `line` marks, as `[!NOTE]` in any case.
fn marked(line: &str) -> Option<Self> {
let name = line.trim().strip_prefix("[!")?.strip_suffix(']')?;
[
Self::Note,
Self::Tip,
Self::Important,
Self::Warning,
Self::Caution,
]
.into_iter()
.find(|kind| kind.title().eq_ignore_ascii_case(name))
}
}
impl Reader<'_> {
/// A root block; alerts and `[toc]` live only here.
pub(crate) fn top(&self, node: &mdast::Node, out: &mut Vec<Node>) {
if let Some(alert) = self.alert(node) {
return out.push(alert);
}
let toc = matches!(node, mdast::Node::Paragraph(_))
&& self.source[span(node)].trim().eq_ignore_ascii_case("[toc]");
match toc {
true => out.push(Node::Toc(self.headings.clone())),
false => self.block(node, out),
}
}
/// A quote whose first line marks it, with more after.
fn alert(&self, node: &mdast::Node) -> Option<Node> {
let mdast::Node::Blockquote(quote) = node else {
return None;
};
let [first @ mdast::Node::Paragraph(paragraph), rest @ ..] = &quote.children[..] else {
return None;
};
let line = self.source[span(first)].lines().next().unwrap_or_default();
let kind = AlertKind::marked(line)?;
let after = after_first_line(&paragraph.children);
if after.is_empty() && rest.is_empty() {
return None;
}
let mut body = Vec::new();
if !after.is_empty() {
let kept = mdast::Paragraph {
children: after,
position: paragraph.position.clone(),
};
self.block(&mdast::Node::Paragraph(kept), &mut body);
}
self.blocks(rest, &mut body);
Some(Node::Alert { kind, body })
}
/// An HTML block's text; comments, scripts and styles drop.
pub(super) fn html_block(&self, written: &str, out: &mut Vec<Node>) {
let mut run = Run::default();
let mut rest = written;
while let Some(at) = rest.find('<') {
run.words(&rest[..at]);
rest = &rest[at..];
if let Some(comment) = rest.strip_prefix("<!--") {
rest = comment.split_once("-->").map_or("", |(_, after)| after);
continue;
}
let opens =
rest[1..].starts_with(|ch: char| ch.is_ascii_alphabetic() || "/!?".contains(ch));
if !opens {
run.words("<");
rest = &rest[1..];
continue;
}
// An unclosed tag drops the rest, as HTML5 does.
let Some(end) = html::tag_end(rest) else {
rest = "";
break;
};
let (written, after) = rest.split_at(end);
rest = after;
match html::tag(written) {
Some(tag) if !tag.closing && matches!(tag.name.as_str(), "script" | "style") => {
let closing = format!("</{}", tag.name);
let skipped = rest.to_ascii_lowercase().find(&closing);
let left = skipped.map_or("", |at| &rest[at..]);
rest = html::tag_end(left).map_or("", |end| &left[end..]);
}
Some(tag) if html::breaks(&tag.name) => run.line(),
// A line's end drops its space.
Some(tag) if tag.closing || tag.name == "br" => {
run.owed = run.owed.filter(|_| tag.name != "br");
run.html(written);
}
Some(_) => {
run.pay();
run.html(written);
}
None => {}
}
}
run.words(rest);
text_block(run.cut(), out);
}
}
impl Run {
/// HTML text: references decoded, white space collapsed.
fn words(&mut self, written: &str) {
let decoded = html::text(written);
let shown = match self.in_code() {
true => decoded,
false => html::emoji(&decoded),
};
for ch in shown.chars() {
if !html::spacing(ch) {
self.pay();
self.text.text.push(ch);
} else if self.owed.is_none() && !self.text.text.ends_with('\n') {
self.owed = (!self.text.text.is_empty()).then_some(' ');
}
}
}
/// A line owed where a block element starts or ends.
fn line(&mut self) {
let text = &self.text.text;
self.owed = (!(text.is_empty() || text.ends_with('\n'))).then_some('\n');
}
/// Writes the white space owed, before a word or tag.
fn pay(&mut self) {
if let Some(owed) = self.owed.take() {
self.text.text.push(owed);
}
}
}
/// Inlines past the first line ending.
fn after_first_line(children: &[mdast::Node]) -> Vec<mdast::Node> {
let mut found = None;
for (at, child) in children.iter().enumerate() {
match child {
mdast::Node::Text(text) => {
if let Some((_, rest)) = text.value.split_once('\n') {
let mut kept = text.clone();
kept.value = rest.to_string();
found = Some((at + 1, (!rest.is_empty()).then_some(kept)));
break;
}
}
mdast::Node::Break(_) => {
found = Some((at + 1, None));
break;
}
_ => {}
}
}
let Some((next, head)) = found else {
return Vec::new();
};
let head = head.map(mdast::Node::Text);
head.into_iter()
.chain(children[next..].iter().cloned())
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::documents::tree::{Inline, Style, parse};
fn alert(source: &str) -> Option<(AlertKind, Vec<Node>)> {
match parse(source).into_iter().next() {
Some(Node::Alert { kind, body }) => Some((kind, body)),
_ => None,
}
}
fn text(nodes: &[Node]) -> Vec<String> {
nodes
.iter()
.map(|node| match node {
Node::Paragraph(line) => line.text.clone(),
Node::Heading(_, line) => format!("# {}", line.text),
other => format!("{other:?}"),
})
.collect()
}
#[test]
fn alerts_read_as_github_reads_them() {
let note = alert("> [!NOTE]\n> one").expect("an alert");
assert_eq!(
(note.0, text(&note.1)),
(AlertKind::Note, vec!["one".into()])
);
let cases = [
("> [!tip]\n> two", Some(AlertKind::Tip)),
("> [!NOTE] \n> trailing", Some(AlertKind::Note)),
("> [!TIP]\n> leading", Some(AlertKind::Tip)),
("> [!Warning]\n>\n> after a blank", Some(AlertKind::Warning)),
("> [!WARNING] same line", None),
("> [!IMPORTANT]", None),
("> [!FOO]\n> five", None),
("> text\n> [!NOTE]", None),
];
for (source, kind) in cases {
assert_eq!(alert(source).map(|found| found.0), kind, "{source:?}");
}
let nested = parse("- > [!CAUTION]\n > in a list");
assert!(matches!(&nested[0], Node::List { .. }), "{nested:?}");
let blocks = alert("> [!NOTE]\n> # head\n> - item").expect("an alert");
assert!(matches!(
&blocks.1[..],
[Node::Heading(1, _), Node::List { .. }]
));
let styled = alert("> [!CAUTION]\n> a **b**\n> c").expect("an alert");
let Node::Paragraph(line) = &styled.1[0] else {
panic!("a paragraph, not {:?}", styled.1)
};
let bold = 2..3;
assert_eq!(
(line.text.as_str(), &line.styles[..]),
("a b c", &[(bold, Style::Strong)][..])
);
}
#[test]
fn a_toc_paragraph_lists_every_heading() {
let nodes = parse("[toc]\n\n# A\n\n> ## B :smile:\n");
let Node::Toc(headings) = &nodes[0] else {
panic!("a toc, not {nodes:?}")
};
let listed: Vec<(u8, &str, usize)> = headings
.iter()
.map(|heading| (heading.depth, heading.title.as_str(), heading.at))
.collect();
assert_eq!(listed, [(1, "A", 7), (2, "B 😄", 14)]);
assert!(matches!(&parse(" [TOC] ")[0], Node::Toc(_)));
assert!(matches!(&parse("[toc] now")[0], Node::Paragraph(_)));
assert!(matches!(&parse("> [toc]")[0], Node::Quote(_)));
}
fn html(source: &str) -> Option<Inline> {
match parse(source).into_iter().next() {
Some(Node::Paragraph(line)) => Some(line),
None => None,
other => panic!("a paragraph, not {other:?}"),
}
}
#[test]
fn an_html_block_shows_its_text_as_a_browser_does() {
let centered = html(
"<div align=\"center\">\n <b>Bold</b> and\n :smile: &amp; <code>:smile:</code>\n</div>",
)
.expect("text");
assert_eq!(centered.text, "Bold and 😄 & :smile:");
let (bold, code) = (0..4, 16..23);
assert_eq!(
centered.styles,
[(bold, Style::Strong), (code, Style::Code)]
);
let parts = html("<p>one <b>two </b></p>\n<p>three &lt;b&gt;</p>").expect("text");
assert_eq!(parts.text, "one two\nthree <b>");
assert_eq!(
html("<div>\na <br> b</br>c</div>").expect("text").text,
"a\nb\nc"
);
let hidden = "<div>\n<script>alert(':smile:')</script>\n<!-- note -->\nshown<style>p{}</style>\n</div>";
assert_eq!(html(hidden).expect("text").text, "shown");
assert_eq!(html("<style>\np { color: red }\n</style>"), None);
assert_eq!(html("<div>\na < b <i").expect("text").text, "a < b");
}
#[test]
fn hostile_html_reads_in_linear_time() {
let started = web_time::Instant::now();
for source in [
"<".repeat(1 << 17),
"<a".repeat(1 << 16),
"<!--".repeat(1 << 16),
] {
let block = format!("<div>\n{source}");
parse(&block);
}
let spent = started.elapsed();
assert!(spent.as_secs() < 2, "took {spent:?}");
}
}
+99 -10
View File
@@ -41,6 +41,63 @@ pub(crate) fn reopens(name: &str) -> bool {
)
}
/// Elements a browser lays as blocks: a line each side.
pub(crate) fn breaks(name: &str) -> bool {
matches!(
name,
"address"
| "article"
| "aside"
| "blockquote"
| "center"
| "details"
| "div"
| "dl"
| "dd"
| "dt"
| "figcaption"
| "figure"
| "footer"
| "h1"
| "h2"
| "h3"
| "h4"
| "h5"
| "h6"
| "header"
| "hr"
| "li"
| "main"
| "nav"
| "ol"
| "p"
| "pre"
| "section"
| "summary"
| "table"
| "tr"
| "ul"
)
}
/// Bytes through the `>` ending the tag at `html`'s start.
pub(crate) fn tag_end(html: &str) -> Option<usize> {
let (mut quote, mut last) = (None, '<');
for (at, ch) in html.char_indices().skip(1) {
match quote {
Some(open) if ch == open => quote = None,
Some(_) => {}
None if ch == '>' => return Some(at + 1),
None if matches!(ch, '"' | '\'') && last == '=' => quote = Some(ch),
None => {}
}
if !spacing(ch) {
last = ch;
}
}
None
}
/// `html` as one tag; comments and the rest are none.
pub(crate) fn tag(html: &str) -> Option<Tag> {
let inner = html.trim().strip_prefix('<')?.strip_suffix('>')?;
@@ -94,24 +151,29 @@ fn attributes(mut rest: &str) -> Vec<(String, String)> {
(&after[..end], &after[end..])
}
};
out.push((name, decoded(value)));
out.push((name, decoded(value, true)));
rest = left;
}
}
/// HTML's whitespace, which is ASCII alone.
fn spacing(ch: char) -> bool {
pub(crate) fn spacing(ch: char) -> bool {
matches!(ch, ' ' | '\t' | '\n' | '\r' | '\x0C')
}
/// An attribute value with references decoded as HTML does.
fn decoded(text: &str) -> String {
/// Text between tags, references decoded as HTML does.
pub(crate) fn text(written: &str) -> String {
decoded(written, false)
}
/// References decoded as HTML does, in a value or text.
fn decoded(text: &str, attribute: bool) -> String {
let mut out = String::with_capacity(text.len());
let mut rest = text;
while let Some(at) = rest.find('&') {
out.push_str(&rest[..at]);
rest = &rest[at..];
match attribute_reference(rest) {
match reference(rest, attribute) {
Some((value, len)) => {
out.push_str(&value);
rest = &rest[len..];
@@ -129,8 +191,8 @@ fn decoded(text: &str) -> String {
/// Longest reference name HTML knows, in bytes.
const LONGEST: usize = 32;
/// A reference at `text`'s start, as HTML reads values.
fn attribute_reference(text: &str) -> Option<(String, usize)> {
/// A reference at `text`'s start, as HTML reads it.
fn reference(text: &str, attribute: bool) -> Option<(String, usize)> {
let body = text.strip_prefix('&')?;
if let Some(number) = body.strip_prefix('#') {
let (digits, radix) = match number.strip_prefix(['x', 'X']) {
@@ -160,8 +222,18 @@ fn attribute_reference(text: &str) -> Option<(String, usize)> {
{
return Some((value, len + 2));
}
let bare = !body[len..].starts_with(['=', ';']);
(bare && legacy(name)).then(|| (decode_named(name, true).expect("a legacy name"), len + 1))
// Text takes a legacy prefix; a value, the whole name.
let shortest = match attribute {
true if body[len..].starts_with(['=', ';']) => return None,
true => len,
false => 1,
};
let name = (shortest..=len.min(LONGEST))
.rev()
.map(|end| &name[..end])
.find(|name| legacy(name))?;
let value = decode_named(name, true).expect("a legacy name");
Some((value, name.len() + 1))
}
/// A name HTML decodes without its semicolon.
@@ -299,7 +371,8 @@ mod tests {
fn long_runs_scan_in_linear_time() {
let (amps, colons) = ("&".repeat(1 << 18), ":zq".repeat(1 << 16));
let started = web_time::Instant::now();
assert_eq!(decoded(&amps).len(), amps.len());
assert_eq!(decoded(&amps, true).len(), amps.len());
assert_eq!(text(&amps).len(), amps.len());
assert_eq!(emoji(&colons).len(), colons.len());
let spent = started.elapsed();
assert!(spent.as_secs() < 2, "256 KiB took {spent:?}");
@@ -313,6 +386,22 @@ mod tests {
assert_eq!(emoji("é :smile: 中"), "é 😄 中");
}
#[test]
fn text_takes_the_longest_legacy_name() {
assert_eq!(
text("&notit; &amp &ampx &lt3 &nope;"),
"¬it; & &x <3 &nope;"
);
assert_eq!(decoded("&notit;&ampx", true), "&notit;&ampx");
}
#[test]
fn a_tag_ends_outside_its_quotes() {
assert_eq!(tag_end("<a href=\"x>y\">z"), Some(14));
assert_eq!(tag_end("<b it's>r"), Some(8));
assert_eq!(tag_end("<b"), None);
}
#[test]
fn numbers_go_up() {
assert_eq!(superscript(1), "¹");
+43 -11
View File
@@ -4,8 +4,11 @@ use markdown::mdast::{self, AlignKind};
use super::markdown::{span, tree, walk};
mod blocks;
mod html;
pub use blocks::{AlertKind, DocumentHeading};
/// How a stretch of inline text prints.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Style {
@@ -62,6 +65,15 @@ pub enum Node {
label: String,
body: Vec<Node>,
},
/// A GitHub alert: a quote under its kind's title.
Alert {
kind: AlertKind,
body: Vec<Node>,
},
/// `[toc]`: the file's headings.
Toc(Vec<DocumentHeading>),
/// Front matter, its fences cut.
FrontMatter(String),
}
/// A tree read against its source and its link definitions.
@@ -70,6 +82,7 @@ pub(crate) struct Reader<'a> {
urls: HashMap<String, String>,
/// Footnotes numbered by their first reference.
notes: HashMap<String, usize>,
pub headings: Vec<DocumentHeading>,
}
/// An open element, as HTML names it.
@@ -87,6 +100,8 @@ struct Run {
open: Vec<Opened>,
/// Inside an autolink: its text stays as written.
verbatim: bool,
/// White space an HTML block owes the next word.
owed: Option<char>,
}
impl Run {
@@ -101,6 +116,11 @@ impl Run {
done
}
/// Inside HTML `<code>`, where shortcodes stay text.
fn in_code(&self) -> bool {
self.open.iter().any(|opened| opened.name == "code")
}
/// The innermost open link's address.
fn link(&self) -> Option<String> {
self.open.iter().rev().find_map(|opened| opened.url.clone())
@@ -178,11 +198,10 @@ fn close(text: &mut Inline, opened: &Opened, end: usize) {
/// `markdown` as blocks.
pub fn parse(markdown: &str) -> Vec<Node> {
let root = tree(markdown);
let mut out = Vec::new();
Reader::new(markdown, &root).blocks(
root.children().expect("the root holds the blocks"),
&mut out,
);
let (reader, mut out) = (Reader::new(markdown, &root), Vec::new());
for node in root.children().expect("the root holds the blocks") {
reader.top(node, &mut out);
}
out
}
@@ -191,7 +210,7 @@ impl<'a> Reader<'a> {
pub(crate) fn new(source: &'a str, root: &mdast::Node) -> Self {
let mut nodes = Vec::new();
walk(root, &mut nodes);
let (mut urls, mut notes) = (HashMap::new(), HashMap::new());
let (mut urls, mut notes, mut headings) = (HashMap::new(), HashMap::new(), Vec::new());
for node in nodes {
match node {
mdast::Node::Definition(definition) => {
@@ -202,14 +221,25 @@ impl<'a> Reader<'a> {
let next = notes.len() + 1;
notes.entry(note.identifier.clone()).or_insert(next);
}
mdast::Node::Heading(heading) => headings.push((heading, span(node).start)),
_ => {}
}
}
Self {
let mut reader = Self {
source,
urls,
notes,
}
headings: Vec::new(),
};
reader.headings = headings
.into_iter()
.map(|(heading, at)| DocumentHeading {
depth: heading.depth,
title: reader.inline(&heading.children).text,
at,
})
.collect();
reader
}
fn blocks(&self, nodes: &[mdast::Node], out: &mut Vec<Node>) {
@@ -293,7 +323,10 @@ impl<'a> Reader<'a> {
},
body: self.nested(&note.children),
}),
mdast::Node::Html(_) | mdast::Node::Definition(_) => {}
mdast::Node::Html(html) => self.html_block(&html.value, out),
mdast::Node::Yaml(yaml) => out.push(Node::FrontMatter(yaml.value.clone())),
mdast::Node::Toml(toml) => out.push(Node::FrontMatter(toml.value.clone())),
mdast::Node::Definition(_) => {}
other => unreachable!("{other:?} is no block outside MDX"),
}
}
@@ -325,8 +358,7 @@ impl<'a> Reader<'a> {
let text = &mut run.text;
let (children, (name, style), url) = match node {
mdast::Node::Text(words) => {
let code = run.open.iter().any(|opened| opened.name == "code");
let shown = match run.verbatim || code {
let shown = match run.verbatim || run.in_code() {
true => words.value.clone(),
false => html::emoji(&words.value),
};
+1
View File
@@ -226,6 +226,7 @@ icons! {
Merge => "merge",
MessageSquare => "message-square",
MessageSquareDiff => "message-square-diff",
MessageSquareWarning => "message-square-warning",
MicOff => "mic-off",
Mic => "mic",
Minimize2 => "minimize-2",