use std::{ops::Range, sync::Arc};
use comrak::{
Arena, Options, ResolvedReference,
nodes::{AstNode, ListType, NodeValue, Sourcepos},
options::{BrokenLinkCallback, BrokenLinkReference},
};
use super::{
Block, BlockDetail, BlockKind, Body, Dialect, Heading, Link, Marker, Piece, Reading, Segments,
Unit, anchor, body, code, dialect,
link::{REF, target},
};
mod columns;
mod definitions;
mod references;
mod text;
pub(super) use columns::written_pos;
use columns::{line_at, precedes_table};
pub(super) use definitions::Defined;
pub(super) use text::strip_prefix;
struct KeepLabel;
impl BrokenLinkCallback for KeepLabel {
fn resolve(&self, r: BrokenLinkReference<'_>) -> Option<ResolvedReference> {
if r.original.starts_with('^') {
return None;
}
Some(ResolvedReference::new(
format!("{REF}{}", r.original),
String::new(),
))
}
}
pub(super) fn options() -> Options<'static> {
let mut options = Options::default();
options.extension.table = true;
options.extension.footnotes = true;
options.extension.tasklist = true;
options.parse.leave_footnote_definitions = true;
options.parse.broken_link_callback = Some(Arc::new(KeepLabel));
options.parse.escaped_char_spans = true;
options
}
pub fn parse(body: &str, first_line: usize, segments: &Segments, dialect: &Dialect) -> Body {
let (masked, written) = mask(body, first_line, segments);
let options = options();
let stripped = anchor::strip(&masked, first_line, &options);
let masked = stripped.text;
let mut lines: Vec<&str> = code::each_line(&masked).map(|l| &masked[l]).collect();
if lines.last() == Some(&"") {
lines.pop();
}
let arena = Arena::new();
let parsed = stripped.parsed.as_deref().unwrap_or(&masked);
let root = comrak::parse_document(&arena, parsed, &options);
let mut p = Parser {
dialect,
source: parsed,
lines: &lines,
defined: Defined::of(root, &lines),
offset: first_line.saturating_sub(1),
body: Body {
first_line,
anchors: stripped.anchors,
..Body::default()
},
covered: Vec::new(),
opening: Vec::new(),
depth: 0,
};
let blocks = p.group(root.children());
p.body.blocks = blocks;
let found = code::code_blocks(root, &written);
p.body.fenced = found
.iter()
.map(|&(first, last, _)| (first + p.offset, last + p.offset))
.collect();
p.body.code_blocks = found.into_iter().map(|(_, _, b)| b).collect();
p.collect_inlines(root);
p.reference_blocks();
p.body.headings.sort_by_key(|h| h.line);
p.body.links.sort_by_key(|l| l.line);
p.body.blocks.sort_by_key(|b| b.line);
p.body.reading_changes.sort();
p.body
}
pub(crate) fn code_spans_in(text: &str) -> Vec<Range<usize>> {
const LEAD: &str = "\u{E000} ";
if !text.contains('`') {
return Vec::new();
}
let source = format!("{LEAD}{text}");
let arena = Arena::new();
let root = comrak::parse_document(&arena, &source, &options());
let mut out: Vec<Range<usize>> = root
.descendants()
.filter_map(|n| {
let ast = n.data();
let sp = ast.sourcepos;
(matches!(ast.value, NodeValue::Code(_))
&& sp.start.line == 1
&& sp.start.column > LEAD.len())
.then(|| {
sp.start.column - 1 - LEAD.len()
..sp.end.column.saturating_sub(LEAD.len()).min(text.len())
})
})
.filter(|r| r.start < r.end)
.collect();
out.sort_by_key(|r| r.start);
out
}
fn empty_cell<'b>(cell: &'b AstNode<'b>) -> bool {
cell.first_child().is_none()
}
fn mask(body: &str, first_line: usize, segments: &Segments) -> (String, Vec<Range<usize>>) {
let mut out = String::with_capacity(body.len());
let lines = code::lines(body);
let mut written = Vec::with_capacity(lines.len());
let mut next = 0;
let mut at = 0;
for (i, line) in body.split_inclusive('\n').enumerate() {
let file_line = first_line + i;
let end = at + line.len();
let hidden = segments.in_region(file_line) || segments.in_definition(file_line);
if hidden {
if line.ends_with('\n') {
out.push('\n');
}
written.push(at..at + line.trim_end_matches(['\n', '\r']).len());
while lines.get(next).is_some_and(|l| l.start < end) {
next += 1;
}
} else {
out.push_str(line);
while let Some(l) = lines.get(next).filter(|l| l.start < end) {
written.push(l.clone());
next += 1;
}
}
at = end;
}
written.extend(lines[next..].iter().cloned());
(out, written)
}
enum Facts {
Heading {
level: u8,
setext: bool,
},
Paragraph,
List(ListType),
Table,
Code {
lang: String,
fence: usize,
literal: String,
},
BlockQuote,
Html(String),
ThematicBreak,
Footnote(String),
Other,
}
fn leading_html<'b>(node: &'b AstNode<'b>) -> Option<String> {
let NodeValue::HtmlBlock(h) = &node.data().value else {
return None;
};
let opens_item = node.previous_sibling().is_none()
&& node
.parent()
.is_some_and(|p| matches!(p.data().value, NodeValue::Item(_)));
let text = h.literal.trim();
(opens_item && !text.starts_with("<!--")).then(|| text.to_string())
}
fn facts(value: &NodeValue) -> Facts {
match value {
NodeValue::Heading(h) => Facts::Heading {
level: h.level,
setext: h.setext,
},
NodeValue::Paragraph => Facts::Paragraph,
NodeValue::List(l) => Facts::List(l.list_type),
NodeValue::Table(_) => Facts::Table,
NodeValue::CodeBlock(c) => Facts::Code {
lang: if c.fenced {
c.info.split_whitespace().next().unwrap_or("").to_string()
} else {
String::new()
},
fence: if c.fenced { c.fence_length } else { 0 },
literal: c.literal.clone(),
},
NodeValue::BlockQuote => Facts::BlockQuote,
NodeValue::HtmlBlock(h) => Facts::Html(h.literal.trim().to_string()),
NodeValue::ThematicBreak(_) => Facts::ThematicBreak,
NodeValue::FootnoteDefinition(d) => Facts::Footnote(d.name.clone()),
_ => Facts::Other,
}
}
struct Parser<'a> {
dialect: &'a Dialect,
source: &'a str,
lines: &'a [&'a str],
defined: Defined,
offset: usize,
body: Body,
covered: Vec<(usize, usize)>,
opening: Vec<(usize, usize)>,
depth: usize,
}
impl<'a> Parser<'a> {
fn line(&self, sp: Sourcepos) -> usize {
sp.start.line + self.offset
}
fn end_line(&self, sp: Sourcepos) -> usize {
sp.end.line + self.offset
}
fn src(&self, file_line: usize) -> &'a str {
file_line
.checked_sub(self.offset)
.map_or("", |n| line_at(self.lines, n))
}
fn cover(&mut self, sp: Sourcepos) {
let range = (self.line(sp), self.end_line(sp));
self.covered.push(range);
}
fn cover_text<'b>(&mut self, node: &'b AstNode<'b>, sp: Sourcepos) {
let mut sp = if self.depth > 0 {
node.data().sourcepos
} else {
sp
};
let opens = match &node.data().value {
NodeValue::Paragraph => true,
NodeValue::Heading(h) => h.setext,
_ => false,
};
if self.depth > 0 && opens {
let floor = node.parent().map_or(1, |p| p.data().sourcepos.start.line);
while sp.start.line > floor {
let above = self.offset + sp.start.line - 1;
let covered = self.covered.iter().any(|&(s, e)| s <= above && above <= e);
if covered || strip_prefix(self.src(above)).trim().is_empty() {
break;
}
sp.start.line -= 1;
}
let start = node.data().sourcepos.start.line;
if sp.start.line < start {
self.opening
.push((self.offset + sp.start.line, self.offset + start - 1));
}
}
self.cover(sp);
}
fn nested<'b>(&mut self, nodes: impl Iterator<Item = &'b AstNode<'b>>) -> Vec<Block> {
self.depth += 1;
let blocks = self.group(nodes);
self.depth -= 1;
blocks
}
#[expect(
clippy::too_many_lines,
reason = "one pass of the parser over sibling nodes, a split of which is a refactor of its own"
)]
fn group<'b>(&mut self, nodes: impl Iterator<Item = &'b AstNode<'b>>) -> Vec<Block> {
let mut blocks: Vec<Block> = Vec::new();
let mut last_prose = false;
for node in nodes {
let after_prose = std::mem::take(&mut last_prose);
let facts = facts(&node.data().value);
let sp = definitions::block_pos(node, self.lines, &self.defined);
let line = self.line(sp);
let end_line = self.end_line(sp);
let block = match facts {
Facts::Heading { level, setext } => {
self.cover_text(node, sp);
let (text, pieces) = self.heading_text(sp, setext);
if self.depth > 0 {
let hashes = format!("{} ", "#".repeat(usize::from(level)));
let pieces: Vec<Piece> = pieces
.into_iter()
.map(|p| Piece {
offset: p.offset + hashes.len(),
..p
})
.collect();
let written = format!("{hashes}{text}");
let unit = Unit {
reading: self.read(&written, &pieces),
pieces,
..Unit::leaf(written, line, end_line)
};
blocks.push(Block::new(BlockKind::Heading, line, end_line, vec![unit]));
continue;
}
let reading = self.read(&text, &pieces);
self.body.headings.push(Heading::new(
level,
text,
reading,
(line, end_line),
setext,
));
continue;
}
Facts::Paragraph => {
self.cover_text(node, sp);
if let Some(images) = images_of(node, self.lines, &self.defined) {
let units = images
.into_iter()
.map(|(url, l)| Unit::leaf(url, l + self.offset, l + self.offset))
.collect();
Block::new(BlockKind::Image, line, end_line, units)
} else {
let (text, pieces) = self.mapped_text(sp);
let unit = Unit {
reading: self.read(&text, &pieces),
pieces,
..Unit::leaf(text, line, end_line)
};
let html = self.html_lines(node);
last_prose = true;
if after_prose {
let prev = blocks.last_mut().expect("last prose block");
prev.units.push(unit);
prev.end_line = end_line;
prev.html_lines.extend(html);
continue;
}
Block {
html_lines: html,
..Block::new(BlockKind::Prose, line, end_line, vec![unit])
}
}
}
Facts::List(list_type) => {
let kind = match list_type {
ListType::Bullet => BlockKind::BulletList,
ListType::Ordered => BlockKind::NumberedList,
};
let html = self.html_lines(node);
let units = node.children().map(|item| self.item(item)).collect();
Block {
html_lines: html,
..Block::new(kind, line, end_line, units)
}
}
Facts::Table => {
self.cover(sp);
let html = self.html_lines(node);
self.table(node, line, end_line, html)
}
Facts::Code {
lang,
fence,
literal,
} => {
let (first, last) = code::span(sp.start.line, sp.end.line, fence > 0, &literal);
let end_line = last + self.offset;
self.covered.push((first + self.offset, end_line));
let unit = Unit::leaf(literal, line, end_line);
Block {
detail: BlockDetail::Code { lang, fence },
..Block::new(BlockKind::Code, line, end_line, vec![unit])
}
}
Facts::BlockQuote => {
let html = self.html_lines(node);
let inner = self.nested(node.children());
let headings = body::headings_in(&inner);
let units = inner
.into_iter()
.filter(|b| b.kind == BlockKind::Prose)
.flat_map(|b| b.units)
.collect();
Block {
kind: BlockKind::Blockquote,
line,
end_line,
units,
detail: BlockDetail::Quote { headings },
html_lines: html,
}
}
Facts::Html(literal) => {
self.cover(sp);
match self.dialect.marker(&literal) {
Some(Marker::Open(target)) => Block {
detail: BlockDetail::Region { target },
..Block::new(BlockKind::Region, line, end_line, Vec::new())
},
Some(_) => continue,
None if dialect::comment(&literal).is_some() => continue,
None => {
let unit = Unit::leaf(literal, line, end_line);
Block::new(BlockKind::Html, line, end_line, vec![unit])
}
}
}
Facts::ThematicBreak => {
self.cover(sp);
Block::new(BlockKind::ThematicBreak, line, end_line, Vec::new())
}
Facts::Footnote(name) => {
self.cover(sp);
self.body.footnote_defs.push((name, line));
last_prose = after_prose;
continue;
}
Facts::Other => continue,
};
blocks.push(block);
}
blocks
}
fn item<'b>(&mut self, item: &'b AstNode<'b>) -> Unit {
let line = self.line(item.data().sourcepos);
let mut text_line = line;
let mut text = String::new();
let mut pieces = Vec::new();
let mut end_line = line;
let mut children = item.children().peekable();
if let Some(first) = children.peek() {
let is_para = matches!(first.data().value, NodeValue::Paragraph);
let sp = definitions::block_pos(first, self.lines, &self.defined);
if is_para && images_of(first, self.lines, &self.defined).is_none() {
let mut covers = first.data().sourcepos;
covers.start = item.data().sourcepos.start;
self.cover(covers);
if line < self.line(sp) {
self.opening.push((line, self.line(sp) - 1));
}
(text, pieces) = self.mapped_text(sp);
text_line = self.line(sp);
end_line = self.end_line(sp);
children.next();
} else if let Some(html) = leading_html(first) {
self.cover(sp);
text = html;
end_line = self.end_line(sp);
children.next();
}
}
let nested = self.nested(children);
let reading = if pieces.is_empty() {
Reading::written(&text)
} else {
self.read(&text, &pieces)
};
Unit {
text,
reading,
line: text_line,
end_line,
nested,
pieces,
}
}
fn table<'b>(
&mut self,
table: &'b AstNode<'b>,
line: usize,
end_line: usize,
html_lines: Vec<usize>,
) -> Block {
let mut columns = Vec::new();
let mut readings = Vec::new();
let mut units = Vec::new();
for row in table.children() {
let (header, sp) = {
let ast = row.data();
let NodeValue::TableRow(row) = &ast.value else {
continue;
};
(row.header, ast.sourcepos)
};
let row_line = self.line(sp);
let cells: Vec<(String, Vec<Piece>, Reading)> = row
.children()
.map(|cell| {
if empty_cell(cell) {
(String::new(), Vec::new(), Reading::written(""))
} else {
let (text, pieces) = self.mapped_text(cell.data().sourcepos);
let reading = self.read(&text, &pieces);
(text, pieces, reading)
}
})
.collect();
if header {
(columns, readings) = cells.into_iter().map(|(t, _, r)| (t, r.text)).unzip();
} else {
let mut text = String::new();
let mut pieces = Vec::new();
let mut reading = Reading::default();
for (i, (cell, cell_pieces, cell_reading)) in cells.into_iter().enumerate() {
if i > 0 {
reading.map.extend(text.len()..text.len() + 3);
reading.text.push_str(" | ");
text.push_str(" | ");
}
let base = text.len();
pieces.extend(cell_pieces.into_iter().map(|p| Piece {
offset: base + p.offset,
..p
}));
let read = cell_reading.text.len();
reading
.map
.extend(cell_reading.map[..read].iter().map(|o| base + o));
reading.text.push_str(&cell_reading.text);
text.push_str(&cell);
}
reading.map.push(text.len());
units.push(Unit {
reading,
pieces,
..Unit::leaf(text, row_line, row_line)
});
}
}
Block {
detail: BlockDetail::Table { columns, readings },
html_lines,
..Block::new(BlockKind::Table, line, end_line, units)
}
}
fn html_lines<'b>(&self, node: &'b AstNode<'b>) -> Vec<usize> {
let mut lines = Vec::new();
for n in node.descendants() {
let ast = n.data();
let inline = matches!(&ast.value, NodeValue::HtmlInline(html) if !html.trim_start().starts_with("<!--"));
if inline {
lines.push(self.line(written_pos(n, self.lines, &self.defined)));
} else if leading_html(n).is_some() {
lines.push(self.line(ast.sourcepos));
}
}
lines.sort_unstable();
lines.dedup();
lines
}
fn collect_inlines<'b>(&mut self, root: &'b AstNode<'b>) {
for n in root.descendants() {
let ast = n.data();
let line = || self.line(written_pos(n, self.lines, &self.defined));
match &ast.value {
NodeValue::Link(link) | NodeValue::Image(link) => {
let is_image = matches!(ast.value, NodeValue::Image(_));
let sp = written_pos(n, self.lines, &self.defined);
let source = self.source_text(sp);
if let Some(target) = target(&link.url, &source, self.dialect.scheme) {
self.body.links.push(Link {
target,
line: self.line(sp),
is_image,
end_line: self.end_line(sp),
inline: inline_destination(&source),
});
}
let spans = self.spans(sp);
self.body.link_spans.extend(spans);
}
NodeValue::FootnoteReference(r) => {
self.body.footnote_refs.push((r.name.clone(), line()));
}
NodeValue::HtmlInline(html) if !html.trim_start().starts_with("<!--") => {
self.body.inline_html.push(line());
}
NodeValue::Code(_) => {
let spans = self.spans(written_pos(n, self.lines, &self.defined));
self.body.code_spans.extend(spans);
}
_ => {}
}
}
self.body.code_spans.sort_unstable();
self.body.link_spans.sort_unstable();
self.body.inline_html.sort_unstable();
self.body.inline_html.dedup();
}
fn spans(&self, sp: Sourcepos) -> Vec<(usize, usize, usize)> {
let line = self.line(sp);
let last = self.end_line(sp);
(line..=last)
.map(|file_line| {
let start = if file_line == line {
sp.start.column.saturating_sub(1)
} else {
0
};
let end = if file_line == last {
sp.end.column
} else {
usize::MAX
};
(file_line, start, end)
})
.collect()
}
}
pub(crate) fn atx_text(line: &str) -> String {
let t = line.trim_start_matches('#').trim();
let rest = t.trim_end_matches('#');
let closes = rest.len() < t.len() && (rest.is_empty() || rest.ends_with([' ', '\t']));
if closes { rest.trim_end() } else { t }.to_string()
}
fn images_of<'b>(
paragraph: &'b AstNode<'b>,
lines: &[&str],
defined: &Defined,
) -> Option<Vec<(String, usize)>> {
let mut images = Vec::new();
for child in paragraph.children() {
let ast = child.data();
match &ast.value {
NodeValue::Image(link) => {
let line = written_pos(child, lines, defined).start.line;
images.push((link.url.clone(), line));
}
NodeValue::SoftBreak | NodeValue::LineBreak => {}
NodeValue::Text(t) if t.trim().is_empty() => {}
_ => return None,
}
}
if images.is_empty() {
None
} else {
Some(images)
}
}
fn inline_destination(source: &str) -> Option<String> {
let source = source.trim_end();
if !source.ends_with(')') {
return None;
}
let tail = &source[source.rfind("](")?..];
(!tail.contains('\n')).then(|| tail.to_string())
}
#[cfg(test)]
mod tests;