use crate::nodes::CalloutFold;
pub(super) struct CalloutHeader<'a> {
pub(super) raw_type: &'a str,
pub(super) metadata: Option<&'a str>,
pub(super) fold: CalloutFold,
pub(super) title_start: usize,
}
pub(super) fn parse_callout_header<'a>(input: &'a str) -> Option<CalloutHeader<'a>> {
let rest = input.strip_prefix("[!")?;
let close = rest.find(']')?;
let payload = &rest[..close];
if payload.is_empty() {
return None;
}
let mut after_close = &rest[close + 1..];
let fold = match after_close.chars().next() {
Some('+') => {
after_close = &after_close[1..];
CalloutFold::Expanded
}
Some('-') => {
after_close = &after_close[1..];
CalloutFold::Collapsed
}
_ => CalloutFold::None,
};
let title_start = match after_close.chars().next() {
None => input.len(),
Some(c) if c.is_whitespace() => input.len() - after_close[c.len_utf8()..].len(),
Some(_) => return None,
};
let (raw_type, metadata) = match payload.split_once('|') {
Some((t, m)) => (t, Some(m)),
None => (payload, None),
};
Some(CalloutHeader {
raw_type,
metadata,
fold,
title_start,
})
}
pub(super) fn normalize_callout_type(raw_type: &str) -> String {
raw_type
.trim()
.to_lowercase()
.split_whitespace()
.collect::<Vec<_>>()
.join("-")
}