use crate::converter::inline::link::has_uri_scheme;
use crate::converter::tier1::bail::BailReason;
use crate::converter::tier1::parse;
use crate::converter::tier1::spec_rules;
use crate::converter::tier1::state::{EscapeCtx, OpenTag, Tier1State};
use crate::converter::tier1::tags::{ListKind, TagKind, TagSpec};
use crate::converter::tier1::{self};
use crate::converter::utility::attributes::NAV_KEYWORDS;
use crate::options::ConversionOptions;
use memchr::{memchr2, memchr3};
const MAX_TAG_NAME_BYTES: usize = 32;
const MAX_ENTITY_NAME_BYTES: usize = 32;
const MIN_SEPARATOR_DASHES: usize = 3;
const MIN_FENCE_LENGTH: usize = 3;
static CUSTOM_ELEMENT_INLINE_SPEC: TagSpec = TagSpec {
kind: TagKind::Inline,
is_void: false,
is_block: false,
optional_close: None,
is_rawtext: false,
};
const HEADING_PREFIXES: [&str; 6] = ["# ", "## ", "### ", "#### ", "##### ", "###### "];
const LIST_ITEM_INDENTS: [&str; 8] = [
"",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
];
#[derive(Debug, Clone, Default)]
pub struct ScanOutput {
pub body: String,
pub head_range: Option<std::ops::Range<usize>>,
}
pub fn scan(html: &str, options: &ConversionOptions) -> Result<ScanOutput, BailReason> {
let bytes = html.as_bytes();
let mut state = Tier1State::new(html.len());
let mut table_probes: Vec<TableLayoutProbe> = Vec::new();
let mut pos = 0usize;
let mut text_start = 0usize;
while pos < bytes.len() {
match bytes[pos] {
b'<' => {
if text_start < pos {
let next_tag_is_list = upcoming_tag_is_list_open(bytes, pos);
let next_tag_is_img = upcoming_tag_is_named(bytes, pos, b"img");
let next_tag_is_span = upcoming_tag_is_named(bytes, pos, b"span");
flush_text(
&mut state,
&html[text_start..pos],
text_start,
next_tag_is_list,
next_tag_is_img,
next_tag_is_span,
)?;
}
let next = bytes.get(pos + 1).copied().unwrap_or(0);
if next == b'!' {
if html[pos..].starts_with("<![CDATA[") {
return Err(BailReason::Cdata { offset: pos });
}
pos = skip_bang(bytes, pos)?;
text_start = pos;
continue;
}
if next == b'?' {
return Err(BailReason::Classifier);
}
if next == b'/' {
let name_start = pos + 2;
let name_end = parse::scan_tag_name(bytes, name_start);
if name_end == name_start {
return Err(BailReason::LiteralLt { offset: pos });
}
let close_bracket =
parse::find_tag_close(bytes, name_end).ok_or(BailReason::LiteralLt { offset: pos })?;
let tag_name_bytes = &bytes[name_start..name_end];
emit_close(&mut state, tag_name_bytes, options, &mut table_probes)?;
pos = close_bracket.0 + 1;
text_start = pos;
continue;
}
if !parse::is_tag_name_start(next) {
flush_text(&mut state, "<", pos, false, false, false)?;
pos += 1;
text_start = pos;
continue;
}
let name_start = pos + 1;
let name_end = parse::scan_tag_name(bytes, name_start);
let tag_name_bytes = &bytes[name_start..name_end];
let mut name_buf = [0u8; MAX_TAG_NAME_BYTES];
let name_lower = lowercase_into(tag_name_bytes, &mut name_buf);
let tag_open_end =
crate::converter::utility::preprocessing::find_tag_end(bytes, pos + 1).unwrap_or(bytes.len());
let tag_slice = &html[pos..tag_open_end];
if crate::converter::utility::preprocessing::tag_has_hidden_attribute(tag_slice)
|| crate::converter::utility::preprocessing::tag_has_hidden_style(tag_slice)
{
return Err(BailReason::HiddenElement { offset: pos });
}
if name_lower == b"svg" {
let tag_open_start = pos;
let Some((close_pos, is_self_closing)) = parse::find_tag_close(bytes, name_end) else {
pos = bytes.len();
text_start = pos;
continue;
};
let open_tag_end = close_pos + 1;
let svg_end = if is_self_closing {
open_tag_end
} else {
find_svg_close(bytes, open_tag_end).unwrap_or(bytes.len())
};
let svg_slice = &html[tag_open_start..svg_end];
emit_svg_from_slice(svg_slice, tag_open_start, &mut state, options)?;
pos = svg_end;
text_start = pos;
continue;
}
if name_lower == b"template" {
let Some((close_pos, is_self_closing)) = parse::find_tag_close(bytes, name_end) else {
pos = bytes.len();
text_start = pos;
continue;
};
let open_tag_end = close_pos + 1;
pos = if is_self_closing {
open_tag_end
} else {
find_balanced_close(bytes, open_tag_end, b"template").unwrap_or(bytes.len())
};
text_start = pos;
continue;
}
let spec: &'static TagSpec = if name_lower.contains(&b'-') {
&CUSTOM_ELEMENT_INLINE_SPEC
} else {
match tier1::lookup(name_lower) {
Some(s) => s,
None => {
return Err(BailReason::UnknownCustomElement {
name: bytes_to_string(tag_name_bytes).into(),
offset: pos,
});
}
}
};
if matches!(spec.kind, TagKind::Ignored) && spec.is_rawtext {
let open_end = match parse::find_tag_close(bytes, name_end) {
Some(close) => close.0 + 1,
None => bytes.len(),
};
pos = find_raw_text_close(bytes, open_end, name_lower).unwrap_or(bytes.len());
if is_adjacent_rawtext_ignored_open(bytes, pos) {
return Err(BailReason::AdjacentRawTextTags { offset: pos });
}
text_start = pos;
if name_lower == b"script" || name_lower == b"style" {
let after_is_word = pos < bytes.len() && !bytes[pos].is_ascii_whitespace();
if after_is_word {
let dest = state.cell_or_output_mut();
let ends_with_word = !dest.is_empty()
&& !dest.ends_with(' ')
&& !dest.ends_with('\t')
&& !dest.ends_with('\n')
&& !dest.ends_with('<')
&& !dest.ends_with("<br>");
if ends_with_word {
dest.push(' ');
}
}
}
continue;
}
if matches!(spec.kind, TagKind::Ignored) {
let open_end = match parse::find_tag_close(bytes, name_end) {
Some(close) => close.0 + 1,
None => bytes.len(),
};
if spec.is_void {
pos = open_end;
text_start = pos;
continue;
}
let (close_start, close_end) = match find_close_tag_range(bytes, open_end, name_lower) {
Some(pair) => pair,
None => (bytes.len(), bytes.len()),
};
if state.head_range.is_none() {
state.head_range = Some(open_end..close_start);
}
pos = close_end;
text_start = pos;
continue;
}
bail_unsupported(spec, pos)?;
if is_preprocessing_skip_candidate(name_lower) {
let close = parse::find_tag_close(bytes, name_end).ok_or(BailReason::LiteralLt { offset: pos })?;
let attrs_end = if close.1 { close.0.saturating_sub(1) } else { close.0 };
let skip_attrs = parse::collect_attrs(bytes, name_end, attrs_end);
if should_skip_preprocessing(name_lower, &skip_attrs, options) {
let open_end = close.0 + 1;
if close.1 {
pos = open_end;
} else {
pos = find_balanced_close(bytes, open_end, name_lower).unwrap_or(bytes.len());
}
text_start = pos;
continue;
}
}
if matches!(spec.kind, TagKind::Pre)
&& options.code_block_style == crate::options::CodeBlockStyle::Tildes
{
return Err(BailReason::Classifier);
}
let close = parse::find_tag_close(bytes, name_end).ok_or(BailReason::LiteralLt { offset: pos })?;
let attrs_end = if close.1 { close.0.saturating_sub(1) } else { close.0 };
let needs_attrs = matches!(
spec.kind,
TagKind::Link
| TagKind::Image
| TagKind::List(ListKind::Ordered)
| TagKind::Table
| TagKind::TableCell { .. }
| TagKind::Pre
| TagKind::Code
) || name_lower == b"abbr";
let attrs: Vec<(&[u8], Option<&[u8]>)> = if needs_attrs {
parse::collect_attrs(bytes, name_end, attrs_end)
} else {
Vec::new()
};
pos = close.0 + 1;
for entry in &mut state.link_stack {
entry.2 = true;
}
if spec.is_void || close.1 {
emit_void(&mut state, spec, &attrs, html, options)?;
text_start = pos;
continue;
}
while let Some(top) = state.stack.last() {
if !spec_rules::should_close_for_new_tag(top.spec, spec) {
break;
}
emit_close_for_implicit(&mut state, options, &mut table_probes)?;
}
if state.in_table_cell() && spec.is_block {
let inlineable = matches!(
spec.kind,
TagKind::Paragraph
| TagKind::Block
| TagKind::Summary
| TagKind::Figcaption
| TagKind::Blockquote
| TagKind::Pre
| TagKind::List(_)
| TagKind::ListItem
| TagKind::Heading(_)
| TagKind::DefinitionTerm
| TagKind::DefinitionDescription
| TagKind::Table
);
if !inlineable {
return Err(BailReason::TableBlockChildInCell);
}
}
if let TagKind::List(kind) = spec.kind {
if kind != ListKind::Definition
&& state.list_depth > 0
&& (kind == ListKind::Ordered || find_parent_list_kind(&state.stack) == Some(ListKind::Ordered))
{
return Err(BailReason::ListNestedOrdered);
}
}
if !state.in_table_cell() && state.list_continuation_indent_width() > 0 {
let bare_marker_line = line_is_bare_list_marker(&state.output);
let bails = match spec.kind {
TagKind::Blockquote | TagKind::Table => true,
TagKind::Block => name_lower == b"div",
TagKind::List(ListKind::Definition) => true,
TagKind::Paragraph => !bare_marker_line,
TagKind::Pre => bare_marker_line,
_ => false,
};
if bails {
return Err(BailReason::ListItemUnsupportedBlockChild);
}
}
let prev_ctx = state.escape_ctx;
let ol_start = if matches!(spec.kind, TagKind::List(ListKind::Ordered)) {
extract_ol_start(&attrs)
} else {
1
};
if matches!(spec.kind, TagKind::Link) {
let (href, title) = extract_link_attrs(&attrs)?;
state.link_stack.push((href, title, false));
}
if name_lower == b"abbr" {
let title = find_attr(&attrs, b"title")
.map(decode_attr)
.transpose()?
.map(|t| t.trim().to_owned())
.filter(|s| !s.is_empty());
state.abbr_titles.push(title);
}
let max_depth = crate::converter::main_helpers::effective_max_depth(options);
if state.stack.len() >= max_depth {
return Err(BailReason::DepthLimitExceeded {
depth: state.stack.len(),
max_depth,
});
}
emit_open(&mut state, spec, name_lower, &attrs, &mut table_probes, options)?;
let output_content_start = state.cell_or_output_mut().len();
let list_index = 0u16;
state.stack.push(OpenTag {
spec,
content_start: output_content_start,
prev_escape_ctx: prev_ctx,
list_index,
ol_start,
name_range: name_start..name_end,
dropped_whitespace_only_text: false,
});
apply_open_escape_ctx(&mut state, spec);
text_start = pos;
}
_ => {
match memchr2(b'<', b'&', &bytes[pos..]) {
Some(offset) if offset > 0 => pos += offset,
Some(_) => pos += 1,
None => pos = bytes.len(),
}
}
}
}
if text_start < pos {
flush_text(&mut state, &html[text_start..pos], text_start, false, false, false)?;
}
while !state.stack.is_empty() {
let buf = &mut state.output;
while matches!(buf.as_bytes().last(), Some(b' ' | b'\t' | b'\n' | b'\r')) {
buf.pop();
}
emit_close_for_implicit(&mut state, options, &mut table_probes)?;
}
crate::converter::main_helpers::trim_line_end_whitespace(&mut state.output);
if state.output.contains("\n\n\n") {
collapse_excess_blank_lines(&mut state.output);
}
if !state.output.is_empty() {
let trimmed_end = state.output.trim_end_matches('\n');
if trimmed_end.is_empty() {
state.output.clear();
} else {
let trimmed_len = trimmed_end.len();
state.output.truncate(trimmed_len);
state.output.push('\n');
}
}
Ok(ScanOutput {
body: state.output,
head_range: state.head_range,
})
}
fn find_close_tag_range(bytes: &[u8], open_end: usize, tag_name: &[u8]) -> Option<(usize, usize)> {
let len = bytes.len();
let mut idx = open_end;
while idx < len {
match memchr3(b'<', b'<', b'<', &bytes[idx..]) {
Some(off) => idx += off,
None => return None,
}
if idx + 2 < len && bytes[idx + 1] == b'/' {
let after_slash = idx + 2;
if after_slash + tag_name.len() <= len
&& bytes[after_slash..after_slash + tag_name.len()].eq_ignore_ascii_case(tag_name)
{
let post_name = after_slash + tag_name.len();
if matches!(bytes.get(post_name), Some(b'>' | b'/' | b' ' | b'\t' | b'\n' | b'\r')) {
let mut j = post_name;
while j < len && bytes[j] != b'>' {
j += 1;
}
if j < len {
return Some((idx, j + 1));
}
return None;
}
}
}
idx += 1;
}
None
}
fn find_svg_close(bytes: &[u8], open_end: usize) -> Option<usize> {
find_balanced_close(bytes, open_end, b"svg")
}
fn find_balanced_close(bytes: &[u8], open_end: usize, tag_name: &[u8]) -> Option<usize> {
let len = bytes.len();
let mut idx = open_end;
let mut depth = 1usize;
while idx < len {
match memchr::memchr(b'<', &bytes[idx..]) {
Some(off) => idx += off,
None => return None,
}
if idx + 1 < len && bytes[idx + 1] == b'/' {
let name_start = idx + 2;
if name_start + tag_name.len() <= len
&& bytes[name_start..name_start + tag_name.len()].eq_ignore_ascii_case(tag_name)
{
let after = name_start + tag_name.len();
if matches!(
bytes.get(after),
Some(b'>' | b'/' | b' ' | b'\t' | b'\n' | b'\r') | None
) {
depth -= 1;
if depth == 0 {
let mut j = after;
while j < len && bytes[j] != b'>' {
j += 1;
}
return if j < len { Some(j + 1) } else { None };
}
}
}
} else if idx + 1 < len {
let name_start = idx + 1;
if name_start + tag_name.len() <= len
&& bytes[name_start..name_start + tag_name.len()].eq_ignore_ascii_case(tag_name)
{
let after = name_start + tag_name.len();
if matches!(
bytes.get(after),
Some(b'>' | b'/' | b' ' | b'\t' | b'\n' | b'\r') | None
) {
let mut j = after;
let mut in_q: Option<u8> = None;
let tag_end = loop {
if j >= len {
break len;
}
match bytes[j] {
b'"' | b'\'' => {
if let Some(q) = in_q {
if q == bytes[j] {
in_q = None;
}
} else {
in_q = Some(bytes[j]);
}
}
b'>' if in_q.is_none() => {
break j + 1;
}
_ => {}
}
j += 1;
};
let is_self_closing = tag_end >= 2 && bytes[tag_end - 2] == b'/';
if !is_self_closing {
depth += 1;
}
}
}
}
idx += 1;
}
None
}
fn emit_svg_from_slice(
svg_slice: &str,
svg_start_offset: usize,
state: &mut Tier1State,
options: &ConversionOptions,
) -> Result<(), BailReason> {
if svg_slice.contains("<;
dest.push_str(&base64_svg);
dest.push(')');
Ok(())
}
fn find_raw_text_close(bytes: &[u8], open_end: usize, tag_name: &[u8]) -> Option<usize> {
let len = bytes.len();
let mut idx = open_end;
while idx < len {
match memchr3(b'<', b'<', b'<', &bytes[idx..]) {
Some(off) => idx += off,
None => return None,
}
if idx + 2 < len && bytes[idx + 1] == b'/' {
let after_slash = idx + 2;
if after_slash + tag_name.len() <= len
&& bytes[after_slash..after_slash + tag_name.len()].eq_ignore_ascii_case(tag_name)
{
let post_name = after_slash + tag_name.len();
if matches!(bytes.get(post_name), Some(b'>' | b'/' | b' ' | b'\t' | b'\n' | b'\r')) {
let mut j = post_name;
while j < len && bytes[j] != b'>' {
j += 1;
}
if j < len {
return Some(j + 1);
}
return None;
}
}
}
idx += 1;
}
None
}
#[inline]
const fn bail_unsupported(spec: &TagSpec, _offset: usize) -> Result<(), BailReason> {
match spec.kind {
TagKind::RawText(_) => Err(BailReason::Classifier),
TagKind::Ignored => Err(BailReason::Classifier),
_ => Ok(()),
}
}
fn block_container_is_passthrough(name_lower: &[u8]) -> bool {
matches!(name_lower, b"colgroup" | b"col" | b"base" | b"html" | b"body")
}
fn emit_open(
state: &mut Tier1State,
spec: &'static TagSpec,
name_lower: &[u8],
attrs: &[(&[u8], Option<&[u8]>)],
table_probes: &mut Vec<TableLayoutProbe>,
options: &ConversionOptions,
) -> Result<(), BailReason> {
state.last_closed_custom_element = false;
state.last_emitted_was_img = false;
if name_lower == b"q" || (name_lower == b"mark" && options.highlight_style != crate::options::HighlightStyle::None)
{
return Err(BailReason::InlineMarkerNotReproduced);
}
if matches!(
spec.kind,
TagKind::Block
| TagKind::Paragraph
| TagKind::Heading(_)
| TagKind::Blockquote
| TagKind::Pre
| TagKind::List(_)
| TagKind::Table
) && state.stack.iter().any(|f| matches!(f.spec.kind, TagKind::Link))
{
return Err(BailReason::Classifier);
}
match spec.kind {
TagKind::Paragraph => open_paragraph(state),
TagKind::Heading(_) => open_heading(state),
TagKind::Blockquote => open_blockquote(state),
TagKind::Pre => open_pre(state, attrs),
TagKind::List(ListKind::Definition) => open_dl(state),
TagKind::List(kind) => open_list(state, kind, options),
TagKind::ListItem => open_list_item(state, options),
TagKind::DefinitionTerm => open_dt(state),
TagKind::DefinitionDescription => open_dd(state),
TagKind::Strong => {
if !state.summary_at_top()
&& !state.escape_ctx.contains(EscapeCtx::STRONG)
&& !state.escape_ctx.contains(EscapeCtx::CODE)
&& !state.escape_ctx.contains(EscapeCtx::PRE)
{
if state.cell_or_output_mut().ends_with("**") {
return Err(BailReason::AdjacentInlineEmphasis);
}
state.cell_or_output_mut().push_str("**");
}
}
TagKind::Emphasis => {
if state.escape_ctx.contains(EscapeCtx::CODE) || state.escape_ctx.contains(EscapeCtx::PRE) {
return Ok(());
}
let buf = state.cell_or_output_mut();
if buf.ends_with('*') && !buf.ends_with("**") {
return Err(BailReason::AdjacentInlineEmphasis);
}
buf.push('*');
}
TagKind::Strikethrough => {
if !state.escape_ctx.contains(EscapeCtx::CODE) && !state.escape_ctx.contains(EscapeCtx::PRE) {
if state.cell_or_output_mut().ends_with("~~") {
return Err(BailReason::AdjacentInlineEmphasis);
}
state.cell_or_output_mut().push_str("~~");
}
}
TagKind::Inserted => {
if !state.escape_ctx.contains(EscapeCtx::CODE) && !state.escape_ctx.contains(EscapeCtx::PRE) {
if state.cell_or_output_mut().ends_with("==") {
return Err(BailReason::AdjacentInlineEmphasis);
}
state.cell_or_output_mut().push_str("==");
}
}
TagKind::Code if !state.escape_ctx.contains(EscapeCtx::PRE) && !state.escape_ctx.contains(EscapeCtx::CODE) => {}
TagKind::Code if state.pre_lang.is_none() && state.escape_ctx.contains(EscapeCtx::PRE) => {
if let Some(lang) = extract_language_from_class(attrs) {
state.pre_lang = Some(lang);
}
}
TagKind::Link => open_link(state),
TagKind::Table => open_table(state, attrs, table_probes),
TagKind::TableCaption => open_table_caption(state),
TagKind::TableHead => open_table_head(state)?,
TagKind::TableBody => open_table_body(state)?,
TagKind::TableFoot => open_table_foot(state),
TagKind::TableRow => open_table_row(state),
TagKind::TableCell { is_header } => open_table_cell(state, attrs, is_header, table_probes)?,
TagKind::Block => {
if block_container_is_passthrough(name_lower) {
} else if state.in_table_cell() {
let br_in_tables = options.br_in_tables;
let cell_buf = state.cell_or_output_mut();
if !cell_buf.is_empty()
&& !cell_buf.ends_with('|')
&& !cell_buf.ends_with("<br>")
&& !cell_buf.ends_with(" \n")
{
crate::converter::main_helpers::emit_table_cell_break(cell_buf, br_in_tables);
}
} else {
let dest = &mut state.output;
if !dest.is_empty() && !dest.ends_with("\n\n") {
crate::converter::tier1::state::trim_trailing_horizontal(dest);
dest.push_str("\n\n");
}
}
}
TagKind::Summary => open_summary(state),
TagKind::Figcaption => open_figcaption(state),
TagKind::Button => {}
TagKind::Inline => {}
_ => {}
}
Ok(())
}
fn open_paragraph(state: &mut Tier1State) {
if state.in_table_cell() {
let cell_buf = state.cell_or_output_mut();
if !cell_buf.is_empty() && !cell_buf.ends_with("<br>") {
cell_buf.push_str("<br>");
}
return;
}
let in_list_item = state
.stack
.iter()
.any(|frame| matches!(frame.spec.kind, TagKind::ListItem));
if in_list_item {
let dest = state.cell_or_output_mut();
if dest.ends_with("- ") || dest.ends_with("* ") || dest.ends_with("+ ") || ends_with_ordered_marker(dest) {
return;
}
}
let dest = state.cell_or_output_mut();
crate::converter::tier1::state::trim_trailing_horizontal(dest);
if !dest.is_empty() && !dest.ends_with("\n\n") {
dest.push_str("\n\n");
}
}
fn open_heading(state: &mut Tier1State) {
if state.in_table_cell() {
return;
}
ensure_blank_line_buf(state.cell_or_output_mut());
}
fn ensure_blank_line_buf(buf: &mut String) {
if buf.is_empty() {
return;
}
while buf.ends_with(' ') || buf.ends_with('\t') {
buf.pop();
}
if buf.ends_with("\n\n") {
return;
}
if buf.ends_with('\n') {
buf.push('\n');
} else {
buf.push_str("\n\n");
}
}
fn open_blockquote(state: &mut Tier1State) {
if state.escape_ctx.contains(EscapeCtx::BLOCKQUOTE) {
state.ensure_blank_line();
}
}
fn open_pre(state: &mut Tier1State, attrs: &[(&[u8], Option<&[u8]>)]) {
state.ensure_blank_line();
if let Some(lang) = extract_language_from_class(attrs) {
state.pre_lang = Some(lang);
}
}
fn extract_language_from_class(attrs: &[(&[u8], Option<&[u8]>)]) -> Option<String> {
let class_bytes = find_attr(attrs, b"class")?;
let class = decode_attr(class_bytes).ok()?;
for cls in class.split_ascii_whitespace() {
if let Some(rest) = cls.strip_prefix("language-") {
return Some(rest.to_owned());
}
if let Some(rest) = cls.strip_prefix("lang-") {
return Some(rest.to_owned());
}
}
None
}
fn strip_leading_bare_marker(text: &str) -> Option<&str> {
let digit_count = text.bytes().take_while(u8::is_ascii_digit).count();
if digit_count > 0 {
if let Some(rest) = text[digit_count..].strip_prefix(". ") {
return Some(rest);
}
}
let mut chars = text.chars();
let first = chars.next()?;
if matches!(first, '-' | '*' | '+') {
return chars.as_str().strip_prefix(' ');
}
None
}
fn line_is_bare_list_marker(output: &str) -> bool {
let line_start = output.rfind('\n').map_or(0, |pos| pos + 1);
let mut rest = output[line_start..].trim_start_matches([' ', '\t']);
if rest.is_empty() {
return false;
}
while let Some(next) = strip_leading_bare_marker(rest) {
if next.is_empty() {
return true;
}
rest = next;
}
false
}
fn open_list(state: &mut Tier1State, kind: ListKind, options: &ConversionOptions) {
if state.in_table_cell() {
let cell_buf = state.cell_or_output_mut();
if !cell_buf.is_empty() && !cell_buf.ends_with('|') && !cell_buf.ends_with(' ') && !cell_buf.ends_with("<br>") {
if options.br_in_tables {
cell_buf.push_str("<br>");
} else {
cell_buf.push(' ');
}
}
state.list_depth = state.list_depth.saturating_add(1);
if matches!(kind, ListKind::Unordered) {
state.ul_depth = state.ul_depth.saturating_add(1);
}
return;
}
let current_list_depth = state.list_depth;
{
let dest = state.cell_or_output_mut();
if !dest.is_empty() {
if current_list_depth == 0 {
let needs_newline = !dest.ends_with("\n\n") && !line_is_bare_list_marker(dest);
if needs_newline {
dest.push_str("\n\n");
}
} else {
let needs_newline = !dest.ends_with('\n') && !line_is_bare_list_marker(dest);
if needs_newline {
crate::converter::tier1::state::trim_trailing_horizontal(dest);
dest.push('\n');
}
}
}
}
state.list_depth = state.list_depth.saturating_add(1);
if matches!(kind, ListKind::Unordered) {
state.ul_depth = state.ul_depth.saturating_add(1);
}
}
const TIER1_BULLETS: [u8; 3] = [b'-', b'*', b'+'];
fn open_list_item(state: &mut Tier1State, options: &ConversionOptions) {
if state.in_table_cell() {
if find_parent_list_kind(&state.stack) == Some(ListKind::Ordered) {
increment_ol_counter(&mut state.stack);
}
let cell_buf = state.cell_or_output_mut();
if !cell_buf.is_empty() && !cell_buf.ends_with('|') && !cell_buf.ends_with(' ') && !cell_buf.ends_with("<br>") {
if options.br_in_tables {
cell_buf.push_str("<br>");
} else {
cell_buf.push(' ');
}
}
return;
}
let parent_kind = find_parent_list_kind(&state.stack);
let indent_depth = state.list_depth.saturating_sub(1);
if indent_depth > 0 && (state.output.is_empty() || state.output.ends_with('\n')) {
push_list_item_indent(&mut state.output, indent_depth);
}
if parent_kind == Some(ListKind::Ordered) {
let counter = increment_ol_counter(&mut state.stack);
let start = find_ol_start(&state.stack);
let index = start.saturating_sub(1) + counter;
let marker = format!("{index}. ");
state.list_item_marker_widths.push(marker.len());
state.output.push_str(&marker);
} else {
let bullet_idx = state.ul_depth.saturating_sub(1) as usize % TIER1_BULLETS.len();
state.list_item_marker_widths.push(2);
state.output.push(TIER1_BULLETS[bullet_idx] as char);
state.output.push(' ');
}
}
fn open_link(state: &mut Tier1State) {
if let Some(ts) = state.table_stack.last_mut() {
ts.link_count += 1;
}
state.cell_or_output_mut().push('[');
}
#[derive(Debug, Clone, Copy, Default)]
struct TableLayoutProbe {
nested_table_count: usize,
has_span: bool,
border_zero: bool,
}
fn open_table(state: &mut Tier1State, attrs: &[(&[u8], Option<&[u8]>)], table_probes: &mut Vec<TableLayoutProbe>) {
let inline_mode = !state.table_stack.is_empty();
state.table_stack.push(crate::converter::tier1::state::TableState {
inline_mode,
..Default::default()
});
table_probes.push(TableLayoutProbe {
border_zero: find_attr(attrs, b"border").is_some_and(|value| value == b"0".as_slice()),
..TableLayoutProbe::default()
});
}
fn open_table_caption(state: &mut Tier1State) {
if let Some(ts) = state.table_stack.last_mut() {
ts.caption_buf.clear();
ts.in_caption = true;
}
}
fn open_table_head(state: &mut Tier1State) -> Result<(), BailReason> {
if let Some(ts) = state.table_stack.last_mut() {
if ts.seen_tbody_close || ts.seen_tfoot {
return Err(BailReason::TableSectionOrder);
}
ts.in_thead = true;
}
Ok(())
}
fn open_table_body(state: &mut Tier1State) -> Result<(), BailReason> {
if let Some(ts) = state.table_stack.last_mut() {
if ts.seen_tfoot {
return Err(BailReason::TableSectionOrder);
}
}
Ok(())
}
fn open_table_foot(state: &mut Tier1State) {
if let Some(ts) = state.table_stack.last_mut() {
ts.seen_tfoot = true;
}
}
fn open_table_row(state: &mut Tier1State) {
if let Some(ts) = state.table_stack.last_mut() {
ts.current_row.clear();
}
}
fn open_table_cell(
state: &mut Tier1State,
attrs: &[(&[u8], Option<&[u8]>)],
is_header: bool,
table_probes: &mut [TableLayoutProbe],
) -> Result<(), BailReason> {
let spanning = has_attr(attrs, b"colspan") || has_attr(attrs, b"rowspan");
if let Some(probe) = table_probes.last_mut() {
probe.has_span |= spanning;
}
let colspan = find_attr(attrs, b"colspan")
.and_then(|b| std::str::from_utf8(b).ok())
.and_then(|s| s.parse::<u16>().ok())
.unwrap_or(1)
.max(1);
if let Some(ts) = state.table_stack.last_mut() {
ts.current_cell.clear();
ts.in_cell = true;
ts.current_cell_colspan = colspan;
if is_header {
ts.has_th = true;
}
}
Ok(())
}
fn emit_void(
state: &mut Tier1State,
spec: &'static TagSpec,
attrs: &[(&[u8], Option<&[u8]>)],
html: &str,
options: &ConversionOptions,
) -> Result<(), BailReason> {
state.last_closed_custom_element = false;
state.last_emitted_was_img = false;
match spec.kind {
TagKind::Hr => {
{
let dest = state.cell_or_output_mut();
if !dest.is_empty() && !dest.ends_with("\n\n") {
if dest.ends_with('\n') {
dest.push('\n');
} else {
dest.push_str("\n\n");
}
}
}
state.cell_or_output_mut().push_str("---\n");
}
TagKind::LineBreak => {
let link_frame_content_start = (!state.escape_ctx.contains(EscapeCtx::CODE))
.then(|| {
state
.stack
.iter()
.rev()
.find_map(|f| matches!(f.spec.kind, TagKind::Link).then_some(f.content_start))
})
.flatten();
if let Some(link_content_start) = link_frame_content_start {
if link_content_start < state.cell_or_output_mut().len() {
state.cell_or_output_mut().push_str(" \n");
}
} else if state.escape_ctx.contains(EscapeCtx::CODE)
&& !state.escape_ctx.contains(EscapeCtx::PRE)
&& state.escape_ctx.contains(EscapeCtx::HEADING)
{
let dest = state.cell_or_output_mut();
crate::converter::main_helpers::trim_trailing_whitespace(dest);
dest.push(' ');
} else if state.escape_ctx.contains(EscapeCtx::PRE) {
state.cell_or_output_mut().push('\n');
} else if state.in_table_cell() {
let emit_literal_br = options.br_in_tables && !state.escape_ctx.contains(EscapeCtx::CODE);
let dest = state.cell_or_output_mut();
crate::converter::main_helpers::trim_trailing_whitespace(dest);
if emit_literal_br {
dest.push_str("<br>");
} else if !dest.is_empty() {
dest.push(' ');
}
} else if state.escape_ctx.contains(EscapeCtx::CODE) {
state.cell_or_output_mut().push('\n');
} else if state.stack.is_empty() {
} else {
let dest = state.cell_or_output_mut();
crate::converter::main_helpers::trim_trailing_whitespace(dest);
dest.push_str(" \n");
}
}
TagKind::Image => {
let src = find_attr(attrs, b"src").unwrap_or_default();
let src_is_lazy_load_placeholder = {
let trimmed = src.trim_ascii();
trimmed.is_empty() || trimmed.starts_with(b"data:")
};
if src_is_lazy_load_placeholder
&& [
b"data-src".as_slice(),
b"data-lazy-src".as_slice(),
b"data-original".as_slice(),
b"data-srcset".as_slice(),
b"srcset".as_slice(),
]
.iter()
.any(|name| find_attr(attrs, name).is_some())
{
return Err(BailReason::ImageLazyLoadSrc);
}
let alt = find_attr(attrs, b"alt").unwrap_or_default();
let title = find_attr(attrs, b"title");
let src = decode_attr(src)?;
let alt_owned = decode_attr(alt)?;
let alt: &str = &alt_owned;
let keep_as_markdown = should_keep_image_as_markdown(html, &state.stack, options);
let dest = state.cell_or_output_mut();
if keep_as_markdown {
let escaped_alt = crate::converter::utility::content::escape_link_label(alt);
if let Some(title_bytes) = title {
let decoded_title = decode_attr(title_bytes)?;
let title_owned =
crate::converter::inline::link::escape_markdown_title(&decoded_title).into_owned();
let title_str: &str = &title_owned;
dest.push_str(";
crate::converter::inline::link::append_url_destination(dest, &src, options.url_escape_style, true);
dest.push_str(" \"");
dest.push_str(title_str);
dest.push_str("\")");
} else {
dest.push_str(";
crate::converter::inline::link::append_url_destination(dest, &src, options.url_escape_style, false);
dest.push(')');
}
} else {
dest.push_str(alt);
}
state.last_emitted_was_img = true;
}
TagKind::Ignored | TagKind::Inline | TagKind::Block => {}
_ => {}
}
Ok(())
}
#[inline]
#[allow(clippy::missing_const_for_fn)]
fn should_keep_image_as_markdown(html: &str, stack: &[OpenTag], options: &ConversionOptions) -> bool {
#[cfg(feature = "inline-images")]
{
keep_inline_image_for_ancestors(html.as_bytes(), stack, &options.keep_inline_images_in)
}
#[cfg(not(feature = "inline-images"))]
{
let _ = html;
let _ = stack;
let _ = options;
true
}
}
#[cfg(feature = "inline-images")]
fn keep_inline_image_for_ancestors(input: &[u8], stack: &[OpenTag], keep: &[String]) -> bool {
for frame in stack.iter().rev() {
if matches!(frame.spec.kind, TagKind::Heading(_)) {
let name = &input[frame.name_range.clone()];
for keep_name in keep {
if eq_ascii_ignore_case(name, keep_name.as_bytes()) {
return true;
}
}
return false;
}
if matches!(frame.spec.kind, TagKind::Link) {
let name = &input[frame.name_range.clone()];
if keep
.iter()
.any(|keep_name| eq_ascii_ignore_case(name, keep_name.as_bytes()))
{
return true;
}
}
}
true
}
#[cfg(feature = "inline-images")]
fn eq_ascii_ignore_case(a: &[u8], b: &[u8]) -> bool {
a.eq_ignore_ascii_case(b)
}
fn is_adjacent_rawtext_ignored_open(bytes: &[u8], pos: usize) -> bool {
const CANDIDATES: [&[u8]; 2] = [b"script", b"style"];
if bytes.get(pos) != Some(&b'<') {
return false;
}
let name_start = pos + 1;
for name in CANDIDATES {
let name_end = name_start + name.len();
if bytes.len() < name_end {
continue;
}
if !bytes[name_start..name_end].eq_ignore_ascii_case(name) {
continue;
}
if let Some(b' ' | b'\t' | b'\n' | b'\r' | b'>' | b'/') = bytes.get(name_end) {
return true;
}
}
false
}
fn emit_close(
state: &mut Tier1State,
tag_name_bytes: &[u8],
options: &ConversionOptions,
table_probes: &mut Vec<TableLayoutProbe>,
) -> Result<(), BailReason> {
let mut name_buf = [0u8; MAX_TAG_NAME_BYTES];
let name_lower = lowercase_into(tag_name_bytes, &mut name_buf);
let spec: &'static TagSpec = if name_lower.contains(&b'-') {
&CUSTOM_ELEMENT_INLINE_SPEC
} else {
match tier1::lookup(name_lower) {
Some(s) => s,
None => {
return Err(BailReason::UnknownCustomElement {
name: bytes_to_string(tag_name_bytes).into(),
offset: 0,
});
}
}
};
state.last_emitted_was_img = false;
while let Some(top) = state.stack.last() {
if kinds_match(&top.spec.kind, &spec.kind) {
break;
}
if top.spec.optional_close.is_some() {
emit_close_for_implicit(state, options, table_probes)?;
} else {
break;
}
}
let actual_depth = state.stack.len() as u8;
let frame = pop_matching_frame(&mut state.stack, spec).ok_or_else(|| BailReason::DepthMismatch {
tag: bytes_to_string(name_lower),
expected: 1,
actual: actual_depth,
})?;
state.escape_ctx = frame.prev_escape_ctx;
state.last_closed_custom_element = std::ptr::eq(spec, &raw const CUSTOM_ELEMENT_INLINE_SPEC);
match spec.kind {
TagKind::Paragraph => close_paragraph(state),
TagKind::Heading(n) => close_heading(state, &frame, n, false)?,
TagKind::Blockquote => close_blockquote(state, &frame),
TagKind::Pre => close_pre(state, &frame, options),
TagKind::Strong
if state.summary_at_top()
|| state.escape_ctx.contains(EscapeCtx::STRONG)
|| state.escape_ctx.contains(EscapeCtx::CODE)
|| state.escape_ctx.contains(EscapeCtx::PRE) => {}
TagKind::Strong => close_inline_marker(state, &frame, "**")?,
TagKind::Emphasis
if state.escape_ctx.contains(EscapeCtx::CODE) || state.escape_ctx.contains(EscapeCtx::PRE) => {}
TagKind::Emphasis => close_inline_marker(state, &frame, "*")?,
TagKind::Strikethrough
if state.escape_ctx.contains(EscapeCtx::CODE) || state.escape_ctx.contains(EscapeCtx::PRE) => {}
TagKind::Strikethrough => close_inline_marker(state, &frame, "~~")?,
TagKind::Inserted
if state.escape_ctx.contains(EscapeCtx::CODE) || state.escape_ctx.contains(EscapeCtx::PRE) => {}
TagKind::Inserted => close_inline_marker(state, &frame, "==")?,
TagKind::Code => close_code(state, &frame)?,
TagKind::Link => close_link(state, &frame, options)?,
TagKind::List(ListKind::Definition) => close_dl(state, &frame),
TagKind::List(kind) => close_list(state, kind),
TagKind::ListItem => close_list_item(state, &frame),
TagKind::DefinitionTerm => close_dt(state),
TagKind::DefinitionDescription => close_dd(state),
TagKind::Hr => {}
TagKind::Table => close_table(state, options, table_probes)?,
TagKind::TableHead => close_table_head(state),
TagKind::TableBody => close_table_body(state),
TagKind::TableFoot => {}
TagKind::TableRow => close_table_row(state),
TagKind::TableCell { .. } => close_table_cell(state, false)?,
TagKind::TableCaption => close_table_caption(state),
TagKind::Block => close_block_container(state, &frame, name_lower),
TagKind::Summary => close_summary(state, &frame),
TagKind::Figcaption => close_figcaption(state, &frame),
TagKind::Button => close_button(state, &frame),
TagKind::Inline => {
if name_lower == b"abbr" {
if let Some(Some(title)) = state.abbr_titles.pop() {
let dest = state.cell_or_output_mut();
dest.push_str(" (");
dest.push_str(&title);
dest.push(')');
}
}
}
TagKind::LineBreak | TagKind::Image => {}
TagKind::RawText(_) | TagKind::Ignored => {}
}
Ok(())
}
fn close_block_container(state: &mut Tier1State, frame: &OpenTag, name_lower: &[u8]) {
if block_container_is_passthrough(name_lower) {
return;
}
if state.in_table_cell() {
return;
}
let buf = state.cell_or_output_mut();
if buf.len() <= frame.content_start {
return;
}
while buf.ends_with(' ') || buf.ends_with('\t') {
buf.pop();
}
if buf.ends_with("\n\n") {
return;
}
if buf.ends_with('\n') {
buf.push('\n');
} else {
buf.push_str("\n\n");
}
}
fn open_summary(state: &mut Tier1State) {
state.push_summary_buf(crate::converter::tier1::state::WrapKind::Summary);
}
fn close_summary(state: &mut Tier1State, _frame: &OpenTag) {
let buf = match state.pop_summary_buf() {
Some(b) => b,
None => return,
};
let trimmed = buf.trim();
if trimmed.is_empty() {
return;
}
let writing_to_cell = state.in_table_cell();
let dest = state.cell_or_output_mut();
if !writing_to_cell && !dest.is_empty() && !dest.ends_with("\n\n") {
if dest.ends_with('\n') {
dest.push('\n');
} else {
dest.push_str("\n\n");
}
}
dest.push_str("**");
dest.push_str(trimmed);
dest.push_str("**\n\n");
}
fn open_figcaption(state: &mut Tier1State) {
state.push_summary_buf(crate::converter::tier1::state::WrapKind::Figcaption);
}
fn close_figcaption(state: &mut Tier1State, _frame: &OpenTag) {
let buf = match state.pop_summary_buf() {
Some(b) => b,
None => return,
};
let trimmed = buf.trim();
if trimmed.is_empty() {
return;
}
let writing_to_cell = state.in_table_cell();
let dest = state.cell_or_output_mut();
while dest.ends_with(' ') || dest.ends_with('\t') {
dest.pop();
}
if !writing_to_cell && !dest.is_empty() && !dest.ends_with("\n\n") {
if dest.ends_with('\n') {
dest.push('\n');
} else {
dest.push_str("\n\n");
}
}
dest.push('*');
dest.push_str(trimmed);
dest.push_str("*\n\n");
}
fn close_button(state: &mut Tier1State, frame: &OpenTag) {
if state.in_table_cell() {
return;
}
let dest = state.cell_or_output_mut();
if dest.len() <= frame.content_start {
return;
}
while dest.ends_with(' ') || dest.ends_with('\t') {
dest.pop();
}
if dest.ends_with("\n\n") {
return;
}
if dest.ends_with('\n') {
dest.push('\n');
} else {
dest.push_str("\n\n");
}
}
fn clamp_to_char_boundary(buf: &str, at: usize) -> usize {
let mut at = at.min(buf.len());
while at > 0 && !buf.is_char_boundary(at) {
at -= 1;
}
at
}
fn close_inline_marker(state: &mut Tier1State, frame: &OpenTag, marker: &str) -> Result<(), BailReason> {
let buf = state.cell_or_output_mut();
let content_start = clamp_to_char_boundary(buf, frame.content_start);
let content_is_absent = buf.len() <= content_start;
let is_whitespace_only_not_empty = !content_is_absent
&& buf[content_start..]
.bytes()
.all(|b| matches!(b, b' ' | b'\t' | b'\n' | b'\r'));
let body_is_empty = content_is_absent || is_whitespace_only_not_empty;
if body_is_empty {
let was_whitespace_only =
is_whitespace_only_not_empty || (content_is_absent && frame.dropped_whitespace_only_text);
if was_whitespace_only && matches!(marker, "**" | "*" | "~~" | "==") {
return Err(BailReason::WhitespaceOnlyInlineEmphasis);
}
let open_marker_start = clamp_to_char_boundary(buf, content_start.saturating_sub(marker.len()));
buf.truncate(open_marker_start);
return Ok(());
}
let content_str = &buf[content_start..];
let leading_len = content_str.len() - content_str.trim_start().len();
let content_start = if leading_len > 0 {
let leading: String = content_str[..leading_len].to_owned();
buf.replace_range(content_start..content_start + leading_len, "");
let marker_start = clamp_to_char_boundary(buf, content_start.saturating_sub(marker.len()));
buf.insert_str(marker_start, &leading);
content_start + leading_len
} else {
content_start
};
let content_str = &buf[content_start..];
let trailing_len = content_str.len() - content_str.trim_end().len();
if trailing_len > 0 {
let trailing_start = buf.len() - trailing_len;
let trailing: String = buf[trailing_start..].to_owned();
buf.truncate(trailing_start);
buf.push_str(marker);
buf.push_str(&trailing);
return Ok(());
}
buf.push_str(marker);
Ok(())
}
fn emit_close_for_implicit(
state: &mut Tier1State,
options: &ConversionOptions,
table_probes: &mut Vec<TableLayoutProbe>,
) -> Result<(), BailReason> {
let frame = state.stack.pop().ok_or_else(|| BailReason::DepthMismatch {
tag: String::from("(implicit)"),
expected: 1,
actual: 0,
})?;
let spec = frame.spec;
state.escape_ctx = frame.prev_escape_ctx;
state.last_closed_custom_element = std::ptr::eq(spec, &raw const CUSTOM_ELEMENT_INLINE_SPEC);
match spec.kind {
TagKind::Paragraph => close_paragraph(state),
TagKind::Heading(n) => close_heading(state, &frame, n, true)?,
TagKind::Blockquote => close_blockquote(state, &frame),
TagKind::Pre => close_pre(state, &frame, options),
TagKind::Strong
if state.summary_at_top()
|| state.escape_ctx.contains(EscapeCtx::STRONG)
|| state.escape_ctx.contains(EscapeCtx::CODE)
|| state.escape_ctx.contains(EscapeCtx::PRE) => {}
TagKind::Strong => close_inline_marker(state, &frame, "**")?,
TagKind::Emphasis
if state.escape_ctx.contains(EscapeCtx::CODE) || state.escape_ctx.contains(EscapeCtx::PRE) => {}
TagKind::Emphasis => close_inline_marker(state, &frame, "*")?,
TagKind::Strikethrough
if state.escape_ctx.contains(EscapeCtx::CODE) || state.escape_ctx.contains(EscapeCtx::PRE) => {}
TagKind::Strikethrough => close_inline_marker(state, &frame, "~~")?,
TagKind::Inserted
if state.escape_ctx.contains(EscapeCtx::CODE) || state.escape_ctx.contains(EscapeCtx::PRE) => {}
TagKind::Inserted => close_inline_marker(state, &frame, "==")?,
TagKind::Code => close_code(state, &frame)?,
TagKind::Link => close_link(state, &frame, options)?,
TagKind::List(ListKind::Definition) => close_dl(state, &frame),
TagKind::List(kind) => close_list(state, kind),
TagKind::ListItem => close_list_item(state, &frame),
TagKind::DefinitionTerm => close_dt(state),
TagKind::DefinitionDescription => close_dd(state),
TagKind::TableCell { .. } => close_table_cell(state, true)?,
TagKind::TableRow => close_table_row(state),
TagKind::Summary => close_summary(state, &frame),
TagKind::Figcaption => close_figcaption(state, &frame),
TagKind::Button => close_button(state, &frame),
TagKind::Table => close_table(state, options, table_probes)?,
TagKind::Block | TagKind::Inline => {}
TagKind::LineBreak
| TagKind::Image
| TagKind::Hr
| TagKind::TableHead
| TagKind::TableBody
| TagKind::TableFoot
| TagKind::TableCaption
| TagKind::RawText(_)
| TagKind::Ignored => {}
}
Ok(())
}
fn close_paragraph(state: &mut Tier1State) {
if state.in_table_cell() {
return;
}
trim_trailing_inline_whitespace(state);
state.cell_or_output_mut().push_str("\n\n");
}
fn close_heading(state: &mut Tier1State, frame: &OpenTag, n: u8, is_implicit: bool) -> Result<(), BailReason> {
if state.in_table_cell() {
let cell_buf = state.cell_or_output_mut();
while cell_buf.ends_with(' ') || cell_buf.ends_with('\t') {
cell_buf.pop();
}
if !is_implicit {
let cell_buf = state.cell_or_output_mut();
let content_start = clamp_to_char_boundary(cell_buf, frame.content_start);
let content = &cell_buf[content_start..];
if content.trim().is_empty() {
state.cell_or_output_mut().truncate(content_start);
}
}
return Ok(());
}
trim_trailing_inline_whitespace(state);
let buf = state.cell_or_output_mut();
let content_start = clamp_to_char_boundary(buf, frame.content_start);
if !is_implicit {
let content = &buf[content_start..];
if content.trim().is_empty() {
buf.truncate(content_start);
let trimmed_len = buf.trim_end_matches('\n').len();
if trimmed_len > 0 {
buf.truncate(trimmed_len);
buf.push('\n');
} else {
buf.clear();
}
return Ok(());
}
}
if buf[content_start..].contains('\n') {
let content = buf[content_start..].to_owned();
let mut normalized = String::with_capacity(content.len());
let mut prev_was_space = false;
for ch in content.chars() {
let is_ws = ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r';
if is_ws {
if !prev_was_space {
normalized.push(' ');
prev_was_space = true;
}
} else {
normalized.push(ch);
prev_was_space = false;
}
}
buf.truncate(content_start);
buf.push_str(normalized.trim_end());
}
let trailing_ws_len = buf[content_start..]
.char_indices()
.rev()
.take_while(|&(_, c)| c.is_whitespace())
.count();
if trailing_ws_len > 0 {
let cut = buf[content_start..]
.char_indices()
.rev()
.take(trailing_ws_len)
.last()
.map_or(buf.len(), |(i, _)| content_start + i);
buf.truncate(cut);
}
let leading_ws_len = buf[content_start..]
.char_indices()
.find(|&(_, c)| !c.is_whitespace())
.map_or(buf.len() - content_start, |(i, _)| i);
if leading_ws_len > 0 {
buf.replace_range(content_start..content_start + leading_ws_len, "");
}
let prefix = heading_prefix(n);
buf.insert_str(content_start, prefix);
ensure_blank_line_buf(state.cell_or_output_mut());
Ok(())
}
fn close_blockquote(state: &mut Tier1State, frame: &OpenTag) {
if state.in_table_cell() {
return;
}
let content_start = clamp_to_char_boundary(&state.output, frame.content_start);
let mut content = state.output[content_start..].to_owned();
crate::converter::main_helpers::trim_trailing_whitespace(&mut content);
let leading_ws_len = content
.char_indices()
.find(|&(_, c)| !c.is_whitespace())
.map_or(content.len(), |(i, _)| i);
if leading_ws_len > 0 {
content.replace_range(0..leading_ws_len, "");
}
let prefixed = prefix_blockquote_lines(&content);
state.output.truncate(content_start);
if frame.prev_escape_ctx.contains(EscapeCtx::BLOCKQUOTE) {
if state.output.ends_with("\n\n") {
state.output.pop();
}
} else if !state.output.is_empty() {
if state.output.ends_with("\n\n") {
state.output.pop();
} else if !state.output.ends_with('\n') {
state.output.push_str("\n\n");
} else {
state.output.push('\n');
}
}
push_list_item_continuation_lines(state, &prefixed);
let in_list_item = state
.stack
.iter()
.any(|open_frame| matches!(open_frame.spec.kind, TagKind::ListItem));
if !in_list_item {
state.ensure_blank_line();
}
}
fn close_pre(state: &mut Tier1State, frame: &OpenTag, options: &ConversionOptions) {
use crate::options::CodeBlockStyle;
if state.in_table_cell() {
return;
}
let content_start = clamp_to_char_boundary(&state.output, frame.content_start);
let raw = state.output[content_start..].to_owned();
state.output.truncate(content_start);
let mut rendered = String::new();
match options.code_block_style {
CodeBlockStyle::Indented => {
rendered.push_str(&indent_pre_lines(&raw));
}
CodeBlockStyle::Backticks => {
let fence_length = (longest_consecutive_backtick_run(&raw) + 1).max(MIN_FENCE_LENGTH);
let fence: String = std::iter::repeat_n('`', fence_length).collect();
rendered.push_str(&fence);
if let Some(lang) = state.pre_lang.take() {
rendered.push_str(&lang);
} else if !options.code_language.is_empty() {
rendered.push_str(&options.code_language);
}
rendered.push('\n');
let raw = raw.strip_prefix('\n').unwrap_or(&raw);
let raw = raw.trim_end_matches('\n');
rendered.push_str(raw);
rendered.push('\n');
rendered.push_str(&fence);
rendered.push_str("\n\n");
}
CodeBlockStyle::Tildes => {
rendered.push_str(&indent_pre_lines(&raw));
}
}
push_list_item_continuation_lines(state, &rendered);
state.pre_lang = None;
}
fn indent_fresh_list_item_text_line(state: &mut Tier1State) {
if state.in_table_cell() || state.in_summary() || state.in_table_caption() {
return;
}
let in_list_item = state
.stack
.iter()
.any(|frame| matches!(frame.spec.kind, TagKind::ListItem));
if !in_list_item {
return;
}
let indent_width = state.list_continuation_indent_width();
if indent_width == 0 {
return;
}
let dest = state.cell_or_output_mut();
if dest.ends_with('\n') && !dest.ends_with("\n\n") {
let indent: String = std::iter::repeat_n(' ', indent_width).collect();
dest.push_str(&indent);
}
}
fn push_list_item_continuation_lines(state: &mut Tier1State, rendered: &str) {
let indent_width = state.list_continuation_indent_width();
if indent_width == 0 {
state.output.push_str(rendered);
return;
}
let is_continuation = !state.output.is_empty() && !line_is_bare_list_marker(&state.output);
let indent: String = std::iter::repeat_n(' ', indent_width).collect();
for (index, segment) in rendered.split_inclusive('\n').enumerate() {
let line = segment.strip_suffix('\n').unwrap_or(segment);
if line.is_empty() || (index == 0 && !is_continuation) {
state.output.push_str(segment);
} else {
state.output.push_str(&indent);
state.output.push_str(segment);
}
}
}
fn close_code(state: &mut Tier1State, frame: &OpenTag) -> Result<(), BailReason> {
if state.escape_ctx.contains(EscapeCtx::PRE) || state.escape_ctx.contains(EscapeCtx::CODE) {
return Ok(());
}
let buf = state.cell_or_output_mut();
let content_start = clamp_to_char_boundary(buf, frame.content_start);
if content_start >= buf.len() {
if frame.dropped_whitespace_only_text {
return Err(BailReason::WhitespaceOnlyInlineEmphasis);
}
return Ok(());
}
if buf[..content_start].ends_with('`') {
return Err(BailReason::AdjacentInlineEmphasis);
}
let content = buf[content_start..].to_owned();
buf.truncate(content_start);
let mut first = true;
for segment in content.split('\n').filter(|segment| !segment.is_empty()) {
if !first {
buf.push_str(" \n");
}
format_inline_code_segment(buf, segment);
first = false;
}
Ok(())
}
fn format_inline_code_segment(buf: &mut String, content: &str) {
let contains_backtick = content.contains('`');
let first_char = content.chars().next();
let last_char = content.chars().last();
let starts_with_space = first_char == Some(' ');
let ends_with_space = last_char == Some(' ');
let starts_with_backtick = first_char == Some('`');
let ends_with_backtick = last_char == Some('`');
let needs_spaces =
starts_with_backtick || ends_with_backtick || (starts_with_space && ends_with_space && contains_backtick);
let num_backticks = if contains_backtick {
min_safe_code_span_delimiter_length(content)
} else {
1
};
for _ in 0..num_backticks {
buf.push('`');
}
if needs_spaces {
buf.push(' ');
}
buf.push_str(content);
if needs_spaces {
buf.push(' ');
}
for _ in 0..num_backticks {
buf.push('`');
}
}
fn longest_consecutive_backtick_run(content: &str) -> usize {
content
.chars()
.fold((0usize, 0usize), |(max, current), c| {
if c == '`' {
let next = current + 1;
(max.max(next), next)
} else {
(max, 0)
}
})
.0
}
fn min_safe_code_span_delimiter_length(content: &str) -> usize {
let mut run_lengths = std::collections::HashSet::new();
let mut current = 0usize;
for c in content.chars() {
if c == '`' {
current += 1;
} else {
if current > 0 {
run_lengths.insert(current);
}
current = 0;
}
}
if current > 0 {
run_lengths.insert(current);
}
let mut candidate = 1usize;
while run_lengths.contains(&candidate) {
candidate += 1;
}
candidate
}
fn is_byte_subsequence(needle: &str, haystack: &str) -> bool {
let mut needle_bytes = needle.bytes();
let Some(mut want) = needle_bytes.next() else {
return true;
};
for byte in haystack.bytes() {
if byte == want {
match needle_bytes.next() {
Some(next_want) => want = next_want,
None => return true,
}
}
}
false
}
fn try_emit_autolink(
dest: &mut String,
trim_start: usize,
frame: &OpenTag,
href_str: &str,
has_nested_tag: bool,
options: &ConversionOptions,
) -> Result<bool, BailReason> {
if !options.autolinks || options.default_title || href_str.is_empty() || !has_uri_scheme(href_str) {
return Ok(false);
}
let mailto_suffix = href_str.strip_prefix("mailto:");
let label = &dest[trim_start..];
if has_nested_tag {
let could_be_autolink = is_byte_subsequence(href_str, label)
|| mailto_suffix.is_some_and(|suffix| is_byte_subsequence(suffix, label));
if could_be_autolink {
return Err(BailReason::LinkAutolinkNestedMarkup);
}
}
let rendered = if label == href_str {
href_str
} else if mailto_suffix == Some(label) {
mailto_suffix.unwrap_or(href_str)
} else {
return Ok(false);
};
let bracket_search_end = clamp_to_char_boundary(dest, frame.content_start);
let label_start = if let Some(bracket_pos) = dest[..bracket_search_end].rfind('[') {
dest.remove(bracket_pos);
trim_start - 1
} else {
trim_start
};
let rendered = rendered.to_owned();
dest.truncate(label_start);
dest.push('<');
dest.push_str(&rendered);
dest.push('>');
Ok(true)
}
fn close_link(state: &mut Tier1State, frame: &OpenTag, options: &ConversionOptions) -> Result<(), BailReason> {
let (href, title, has_nested_tag) = state.link_stack.pop().unwrap_or((None, None, false));
let dest = state.cell_or_output_mut();
let trim_start = clamp_to_char_boundary(dest, frame.content_start);
let trimmed_end = dest[trim_start..].trim_end_matches(|c: char| c.is_whitespace()).len();
dest.truncate(trim_start + trimmed_end);
if dest[trim_start..].contains('\u{00a0}') {
let normalised: String = dest[trim_start..]
.chars()
.map(|c| if c == '\u{00a0}' { ' ' } else { c })
.collect();
dest.truncate(trim_start);
dest.push_str(&normalised);
}
if let Some(href_str) = href.as_deref() {
if try_emit_autolink(dest, trim_start, frame, href_str, has_nested_tag, options)? {
return Ok(());
}
}
if let Some(href_str) = href.as_deref() {
if href_str.starts_with('#') && dest.len() == trim_start + 1 && dest.as_bytes()[trim_start] == b'^' {
dest.truncate(trim_start);
dest.push('↑');
}
}
if let Some(href) = href {
match crate::converter::utility::content::escape_link_label(&dest[trim_start..]) {
std::borrow::Cow::Borrowed(_) => {}
std::borrow::Cow::Owned(escaped_label) => {
dest.truncate(trim_start);
dest.push_str(&escaped_label);
}
}
if let Some(title) = title {
let escaped_title = crate::converter::inline::link::escape_markdown_title(&title);
let title_out: &str = &escaped_title;
#[allow(clippy::format_push_string)]
dest.push_str(&format!("]({href} \"{title_out}\")"));
} else {
#[allow(clippy::format_push_string)]
dest.push_str(&format!("]({href})"));
}
} else {
let bracket_search_end = clamp_to_char_boundary(dest, frame.content_start);
if let Some(bracket_pos) = dest[..bracket_search_end].rfind('[') {
dest.remove(bracket_pos);
}
}
Ok(())
}
fn close_list(state: &mut Tier1State, kind: ListKind) {
state.list_depth = state.list_depth.saturating_sub(1);
if matches!(kind, ListKind::Unordered) {
state.ul_depth = state.ul_depth.saturating_sub(1);
}
if state.in_table_cell() {
return;
}
let dest = state.cell_or_output_mut();
if !dest.ends_with('\n') {
dest.push('\n');
}
}
fn close_list_item(state: &mut Tier1State, frame: &OpenTag) {
if state.in_table_cell() {
let cell_buf = state.cell_or_output_mut();
while cell_buf.ends_with(' ') || cell_buf.ends_with('\t') {
cell_buf.pop();
}
return;
}
state.list_item_marker_widths.pop();
trim_trailing_inline_whitespace(state);
let dest = state.cell_or_output_mut();
let had_block_children = {
let start = clamp_to_char_boundary(dest, frame.content_start);
dest[start..].contains("\n\n")
};
if had_block_children {
if !dest.ends_with("\n\n") {
if dest.ends_with('\n') {
dest.push('\n');
} else {
dest.push_str("\n\n");
}
}
} else if !dest.is_empty() && !dest.ends_with('\n') {
dest.push('\n');
}
}
fn open_dl(state: &mut Tier1State) {
if state.in_table_cell() {
return;
}
state.ensure_blank_line();
}
const fn open_dt(_state: &mut Tier1State) {}
const fn open_dd(_state: &mut Tier1State) {}
fn close_dt(state: &mut Tier1State) {
if state.in_table_cell() {
return;
}
trim_trailing_inline_whitespace(state);
let buf = state.cell_or_output_mut();
if buf.is_empty() || buf.ends_with('\n') {
return;
}
buf.push('\n');
}
fn close_dd(state: &mut Tier1State) {
if state.in_table_cell() {
return;
}
trim_trailing_inline_whitespace(state);
let buf = state.cell_or_output_mut();
if buf.is_empty() {
return;
}
if buf.ends_with("\n\n") {
return;
}
if buf.ends_with('\n') {
buf.push('\n');
} else {
buf.push_str("\n\n");
}
}
fn close_dl(state: &mut Tier1State, frame: &OpenTag) {
if state.in_table_cell() {
return;
}
let buf = state.cell_or_output_mut();
if buf.len() <= frame.content_start {
return;
}
while buf.len() > frame.content_start {
let last = buf.as_bytes()[buf.len() - 1];
if matches!(last, b' ' | b'\t' | b'\n' | b'\r') {
buf.pop();
} else {
break;
}
}
if buf.len() == frame.content_start {
return;
}
buf.push_str("\n\n");
}
fn close_table(
state: &mut Tier1State,
options: &ConversionOptions,
table_probes: &mut Vec<TableLayoutProbe>,
) -> Result<(), BailReason> {
let Some(ts) = state.table_stack.pop() else {
return Ok(());
};
let probe = table_probes.pop().unwrap_or_default();
table_probes.truncate(state.table_stack.len());
let has_caption = ts.caption_text.is_some();
if !ts.has_th
&& !has_caption
&& !probe.has_span
&& probe.nested_table_count > 0
&& ts.rows.len() == 1
&& ts.first_row_col_count == Some(1)
{
return Err(BailReason::TableNestedTable);
}
if !ts.has_th && !has_caption {
let row_count = ts.rows.len();
let expanded_cols = |row: &Vec<(String, u16)>| -> usize { row.iter().map(|(_, c)| usize::from(*c)).sum() };
let inconsistent_cols = {
let first = ts.first_row_col_count.unwrap_or(0);
ts.rows.iter().any(|r| expanded_cols(r) != first)
};
let link_heavy = row_count <= 2 && ts.link_count >= 3;
let is_blank = ts.rows.is_empty() || ts.rows.iter().all(|r| r.iter().all(|(c, _)| c.trim().is_empty()));
let multiple_nested_tables = probe.nested_table_count > 1;
let spanning_borderless = probe.has_span && probe.border_zero;
if inconsistent_cols || link_heavy || is_blank || multiple_nested_tables || spanning_borderless {
return Err(BailReason::Classifier);
}
}
if ts.had_single_cell_nested_table_row {
return Err(BailReason::TableNestedTableInSingleCellRow);
}
if ts.inline_mode {
if let Some(outer) = state.table_stack.last_mut() {
outer.had_nested_table = true;
}
if let Some(outer_probe) = table_probes.last_mut() {
outer_probe.nested_table_count += 1;
}
let mut nested = String::new();
emit_gfm_table(&mut nested, ts);
if nested.contains('|') {
nested = crate::converter::utility::content::escape_bare_pipes_outside_code_spans(&nested);
}
let nested = crate::converter::block::table::cell::fold_nested_table_rows(&nested, options.br_in_tables);
let dest = state.cell_or_output_mut();
if !nested.is_empty() && !dest.trim_end().is_empty() {
crate::converter::main_helpers::emit_table_cell_break(dest, options.br_in_tables);
}
dest.push_str(&nested);
} else {
emit_gfm_table(&mut state.output, ts);
}
Ok(())
}
fn close_table_head(state: &mut Tier1State) {
if let Some(ts) = state.table_stack.last_mut() {
ts.in_thead = false;
}
}
fn close_table_body(state: &mut Tier1State) {
if let Some(ts) = state.table_stack.last_mut() {
ts.seen_tbody_close = true;
}
}
fn close_table_caption(state: &mut Tier1State) {
let Some(ts) = state.table_stack.last_mut() else {
return;
};
ts.in_caption = false;
let raw = std::mem::take(&mut ts.caption_buf);
let trimmed = raw.trim();
if !trimmed.is_empty() {
ts.caption_text = Some(trimmed.replace('-', r"\-"));
}
}
fn close_table_row(state: &mut Tier1State) {
let Some(ts) = state.table_stack.last_mut() else {
return;
};
if ts.current_row.is_empty() {
return;
}
if ts.pending_single_cell_nested_table && ts.current_row.len() == 1 {
ts.had_single_cell_nested_table_row = true;
}
ts.pending_single_cell_nested_table = false;
let col_count: usize = ts.current_row.iter().map(|(_, c)| usize::from(*c)).sum();
if ts.first_row_col_count.is_none() {
ts.first_row_col_count = Some(col_count);
}
let row = std::mem::take(&mut ts.current_row);
ts.rows.push(row);
}
fn close_table_cell(state: &mut Tier1State, is_implicit: bool) -> Result<(), BailReason> {
let Some(ts) = state.table_stack.last_mut() else {
return Ok(());
};
ts.in_cell = false;
let cell_text_raw = ts.current_cell.trim().to_owned();
let cell_text = if cell_text_raw.contains('\n') {
cell_text_raw.replace('\n', " ")
} else {
cell_text_raw
};
let cell_text = cell_text.trim().to_owned();
let allow_pipes = ts.had_nested_table;
ts.had_nested_table = false;
if !is_implicit && !allow_pipes && cell_text.contains('|') {
return Err(BailReason::TableBlockChildInCell);
}
ts.pending_single_cell_nested_table = allow_pipes && ts.current_row.is_empty();
let colspan = ts.current_cell_colspan;
ts.current_row.push((cell_text, colspan));
ts.current_cell.clear();
ts.current_cell_colspan = 1;
Ok(())
}
fn ends_with_ordered_marker(s: &str) -> bool {
let bytes = s.as_bytes();
let len = bytes.len();
if len < 3 || bytes[len - 1] != b' ' {
return false;
}
let punct = bytes[len - 2];
if punct != b'.' && punct != b')' {
return false;
}
let mut i = len - 2;
while i > 0 && bytes[i - 1].is_ascii_digit() {
i -= 1;
}
i < len - 2 && (i == 0 || !bytes[i - 1].is_ascii_digit())
}
fn output_ends_with_inline_close_marker(output: &str) -> bool {
if output.is_empty() || output.ends_with('\n') || output.ends_with(' ') || output.ends_with('\t') {
return false;
}
if output.ends_with("**") || output.ends_with('`') || output.ends_with(')') {
return true;
}
output.ends_with('*') && !output.ends_with("**")
}
fn output_ends_with_inline_text(output: &str) -> bool {
if output.is_empty() || output.ends_with('\n') || output.ends_with(' ') || output.ends_with('\t') {
return false;
}
!output_ends_with_inline_close_marker(output)
}
fn flush_text(
state: &mut Tier1State,
raw: &str,
base_offset: usize,
next_tag_is_list: bool,
next_tag_is_img: bool,
next_tag_is_span: bool,
) -> Result<(), BailReason> {
if raw.is_empty() {
return Ok(());
}
let after_custom_element_close = state.last_closed_custom_element;
state.last_closed_custom_element = false;
let after_img = state.last_emitted_was_img;
state.last_emitted_was_img = false;
if !state.table_stack.is_empty() && !state.in_table_cell() && !state.in_table_caption() {
if raw.trim().is_empty() {
return Ok(());
}
return Err(BailReason::Classifier);
}
let in_pre = state.escape_ctx.contains(EscapeCtx::PRE);
let in_code = state.escape_ctx.contains(EscapeCtx::CODE);
const UNICODE_WS_ENTITIES: &[&str] = &[
" ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ",
];
let raw_owned_nbsp;
let raw: &str = if !in_pre && !in_code {
let has_ws_entity = UNICODE_WS_ENTITIES.iter().any(|p| raw.contains(p));
let has_unicode_ws_literal = raw.bytes().any(|b| b >= 0x80)
&& raw
.chars()
.any(|c| c.is_whitespace() && c != ' ' && c != '\t' && c != '\n' && c != '\r');
if has_ws_entity || has_unicode_ws_literal {
let mut stripped = raw.to_owned();
for p in UNICODE_WS_ENTITIES {
if stripped.contains(p) {
stripped = stripped.replace(p, "");
}
}
let is_logically_whitespace = stripped.chars().all(char::is_whitespace);
if is_logically_whitespace {
raw
} else {
let mut after_entities = raw.to_owned();
for p in UNICODE_WS_ENTITIES {
if after_entities.contains(p) {
after_entities = after_entities.replace(p, " ");
}
}
let mut tmp = String::with_capacity(after_entities.len());
for c in after_entities.chars() {
if c.is_whitespace() && c != ' ' && c != '\t' && c != '\n' && c != '\r' {
tmp.push(' ');
} else {
tmp.push(c);
}
}
raw_owned_nbsp = tmp;
raw_owned_nbsp.as_str()
}
} else {
raw
}
} else {
raw
};
let in_summary_snapshot = state.in_summary();
let at_inline_frame_start = match state.stack.last() {
Some(frame) => {
let cs = frame.content_start;
let kind = frame.spec.kind;
let buf_len = state.cell_or_output_mut().len();
cs >= buf_len
&& (matches!(
kind,
TagKind::Link | TagKind::Strong | TagKind::Emphasis | TagKind::Code
) || (in_summary_snapshot
&& matches!(
kind,
TagKind::Inline | TagKind::Block | TagKind::Paragraph | TagKind::Heading(_)
)))
}
None => false,
};
let (active_empty, active_ends_newline, active_ends_list_marker, active_ends_ordered) = {
let buf: &str = state.cell_or_output_mut();
(
buf.is_empty(),
buf.ends_with('\n'),
buf.ends_with("- ") || buf.ends_with("* ") || buf.ends_with("+ "),
ends_with_ordered_marker(buf),
)
};
let is_block_edge =
active_empty || active_ends_newline || active_ends_list_marker || active_ends_ordered || at_inline_frame_start;
let raw_is_whitespace = raw.bytes().all(|b| b == b' ' || b == b'\t' || b == b'\n' || b == b'\r');
let in_list_item_frame_for_ws = state
.stack
.iter()
.any(|frame| matches!(frame.spec.kind, TagKind::ListItem));
let document_start_drops_ws = state.at_document_start
&& !state.in_table_cell()
&& !in_list_item_frame_for_ws
&& !state.escape_ctx.contains(EscapeCtx::HEADING);
if !in_pre && (is_block_edge || document_start_drops_ws) && raw_is_whitespace {
if at_inline_frame_start {
if let Some(frame) = state.stack.last_mut() {
if matches!(
frame.spec.kind,
TagKind::Strong | TagKind::Emphasis | TagKind::Strikethrough | TagKind::Inserted | TagKind::Code
) {
frame.dropped_whitespace_only_text = true;
}
}
}
return Ok(());
}
if !in_pre && state.in_table_cell() && raw_is_whitespace && !is_block_edge {
if matches!(state.stack.last().map(|f| f.spec.kind), Some(TagKind::List(_))) {
return Ok(());
}
let dest = state.cell_or_output_mut();
if !dest.is_empty() && !dest.ends_with('\n') && (after_custom_element_close || !dest.ends_with(' ')) {
dest.push(' ');
}
return Ok(());
}
if !in_pre && !state.in_table_cell() && raw_is_whitespace {
let inside_inline = state.in_summary()
|| state.stack.iter().any(|frame| {
matches!(
frame.spec.kind,
TagKind::Link | TagKind::Strong | TagKind::Emphasis | TagKind::Code
)
});
if !inside_inline {
let in_direct_paragraph = matches!(
state.stack.last().map(|frame| frame.spec.kind),
Some(TagKind::Paragraph)
);
if in_direct_paragraph && after_img && next_tag_is_img {
return Ok(());
}
let active_tail: &str = state.cell_or_output_mut();
if output_ends_with_inline_close_marker(active_tail)
|| output_ends_with_inline_text(active_tail)
|| (after_custom_element_close && !active_tail.is_empty() && !active_tail.ends_with('\n'))
{
let dest = state.cell_or_output_mut();
if next_tag_is_list && raw.contains('\n') {
} else if next_tag_is_list {
dest.push_str(raw);
} else {
dest.push(' ');
}
}
return Ok(());
}
let active_tail: &str = state.cell_or_output_mut();
if !active_tail.is_empty() && !active_tail.ends_with(' ') && !active_tail.ends_with('\n') {
let dest = state.cell_or_output_mut();
dest.push(' ');
}
return Ok(());
}
let was_at_document_start = state.at_document_start;
state.at_document_start = false;
let in_list_item_frame = state
.stack
.iter()
.any(|frame| matches!(frame.spec.kind, TagKind::ListItem));
let document_start_strip = was_at_document_start
&& !state.in_table_cell()
&& !in_list_item_frame
&& !state.escape_ctx.contains(EscapeCtx::HEADING);
let block_separator_after = {
let active: &str = state.cell_or_output_mut();
active.ends_with("\n\n")
|| active.ends_with("- ")
|| active.ends_with("* ")
|| active.ends_with("+ ")
|| ends_with_ordered_marker(active)
};
let leading_ws_migrates_out = at_inline_frame_start
&& !document_start_strip
&& matches!(
state.stack.last().map(|frame| frame.spec.kind),
Some(TagKind::Strong | TagKind::Emphasis)
);
let in_link_frame = matches!(state.stack.last().map(|frame| frame.spec.kind), Some(TagKind::Link));
let after_link_hard_break = in_link_frame && state.cell_or_output_mut().ends_with('\n');
let bare_inline_frame_start_after_space = {
let buf_len = state.cell_or_output_mut().len();
let is_bare_inline_start = matches!(
state.stack.last(),
Some(frame) if frame.content_start >= buf_len && matches!(frame.spec.kind, TagKind::Inline)
);
is_bare_inline_start && state.cell_or_output_mut().ends_with(' ')
};
let raw = if !in_pre
&& !in_code
&& (!state.in_table_cell() || in_link_frame)
&& (at_inline_frame_start
|| block_separator_after
|| document_start_strip
|| after_link_hard_break
|| bare_inline_frame_start_after_space)
{
let trimmed = raw.trim_start_matches([' ', '\t', '\n', '\r']);
if leading_ws_migrates_out && trimmed.len() < raw.len() {
state.cell_or_output_mut().push(' ');
}
trimmed
} else {
raw
};
if raw.is_empty() {
return Ok(());
}
let has_entities = raw.contains('&');
if in_pre {
if has_entities {
let dest = state.cell_or_output_mut();
decode_entities_into(dest, raw, base_offset)?;
} else {
state.cell_or_output_mut().push_str(raw);
}
return Ok(());
}
if in_code {
let decoded: std::borrow::Cow<'_, str> = if has_entities {
let mut buf = String::with_capacity(raw.len());
decode_entities_into(&mut buf, raw, base_offset)?;
std::borrow::Cow::Owned(buf)
} else {
std::borrow::Cow::Borrowed(raw)
};
let folded = crate::text::fold_cell_line_breaks_verbatim_cow(decoded.as_ref());
state.cell_or_output_mut().push_str(folded.as_ref());
return Ok(());
}
let inside_inline = state.in_table_cell()
|| state.in_summary()
|| state.stack.iter().any(|frame| matches!(frame.spec.kind, TagKind::Link));
let raw_owned;
let raw = if !inside_inline && !state.in_table_cell() {
let trim_chars: &[char] = &['\n', '\r', ' ', '\t'];
let after_lead = raw.trim_start_matches(trim_chars);
let leading_len = raw.len() - after_lead.len();
let lead_has_nl = leading_len > 0 && raw.as_bytes()[..leading_len].iter().any(|&b| b == b'\n' || b == b'\r');
let trimmed_len = raw.trim_end_matches(trim_chars).len();
let trailing_len = raw.len() - trimmed_len;
let trail_has_nl = trailing_len > 0 && raw.as_bytes()[trimmed_len..].iter().any(|&b| b == b'\n' || b == b'\r');
if lead_has_nl || trail_has_nl {
let core_start = leading_len;
let core_end = trimmed_len;
if core_start >= core_end {
raw
} else {
let core = &raw[core_start..core_end];
let trailing = &raw[core_end..];
let prefix = if leading_len > 0 { " " } else { "" };
let suffix = if trailing.contains("\n\n") {
"\n\n"
} else if trailing.bytes().any(|b| b == b' ' || b == b'\t') {
" "
} else if trail_has_nl {
trailing_single_newline_join(state, next_tag_is_span)
} else {
trailing
};
raw_owned = format!("{prefix}{core}{suffix}");
raw_owned.as_str()
}
} else {
raw
}
} else {
raw
};
if raw.is_empty() {
return Ok(());
}
let has_entities = raw.contains('&');
let in_cell = state.in_table_cell();
indent_fresh_list_item_text_line(state);
if inside_inline && !in_cell && raw.contains('\\') {
let mut staged = String::with_capacity(raw.len() + 8);
if has_entities {
decode_entities_into(&mut staged, raw, base_offset)?;
} else {
staged.push_str(raw);
}
escape_backslash_run(&mut staged, 0, false);
let dest = state.cell_or_output_mut();
return decode_and_collapse_into_inline(dest, &staged, false, base_offset);
}
let dest = state.cell_or_output_mut();
let emitted_from = dest.len();
if !has_entities {
let needle_present = if inside_inline {
memchr3(b' ', b'\t', b'\n', raw.as_bytes()).is_some()
} else {
memchr::memchr2(b' ', b'\t', raw.as_bytes()).is_some()
};
if !needle_present {
dest.push_str(raw);
} else if inside_inline {
decode_and_collapse_into_inline(dest, raw, false, base_offset)?;
} else {
decode_and_collapse_into(dest, raw, false, base_offset)?;
}
} else if inside_inline {
decode_and_collapse_into_inline(dest, raw, has_entities, base_offset)?;
} else {
decode_and_collapse_into(dest, raw, has_entities, base_offset)?;
}
escape_backslash_run(dest, emitted_from, in_cell);
Ok(())
}
fn escape_backslash_run(buffer: &mut String, from: usize, run_ends_at_last_byte: bool) {
if memchr::memchr(b'\\', &buffer.as_bytes()[from..]).is_none() {
return;
}
let run = buffer[from..].to_owned();
let run_end = if run_ends_at_last_byte {
run.len()
} else {
run.trim_end().len()
};
let bytes = &run.as_bytes()[..run_end];
let mut rewritten = String::with_capacity(run.len() + 4);
let mut copied_to = 0usize;
for i in memchr::memchr_iter(b'\\', bytes) {
if crate::text::backslash_needs_escape(bytes, i) {
rewritten.push_str(&run[copied_to..i]);
rewritten.push_str(r"\\");
copied_to = i + 1;
}
}
rewritten.push_str(&run[copied_to..]);
buffer.truncate(from);
buffer.push_str(&rewritten);
}
fn decode_entities_into(out: &mut String, s: &str, base_offset: usize) -> Result<(), BailReason> {
let bytes = s.as_bytes();
let mut i = 0;
while i < bytes.len() {
if let Some(pos) = memchr::memchr(b'&', &bytes[i..]) {
let amp_pos = i + pos;
if amp_pos > i {
out.push_str(&s[i..amp_pos]);
}
i = decode_entity_at(bytes, s, amp_pos, out, base_offset)?;
} else {
if i < bytes.len() {
out.push_str(&s[i..]);
}
break;
}
}
Ok(())
}
fn decode_and_collapse_into(
out: &mut String,
s: &str,
has_entities: bool,
base_offset: usize,
) -> Result<(), BailReason> {
decode_and_collapse_into_inner(out, s, has_entities, base_offset, false)
}
fn decode_and_collapse_into_inline(
out: &mut String,
s: &str,
has_entities: bool,
base_offset: usize,
) -> Result<(), BailReason> {
decode_and_collapse_into_inner(out, s, has_entities, base_offset, true)
}
fn decode_and_collapse_into_inner(
out: &mut String,
s: &str,
has_entities: bool,
base_offset: usize,
collapse_newlines: bool,
) -> Result<(), BailReason> {
let bytes = s.as_bytes();
let mut i = 0;
let mut prev_was_space = false;
let mut at_line_start = false;
while i < bytes.len() {
let next_special = match (has_entities, collapse_newlines) {
(true, true) => {
let s_pos = memchr3(b' ', b'\t', b'\n', &bytes[i..]).map(|pos| i + pos);
let e_pos = memchr::memchr(b'&', &bytes[i..]).map(|pos| i + pos);
match (s_pos, e_pos) {
(Some(a), Some(b)) => Some(a.min(b)),
(Some(a), None) | (None, Some(a)) => Some(a),
(None, None) => None,
}
}
(true, false) => memchr3(b' ', b'\t', b'&', &bytes[i..]).map(|pos| i + pos),
(false, true) => memchr3(b' ', b'\t', b'\n', &bytes[i..]).map(|pos| i + pos),
(false, false) => memchr::memchr2(b' ', b'\t', &bytes[i..]).map(|pos| i + pos),
};
if let Some(pos) = next_special {
if pos > i {
out.push_str(&s[i..pos]);
prev_was_space = false;
at_line_start = !collapse_newlines && bytes[pos - 1] == b'\n';
}
match bytes[pos] {
b' ' | b'\t' if at_line_start => {
i = pos + 1;
}
b' ' | b'\t' => {
if !prev_was_space {
out.push(' ');
}
prev_was_space = true;
i = pos + 1;
}
b'\n' if collapse_newlines => {
if !prev_was_space {
out.push(' ');
}
prev_was_space = true;
i = pos + 1;
}
b'&' => {
prev_was_space = false;
at_line_start = false;
i = decode_entity_at(bytes, s, pos, out, base_offset)?;
}
_ => unreachable!(),
}
} else {
if i < bytes.len() {
out.push_str(&s[i..]);
}
break;
}
}
Ok(())
}
fn decode_entity_at(
bytes: &[u8],
s: &str,
amp_pos: usize,
out: &mut String,
_base_offset: usize,
) -> Result<usize, BailReason> {
let amp = amp_pos;
let mut end = amp + 1;
while end < bytes.len() && end - amp <= MAX_ENTITY_NAME_BYTES && bytes[end] != b';' {
end += 1;
}
if end < bytes.len() && bytes[end] == b';' && end > amp + 1 {
let entity = &s[amp + 1..end];
if decode_entity_into(out, entity) {
return Ok(end + 1);
}
out.push_str(&s[amp..=end]);
return Ok(end + 1);
}
out.push('&');
Ok(amp + 1)
}
#[inline]
fn apply_open_escape_ctx(state: &mut Tier1State, spec: &TagSpec) {
if spec.kind == TagKind::Pre {
state.escape_ctx |= EscapeCtx::PRE | EscapeCtx::CODE;
return;
}
let bit = match spec.kind {
TagKind::Code => EscapeCtx::CODE,
TagKind::Link => EscapeCtx::LINK,
TagKind::Blockquote => EscapeCtx::BLOCKQUOTE,
TagKind::Heading(_) => EscapeCtx::HEADING,
TagKind::Strong => EscapeCtx::STRONG,
_ => return,
};
state.escape_ctx |= bit;
}
fn has_attr(attrs: &[(&[u8], Option<&[u8]>)], key: &[u8]) -> bool {
attrs.iter().any(|(k, _)| k.eq_ignore_ascii_case(key))
}
fn find_attr<'a>(attrs: &[(&'a [u8], Option<&'a [u8]>)], key: &[u8]) -> Option<&'a [u8]> {
for (k, v) in attrs {
if k.eq_ignore_ascii_case(key) {
return *v;
}
}
None
}
fn is_preprocessing_skip_candidate(name_lower: &[u8]) -> bool {
matches!(name_lower, b"nav" | b"header" | b"footer" | b"aside" | b"form")
}
fn should_skip_preprocessing(name_lower: &[u8], attrs: &[(&[u8], Option<&[u8]>)], options: &ConversionOptions) -> bool {
use crate::options::PreprocessingPreset;
if !options.preprocessing.enabled {
return false;
}
if options.preprocessing.preset == PreprocessingPreset::Minimal {
return false;
}
if options.preprocessing.remove_forms && name_lower == b"form" {
return true;
}
if !options.preprocessing.remove_navigation {
return false;
}
if name_lower == b"nav" {
return true;
}
if matches!(name_lower, b"header" | b"footer" | b"aside") {
return byte_attrs_have_navigation_hint(attrs);
}
false
}
fn byte_attrs_have_navigation_hint(attrs: &[(&[u8], Option<&[u8]>)]) -> bool {
if let Some(role) = find_attr(attrs, b"role") {
let role_lc = role.to_ascii_lowercase();
if matches!(role_lc.as_slice(), b"navigation" | b"menubar" | b"tablist" | b"toolbar") {
return true;
}
}
if let Some(label) = find_attr(attrs, b"aria-label") {
let label_lc = label.to_ascii_lowercase();
const ARIA_SUBSTRINGS: &[&[u8]] = &[b"navigation", b"menu", b"contents", b"table of contents", b"toc"];
if ARIA_SUBSTRINGS
.iter()
.any(|sub| label_lc.windows(sub.len()).any(|w| w == *sub))
{
return true;
}
}
for attr_name in [b"class".as_slice(), b"id".as_slice()] {
if let Some(value) = find_attr(attrs, attr_name) {
if byte_value_has_nav_keyword(value) {
return true;
}
}
}
false
}
fn byte_value_has_nav_keyword(value: &[u8]) -> bool {
let mut start = 0;
let len = value.len();
loop {
while start < len && value[start].is_ascii_whitespace() {
start += 1;
}
if start >= len {
break;
}
let mut end = start;
while end < len && !value[end].is_ascii_whitespace() {
end += 1;
}
let token_bytes = &value[start..end];
let mut buf = [0u8; 64];
let normalised: &[u8] = if token_bytes.len() <= buf.len() {
let n = token_bytes.len();
for (i, &b) in token_bytes.iter().enumerate() {
buf[i] = match b {
b'_' | b':' | b'.' | b'/' => b'-',
_ => b.to_ascii_lowercase(),
};
}
&buf[..n]
} else {
start = end;
continue;
};
if NAV_KEYWORDS.iter().any(|kw| kw.as_bytes() == normalised) {
return true;
}
start = end;
}
false
}
fn extract_link_attrs(attrs: &[(&[u8], Option<&[u8]>)]) -> Result<(Option<String>, Option<String>), BailReason> {
let href = find_attr(attrs, b"href").map(decode_attr).transpose()?;
let title = find_attr(attrs, b"title").map(decode_attr).transpose()?;
Ok((href, title))
}
fn extract_ol_start(attrs: &[(&[u8], Option<&[u8]>)]) -> u16 {
find_attr(attrs, b"start")
.and_then(|b| std::str::from_utf8(b).ok())
.and_then(|s| s.parse::<u16>().ok())
.unwrap_or(1)
}
fn decode_attr(bytes: &[u8]) -> Result<String, BailReason> {
let s = std::str::from_utf8(bytes).map_err(|_| BailReason::Classifier)?;
if !s.contains('&') {
return Ok(s.to_owned());
}
let mut out = String::with_capacity(s.len());
decode_entities_into(&mut out, s, 0)?;
Ok(out)
}
fn pop_matching_frame(stack: &mut Vec<OpenTag>, spec: &'static TagSpec) -> Option<OpenTag> {
let top = stack.last()?;
if kinds_match(&top.spec.kind, &spec.kind) {
stack.pop()
} else {
None
}
}
fn kinds_match(a: &TagKind, b: &TagKind) -> bool {
match (a, b) {
(TagKind::List(la), TagKind::List(lb)) => la == lb,
(TagKind::Heading(_), TagKind::Heading(_)) => true,
(TagKind::TableCell { is_header: a_h }, TagKind::TableCell { is_header: b_h }) => a_h == b_h,
_ => std::mem::discriminant(a) == std::mem::discriminant(b),
}
}
fn find_parent_list_kind(stack: &[OpenTag]) -> Option<ListKind> {
for frame in stack.iter().rev() {
if let TagKind::List(kind) = frame.spec.kind {
return Some(kind);
}
}
None
}
fn increment_ol_counter(stack: &mut [OpenTag]) -> u16 {
for frame in stack.iter_mut().rev() {
if frame.spec.kind == TagKind::List(ListKind::Ordered) {
frame.list_index = frame.list_index.saturating_add(1);
return frame.list_index;
}
}
1
}
fn find_ol_start(stack: &[OpenTag]) -> u16 {
for frame in stack.iter().rev() {
if frame.spec.kind == TagKind::List(ListKind::Ordered) {
return frame.ol_start;
}
}
1
}
fn heading_prefix(n: u8) -> &'static str {
let idx = (n as usize).saturating_sub(1).min(5);
HEADING_PREFIXES[idx]
}
fn push_list_item_indent(out: &mut String, depth: u16) {
let idx = depth as usize;
if idx < LIST_ITEM_INDENTS.len() {
out.push_str(LIST_ITEM_INDENTS[idx]);
} else {
out.reserve(idx * 2);
for _ in 0..idx {
out.push_str(" ");
}
}
}
fn prefix_blockquote_lines(content: &str) -> String {
let content = content.trim_end_matches('\n');
if content.is_empty() {
return String::new();
}
let lines: Vec<&str> = content.split('\n').collect();
let mut result = String::with_capacity(content.len() + lines.len() * 2);
for (i, line) in lines.iter().enumerate() {
if line.is_empty() {
result.push('>');
} else {
result.push_str("> ");
result.push_str(line);
}
if i < lines.len() - 1 {
result.push('\n');
}
}
result.push('\n');
result
}
fn indent_pre_lines(raw: &str) -> String {
let raw = raw.strip_prefix('\n').unwrap_or(raw);
let raw = raw.trim_end_matches('\n');
if raw.is_empty() {
return String::new();
}
let min_indent = raw
.lines()
.filter(|line| !line.trim().is_empty())
.map(|line| line.chars().take_while(|c| c.is_whitespace()).count())
.min()
.unwrap_or(0);
let mut result = String::with_capacity(raw.len() + raw.lines().count() * 4);
for line in raw.lines() {
if line.trim().is_empty() {
} else {
result.push_str(" ");
let mut remaining = min_indent;
let mut cut = 0;
for (idx, ch) in line.char_indices() {
if remaining == 0 {
break;
}
if ch.is_whitespace() {
remaining -= 1;
cut = idx + ch.len_utf8();
} else {
break;
}
}
result.push_str(&line[cut..]);
}
result.push('\n');
}
result
}
fn emit_gfm_table(target: &mut String, ts: crate::converter::tier1::state::TableState) {
if let Some(ref caption) = ts.caption_text {
if !caption.is_empty() {
if !target.is_empty() && !target.ends_with("\n\n") {
if target.ends_with('\n') {
target.push('\n');
} else {
target.push_str("\n\n");
}
}
target.push('*');
target.push_str(caption);
target.push_str("*\n\n");
}
}
if ts.rows.is_empty() {
return;
}
if !target.is_empty() && !target.ends_with("\n\n") {
if target.ends_with('\n') {
target.push('\n');
} else {
target.push_str("\n\n");
}
}
let col_count = ts
.rows
.iter()
.map(|r| r.iter().map(|(_, c)| usize::from(*c)).sum::<usize>())
.max()
.unwrap_or(0);
let mut col_widths: Vec<usize> = vec![0; col_count];
for row in &ts.rows {
let mut col = 0usize;
for (cell, span) in row {
let w = cell.chars().count();
if col < col_widths.len() && w > col_widths[col] {
col_widths[col] = w;
}
col += usize::from(*span);
}
}
for (row_index, row) in ts.rows.iter().enumerate() {
target.push('|');
let mut col = 0usize;
for (cell, span) in row {
target.push(' ');
target.push_str(cell);
let cell_len = cell.chars().count();
let col_w = col_widths.get(col).copied().unwrap_or(0);
for _ in cell_len..col_w {
target.push(' ');
}
for _ in 0..*span {
target.push_str(" |");
}
col += usize::from(*span);
}
target.push('\n');
if row_index == 0 {
target.push_str("| ");
for i in 0..col_count.max(1) {
if i > 0 {
target.push_str(" | ");
}
let dash_count = col_widths.get(i).copied().unwrap_or(0).max(MIN_SEPARATOR_DASHES);
for _ in 0..dash_count {
target.push('-');
}
}
target.push_str(" |\n");
}
}
}
fn trim_trailing_inline_whitespace(state: &mut Tier1State) {
let buf = state.cell_or_output_mut();
while buf.ends_with(' ') || buf.ends_with('\t') {
buf.pop();
}
}
fn collapse_excess_blank_lines(output: &mut String) {
let mut consecutive = 0usize;
output.retain(|c| {
if c == '\n' {
consecutive += 1;
consecutive <= 2
} else {
consecutive = 0;
true
}
});
}
fn decode_entity_into(out: &mut String, name: &str) -> bool {
let s: &str = match name {
"amp" => "&",
"lt" => "<",
"gt" => ">",
"quot" => "\"",
"apos" => "'",
"nbsp" => "\u{00A0}",
"copy" => "\u{00A9}",
"reg" => "\u{00AE}",
"trade" => "\u{2122}",
"mdash" => "\u{2014}",
"ndash" => "\u{2013}",
"hellip" => "\u{2026}",
"laquo" => "\u{00AB}",
"raquo" => "\u{00BB}",
"lsquo" => "\u{2018}",
"rsquo" => "\u{2019}",
"ldquo" => "\u{201C}",
"rdquo" => "\u{201D}",
"prime" => "\u{2032}",
"Prime" => "\u{2033}",
"bull" => "\u{2022}",
"middot" => "\u{00B7}",
"deg" => "\u{00B0}",
"plusmn" => "\u{00B1}",
"times" => "\u{00D7}",
"divide" => "\u{00F7}",
"frac12" => "\u{00BD}",
"frac14" => "\u{00BC}",
"frac34" => "\u{00BE}",
"euro" => "\u{20AC}",
"pound" => "\u{00A3}",
"yen" => "\u{00A5}",
"cent" => "\u{00A2}",
"larr" => "\u{2190}",
"rarr" => "\u{2192}",
"uarr" => "\u{2191}",
"darr" => "\u{2193}",
"harr" => "\u{2194}",
"infin" => "\u{221E}",
"alpha" => "\u{03B1}",
"beta" => "\u{03B2}",
"gamma" => "\u{03B3}",
"delta" => "\u{03B4}",
"pi" => "\u{03C0}",
"sigma" => "\u{03C3}",
"omega" => "\u{03C9}",
"iexcl" => "\u{00A1}",
"brvbar" => "\u{00A6}",
"sect" => "\u{00A7}",
"uml" => "\u{00A8}",
"ordf" => "\u{00AA}",
"not" => "\u{00AC}",
"shy" => "\u{00AD}",
"macr" => "\u{00AF}",
"sup2" => "\u{00B2}",
"sup3" => "\u{00B3}",
"acute" => "\u{00B4}",
"micro" => "\u{00B5}",
"para" => "\u{00B6}",
"cedil" => "\u{00B8}",
"sup1" => "\u{00B9}",
"ordm" => "º",
"iquest" => "\u{00BF}",
"Agrave" => "\u{00C0}",
"Aacute" => "\u{00C1}",
"Acirc" => "\u{00C2}",
"Atilde" => "\u{00C3}",
"Auml" => "\u{00C4}",
"Aring" => "\u{00C5}",
"AElig" => "\u{00C6}",
"Ccedil" => "\u{00C7}",
"Egrave" => "\u{00C8}",
"Eacute" => "\u{00C9}",
"Ecirc" => "\u{00CA}",
"Euml" => "\u{00CB}",
"Igrave" => "\u{00CC}",
"Iacute" => "\u{00CD}",
"Icirc" => "\u{00CE}",
"Iuml" => "\u{00CF}",
"ETH" => "\u{00D0}",
"Ntilde" => "\u{00D1}",
"Ograve" => "\u{00D2}",
"Oacute" => "\u{00D3}",
"Ocirc" => "\u{00D4}",
"Otilde" => "\u{00D5}",
"Ouml" => "\u{00D6}",
"Oslash" => "\u{00D8}",
"Ugrave" => "\u{00D9}",
"Uacute" => "\u{00DA}",
"Ucirc" => "\u{00DB}",
"Uuml" => "\u{00DC}",
"Yacute" => "\u{00DD}",
"THORN" => "\u{00DE}",
"szlig" => "\u{00DF}",
"agrave" => "\u{00E0}",
"aacute" => "\u{00E1}",
"acirc" => "\u{00E2}",
"atilde" => "\u{00E3}",
"auml" => "\u{00E4}",
"aring" => "\u{00E5}",
"aelig" => "\u{00E6}",
"ccedil" => "\u{00E7}",
"egrave" => "\u{00E8}",
"eacute" => "\u{00E9}",
"ecirc" => "\u{00EA}",
"euml" => "\u{00EB}",
"igrave" => "\u{00EC}",
"iacute" => "\u{00ED}",
"icirc" => "\u{00EE}",
"iuml" => "\u{00EF}",
"eth" => "\u{00F0}",
"ntilde" => "\u{00F1}",
"ograve" => "\u{00F2}",
"oacute" => "\u{00F3}",
"ocirc" => "\u{00F4}",
"otilde" => "\u{00F5}",
"ouml" => "\u{00F6}",
"oslash" => "\u{00F8}",
"ugrave" => "\u{00F9}",
"uacute" => "\u{00FA}",
"ucirc" => "\u{00FB}",
"uuml" => "\u{00FC}",
"yacute" => "\u{00FD}",
"thorn" => "\u{00FE}",
"yuml" => "\u{00FF}",
_ => return decode_named_entity_fallback(out, name),
};
out.push_str(s);
true
}
fn decode_named_entity_fallback(out: &mut String, name: &str) -> bool {
let name_bytes = name.as_bytes();
if let Ok(index) = html_escape::NAMED_ENTITIES.binary_search_by(|(entity_name, _)| entity_name.cmp(&name_bytes)) {
out.push_str(html_escape::NAMED_ENTITIES[index].1);
return true;
}
decode_numeric_entity_into(out, name)
}
fn decode_numeric_entity_into(out: &mut String, name: &str) -> bool {
let Some(rest) = name.strip_prefix('#') else {
return false;
};
let (digits, radix) = match rest.strip_prefix(['x', 'X']) {
Some(hex) => (hex, 16),
None => (rest, 10),
};
let Ok(code_point) = crate::text::parse_character_reference_number(digits, radix) else {
return false;
};
if let Some(replacement) = crate::text::numeric_character_reference_override(code_point) {
out.push(replacement);
return true;
}
match u32::try_from(code_point).ok().and_then(char::from_u32) {
Some(ch) => {
out.push(ch);
true
}
None => false,
}
}
fn skip_bang(bytes: &[u8], pos: usize) -> Result<usize, BailReason> {
let start = pos + 2;
if bytes.get(start) == Some(&b'-') && bytes.get(start + 1) == Some(&b'-') {
let comment_start = start + 2;
let mut i = comment_start;
while i + 2 < bytes.len() {
if bytes[i] == b'-' && bytes[i + 1] == b'-' && bytes[i + 2] == b'>' {
return Ok(i + 3);
}
i += 1;
}
return Err(BailReason::LiteralLt { offset: pos });
}
let mut i = start;
while i < bytes.len() {
if bytes[i] == b'>' {
return Ok(i + 1);
}
i += 1;
}
Err(BailReason::LiteralLt { offset: pos })
}
fn lowercase_into<'b>(bytes: &[u8], buf: &'b mut [u8; MAX_TAG_NAME_BYTES]) -> &'b [u8] {
let len = bytes.len().min(MAX_TAG_NAME_BYTES);
for (i, &b) in bytes[..len].iter().enumerate() {
buf[i] = b.to_ascii_lowercase();
}
&buf[..len]
}
fn bytes_to_string(b: &[u8]) -> String {
String::from_utf8_lossy(b).into_owned()
}
fn upcoming_open_tag_name<'b>(bytes: &[u8], lt_pos: usize, buf: &'b mut [u8; MAX_TAG_NAME_BYTES]) -> Option<&'b [u8]> {
if bytes.get(lt_pos) != Some(&b'<') {
return None;
}
let &next = bytes.get(lt_pos + 1)?;
if !parse::is_tag_name_start(next) {
return None;
}
let name_start = lt_pos + 1;
let name_end = parse::scan_tag_name(bytes, name_start);
Some(lowercase_into(&bytes[name_start..name_end], buf))
}
fn trailing_single_newline_join(state: &Tier1State, next_tag_is_span: bool) -> &'static str {
if next_tag_is_span {
return "";
}
let block_start = clamp_to_char_boundary(&state.output, nearest_block_content_start(state));
if state.output[block_start..].ends_with("\n\n") {
return "";
}
let in_paragraph_or_inline_wrapper = state.stack.iter().any(|frame| {
matches!(
frame.spec.kind,
TagKind::Paragraph | TagKind::Strong | TagKind::Emphasis
)
});
if in_paragraph_or_inline_wrapper { " " } else { "\n" }
}
fn nearest_block_content_start(state: &Tier1State) -> usize {
state
.stack
.iter()
.rev()
.find(|frame| matches!(frame.spec.kind, TagKind::Paragraph | TagKind::Block))
.map_or(0, |frame| frame.content_start)
}
fn upcoming_tag_is_list_open(bytes: &[u8], lt_pos: usize) -> bool {
let mut name_buf = [0u8; MAX_TAG_NAME_BYTES];
matches!(
upcoming_open_tag_name(bytes, lt_pos, &mut name_buf),
Some(b"ul" | b"ol")
)
}
fn upcoming_tag_is_named(bytes: &[u8], lt_pos: usize, name: &[u8]) -> bool {
let mut name_buf = [0u8; MAX_TAG_NAME_BYTES];
upcoming_open_tag_name(bytes, lt_pos, &mut name_buf) == Some(name)
}