use std::borrow::Cow;
const HTML_BLOCK_RAW_TEXT_TAGS: [&str; 4] = ["pre", "script", "style", "textarea"];
const HTML_BLOCK_TAGS: [&str; 62] = [
"address",
"article",
"aside",
"base",
"basefont",
"blockquote",
"body",
"caption",
"center",
"col",
"colgroup",
"dd",
"details",
"dialog",
"dir",
"div",
"dl",
"dt",
"fieldset",
"figcaption",
"figure",
"footer",
"form",
"frame",
"frameset",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"head",
"header",
"hr",
"html",
"iframe",
"legend",
"li",
"link",
"main",
"menu",
"menuitem",
"nav",
"noframes",
"ol",
"optgroup",
"option",
"p",
"param",
"search",
"section",
"summary",
"table",
"tbody",
"td",
"tfoot",
"th",
"thead",
"title",
"tr",
"track",
"ul",
];
pub(super) fn escape_block_openers_on_continuation_lines(text: &str) -> Cow<'_, str> {
if !text.contains('\n') {
return Cow::Borrowed(text);
}
let mut escape_at: Vec<usize> = Vec::new();
let mut line_start = 0usize;
for (index, line) in text.split('\n').enumerate() {
if index > 0 {
if let Some(offset) = block_opener_escape_offset(line) {
escape_at.push(line_start + offset);
}
}
line_start += line.len() + 1;
}
if escape_at.is_empty() {
return Cow::Borrowed(text);
}
let mut result = String::with_capacity(text.len() + escape_at.len());
let mut copied = 0usize;
for position in escape_at {
result.push_str(&text[copied..position]);
result.push('\\');
copied = position;
}
result.push_str(&text[copied..]);
Cow::Owned(result)
}
fn block_opener_escape_offset(line: &str) -> Option<usize> {
let (indent, column) = leading_indent(line);
if column >= 4 {
return None;
}
let rest = line.get(indent..)?;
block_opener_offset(rest).map(|offset| indent + offset)
}
pub fn escape_paragraph_start(rest: &str, underline: u8) -> Cow<'_, str> {
fresh_block_opener_offset(rest)
.or_else(|| starts_link_reference_definition(rest, underline).then_some(0))
.map_or(Cow::Borrowed(rest), |at| {
Cow::Owned(format!("{}\\{}", &rest[..at], &rest[at..]))
})
}
fn fresh_block_opener_escape_offset(line: &str) -> Option<usize> {
let (indent, column) = leading_indent(line);
if column >= 4 {
return None;
}
fresh_block_opener_offset(line.get(indent..)?).map(|offset| indent + offset)
}
fn fresh_block_opener_offset(rest: &str) -> Option<usize> {
let structural_opener = match rest.as_bytes().first() {
Some(b'<' | b'=') | None => None,
Some(_) => block_opener_offset(rest),
};
structural_opener.or_else(|| list_marker_offset(rest))
}
fn starts_link_reference_definition(rest: &str, underline: u8) -> bool {
let Some(after_label) = link_label_len(rest).and_then(|len| rest[len..].strip_prefix(':')) else {
return false;
};
let destination = after_label.trim_matches([' ', '\t']);
if destination.is_empty() {
return underline == b'=';
}
let Some(len) = link_destination_len(destination) else {
return false;
};
let after_destination = &destination[len..];
let title = after_destination.trim_start_matches([' ', '\t']);
title.is_empty() || (title.len() < after_destination.len() && is_link_title(title))
}
fn link_label_len(text: &str) -> Option<usize> {
let bytes = text.as_bytes();
if bytes.first() != Some(&b'[') {
return None;
}
let mut at = 1;
while at < bytes.len() {
match bytes[at] {
b'\\' => at += 2,
b'[' => return None,
b']' => {
let label = &text[1..at];
let valid = !label.trim_matches([' ', '\t']).is_empty() && label.chars().count() <= 999;
return valid.then_some(at + 1);
}
_ => at += 1,
}
}
None
}
fn link_destination_len(text: &str) -> Option<usize> {
let bytes = text.as_bytes();
if bytes.first() == Some(&b'<') {
let mut at = 1;
while at < bytes.len() {
match bytes[at] {
b'\\' => at += 2,
b'>' => return Some(at + 1),
b'<' => return None,
_ => at += 1,
}
}
return None;
}
let mut depth = 0usize;
let mut at = 0;
while at < bytes.len() {
match bytes[at] {
b'\\' if bytes.get(at + 1).is_some_and(u8::is_ascii_punctuation) => at += 1,
b'(' => depth += 1,
b')' if depth == 0 => break,
b')' => depth -= 1,
byte if byte <= b' ' || byte == 0x7f => break,
_ => {}
}
at += 1;
}
(at > 0 && depth == 0).then_some(at)
}
fn is_link_title(text: &str) -> bool {
let bytes = text.as_bytes();
let close = match bytes.first() {
Some(b'"') => b'"',
Some(b'\'') => b'\'',
Some(b'(') => b')',
_ => return false,
};
let mut at = 1;
while at < bytes.len() {
match bytes[at] {
b'\\' => at += 2,
byte if byte == close => return at + 1 == bytes.len(),
b'(' if close == b')' => return false,
_ => at += 1,
}
}
false
}
pub fn atx_closing_sequence_offset(text: &str) -> Option<usize> {
let run_start = text.trim_end_matches('#').len();
let closes = run_start < text.len() && (run_start == 0 || matches!(text.as_bytes()[run_start - 1], b' ' | b'\t'));
closes.then_some(run_start)
}
fn list_marker_offset(rest: &str) -> Option<usize> {
let bytes = rest.as_bytes();
let delimiter = match bytes.first()? {
b'-' | b'*' | b'+' => 0,
b'0'..=b'9' => {
let digits = bytes.iter().take_while(|byte| byte.is_ascii_digit()).count();
if digits > 9 || !matches!(bytes.get(digits), Some(b'.' | b')')) {
return None;
}
digits
}
_ => return None,
};
matches!(bytes.get(delimiter + 1), None | Some(b' ' | b'\t')).then_some(delimiter)
}
pub fn line_opens_block(line: &str) -> bool {
block_opener_escape_offset(line).is_some()
}
pub fn escape_continuation_line_start(buffer: &mut String, from: usize, after_external_hard_break: bool) {
let before = &buffer[..from];
let continues_paragraph = if let Some(line_end) = before.trim_end_matches([' ', '\t']).strip_suffix('\n') {
let line_above = &line_end[line_end.rfind('\n').map_or(0, |pos| pos + 1)..];
!line_above.trim().is_empty()
} else {
after_external_hard_break && before.trim_matches([' ', '\t']).is_empty()
};
if !continues_paragraph {
return;
}
let text = &buffer[from..];
let line = &text[..text.find('\n').unwrap_or(text.len())];
if let Some(offset) = block_opener_escape_offset(line) {
buffer.insert(from + offset, '\\');
}
}
pub fn ends_with_hard_break(buffer: &str) -> bool {
let Some(before_newline) = buffer.strip_suffix('\n') else {
return false;
};
let line = &before_newline[before_newline.rfind('\n').map_or(0, |position| position + 1)..];
let without_spaces = line.trim_end_matches(' ');
let spaces = line.len() - without_spaces.len();
if without_spaces.trim_matches([' ', '\t']).is_empty() {
return false;
}
if spaces >= 2 {
return true;
}
let backslashes = without_spaces.len() - without_spaces.trim_end_matches('\\').len();
spaces == 0 && !backslashes.is_multiple_of(2)
}
pub fn escape_block_start(buffer: &mut String, from: usize, in_list_item: bool, followed_by_inline: bool) {
let before = &buffer[..from];
if before
.trim_end_matches([' ', '\t'])
.as_bytes()
.last()
.is_some_and(|byte| byte.is_ascii_alphabetic() || *byte == 0x0c)
{
return;
}
let line_start = before.rfind('\n').map_or(0, |position| position + 1);
let prefix = &before[line_start..];
let content_start = if in_list_item {
list_item_content_start(prefix).map_or(from, |offset| line_start + offset)
} else if prefix.trim().is_empty() || is_unfinished_ordered_marker(prefix) {
line_start
} else {
return;
};
let text = &buffer[content_start..];
let first_line = &text[..text.find('\n').unwrap_or(text.len())];
let continues_paragraph = before
.trim_end_matches([' ', '\t'])
.strip_suffix('\n')
.is_some_and(|preceding| {
let preceding_line = &preceding[preceding.rfind('\n').map_or(0, |position| position + 1)..];
!preceding_line.trim().is_empty()
});
if followed_by_inline {
let mut continued = String::with_capacity(first_line.len() + 1);
continued.push_str(first_line);
continued.push('x');
if fresh_block_opener_escape_offset(&continued).is_none() {
return;
}
}
let escape_at = if continues_paragraph {
None
} else {
fresh_block_opener_escape_offset(first_line).map(|offset| content_start + offset)
};
if let Some(position) = escape_at {
buffer.insert(position, '\\');
}
}
fn is_unfinished_ordered_marker(prefix: &str) -> bool {
let (indent_len, column) = leading_indent(prefix);
if column >= 4 {
return false;
}
let rest = &prefix[indent_len..];
let digits = rest.bytes().take_while(u8::is_ascii_digit).count();
(1..=9).contains(&digits) && matches!(rest.as_bytes().get(digits), Some(b'.' | b')')) && rest.len() == digits + 1
}
fn list_item_content_start(line: &str) -> Option<usize> {
let (indent_len, _) = leading_indent(line);
let mut position = indent_len;
let mut found = false;
loop {
let rest = &line[position..];
let marker_len = match rest.as_bytes().first() {
Some(b'-' | b'*' | b'+') => 1,
Some(b'0'..=b'9') => {
let digits = rest.bytes().take_while(u8::is_ascii_digit).count();
let Some(delimiter) = rest.as_bytes().get(digits) else {
break;
};
if !matches!(delimiter, b'.' | b')') {
break;
}
digits + 1
}
_ => break,
};
let spacing = rest[marker_len..]
.bytes()
.take_while(|byte| matches!(byte, b' ' | b'\t'))
.count();
if spacing == 0 {
break;
}
found = true;
position += marker_len + spacing;
}
found.then_some(position)
}
pub fn escape_djot_list_item_start(buffer: &mut String, from: usize, in_list_item: bool) {
if !in_list_item {
return;
}
let before = &buffer[..from];
let line_start = before.rfind('\n').map_or(0, |position| position + 1);
let Some(content_offset) = list_item_content_start(&before[line_start..]) else {
return;
};
let content_start = line_start + content_offset;
let text = &buffer[content_start..];
let first_line = &text[..text.find('\n').unwrap_or(text.len())];
let digits = first_line.bytes().take_while(u8::is_ascii_digit).count();
if digits == 0
|| !matches!(first_line.as_bytes().get(digits), Some(b'.' | b')'))
|| !matches!(first_line.as_bytes().get(digits + 1), Some(b' ' | b'\t'))
{
return;
}
buffer.insert(content_start + digits, '\\');
}
pub fn escape_djot_continuation_line_start(buffer: &mut String, from: usize, after_external_hard_break: bool) {
let line_start = if let Some(position) = buffer.rfind('\n') {
position + 1
} else if after_external_hard_break && buffer[..from].trim_matches([' ', '\t']).is_empty() {
from
} else {
return;
};
if line_start > 0 {
let previous_line = &buffer[..line_start - 1];
let previous_line_start = previous_line.rfind('\n').map_or(0, |position| position + 1);
let previous_line = &previous_line[previous_line_start..];
let trailing_backslashes = previous_line.len() - previous_line.trim_end_matches('\\').len();
if trailing_backslashes.is_multiple_of(2) {
return;
}
}
let indent = buffer[line_start..]
.bytes()
.take_while(|byte| matches!(byte, b' ' | b'\t'))
.count();
let run_start = line_start + indent;
let Some(marker) = buffer.as_bytes().get(run_start).and_then(|byte| match byte {
b'-' | b'`' => Some(*byte),
b'\\' => buffer
.as_bytes()
.get(run_start + 1)
.filter(|next| matches!(next, b'-' | b'`'))
.copied(),
_ => None,
}) else {
return;
};
let mut position = run_start;
let mut count = 0usize;
while position < buffer.len() {
if buffer.as_bytes()[position] == marker {
position += 1;
count += 1;
} else if buffer.as_bytes()[position] == b'\\' && buffer.as_bytes().get(position + 1) == Some(&marker) {
position += 2;
count += 1;
} else {
break;
}
}
if count == 0 || (marker == b'-' && count < 2) {
return;
}
let mut escaped = String::with_capacity(count * 2);
for _ in 0..count {
escaped.push('\\');
escaped.push(char::from(marker));
}
buffer.replace_range(run_start..position, &escaped);
}
pub fn opens_block(rest: &str) -> bool {
block_opener_offset(rest).is_some()
}
pub fn is_heading_underline(rest: &str) -> bool {
is_setext_underline(rest, b'=') || is_setext_underline(rest, b'-')
}
pub fn is_rule(rest: &str) -> bool {
rest.bytes()
.next()
.is_some_and(|marker| matches!(marker, b'-' | b'*' | b'_') && is_thematic_break(rest, marker))
}
pub fn code_fence(rest: &str) -> Option<(u8, usize)> {
let marker = *rest.as_bytes().first()?;
let run = rest.bytes().take_while(|&byte| byte == marker).count();
(matches!(marker, b'`' | b'~') && is_code_fence(rest, marker)).then_some((marker, run))
}
pub(super) fn block_opener_offset(rest: &str) -> Option<usize> {
let marker = *rest.as_bytes().first()?;
let opens = match marker {
b'>' => true,
b'#' => is_atx_heading(rest),
b'`' | b'~' => is_code_fence(rest, marker),
b'=' => is_setext_underline(rest, b'='),
b'-' => is_setext_underline(rest, b'-') || is_thematic_break(rest, b'-') || is_bullet_list_item(rest),
b'*' => is_thematic_break(rest, b'*') || is_bullet_list_item(rest),
b'+' => is_bullet_list_item(rest),
b'_' => is_thematic_break(rest, b'_'),
b'<' => is_html_block_opener(rest),
b'0'..=b'9' => return ordered_list_delimiter_offset(rest),
_ => false,
};
opens.then_some(0)
}
pub fn leading_indent(line: &str) -> (usize, usize) {
let mut length = 0usize;
let mut column = 0usize;
for &byte in line.as_bytes() {
match byte {
b' ' => column += 1,
b'\t' => column += 4 - column % 4,
_ => break,
}
length += 1;
}
(length, column)
}
fn is_atx_heading(rest: &str) -> bool {
let hashes = rest.bytes().take_while(|&byte| byte == b'#').count();
(1..=6).contains(&hashes) && matches!(rest.as_bytes().get(hashes), None | Some(b' ' | b'\t' | b'\r'))
}
fn is_code_fence(rest: &str, fence: u8) -> bool {
let run = rest.bytes().take_while(|&byte| byte == fence).count();
run >= 3 && (fence != b'`' || !rest[run..].contains('`'))
}
fn is_setext_underline(rest: &str, marker: u8) -> bool {
let trimmed = rest.trim_end();
!trimmed.is_empty() && trimmed.bytes().all(|byte| byte == marker)
}
fn is_thematic_break(rest: &str, marker: u8) -> bool {
let mut count = 0usize;
for byte in rest.bytes() {
if byte == marker {
count += 1;
} else if !matches!(byte, b' ' | b'\t' | b'\r') {
return false;
}
}
count >= 3
}
fn is_bullet_list_item(rest: &str) -> bool {
matches!(rest.as_bytes().get(1), Some(b' ' | b'\t')) && rest.get(2..).is_some_and(|tail| !tail.trim().is_empty())
}
fn ordered_list_delimiter_offset(rest: &str) -> Option<usize> {
let digits = first_ordered_marker_len(rest)? - 1;
let bytes = rest.as_bytes();
let starts_a_list = matches!(bytes.get(digits + 1), Some(b' ' | b'\t'))
&& rest.get(digits + 2..).is_some_and(|tail| !tail.trim().is_empty());
starts_a_list.then_some(digits)
}
pub fn first_ordered_marker_len(rest: &str) -> Option<usize> {
let bytes = rest.as_bytes();
let digits = bytes.iter().take_while(|byte| byte.is_ascii_digit()).count();
let value_is_one =
(1..=9).contains(&digits) && bytes[digits - 1] == b'1' && bytes[..digits - 1].iter().all(|&byte| byte == b'0');
(value_is_one && matches!(bytes.get(digits), Some(b'.' | b')'))).then_some(digits + 1)
}
fn is_html_block_opener(rest: &str) -> bool {
let after_bracket = &rest[1..];
if after_bracket.starts_with("!--")
|| after_bracket.starts_with('?')
|| after_bracket.starts_with("![CDATA[")
|| (after_bracket.starts_with('!') && after_bracket.as_bytes().get(1).is_some_and(u8::is_ascii_alphabetic))
{
return true;
}
let is_closing = after_bracket.starts_with('/');
let name_start = usize::from(is_closing);
let name: String = after_bracket[name_start..]
.bytes()
.take_while(u8::is_ascii_alphanumeric)
.map(|byte| byte.to_ascii_lowercase() as char)
.collect();
if name.is_empty() {
return false;
}
let tail = &after_bracket[name_start + name.len()..];
let is_terminated =
tail.is_empty() || tail.starts_with('>') || tail.starts_with("/>") || tail.starts_with(char::is_whitespace);
is_terminated
&& (HTML_BLOCK_TAGS.contains(&name.as_str())
|| (!is_closing && HTML_BLOCK_RAW_TEXT_TAGS.contains(&name.as_str())))
}
pub fn is_block_level_name(tag_name: &str, is_inline: bool) -> bool {
!is_inline
&& matches!(
tag_name,
"address"
| "article"
| "aside"
| "blockquote"
| "canvas"
| "center"
| "dd"
| "details"
| "dialog"
| "div"
| "dl"
| "dt"
| "fieldset"
| "figcaption"
| "figure"
| "footer"
| "form"
| "h1"
| "h2"
| "h3"
| "h4"
| "h5"
| "h6"
| "header"
| "hgroup"
| "hr"
| "legend"
| "li"
| "main"
| "menu"
| "nav"
| "ol"
| "p"
| "pre"
| "search"
| "section"
| "summary"
| "table"
| "tfoot"
| "ul"
)
}