documents: a TOC press follows the edit since its read; filtered tags keep shortcodes; tag names end as HTML5 ends them; block starts end p, li, dd and dt; owed space shifts only what opened after it

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-14 00:11:57 -04:00
1 parent 98f19a8aac
commit a744316274
7 files changed
+242 -42

No files matched your search

+1 -1
View File
@@ -13,7 +13,7 @@ Ely GPUI Component. A component library for GPUI, in light and dark.
- Web views: `wry` 0.57 (Apache-2.0 OR MIT, default features off, macOS only) lays a child WKWebView over the window.
- Terminal: `alacritty_terminal` 0.26 (Apache-2.0, default features off, native only) for the grid, its parser and the pseudo-terminal; `futures` carries its events. `vte` 0.15 (Apache-2.0 OR MIT), the parser it re-exports, reads `AnsiText`'s codes alone, so colored output builds for wasm32.
- Diffs: `similar` 3.2 (Apache-2.0) for line and word diffs and three-way merges; its `unicode` feature splits words at punctuation.
- Markdown: `markdown` 1.0.0 (markdown-rs, MIT) reads GitHub's flavor and math into a syntax tree with byte positions for `MarkdownRenderer`, `MarkdownEditor`, the highlights, block ranges and outline. `MarkdownRenderer` draws display math through `typography::Latex`, a ```` ```mermaid ```` block through `documents::Diagram`, and pictures from the owner's folder (`base`) or `data:` addresses (`base64` 0.22, MIT OR Apache-2.0); web pictures stay unloaded. Inline math shows as TeX: gpui text carries no inline pictures. Inline it reads as Typora's defaults: `:shortcode:` turns to its emoji as GitHub reads it (the `emojis` crate's names, GitHub's today, so `:cricket:` is the insect; read after escapes and references, so `\:smile:` turns too; inside an autolink or `<code>` it stays text), inline HTML's `u`, `b`/`strong`, `i`/`em`, `s`/`del`/`strike`, `code`/`kbd`, `mark`, `br` and `a href` draw as what they mean GFM's filtered tags (`script`, `style`, `title`, `textarea`, `iframe`, `xmp`, `noembed`, `noframes`, `plaintext`) show as text, and other tags, hyphenated names too, vanish, leaving their text; attribute values split on ASCII whitespace alone and decode as HTML decodes them; each Markdown container is the element GitHub writes (`em`, `strong`, `del`, `a`), on one stack with the tags and repaired as HTML5 repairs: an end tag closes the innermost element of its name and all above it, of which only formatting elements (`a`, `b`, `code`, `em`, `i`, `s`, `strike`, `strong`, `u`) reopen, a new `a` ends the open one and an `a` without `href` links nothing; other tags are not tracked, and `</br>` breaks as `<br>` does; and a footnote reference is its number in superscript, numbered by first reference, as its definition is; one `~` still strikes. At the root, blocks read as GitHub shows them: a quote whose first line is plain text reading `[!NOTE]`, `[!TIP]`, `[!IMPORTANT]`, `[!WARNING]` or `[!CAUTION]` in any case, after escapes and references, spaces and tabs around it, with more after it, is an alert under its title (Important in the palette's violet, `syntax.keyword`, read at 4.5:1); a paragraph that is `[toc]` alone, as Typora reads it, lists every heading by depth, and a press on one reveals it as a link press does (`MarkdownDocument`'s `reveal`), the heading found again in the text as it is now, and a secondary press acts on press, since a block writing back moves the list; front matter shows in a box without its fences; an HTML block shows its text with the inline tags above, white space collapsed and owed as a browser lays it, block elements breaking lines and closing what opened inside them, comments dropped, the filtered tags shown as text, and an unclosed tag dropping the rest; a rule is a 2px `border_strong` line; bullets go •, ◦, ▪ by list depth, ordered lists counted. `MarkdownDocument` holds a whole file in one `CodeEditor` (text and undo, colored by `CodeEditor::highlighter` with `markdown_highlights`): visual draws the file's top-level blocks in a gpui `list`, a pressed block opening as its source in a `TextInput` whose typing is the document's text at once and whose write-back is one undo step; source is the editor; preview draws without presses. A written-back block, a mode switch or a new text is read at once up to 128 KiB, longer ones on the background executor; typing in source or split reads 300 ms after the last key, off the main thread (markdown-rs grows with the square of a paragraph's lines: 40,000 lines take 0.9 s). Texts past `LARGEST_READ` (2 MiB) are never read: no blocks, no outline. A new read keeps the list's blocks alike at both ends. One caret serves every mode: the editor's selection, which a written-back block moves; `set_caret` and `set_top_line` set caret and view apart; a top line asked (numbered, so a late frame callback finishes only its own ask; new text places it again, clamped to the text as it then is) waits for the read, lands on its block, then a frame later at its share of the block's height; `reveal` is such an ask with the caret at its line; a switch between source and the list keeps the top line, one between visual and preview keeps the list's scroll as it is. A block opens only when the read is of the text as it is now. A pressed block keeps its identity across the write-back of the one before it. Blocks are Tab stops that Enter or Space opens; preview takes no presses, undo or redo. `CodeEditor` keeps at most 64 MiB of undo and redo text, dropping the oldest undo and then the farthest redo after every record, composition, undo and redo. `BlockData::from_markdown` and its export still read with `pulldown-cmark` 0.13 (MIT, default features off); it adds GitHub's alerts, a callout's form, and reads each field's inline Markdown from the source by offset, as written.
- Markdown: `markdown` 1.0.0 (markdown-rs, MIT) reads GitHub's flavor and math into a syntax tree with byte positions for `MarkdownRenderer`, `MarkdownEditor`, the highlights, block ranges and outline. `MarkdownRenderer` draws display math through `typography::Latex`, a ```` ```mermaid ```` block through `documents::Diagram`, and pictures from the owner's folder (`base`) or `data:` addresses (`base64` 0.22, MIT OR Apache-2.0); web pictures stay unloaded. Inline math shows as TeX: gpui text carries no inline pictures. Inline it reads as Typora's defaults: `:shortcode:` turns to its emoji as GitHub reads it (the `emojis` crate's names, GitHub's today, so `:cricket:` is the insect; read after escapes and references, so `\:smile:` turns too; inside an autolink, `<code>` or a filtered tag it stays text), inline HTML's `u`, `b`/`strong`, `i`/`em`, `s`/`del`/`strike`, `code`/`kbd`, `mark`, `br` and `a href` draw as what they mean GFM's filtered tags (`script`, `style`, `title`, `textarea`, `iframe`, `xmp`, `noembed`, `noframes`, `plaintext`) show as text, and other tags, hyphenated names too, vanish, leaving their text; a tag's name runs to white space or `/`, as HTML5 reads it; attribute values split on ASCII whitespace alone and decode as HTML decodes them; each Markdown container is the element GitHub writes (`em`, `strong`, `del`, `a`), on one stack with the tags and repaired as HTML5 repairs: an end tag closes the innermost element of its name and all above it, of which only formatting elements (`a`, `b`, `code`, `em`, `i`, `s`, `strike`, `strong`, `u`) reopen, a new `a` ends the open one and an `a` without `href` links nothing; other tags are not tracked, and `</br>` breaks as `<br>` does; and a footnote reference is its number in superscript, numbered by first reference, as its definition is; one `~` still strikes. At the root, blocks read as GitHub shows them: a quote whose first line is plain text reading `[!NOTE]`, `[!TIP]`, `[!IMPORTANT]`, `[!WARNING]` or `[!CAUTION]` in any case, after escapes and references, spaces and tabs around it, with more after it, is an alert under its title (Important in the palette's violet, `syntax.keyword`, read at 4.5:1); a paragraph that is `[toc]` alone, as Typora reads it, lists every heading by depth, and a press on one reveals it as a link press does (`MarkdownDocument`'s `reveal`), the heading moved by the one edit between the text the list drew from and the text now, gone if that edit took it, and a secondary press acts on press, since a block writing back moves the list; front matter shows in a box without its fences; an HTML block shows its text with the inline tags above, white space collapsed and owed as a browser lays it, block elements breaking lines and closing what opened inside them, a block's start ending an open `p` and an `li` start an `li`, a `dd` or `dt` start either, as HTML5 ends them, comments dropped, the filtered tags shown as text, and an unclosed tag dropping the rest; a rule is a 2px `border_strong` line; bullets go •, ◦, ▪ by list depth, ordered lists counted. `MarkdownDocument` holds a whole file in one `CodeEditor` (text and undo, colored by `CodeEditor::highlighter` with `markdown_highlights`): visual draws the file's top-level blocks in a gpui `list`, a pressed block opening as its source in a `TextInput` whose typing is the document's text at once and whose write-back is one undo step; source is the editor; preview draws without presses. A written-back block, a mode switch or a new text is read at once up to 128 KiB, longer ones on the background executor; typing in source or split reads 300 ms after the last key, off the main thread (markdown-rs grows with the square of a paragraph's lines: 40,000 lines take 0.9 s). Texts past `LARGEST_READ` (2 MiB) are never read: no blocks, no outline. A new read keeps the list's blocks alike at both ends. One caret serves every mode: the editor's selection, which a written-back block moves; `set_caret` and `set_top_line` set caret and view apart; a top line asked (numbered, so a late frame callback finishes only its own ask; new text places it again, clamped to the text as it then is) waits for the read, lands on its block, then a frame later at its share of the block's height; `reveal` is such an ask with the caret at its line; a switch between source and the list keeps the top line, one between visual and preview keeps the list's scroll as it is. A block opens only when the read is of the text as it is now. A pressed block keeps its identity across the write-back of the one before it. Blocks are Tab stops that Enter or Space opens; preview takes no presses, undo or redo. `CodeEditor` keeps at most 64 MiB of undo and redo text, dropping the oldest undo and then the farthest redo after every record, composition, undo and redo. `BlockData::from_markdown` and its export still read with `pulldown-cmark` 0.13 (MIT, default features off); it adds GitHub's alerts, a callout's form, and reads each fieLine truncated
- Pictures: `image` 0.25 (MIT OR Apache-2.0), the crate gpui decodes with, default features off; it turns decoded frames and rims map tiles.
- Math and diagrams: RaTeX 0.1.14 (`ratex-parser`, `ratex-layout`, `ratex-svg` with `embed-fonts`, `ratex-types`; MIT, KaTeX's fonts OFL) typesets `Latex` to SVG with outlined glyphs; `mermaid-rs-renderer` 0.3.1 (MIT, default features off) draws `Diagram`. Both go through gpui's `SvgRenderer` at the window's scale, loaded as gpui assets off the main thread; `usvg` 0.46, gpui's own, reads a diagram's size. A RaTeX or Mermaid panic on parsed input becomes the element's error. Each element holds its picture through `primitives::held`: a new source, or the element leaving the window, removes the old asset from gpui's cache and drops its image from the atlas after the frame.
- Maps: Natural Earth's 110m countries (public domain). `assets/maps/countries.geojson`, trimmed by `scripts/countries.py` to an ISO code, a name and positions to 0.01°, backs `maps::WorldMap`; the gallery's tiles are cut from the same file, z0 to z3, by `scripts/tiles.py` with Pillow.
+31 -1
View File
@@ -12,8 +12,27 @@ use crate::{
forms::{InputEvent, TextInput},
};
/// The one edit from `old` to `new`, by shared ends.
pub(super) fn between(old: &str, new: &str) -> Option<(Range<usize>, usize)> {
let head: usize = old
.chars()
.zip(new.chars())
.take_while(|(a, b)| a == b)
.map(|(ch, _)| ch.len_utf8())
.sum();
let room = old.len().min(new.len()) - head;
let mut tail = 0;
for (a, b) in old.chars().rev().zip(new.chars().rev()) {
if a != b || tail + a.len_utf8() > room {
break;
}
tail += a.len_utf8();
}
(old != new).then(|| (head..old.len() - tail, new.len() - tail - head))
}
/// Where byte `at` lies after `edit`, if it survives.
fn moved(at: usize, edit: &Option<(Range<usize>, usize)>) -> Option<usize> {
pub(super) fn moved(at: usize, edit: &Option<(Range<usize>, usize)>) -> Option<usize> {
match edit {
None => Some(at),
Some((replaced, _)) if at < replaced.start => Some(at),
@@ -181,4 +200,15 @@ mod tests {
assert_eq!(super::moved(3, &edit), None, "inside is gone");
assert_eq!(super::moved(7, &None), Some(7));
}
#[test]
fn two_texts_part_at_their_shared_ends() {
assert_eq!(super::between("abc", "abc"), None);
assert_eq!(super::between("before\nB", "## B\nB"), Some((0..6, 4)));
assert_eq!(super::between("ab", "aXb"), Some((1..1, 1)));
assert_eq!(super::between("aa", "aaa"), Some((2..2, 1)));
assert_eq!(super::between("aXYb", "ab"), Some((1..3, 0)));
assert_eq!(super::between("é😄", "é"), Some((2..6, 0)));
assert_eq!(super::between("x😄", "y😄"), Some((0..1, 1)));
}
}
+15 -12
View File
@@ -1,6 +1,11 @@
use std::sync::Arc;
use gpui::{App, Context, Focusable as _, ListOffset, ScrollStrategy, Window, px};
use super::{DocumentHeading, MarkdownDocument, Placement};
use super::{
MarkdownDocument, Placement,
edit::{between, moved},
};
use crate::documents::{MarkdownMode, modes::joint};
/// Line and column, from zero, of byte `at` in `text`.
@@ -189,27 +194,25 @@ impl MarkdownDocument {
.saturating_sub(1)
}
/// Reveals the pressed heading, found again in the text.
/// Reveals the pressed heading, moved by the edit since.
pub(super) fn reveal_at(&mut self, at: usize, window: &mut Window, cx: &mut Context<Self>) {
let headings = &self.parsed.headings;
let Some(ix) = headings.iter().position(|heading| heading.at == at) else {
if !self.parsed.headings.iter().any(|heading| heading.at == at) {
return log::warn!("markdown document: a heading read away");
};
let pressed = headings[ix].clone();
let same = |heading: &&DocumentHeading| {
(heading.depth, &heading.title) == (pressed.depth, &pressed.title)
};
let nth = headings[..ix].iter().filter(same).count();
}
// The heading list drew from this read.
let drawn = Arc::clone(&self.parsed);
if self.commit(cx).is_some() || !self.read_now(cx) {
self.reparse(true, cx);
}
if !self.read_now(cx) {
return log::warn!("markdown document: a heading pressed while the text is read again");
}
let Some(found) = self.parsed.headings.iter().filter(same).nth(nth) else {
let edit = between(&drawn.text, self.editor.read(cx).text());
let headings = &self.parsed.headings;
let Some(found) = moved(at, &edit).filter(|at| headings.iter().any(|h| h.at == *at)) else {
return log::warn!("markdown document: the pressed heading changed as one wrote");
};
let line = self.editor.read(cx).buffer.line_of(found.at);
let line = self.editor.read(cx).buffer.line_of(found);
self.reveal(line, window, cx);
}
+68 -2
View File
@@ -1,6 +1,6 @@
use gpui::{Modifiers, MouseButton, TestAppContext, VisualTestContext, point, px};
use gpui::{Entity, Modifiers, MouseButton, TestAppContext, VisualTestContext, point, px};
use super::{MarkdownMode, frames, open, type_in};
use super::{MarkdownDocument, MarkdownMode, frames, open, type_in};
const LISTED: &str = "[toc]\n\n# A\n\ntext\n\n## B\n";
@@ -11,10 +11,76 @@ fn press_entry(ix: usize, modifiers: Modifiers, cx: &mut VisualTestContext) {
.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);
frames(cx);
cx.simulate_mouse_up(at, MouseButton::Left, modifiers);
cx.run_until_parked();
}
#[gpui::test]
fn a_heading_written_above_with_the_same_title_is_not_the_pressed_one(cx: &mut TestAppContext) {
let text = "before\n\n[toc]\n\n# A\n\ntext\n\n## B\n";
let (document, cx) = open(text, MarkdownMode::Visual, cx);
type_in(&document, Some(0), Some("## B"), cx);
frames(cx);
press_entry(1, Modifiers::secondary_key(), cx);
let (caret, now) =
document.read_with(cx, |document, cx| (document.caret(cx), document.text(cx)));
assert_eq!(now, "## B\n\n[toc]\n\n# A\n\ntext\n\n## B\n");
assert_eq!(caret, (8, 0));
}
#[gpui::test]
fn a_twin_typed_above_while_the_text_waits_is_not_the_pressed_one(cx: &mut TestAppContext) {
let (document, cx) = open(LISTED, MarkdownMode::Split, cx);
document.update(cx, |document, cx| {
document.editor.update(cx, |editor, cx| {
editor.edit([(0..0, "## B\n\n".to_string())], cx);
});
});
cx.run_until_parked();
assert!(!document.read_with(cx, |document, cx| document.read_now(cx)));
press_entry(1, Modifiers::none(), cx);
assert_eq!(
document.read_with(cx, |document, cx| document.caret(cx)),
(8, 0)
);
}
const TWINS: &str = "[toc]\n\n## B\n\ntext\n\n## B\n";
/// Deletes the first twin while the text waits.
fn delete_first_twin(document: &Entity<MarkdownDocument>, cx: &mut VisualTestContext) {
document.update(cx, |document, cx| {
document.editor.update(cx, |editor, cx| {
editor.edit([(7..13, String::new())], cx);
});
});
cx.run_until_parked();
}
#[gpui::test]
fn a_deleted_twin_leaves_the_other_found(cx: &mut TestAppContext) {
let (document, cx) = open(TWINS, MarkdownMode::Split, cx);
delete_first_twin(&document, cx);
press_entry(1, Modifiers::none(), cx);
assert_eq!(
document.read_with(cx, |document, cx| document.caret(cx)),
(4, 0)
);
}
#[gpui::test]
fn a_press_on_a_deleted_twin_moves_nothing(cx: &mut TestAppContext) {
let (document, cx) = open(TWINS, MarkdownMode::Split, cx);
delete_first_twin(&document, cx);
let before = document.read_with(cx, |document, cx| document.caret(cx));
press_entry(0, Modifiers::none(), cx);
assert_eq!(
document.read_with(cx, |document, cx| document.caret(cx)),
before
);
}
#[gpui::test]
fn a_press_in_preview_reveals_the_heading(cx: &mut TestAppContext) {
let (document, cx) = open(LISTED, MarkdownMode::Preview, cx);
+99 -15
View File
@@ -90,7 +90,7 @@ impl Reader<'_> {
Some(Node::Alert { kind, body })
}
/// An HTML block's text; comments, scripts and styles drop.
/// An HTML block's text, as a browser shows it.
pub(super) fn html_block(&self, written: &str, out: &mut Vec<Node>) {
let mut run = Run::default();
let mut rest = written;
@@ -116,14 +116,21 @@ impl Reader<'_> {
let (written, after) = rest.split_at(end);
rest = after;
match html::tag(written) {
Some(tag) if html::filtered(&tag.name) => run.words(written),
Some(tag) if html::filtered(&tag.name) => {
run.filter(&tag);
run.spaced(&html::text(written));
}
// A block closes what opened inside it.
Some(tag) if html::breaks(&tag.name) => {
run.line();
match tag.closing {
true => run.end(&tag.name),
false if tag.name != "hr" => run.start(&tag.name, None, None),
false => {}
false => {
run.implied(&tag.name);
if tag.name != "hr" {
run.start(&tag.name, None, None);
}
}
}
}
// A line's end drops its space.
@@ -147,36 +154,70 @@ 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),
};
match self.reads_emoji() {
true => self.spaced(&html::emoji(&decoded)),
false => self.spaced(&decoded),
}
}
/// Text with its white space collapsed.
fn spaced(&mut self, shown: &str) {
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(' ');
let open = self.open.len();
self.owed = (!self.text.text.is_empty()).then_some((' ', open));
}
}
}
/// A line owed where a block element starts or ends.
fn line(&mut self) {
let from = self.owed.map_or(self.open.len(), |(_, from)| from);
let text = &self.text.text;
self.owed = (!(text.is_empty() || text.ends_with('\n'))).then_some('\n');
self.owed = (!(text.is_empty() || text.ends_with('\n'))).then_some(('\n', from));
}
/// Writes the white space owed, before a word or tag.
fn pay(&mut self) {
let Some(owed) = self.owed.take() else {
let Some((owed, from)) = self.owed.take() else {
return;
};
let at = self.text.text.len();
self.text.text.push(owed);
// Reopened elements start after it.
for opened in self.open.iter_mut().filter(|opened| opened.start == at) {
opened.start = self.text.text.len();
let start = self.text.text.len();
// What opened after it starts after it.
for opened in &mut self.open[from..] {
opened.start = start;
}
}
/// Ends what a block's start tag closes, as HTML5 does.
fn implied(&mut self, name: &str) {
let items: &[&str] = match name {
"li" => &["li"],
"dd" | "dt" => &["dd", "dt"],
_ => &[],
};
// The search stops at a special element.
let stops = |name: &str| html::breaks(name) && !matches!(name, "address" | "div" | "p");
let found = items
.iter()
.any(|item| self.count(item) > 0)
.then(|| {
let mut names = self.open.iter().rev().map(|opened| opened.name.as_str());
names.find(|name| items.contains(name) || stops(name))
})
.flatten()
.filter(|name| items.contains(name))
.map(str::to_string);
if let Some(item) = found {
self.end(&item);
}
// A table start closes it, so it is in scope.
if name != "tr" && self.count("p") > 0 {
self.end("p");
}
}
}
@@ -347,6 +388,45 @@ mod tests {
);
let bold = html("<div><b>x</div>y").expect("text");
assert_eq!(bold.styles, [(0..1, Style::Strong), (2..3, Style::Strong)]);
let kept = html("<div><mark><p>x</p>y</mark></div>").expect("text");
assert_eq!(kept.styles, [(0..3, Style::Mark)]);
}
#[test]
fn a_block_start_ends_what_html5_ends() {
let mark = [(0..1, Style::Mark)];
for source in [
"<ul><li><mark>x<li>y</ul>z",
"<div><p><mark>x<div>y</div>z</div>",
"<dl><dt><mark>x<dd>y</dl>z",
] {
let line = html(source).expect("text");
assert_eq!(
(line.text.as_str(), &line.styles[..]),
("x\ny\nz", &mark[..])
);
}
let kbd = html("<div><p><kbd>x<p>y</div>z").expect("text");
assert_eq!(kbd.styles, [(0..1, Style::Code)]);
let nested = html("<ul><li><mark>a<ul><li>b</li></ul></li></ul>").expect("text");
assert_eq!(
nested.styles,
[(0..3, Style::Mark)],
"a list stops the search"
);
}
#[test]
fn filtered_tags_keep_shortcodes_and_names_end_as_html5_ends_them() {
let block = html("<script title=\":smile:\">:smile:</script>").expect("text");
assert_eq!(block.text, "<script title=\":smile:\">:smile:</script>");
assert_eq!(html("<div>:smile:</div>").expect("text").text, "😄");
let attribute = html("a <script title=\":smile:\">x</script> c").expect("text");
assert_eq!(attribute.text, "a <script title=\":smile:\">x</script> c");
let body = html("a <script>:smile:</script> :smile:").expect("text");
assert_eq!(body.text, "a <script>:smile:</script> 😄");
let named = "<div>a<script:custom>x</script:custom>b<script!>y</script!>c</div>";
assert_eq!(html(named).expect("text").text, "axbyc");
}
#[test]
@@ -359,6 +439,10 @@ mod tests {
"<b>".repeat(1 << 16),
"<b>".repeat(1 << 15) + &"</i>".repeat(1 << 15),
"<script></script>".repeat(1 << 13),
"<div>a ".repeat(1 << 16),
"<b> ".repeat(1 << 16),
"<li><b>x ".repeat(1 << 15),
"<p><i>x ".repeat(1 << 15),
] {
let block = format!("<div>\n{source}");
parse(&block);
+1 -1
View File
@@ -125,7 +125,7 @@ pub(crate) fn tag(html: &str) -> Option<Tag> {
return None;
}
let named = inner
.find(|ch: char| !(ch.is_ascii_alphanumeric() || ch == '-'))
.find(|ch: char| spacing(ch) || ch == '/')
.unwrap_or(inner.len());
Some(Tag {
name: inner[..named].to_ascii_lowercase(),
+27 -10
View File
@@ -102,8 +102,10 @@ struct Run {
named: HashMap<String, usize>,
/// Inside an autolink: its text stays as written.
verbatim: bool,
/// White space an HTML block owes the next word.
owed: Option<char>,
/// Filtered tags open: their text keeps shortcodes.
filtered: usize,
/// White space owed, and the open elements before it.
owed: Option<(char, usize)>,
}
impl Run {
@@ -122,21 +124,32 @@ impl Run {
self.named.get(name).copied().unwrap_or(0)
}
/// Inside HTML `<code>`, where shortcodes stay text.
fn in_code(&self) -> bool {
self.count("code") > 0
/// Outside autolinks, `<code>` and filtered tags.
fn reads_emoji(&self) -> bool {
!self.verbatim && self.count("code") == 0 && self.filtered == 0
}
/// Text as shown: shortcodes read, lines joined.
fn push_text(&mut self, written: &str) {
let shown = match self.verbatim || self.in_code() {
true => written.to_string(),
false => html::emoji(written),
};
match self.reads_emoji() {
true => self.push_plain(&html::emoji(written)),
false => self.push_plain(written),
}
}
fn push_plain(&mut self, shown: &str) {
let soft = shown.replace("\r\n", " ").replace(['\r', '\n'], " ");
self.text.text.push_str(&soft);
}
/// Counts a filtered tag opened or closed.
fn filter(&mut self, tag: &html::Tag) {
self.filtered = match tag.closing {
true => self.filtered.saturating_sub(1),
false => self.filtered + 1,
};
}
fn push(&mut self, opened: Opened) {
*self.named.entry(opened.name.clone()).or_default() += 1;
self.open.push(opened);
@@ -156,7 +169,8 @@ impl Run {
return self.text.text.push('\n');
}
if html::filtered(&tag.name) {
return self.push_text(&html::text(written));
self.filter(&tag);
return self.push_plain(&html::text(written));
}
let Some(style) = html::inline_style(&tag.name) else {
return;
@@ -196,6 +210,9 @@ impl Run {
.rposition(|opened| opened.name == name)
.expect("a counted name is open");
let end = self.text.text.len();
if let Some((_, from)) = &mut self.owed {
*from = (*from).min(at);
}
let closed = self.open.split_off(at);
for opened in &closed {
close(&mut self.text, opened, end);