use super::*;
#[must_use]
pub fn parse_markdown_page(route: &str, default_section: &str, raw: &str) -> DocPage {
let (frontmatter, body): (DocFrontmatter, &str) = split_frontmatter(raw);
parse_markdown_page_with_frontmatter(route, default_section, frontmatter, body)
}
fn parse_markdown_page_with_frontmatter(
route: &str,
default_section: &str,
frontmatter: DocFrontmatter,
body: &str,
) -> DocPage {
let mut blocks: Vec<DocBlock> = Vec::with_capacity((body.len() / 160).clamp(8, 256));
let mut toc: Vec<DocTocItem> = Vec::with_capacity((body.len() / 512).clamp(4, 32));
let mut seen_slugs: BTreeMap<String, usize> = BTreeMap::new();
let mut active_heading: Option<(u8, Option<CowStr<'_>>)> = None;
let mut first_h1_title: Option<String> = None;
let mut in_paragraph: bool = false;
let mut list_stack: Vec<String> = Vec::new();
let mut blockquote_depth: usize = 0;
let mut in_table: bool = false;
let mut in_table_head: bool = false;
let mut in_table_cell: bool = false;
let mut table_headers: Vec<String> = Vec::new();
let mut table_rows: Vec<Vec<String>> = Vec::new();
let mut current_row: Vec<String> = Vec::new();
let mut active_code_lang: Option<String> = None;
let mut active_image_src: Option<String> = None;
let mut image_alt_buf: String = String::new();
let mut buffer: String = String::with_capacity(256);
let parser: Parser<'_> = Parser::new_ext(body, Options::all());
for event in parser {
match event {
Event::Start(Tag::Heading { level, id, .. }) => {
active_heading = Some((heading_level_num(level), id));
buffer.clear();
}
Event::End(TagEnd::Heading(_)) => {
if let Some((level, custom_id)) = active_heading.take() {
let text: String = buffer.trim().to_string();
if !text.is_empty() {
if level == 1 && first_h1_title.is_none() {
first_h1_title = Some(text.clone());
}
let raw_slug: String = custom_id
.as_deref()
.map(slugify)
.filter(|s: &String| !s.is_empty())
.unwrap_or_else(|| slugify(&text));
let slug: String = deduplicate_slug(raw_slug, &mut seen_slugs);
if (2..=4).contains(&level) {
toc.push(DocTocItem::new(level, slug.clone(), text.clone()));
}
blocks.push(DocBlock::Heading { level, slug, text });
}
}
buffer.clear();
}
Event::Start(Tag::BlockQuote(_)) => {
blockquote_depth += 1;
}
Event::End(TagEnd::BlockQuote(_)) => {
blockquote_depth = blockquote_depth.saturating_sub(1);
}
Event::Start(Tag::Table(_)) => {
in_table = true;
table_headers.clear();
table_rows.clear();
}
Event::End(TagEnd::Table) => {
if in_table {
in_table = false;
let headers: Vec<String> = take(&mut table_headers);
let rows: Vec<Vec<String>> = take(&mut table_rows);
if !headers.is_empty() || !rows.is_empty() {
blocks.push(DocBlock::Table { headers, rows });
}
}
}
Event::Start(Tag::TableHead) => {
in_table_head = true;
current_row.clear();
}
Event::End(TagEnd::TableHead) => {
in_table_head = false;
table_headers = take(&mut current_row);
}
Event::Start(Tag::TableRow) => {
current_row.clear();
}
Event::End(TagEnd::TableRow) => {
if !in_table_head {
table_rows.push(take(&mut current_row));
}
}
Event::Start(Tag::TableCell) => {
in_table_cell = true;
buffer.clear();
}
Event::End(TagEnd::TableCell) => {
if in_table_cell {
in_table_cell = false;
current_row.push(buffer.trim().to_string());
}
buffer.clear();
}
Event::Start(Tag::Paragraph) => {
if list_stack.is_empty() {
in_paragraph = true;
buffer.clear();
}
}
Event::End(TagEnd::Paragraph) => {
if in_paragraph {
in_paragraph = false;
let text: String = buffer.trim().to_string();
if !text.is_empty() {
if blockquote_depth > 0 {
blocks.push(DocBlock::BlockQuote(text));
} else {
blocks.push(DocBlock::Paragraph(text));
}
}
buffer.clear();
} else if let Some(top) = list_stack.last_mut()
&& !top.is_empty()
{
top.push(' ');
}
}
Event::Start(Tag::Item) => {
if let Some(parent_text) = list_stack.last_mut() {
let trimmed: String = parent_text.trim().to_string();
if !trimmed.is_empty() {
blocks.push(DocBlock::ListItem(trimmed));
parent_text.clear();
}
}
list_stack.push(String::new());
}
Event::End(TagEnd::Item) => {
if let Some(item_buf) = list_stack.pop() {
let text: String = item_buf.trim().to_string();
if !text.is_empty() {
blocks.push(DocBlock::ListItem(text));
}
}
}
Event::Start(Tag::CodeBlock(kind)) => {
let lang: String = match kind {
CodeBlockKind::Fenced(tag) => {
let trimmed: &str = tag.split_whitespace().next().unwrap_or("");
if trimmed.is_empty() {
DEFAULT_CODE_LANG.to_string()
} else {
trimmed.to_string()
}
}
CodeBlockKind::Indented => DEFAULT_CODE_LANG.to_string(),
};
active_code_lang = Some(lang);
buffer.clear();
}
Event::End(TagEnd::CodeBlock) => {
if let Some(language) = active_code_lang.take() {
let code: String = buffer.trim_end().to_string();
blocks.push(DocBlock::Code { language, code });
}
buffer.clear();
}
Event::Start(Tag::Image { dest_url, .. }) => {
active_image_src = Some(dest_url.into_string());
image_alt_buf.clear();
}
Event::End(TagEnd::Image) => {
if let Some(src) = active_image_src.take() {
let alt: String = image_alt_buf.trim().to_string();
image_alt_buf.clear();
if !src.is_empty() {
blocks.push(DocBlock::Image { alt, src });
}
}
}
Event::TaskListMarker(checked) => {
let marker: &str = if checked { "[x] " } else { "[ ] " };
if let Some(top) = list_stack.last_mut() {
top.push_str(marker);
} else {
buffer.push_str(marker);
}
}
Event::Rule => {
blocks.push(DocBlock::HorizontalRule);
}
Event::Text(text) | Event::Code(text) => {
if active_image_src.is_some() {
image_alt_buf.push_str(&text);
} else if !list_stack.is_empty()
&& active_code_lang.is_none()
&& active_heading.is_none()
&& !in_table_cell
{
if let Some(top) = list_stack.last_mut() {
top.push_str(&text);
}
} else {
buffer.push_str(&text);
}
}
Event::SoftBreak | Event::HardBreak => {
let sep: char = if active_code_lang.is_some() {
'\n'
} else {
' '
};
if active_image_src.is_some() {
image_alt_buf.push(sep);
} else if !list_stack.is_empty()
&& active_code_lang.is_none()
&& active_heading.is_none()
&& !in_table_cell
{
if let Some(top) = list_stack.last_mut() {
top.push(sep);
}
} else {
buffer.push(sep);
}
}
_ => {}
}
}
let DocFrontmatter {
title: fm_title,
order: fm_order,
tagline: fm_tagline,
section: fm_section,
..
} = frontmatter;
let fallback_title: String = first_h1_title
.or_else(|| {
toc.first()
.map(|item: &DocTocItem| item.get_title().clone())
})
.unwrap_or_else(|| title_from_route(route));
let title: String = fm_title.unwrap_or(fallback_title);
if toc
.first()
.is_some_and(|item: &DocTocItem| item.get_title().trim().eq_ignore_ascii_case(title.trim()))
{
let _: DocTocItem = toc.remove(0);
}
let default_order: i32 = if route == ROUTE_ROOT { -1000 } else { 100 };
let order: i32 = fm_order.unwrap_or(default_order);
let section: String = fm_section.unwrap_or_else(|| default_section.to_string());
let summary: String = fm_tagline.unwrap_or_else(|| {
blocks
.iter()
.find_map(|b: &DocBlock| match b {
DocBlock::Paragraph(p) | DocBlock::BlockQuote(p) => Some(p.clone()),
_ => None,
})
.unwrap_or_default()
});
let mut page: DocPage = DocPage::new(
route.to_string(),
title,
order,
section,
summary,
blocks,
toc,
);
page.set_markdown_body(body.to_string());
page
}
pub fn load_doc_site(root_dir: &Path) -> io::Result<DocSite> {
load_doc_site_with_options(root_dir, None)
}
pub fn load_doc_site_with_options(
root_dir: &Path,
title_override: Option<&str>,
) -> io::Result<DocSite> {
load_doc_site_excluding(root_dir, title_override, None)
}
pub(crate) fn load_doc_site_excluding(
root_dir: &Path,
title_override: Option<&str>,
exclude_dir: Option<&Path>,
) -> io::Result<DocSite> {
if !root_dir.exists() {
return Err(Error::new(
ErrorKind::NotFound,
root_dir.display().to_string(),
));
}
let canonical_exclude: Option<PathBuf> =
exclude_dir.map(|p: &Path| fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf()));
let mut files: Vec<PathBuf> = Vec::new();
collect_markdown_files(root_dir, canonical_exclude.as_deref(), &mut files)?;
files.sort();
let mut pages: Vec<DocPage> = Vec::with_capacity(files.len());
let mut seen_routes: BTreeSet<String> = BTreeSet::new();
let mut detected_title: Option<String> = None;
let mut detected_tagline: Option<String> = None;
for file_path in files.into_iter() {
let raw: String = fs::read_to_string(&file_path)?;
let rel: &Path = file_path.strip_prefix(root_dir).unwrap_or(&file_path);
let (mut route, section): (String, String) = route_and_section_from_rel_path(rel);
let (mut frontmatter, body): (DocFrontmatter, &str) = split_frontmatter(&raw);
if route == ROUTE_ROOT
&& let Some(site_cfg) = frontmatter.site.take()
{
if detected_title.is_none() {
detected_title = site_cfg.try_get_title().clone();
}
if detected_tagline.is_none() {
detected_tagline = site_cfg.try_get_description().clone();
}
}
if seen_routes.contains(&route) {
let stem: String = file_path
.file_stem()
.map(|s: &OsStr| slugify(&s.to_string_lossy()))
.filter(|s: &String| !s.is_empty())
.unwrap_or_else(|| FALLBACK_SLUG_PREFIX.to_string());
let base_route: String = if route == ROUTE_ROOT {
format!("/{stem}")
} else {
format!("{route}-{stem}")
};
route = base_route.clone();
let mut suffix: usize = 2;
while seen_routes.contains(&route) {
route = format!("{base_route}-{suffix}");
suffix += 1;
}
}
let _: bool = seen_routes.insert(route.clone());
pages.push(parse_markdown_page_with_frontmatter(
&route,
§ion,
frontmatter,
body,
));
}
pages.sort_by(|a: &DocPage, b: &DocPage| {
section_sort_rank(a.get_section())
.cmp(§ion_sort_rank(b.get_section()))
.then_with(|| a.get_section().cmp(b.get_section()))
.then_with(|| a.get_order().cmp(&b.get_order()))
.then_with(|| a.get_route().cmp(b.get_route()))
});
let fallback_site_title: String = root_dir
.file_name()
.map(|s: &OsStr| s.to_string_lossy().into_owned())
.filter(|raw: &String| {
raw.len() > 2
&& !raw.eq_ignore_ascii_case(GENERIC_DOCS_DIR)
&& !raw.eq_ignore_ascii_case(GENERIC_SRC_DIR)
&& raw != "."
&& raw != ".."
})
.map(|raw: String| format_section_label(&raw))
.unwrap_or_else(|| DOCS_SITE_TITLE.to_string());
let site_title: String = title_override
.map(str::to_string)
.or(detected_title)
.unwrap_or(fallback_site_title);
let site_tagline: String = detected_tagline.unwrap_or_else(|| DOCS_SITE_TAGLINE.to_string());
Ok(DocSite::new(site_title, site_tagline, pages))
}
pub(crate) fn section_sort_rank(section: &str) -> u8 {
if section.eq_ignore_ascii_case(DEFAULT_SECTION) {
0
} else if section.eq_ignore_ascii_case(GUIDE_SECTION) {
1
} else {
2
}
}
fn split_frontmatter(raw: &str) -> (DocFrontmatter, &str) {
let trimmed: &str = raw.trim_start_matches('\u{feff}');
let Some(rest) = trimmed.strip_prefix(FRONTMATTER_DELIMITER) else {
return (DocFrontmatter::default(), trimmed);
};
let rest: &str = rest
.strip_prefix("\r\n")
.or_else(|| rest.strip_prefix('\n'))
.unwrap_or(rest);
let mut offset: usize = 0;
for line in rest.split_inclusive('\n') {
let clean: &str = line.trim_end_matches(['\r', '\n']);
if clean.trim() == FRONTMATTER_DELIMITER {
let yaml_slice: &str = &rest[..offset];
let body_slice: &str = &rest[(offset + line.len())..];
let parsed: DocFrontmatter = from_str::<DocFrontmatter>(yaml_slice).unwrap_or_default();
return (parsed, body_slice);
}
offset += line.len();
}
(DocFrontmatter::default(), trimmed)
}
pub(crate) fn heading_level_num(level: HeadingLevel) -> u8 {
match level {
HeadingLevel::H1 => 1,
HeadingLevel::H2 => 2,
HeadingLevel::H3 => 3,
HeadingLevel::H4 => 4,
HeadingLevel::H5 => 5,
HeadingLevel::H6 => 6,
}
}
fn push_slugified(out: &mut String, input: &str) {
let start_len: usize = out.len();
let mut prev_dash: bool = false;
for ch in input.chars() {
if ch.is_ascii_alphanumeric() {
out.push(ch.to_ascii_lowercase());
prev_dash = false;
} else if ch.is_alphanumeric() {
for lower in ch.to_lowercase() {
out.push(lower);
}
prev_dash = false;
} else if !prev_dash && out.len() > start_len {
out.push('-');
prev_dash = true;
}
}
while out.len() > start_len && out.ends_with('-') {
let _: Option<char> = out.pop();
}
}
pub(crate) fn slugify(input: &str) -> String {
let mut out: String = String::with_capacity(input.len());
push_slugified(&mut out, input);
out
}
pub(crate) fn deduplicate_slug(raw_slug: String, seen: &mut BTreeMap<String, usize>) -> String {
let base: String = if raw_slug.is_empty() {
FALLBACK_SLUG_PREFIX.to_string()
} else {
raw_slug
};
if let Some(count) = seen.get_mut(&base) {
*count += 1;
return format!("{base}-{}", *count);
}
let _: Option<usize> = seen.insert(base.clone(), 1);
base
}
fn collect_markdown_files(
dir: &Path,
exclude_dir: Option<&Path>,
out: &mut Vec<PathBuf>,
) -> io::Result<()> {
for entry in fs::read_dir(dir)? {
let entry_val: DirEntry = entry?;
let path: PathBuf = entry_val.path();
let is_dir: bool = match entry_val.file_type() {
Ok(ft) if ft.is_dir() => true,
Ok(ft) if ft.is_file() => false,
_ => path.is_dir(),
};
if is_dir {
let file_name: OsString = entry_val.file_name();
let name: Cow<'_, str> = file_name.to_string_lossy();
if should_skip_dir(&name) {
continue;
}
if let Some(excluded) = exclude_dir {
let candidate: PathBuf = fs::canonicalize(&path).unwrap_or_else(|_| path.clone());
if candidate == excluded || candidate.starts_with(excluded) {
continue;
}
}
collect_markdown_files(&path, exclude_dir, out)?;
} else if path
.extension()
.and_then(|s: &OsStr| s.to_str())
.is_some_and(|ext: &str| ext.eq_ignore_ascii_case(MARKDOWN_EXT))
{
out.push(path);
}
}
Ok(())
}
pub(crate) fn should_skip_dir(name: &str) -> bool {
name.starts_with('.')
|| IGNORED_SCAN_DIRS
.iter()
.any(|ignored: &&str| name.eq_ignore_ascii_case(ignored))
}
fn push_route_segment(out: &mut String, seg: &str) {
out.push('/');
let start_len: usize = out.len();
push_slugified(out, seg);
if out.len() == start_len {
for ch in seg.chars() {
out.push(ch.to_ascii_lowercase());
}
}
}
fn route_and_section_from_rel_path(rel: &Path) -> (String, String) {
let mut comps: Peekable<Components<'_>> = rel.components().peekable();
let Some(first_comp) = comps.next() else {
return (ROUTE_ROOT.to_string(), DEFAULT_SECTION.to_string());
};
let mut route: String = String::with_capacity(rel.as_os_str().len() + 1);
let section: String = if comps.peek().is_some() {
let first_cow: Cow<'_, str> = first_comp.as_os_str().to_string_lossy();
let sec: String = format_section_label(&first_cow);
push_route_segment(&mut route, &first_cow);
while let Some(comp) = comps.next() {
if comps.peek().is_some() {
let seg_cow: Cow<'_, str> = comp.as_os_str().to_string_lossy();
push_route_segment(&mut route, &seg_cow);
}
}
sec
} else {
DEFAULT_SECTION.to_string()
};
let stem_cow: Cow<'_, str> = rel
.file_stem()
.map(OsStr::to_string_lossy)
.unwrap_or(Cow::Borrowed(""));
if !stem_cow.eq_ignore_ascii_case(README_STEM) && !stem_cow.eq_ignore_ascii_case(INDEX_STEM) {
push_route_segment(&mut route, &stem_cow);
}
if route.is_empty() {
route.push_str(ROUTE_ROOT);
}
(route, section)
}
fn format_section_label(raw: &str) -> String {
let mut out: String = String::with_capacity(raw.len());
for part in raw.split(['-', '_']) {
let trimmed: &str = part.trim();
if trimmed.is_empty() {
continue;
}
let mut chars: Chars<'_> = trimmed.chars();
if let Some(first) = chars.next() {
if !out.is_empty() {
out.push(' ');
}
for up in first.to_uppercase() {
out.push(up);
}
out.push_str(chars.as_str());
}
}
if out.is_empty() {
out.push_str(DEFAULT_SECTION);
}
out
}
fn title_from_route(route: &str) -> String {
let trimmed: &str = route.trim_matches('/');
if trimmed.is_empty() {
return DEFAULT_DOC_TITLE.to_string();
}
trimmed
.rsplit('/')
.next()
.map(format_section_label)
.unwrap_or_else(|| DEFAULT_DOC_TITLE.to_string())
}