use crate::markdown_it::{MarkdownIt, RulerError, StateBlock, StateInline, js};
use super::super::{TokenizeError, throw};
const OPEN: &[u16] = &[b'<' as u16, b'!' as u16, b'-' as u16, b'-' as u16];
const CLOSE: &[u16] = &[b'-' as u16, b'-' as u16, b'>' as u16];
fn block(state: &mut StateBlock, start_line: usize, _end_line: usize, silent: bool) -> bool {
if start_line >= state.line_max {
return false;
}
let start = state.b_marks[start_line] + state.t_shift[start_line];
if !js::starts_with(&state.src, OPEN, start) {
return false;
}
let Some(close) = js::index_of(&state.src, CLOSE, start) else {
return false;
};
if silent {
return true;
}
let content = js::slice_to(&state.src, start + OPEN.len(), close);
let lines = 1 + content.iter().filter(|&&c| c == b'\n' as u16).count();
let content = js::to_string(js::trim(content));
let token = state.push("comment", "", 0);
token.content = content;
token.map = Some([start_line, start_line + lines]);
state.line += lines;
true
}
fn inline(state: &mut StateInline, silent: bool) -> bool {
if !js::starts_with(&state.src, OPEN, state.pos) {
return false;
}
let Some(close) = js::index_of(&state.src, CLOSE, state.pos) else {
return false;
};
if silent {
throw(
state.env,
TokenizeError {
name: "Error".to_owned(),
message: "inline rule didn't increment state.pos".to_owned(),
},
);
return false;
}
let content = js::to_string(js::trim(js::slice_to(
&state.src,
state.pos + OPEN.len(),
close,
)));
let token = state.push("comment", "", 0);
token.content = content;
state.pos = close + CLOSE.len();
true
}
pub fn plugin(md: &mut MarkdownIt) -> Result<(), RulerError> {
md.block
.ruler
.before("table", "comment", block, &["paragraph"])?;
md.inline.ruler.push("comment", inline, &[]);
Ok(())
}