use crate::markdown_it::{MarkdownIt, RulerError, StateBlock, js};
const FENCE: &[u16] = &[b'-' as u16, b'-' as u16, b'-' as u16];
fn get_line<'s>(state: &'s StateBlock, n: usize) -> &'s [u16] {
js::trim(js::slice_to(&state.src, state.b_marks[n], state.e_marks[n]))
}
fn find_close(state: &StateBlock, end_line: usize) -> Option<usize> {
(1..end_line).find(|&line| get_line(state, line) == FENCE)
}
fn block(state: &mut StateBlock, start_line: usize, end_line: usize, silent: bool) -> bool {
if start_line >= state.line_max {
return false;
}
let end_line = end_line.min(state.line_max);
if start_line != 0 || get_line(state, 0) != FENCE {
return false;
}
let Some(close) = find_close(state, end_line) else {
return false;
};
if silent {
return true;
}
let content = js::to_string(js::trim(js::slice_to(
&state.src,
state.e_marks[0],
state.b_marks[close],
)));
let token = state.push("frontmatter", "", 0);
token.content = content;
token.map = Some([0, close]);
token.hidden = true;
state.line = close + 1;
true
}
pub fn plugin(md: &mut MarkdownIt) -> Result<(), RulerError> {
md.block.ruler.before("hr", "frontmatter", block, &[])
}