use crate::model::*;
use pulldown_cmark::{CodeBlockKind as ParsedCodeBlockKind, Event, Options, Parser, Tag};
use std::{collections::HashSet, fmt, ops::Range};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ParseLimits {
max_source_bytes: usize,
max_nesting_depth: usize,
max_events: usize,
}
impl ParseLimits {
pub const MAX_NESTING_DEPTH: usize = 128;
pub const fn new(max_source_bytes: usize, max_nesting_depth: usize, max_events: usize) -> Self {
Self {
max_source_bytes,
max_nesting_depth: if max_nesting_depth > Self::MAX_NESTING_DEPTH {
Self::MAX_NESTING_DEPTH
} else {
max_nesting_depth
},
max_events,
}
}
pub const fn max_source_bytes(self) -> usize {
self.max_source_bytes
}
pub const fn max_nesting_depth(self) -> usize {
self.max_nesting_depth
}
pub const fn max_events(self) -> usize {
self.max_events
}
pub const fn with_max_source_bytes(mut self, bytes: usize) -> Self {
self.max_source_bytes = bytes;
self
}
pub const fn with_max_nesting_depth(mut self, depth: usize) -> Self {
self.max_nesting_depth = if depth > Self::MAX_NESTING_DEPTH {
Self::MAX_NESTING_DEPTH
} else {
depth
};
self
}
pub const fn with_max_events(mut self, events: usize) -> Self {
self.max_events = events;
self
}
}
impl Default for ParseLimits {
fn default() -> Self {
Self::new(2 * 1024 * 1024, 64, 200_000)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ParseError {
TooLarge {
bytes: usize,
limit: usize,
},
TooDeep {
limit: usize,
},
TooComplex {
limit: usize,
},
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TooLarge { bytes, limit } => {
write!(f, "document is {bytes} bytes; limit is {limit}")
}
Self::TooDeep { limit } => write!(f, "document nesting exceeds {limit}"),
Self::TooComplex { limit } => write!(f, "document exceeds {limit} parser events"),
}
}
}
impl std::error::Error for ParseError {}
struct Node<'a> {
event: Event<'a>,
range: Range<usize>,
children: Vec<Node<'a>>,
}
struct Builder {
document: Document,
anchors: HashSet<String>,
}
impl Document {
pub fn parse(source: &str) -> Result<Self, ParseError> {
Self::parse_with_limits(source, ParseLimits::default())
}
pub fn parse_with_limits(source: &str, limits: ParseLimits) -> Result<Self, ParseError> {
if source.len() > limits.max_source_bytes() {
return Err(ParseError::TooLarge {
bytes: source.len(),
limit: limits.max_source_bytes(),
});
}
let options =
Options::ENABLE_TABLES | Options::ENABLE_TASKLISTS | Options::ENABLE_STRIKETHROUGH;
let mut stack: Vec<Node<'_>> = Vec::new();
let mut nodes = Vec::new();
for (count, (event, range)) in Parser::new_ext(source, options)
.into_offset_iter()
.enumerate()
{
if count >= limits.max_events() {
return Err(ParseError::TooComplex {
limit: limits.max_events(),
});
}
if matches!(event, Event::Start(_)) {
if stack.len() >= limits.max_nesting_depth() {
return Err(ParseError::TooDeep {
limit: limits.max_nesting_depth(),
});
}
stack.push(Node {
event,
range,
children: Vec::new(),
});
continue;
}
let node = if matches!(event, Event::End(_)) {
let mut node = stack.pop().expect("parser start/end events are balanced");
node.range.end = node.range.end.max(range.end);
node
} else {
Node {
event,
range,
children: Vec::new(),
}
};
if let Some(parent) = stack.last_mut() {
parent.children.push(node);
} else {
nodes.push(node);
}
}
let mut builder = Builder {
document: Document {
blocks: Vec::new(),
headings: Vec::new(),
links: Vec::new(),
},
anchors: HashSet::new(),
};
builder.document.blocks = builder.blocks(&nodes);
Ok(builder.document)
}
}
impl Builder {
fn blocks(&mut self, nodes: &[Node<'_>]) -> Vec<Block> {
let mut out = Vec::new();
let mut pending = Vec::new();
for node in nodes {
let kind = match &node.event {
Event::Start(Tag::Paragraph) => {
Some(BlockKind::Paragraph(self.inlines(&node.children)))
}
Event::Start(Tag::Heading { level, .. }) => {
let content = self.inlines(&node.children);
let text = plain_text(&content);
let slug = slug(&text);
let mut id = slug.clone();
let mut suffix = 0;
while !self.anchors.insert(id.clone()) {
suffix += 1;
id = format!("{slug}-{suffix}");
}
let id = HeadingId::from_generated(id);
let level = heading_level(*level);
self.document.headings.push(Heading {
id: id.clone(),
level,
text,
span: SourceSpan::from_range(node.range.clone()),
});
Some(BlockKind::Heading { level, id, content })
}
Event::Start(Tag::BlockQuote(_)) => {
Some(BlockKind::Quote(self.blocks(&node.children)))
}
Event::Start(Tag::List(start)) => Some(BlockKind::List {
kind: start.map_or(ListKind::Unordered, |start| ListKind::Ordered { start }),
items: node
.children
.iter()
.map(|item| ListItem {
task: task_marker(item),
blocks: self.blocks(&item.children),
})
.collect(),
}),
Event::Start(Tag::CodeBlock(kind)) => Some(BlockKind::Code {
kind: match kind {
ParsedCodeBlockKind::Fenced(info) => {
CodeBlockKind::Fenced(info.to_string())
}
ParsedCodeBlockKind::Indented => CodeBlockKind::Indented,
},
text: literal(node),
}),
Event::Start(Tag::HtmlBlock) => Some(BlockKind::Literal(literal(node))),
Event::Start(Tag::Table(align)) => {
let mut header = TableRow::from_cells(Vec::new());
let mut rows = Vec::new();
for row in &node.children {
let cells = TableRow::from_cells(
row.children
.iter()
.map(|cell| TableCell::from_inlines(self.inlines(&cell.children)))
.collect(),
);
if matches!(row.event, Event::Start(Tag::TableHead)) {
header = cells;
} else {
rows.push(cells);
}
}
Some(BlockKind::Table(Table::new(
align
.iter()
.map(|a| match a {
pulldown_cmark::Alignment::None => Alignment::None,
pulldown_cmark::Alignment::Left => Alignment::Left,
pulldown_cmark::Alignment::Center => Alignment::Center,
pulldown_cmark::Alignment::Right => Alignment::Right,
})
.collect(),
header,
rows,
)))
}
Event::Rule => Some(BlockKind::Rule),
_ => None,
};
if let Some(kind) = kind {
flush(&mut pending, &mut out);
out.push(Block {
kind,
span: SourceSpan::from_range(node.range.clone()),
});
} else {
pending.extend(self.inlines(std::slice::from_ref(node)));
}
}
flush(&mut pending, &mut out);
out
}
fn inlines(&mut self, nodes: &[Node<'_>]) -> Vec<Inline> {
nodes
.iter()
.filter_map(|node| {
let kind = match &node.event {
Event::Text(s) | Event::Html(s) | Event::InlineHtml(s) => {
InlineKind::Text(s.to_string())
}
Event::Code(s) => InlineKind::Code(s.to_string()),
Event::SoftBreak => InlineKind::SoftBreak,
Event::HardBreak => InlineKind::HardBreak,
Event::Start(Tag::Emphasis) => {
InlineKind::Emphasis(self.inlines(&node.children))
}
Event::Start(Tag::Strong) => InlineKind::Strong(self.inlines(&node.children)),
Event::Start(Tag::Strikethrough) => {
InlineKind::Strikethrough(self.inlines(&node.children))
}
Event::Start(Tag::Link {
dest_url, title, ..
}) => {
let content = self.inlines(&node.children);
let id = LinkId::from_index(self.document.links.len());
self.document.links.push(Link {
id,
destination: LinkDestination::from_parser(dest_url.to_string()),
title: (!title.is_empty()).then(|| title.to_string()),
text: plain_text(&content),
span: SourceSpan::from_range(node.range.clone()),
});
InlineKind::Link { id, content }
}
Event::Start(Tag::Image {
dest_url, title, ..
}) => InlineKind::Image {
destination: LinkDestination::from_parser(dest_url.to_string()),
title: (!title.is_empty()).then(|| title.to_string()),
alt: self.inlines(&node.children),
},
Event::TaskListMarker(_) => return None,
_ => InlineKind::Text(literal(node)),
};
Some(Inline {
kind,
span: SourceSpan::from_range(node.range.clone()),
})
})
.collect()
}
}
fn flush(pending: &mut Vec<Inline>, blocks: &mut Vec<Block>) {
if let (Some(first), Some(last)) = (pending.first(), pending.last()) {
let range = first.span.start()..last.span.end();
blocks.push(Block {
kind: BlockKind::Paragraph(std::mem::take(pending)),
span: SourceSpan::from_range(range),
});
}
}
fn literal(node: &Node<'_>) -> String {
match &node.event {
Event::Text(s)
| Event::Code(s)
| Event::Html(s)
| Event::InlineHtml(s)
| Event::InlineMath(s)
| Event::DisplayMath(s)
| Event::FootnoteReference(s) => s.to_string(),
Event::SoftBreak | Event::HardBreak => "\n".to_owned(),
_ => node.children.iter().map(literal).collect(),
}
}
fn task_marker(node: &Node<'_>) -> TaskState {
for child in &node.children {
match child.event {
Event::TaskListMarker(true) => return TaskState::Checked,
Event::TaskListMarker(false) => return TaskState::Unchecked,
Event::Start(Tag::Paragraph) => {
let task = task_marker(child);
if task != TaskState::Plain {
return task;
}
}
_ => {}
}
}
TaskState::Plain
}
fn heading_level(level: pulldown_cmark::HeadingLevel) -> HeadingLevel {
match level {
pulldown_cmark::HeadingLevel::H1 => HeadingLevel::H1,
pulldown_cmark::HeadingLevel::H2 => HeadingLevel::H2,
pulldown_cmark::HeadingLevel::H3 => HeadingLevel::H3,
pulldown_cmark::HeadingLevel::H4 => HeadingLevel::H4,
pulldown_cmark::HeadingLevel::H5 => HeadingLevel::H5,
pulldown_cmark::HeadingLevel::H6 => HeadingLevel::H6,
}
}
fn slug(text: &str) -> String {
let mut out = String::new();
for c in text.to_lowercase().chars() {
if c.is_alphanumeric() || c == '-' || c == '_' {
out.push(c);
} else if c.is_whitespace() && !out.ends_with('-') {
out.push('-');
}
}
let out = out.trim_matches('-');
if out.is_empty() {
"section".to_owned()
} else {
out.to_owned()
}
}