pub mod code_blocks;
pub mod headings;
pub mod links;
pub mod sections;
pub mod tables;
use crate::model::document::{CodeBlock, Heading, Link, Section, Table, TableAlignment};
use pulldown_cmark::{Event, Parser};
pub struct MarkdownParser;
impl MarkdownParser {
pub fn parse(
body: &str,
) -> (
Vec<Heading>,
Vec<Link>,
String,
Vec<Section>,
Vec<Table>,
Vec<CodeBlock>,
) {
let parser = Parser::new_ext(body, pulldown_cmark::Options::ENABLE_TABLES);
let events: Vec<_> = parser.collect();
let headings = headings::extract_headings(&events);
let links = links::extract_links(&events, body);
let tables = tables::extract_tables(&events);
let code_blocks = code_blocks::extract_code_blocks(&events);
let sections = sections::build_sections(&events, body);
let searchable_text = events
.iter()
.filter_map(|e| match e {
Event::Text(t) | Event::Code(t) => Some(t.to_string()),
_ => None,
})
.collect::<Vec<_>>()
.join(" ");
(
headings,
links,
searchable_text,
sections,
tables,
code_blocks,
)
}
}