use super::{Block, Collector, FootnoteBlock, Indent, Interrupt, Line, Position, inline, parse_blocks};
pub(super) const FOOTNOTE_INDENT: usize = 4;
pub(super) struct FootnoteMarker<'a> {
label: &'a str,
pub(super) content: usize,
}
pub(super) fn footnote_marker<'a>(line: &Line<'a>) -> Option<FootnoteMarker<'a>> {
if !line.flavor.has_gfm() {
return None;
}
let Indent { columns, bytes: indent } = line.indent();
let rest = line.text[indent..].strip_prefix("[^")?;
if columns >= line.code_indent() {
return None;
}
let bytes = rest.as_bytes();
let mut close = 0;
while close < bytes.len() && bytes[close] != b']' {
if bytes[close] == b'[' {
return None;
}
close += if bytes[close] == b'\\' && matches!(bytes.get(close + 1), Some(b'[' | b'\\' | b']')) {
2
} else {
1
};
}
if close >= bytes.len() {
return None;
}
let label = &rest[..close];
if label.is_empty() || label.bytes().any(|b| matches!(b, b' ' | b'\t' | b'\r' | b'\n')) {
return None;
}
let after = rest[close + 1..].strip_prefix(':')?;
let blanks = after.len() - after.trim_start_matches([' ', '\t']).len();
Some(FootnoteMarker {
label,
content: indent + 2 + close + 2 + blanks,
})
}
pub(super) fn footnote(
lines: &[Line<'_>],
start: usize,
marker: &FootnoteMarker<'_>,
depth: usize,
interrupting: Interrupt,
blocks: &mut Vec<Block>,
) -> usize {
let mut collector = Collector::new(depth, interrupting);
collector.push(lines[start].skip(marker.content));
let mut index = start + 1;
while let Some(line) = lines.get(index) {
if line.is_blank() {
collector.blank(line.skip(line.text.len()));
} else if line.indent().columns >= FOOTNOTE_INDENT {
collector.push(line.skip_columns(FOOTNOTE_INDENT));
} else if !collector.lazy(lines, index) {
break;
}
index += 1;
}
let Collector {
lines: inner, blanks, ..
} = collector;
index -= blanks.len();
let end = blanks
.last()
.or(inner.last())
.map_or_else(|| lines[start].end(), Line::end);
let position = Position {
start: lines[start].point(0),
end,
};
blocks.push(Block::Footnote(FootnoteBlock {
ident: inline::normalize(marker.label),
children: parse_blocks(&inner, depth + 1, interrupting),
position,
}));
index
}