use crate::markdown_it::common::utils::unescape_all;
use crate::markdown_it::js;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ParseLinkTitleResult {
pub ok: bool,
pub can_continue: bool,
pub pos: usize,
pub str: Vec<u16>,
pub marker: u16,
}
pub fn parse_link_title(
str: &[u16],
start: usize,
max: usize,
prev_state: Option<&ParseLinkTitleResult>,
) -> ParseLinkTitleResult {
let mut pos = start;
let mut start = start;
let mut state = ParseLinkTitleResult {
ok: false,
can_continue: false,
pos: 0,
str: Vec::new(),
marker: 0,
};
if let Some(prev_state) = prev_state {
state.str = prev_state.str.clone();
state.marker = prev_state.marker;
} else {
if pos >= max {
return state;
}
let Some(mut marker) = js::char_code_at(str, pos) else {
return state; };
if marker != 0x22 && marker != 0x27 && marker != 0x28 {
return state;
}
start += 1;
pos += 1;
if marker == 0x28 {
marker = 0x29;
}
state.marker = marker;
}
while pos < max {
let code = js::char_code_at(str, pos);
if code.is_none() {
pos = max;
break;
}
if code == Some(state.marker) {
state.pos = pos + 1;
state
.str
.extend(unescape_all(js::slice_to(str, start, pos)));
state.ok = true;
return state;
} else if code == Some(0x28 ) && state.marker == 0x29
{
return state;
} else if code == Some(0x5C ) && pos + 1 < max {
pos += 1;
}
pos += 1;
}
state.can_continue = true;
state
.str
.extend(unescape_all(js::slice_to(str, start, pos)));
state
}