use comrak::nodes::Sourcepos;
use super::Parser;
use crate::markdown::Piece;
impl Parser<'_> {
pub(super) fn source_text(&self, sp: Sourcepos) -> String {
self.mapped_text(sp).0
}
pub(super) fn mapped_text(&self, sp: Sourcepos) -> (String, Vec<Piece>) {
let start = self.line(sp);
let end = self.end_line(sp);
let mut text = String::new();
let mut pieces = Vec::new();
for n in start..=end {
let line = self.src(n);
let to = if n == end {
line.ceil_char_boundary(sp.end.column.min(line.len()))
} else {
line.len()
};
let from = if n == start {
line.floor_char_boundary(sp.start.column.saturating_sub(1).min(line.len()))
} else {
let head = &line[..to];
head.len() - strip_prefix(head).len()
};
let slice = &line[from..to.max(from)];
let piece = slice.trim();
if piece.is_empty() {
continue;
}
if !text.is_empty() {
text.push(' ');
}
let col = from + (slice.len() - slice.trim_start().len());
pieces.push(Piece {
offset: text.len(),
line: n,
col,
});
text.push_str(piece);
}
(text, pieces)
}
}
pub(super) fn strip_prefix(line: &str) -> &str {
let mut t = line.trim_start();
while let Some(rest) = t.strip_prefix('>') {
t = rest.trim_start();
}
t
}