#[derive(Debug, Clone, Default, PartialEq)]
pub struct Document {
pub blocks: Vec<Block>,
pub footnotes: Vec<(String, Vec<Block>)>,
pub title: Option<String>,
pub front_matter: Option<String>,
}
impl Document {
pub fn headings(&self) -> Vec<(u8, String, String)> {
let mut out = Vec::new();
collect_headings(&self.blocks, &mut out);
out
}
}
fn collect_headings(blocks: &[Block], out: &mut Vec<(u8, String, String)>) {
for b in blocks {
match b {
Block::Heading { level, id, content } => {
out.push((*level, plain_text(content), id.clone()));
}
Block::BlockQuote { blocks, .. } => collect_headings(blocks, out),
Block::List(list) => {
for item in &list.items {
collect_headings(&item.blocks, out);
}
}
_ => {}
}
}
}
pub fn plain_text(inlines: &[Inline]) -> String {
let mut s = String::new();
push_plain(inlines, &mut s);
s
}
fn push_plain(inlines: &[Inline], out: &mut String) {
for i in inlines {
match i {
Inline::Text(t) | Inline::Code(t) | Inline::Math { text: t, .. } => out.push_str(t),
Inline::Emph(c) | Inline::Strong(c) | Inline::Strike(c) => push_plain(c, out),
Inline::Superscript(c) | Inline::Subscript(c) => push_plain(c, out),
Inline::Link { content, .. } => push_plain(content, out),
Inline::Image(img) => out.push_str(&img.alt),
Inline::SoftBreak | Inline::HardBreak => out.push(' '),
Inline::FootnoteRef(label) => {
out.push('[');
out.push_str(label);
out.push(']');
}
Inline::Html(_) => {}
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Block {
Heading {
level: u8,
id: String,
content: Inlines,
},
Paragraph(Inlines),
CodeBlock {
language: Option<String>,
code: String,
},
BlockQuote {
kind: Option<AlertKind>,
blocks: Vec<Block>,
},
List(List),
Table(Table),
Figure {
image: ImageRef,
caption: Inlines,
},
Rule,
Html(String),
DisplayMath(String),
DefinitionList(Vec<(Inlines, Vec<Vec<Block>>)>),
FootnoteDefinition {
label: String,
blocks: Vec<Block>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AlertKind {
Note,
Tip,
Important,
Warning,
Caution,
}
impl AlertKind {
pub fn label(self) -> &'static str {
match self {
Self::Note => "NOTE",
Self::Tip => "TIP",
Self::Important => "IMPORTANT",
Self::Warning => "WARNING",
Self::Caution => "CAUTION",
}
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct List {
pub start: Option<u64>,
pub tight: bool,
pub items: Vec<ListItem>,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ListItem {
pub task: Option<bool>,
pub blocks: Vec<Block>,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Table {
pub alignments: Vec<Alignment>,
pub head: Vec<Cell>,
pub rows: Vec<Vec<Cell>>,
}
pub type Cell = Inlines;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Alignment {
#[default]
None,
Left,
Center,
Right,
}
pub type Inlines = Vec<Inline>;
#[derive(Debug, Clone, PartialEq)]
pub enum Inline {
Text(String),
Code(String),
Emph(Inlines),
Strong(Inlines),
Strike(Inlines),
Superscript(Inlines),
Subscript(Inlines),
Link {
dest: String,
title: Option<String>,
content: Inlines,
},
Image(ImageRef),
SoftBreak,
HardBreak,
FootnoteRef(String),
Math {
text: String,
display: bool,
},
Html(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ImageRef {
pub url: String,
pub alt: String,
pub title: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn plain_text_flattens_nested_inlines() {
let inlines = vec![
Inline::Text("a ".into()),
Inline::Strong(vec![Inline::Emph(vec![Inline::Text("b".into())])]),
Inline::SoftBreak,
Inline::Code("c".into()),
];
assert_eq!(plain_text(&inlines), "a b c");
}
#[test]
fn headings_are_collected_in_document_order() {
let doc = Document {
blocks: vec![
Block::Heading {
level: 1,
id: "one".into(),
content: vec![Inline::Text("One".into())],
},
Block::BlockQuote {
kind: None,
blocks: vec![Block::Heading {
level: 2,
id: "two".into(),
content: vec![Inline::Text("Two".into())],
}],
},
],
..Default::default()
};
let h = doc.headings();
assert_eq!(h.len(), 2);
assert_eq!(h[0], (1, "One".to_string(), "one".to_string()));
assert_eq!(h[1].0, 2);
}
}
pub fn rebase_urls(doc: &mut Document, base: &std::path::Path) {
rebase_blocks(&mut doc.blocks, base);
for (_, blocks) in &mut doc.footnotes {
rebase_blocks(blocks, base);
}
}
fn rebase_blocks(blocks: &mut [Block], base: &std::path::Path) {
for block in blocks {
match block {
Block::Heading { content, .. } | Block::Paragraph(content) => {
rebase_inlines(content, base)
}
Block::BlockQuote { blocks, .. } | Block::FootnoteDefinition { blocks, .. } => {
rebase_blocks(blocks, base)
}
Block::List(list) => {
for item in &mut list.items {
rebase_blocks(&mut item.blocks, base);
}
}
Block::Table(table) => {
for cell in table.head.iter_mut().chain(table.rows.iter_mut().flatten()) {
rebase_inlines(cell, base);
}
}
Block::Figure { image, caption } => {
rebase_url(&mut image.url, base);
rebase_inlines(caption, base);
}
Block::DefinitionList(items) => {
for (term, defs) in items {
rebase_inlines(term, base);
for blocks in defs {
rebase_blocks(blocks, base);
}
}
}
Block::CodeBlock { .. } | Block::Rule | Block::Html(_) | Block::DisplayMath(_) => {}
}
}
}
fn rebase_inlines(inlines: &mut [Inline], base: &std::path::Path) {
for inline in inlines {
match inline {
Inline::Image(img) => rebase_url(&mut img.url, base),
Inline::Link { dest, content, .. } => {
rebase_url(dest, base);
rebase_inlines(content, base);
}
Inline::Emph(c) | Inline::Strong(c) | Inline::Strike(c) => rebase_inlines(c, base),
Inline::Superscript(c) | Inline::Subscript(c) => rebase_inlines(c, base),
_ => {}
}
}
}
fn rebase_url(url: &mut String, base: &std::path::Path) {
if url.is_empty()
|| url.starts_with('#')
|| url.starts_with('/')
|| url.starts_with("data:")
|| url.split_once("://").is_some()
|| url.split_once(':').is_some_and(|(scheme, _)| {
!scheme.is_empty()
&& scheme
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '+' || c == '-')
})
{
return;
}
let joined = base.join(&*url);
let absolute = std::path::absolute(&joined).unwrap_or(joined);
if let Some(s) = absolute.to_str() {
*url = s.to_string();
}
}
#[cfg(test)]
mod rebase_tests {
use super::*;
use std::path::{Path, PathBuf};
fn base() -> PathBuf {
std::path::absolute("docs").expect("a working directory")
}
fn url_after_rebase(url: &str) -> String {
rebase_from(&base(), url)
}
fn rebase_from(base: &Path, url: &str) -> String {
let mut doc = Document {
blocks: vec![Block::Figure {
image: ImageRef {
url: url.into(),
..Default::default()
},
caption: Vec::new(),
}],
..Default::default()
};
rebase_urls(&mut doc, base);
match &doc.blocks[0] {
Block::Figure { image, .. } => image.url.clone(),
_ => unreachable!(),
}
}
#[test]
fn makes_relative_paths_absolute() {
assert_eq!(
Path::new(&url_after_rebase("img/logo.png")),
base().join("img/logo.png")
);
assert_eq!(
Path::new(&url_after_rebase("./logo.png")),
base().join("logo.png")
);
}
#[test]
fn leaves_everything_else_alone() {
for url in [
"https://example.com/a.png",
"http://example.com/a.png",
"/absolute/a.png",
"data:image/png;base64,AAAA",
"#anchor",
"mailto:someone@example.com",
] {
assert_eq!(url_after_rebase(url), url, "{url} should not be rebased");
}
}
#[test]
fn a_relative_base_still_yields_an_absolute_path() {
let url = rebase_from(Path::new("doc"), "images/x.png");
let path = Path::new(&url);
assert!(path.is_absolute(), "got {url}");
assert!(path.ends_with("doc/images/x.png"), "got {url}");
}
#[test]
fn rebases_inside_nested_blocks() {
let mut doc = Document {
blocks: vec![Block::BlockQuote {
kind: None,
blocks: vec![Block::Paragraph(vec![Inline::Link {
dest: "other.md".into(),
title: None,
content: vec![Inline::Text("x".into())],
}])],
}],
..Default::default()
};
rebase_urls(&mut doc, &base());
let Block::BlockQuote { blocks, .. } = &doc.blocks[0] else {
panic!()
};
let Block::Paragraph(inlines) = &blocks[0] else {
panic!()
};
let Inline::Link { dest, .. } = &inlines[0] else {
panic!("expected a link")
};
assert_eq!(Path::new(dest), base().join("other.md"));
}
}