fn inline_break_starts_paragraph(state: &Tier1State, buffer_len: usize) -> bool {
let Some((paragraph_index, paragraph_start)) = state
.stack
.iter()
.enumerate()
.rev()
.find(|(_, frame)| matches!(frame.spec.kind, TagKind::Paragraph))
.map(|(index, frame)| (index, frame.content_start))
else {
return false;
};
let mut expected_start = paragraph_start;
let mut found_inline_buffer = false;
for frame in state.stack[paragraph_index + 1..]
.iter()
.filter(|frame| frame.own_buffer)
{
let marker_width = emitted_inline_marker_width(state, frame);
if frame.content_start.checked_sub(marker_width) != Some(expected_start) {
return false;
}
expected_start = frame.content_start;
found_inline_buffer = true;
}
found_inline_buffer && expected_start == buffer_len
}
fn emitted_inline_marker_width(state: &Tier1State, frame: &OpenTag) -> usize {
match frame.spec.kind {
TagKind::Strong
if !state.summary_at_top()
&& !frame
.prev_escape_ctx
.intersects(EscapeCtx::STRONG | EscapeCtx::CODE | EscapeCtx::PRE) =>
{
2
}
TagKind::Emphasis if !frame.prev_escape_ctx.intersects(EscapeCtx::CODE | EscapeCtx::PRE) => 1,
TagKind::Strikethrough | TagKind::Inserted
if !frame.prev_escape_ctx.intersects(EscapeCtx::CODE | EscapeCtx::PRE) =>
{
2
}
_ => 0,
}
}
fn emit_void(
state: &mut Tier1State,
spec: &'static TagSpec,
name_lower: &[u8],
attrs: &[(&[u8], Option<&[u8]>)],
html: &str,
options: &ConversionOptions,
) -> Result<(), BailReason> {
state.last_closed_custom_element = false;
let input_type = (name_lower == b"input").then(|| find_attr(attrs, b"type").unwrap_or_default());
if input_type.is_some_and(|input_type| input_type.eq_ignore_ascii_case(b"checkbox")) {
if state
.stack
.iter()
.any(|frame| matches!(frame.spec.kind, TagKind::ListItem))
{
return Err(BailReason::ListItemCheckbox);
}
return Err(BailReason::InlineMarkerNotReproduced);
}
state.last_emitted_was_img = false;
if let Some(join_end) = state.pending_newline_join.take() {
let dest = state.cell_or_output_mut();
if matches!(spec.kind, TagKind::LineBreak) && join_end == dest.len() {
dest.pop();
}
}
if std::mem::take(&mut state.last_closed_block)
&& is_inline_tag(name_lower)
&& !(state.in_table_cell() && matches!(spec.kind, TagKind::LineBreak))
{
separate_inline_after_block(state, options.br_in_tables)?;
}
state.last_closed_block = is_block_tag(name_lower);
match spec.kind {
TagKind::Hr => emit_hr(state)?,
TagKind::LineBreak => emit_line_break(state, options)?,
TagKind::Image => emit_image(state, attrs, html, options)?,
TagKind::Ignored | TagKind::Inline | TagKind::Block => {}
_ => {}
}
Ok(())
}
fn emit_hr(state: &mut Tier1State) -> Result<(), BailReason> {
if state.in_summary()
|| state.in_table_caption()
|| state.stack.iter().any(|frame| {
matches!(
frame.spec.kind,
TagKind::Strong | TagKind::Emphasis | TagKind::Strikethrough | TagKind::Inserted
)
})
{
return Err(BailReason::RuleBetweenInlineMarkers);
}
if state.escape_ctx.contains(EscapeCtx::HEADING) && !state.link_stack.is_empty() {
return Err(BailReason::Classifier);
}
if !state.in_table_cell() && state.list_continuation_indent_width() > 0 && !inside_stray_definition(state) {
if !line_is_bare_list_marker(&state.output) {
return Err(BailReason::ListItemUnsupportedBlockChild);
}
state.output.push_str("___\n");
return Ok(());
}
let in_cell = state.in_table_cell();
let write_rule = |dest: &mut String| {
if in_cell {
crate::converter::main_helpers::trim_trailing_whitespace(dest);
}
if !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("---\n");
};
if in_cell {
with_cell_scratch(state, write_rule);
} else {
write_rule(state.cell_or_output_mut());
}
Ok(())
}
fn emit_line_break(state: &mut Tier1State, options: &ConversionOptions) -> Result<(), BailReason> {
let in_code = state.escape_ctx.contains(EscapeCtx::CODE);
let in_pre = state.escape_ctx.contains(EscapeCtx::PRE);
if in_code && !in_pre && !state.in_table_cell() && !state.escape_ctx.contains(EscapeCtx::HEADING) {
let start = state
.stack
.iter()
.rev()
.find(|frame| matches!(frame.spec.kind, TagKind::Code))
.map(|frame| frame.content_start);
if start.is_some_and(|start| {
state.cell_or_output_mut().get(start..).is_some_and(|content| {
content
.chars()
.all(crate::converter::utility::white_space::is_collapsible)
})
}) {
return Err(BailReason::Classifier);
}
}
let label_start = (!in_code)
.then(|| {
state
.stack
.iter()
.rev()
.find(|frame| matches!(frame.spec.kind, TagKind::Link))
})
.flatten()
.map(|frame| frame.content_start);
if let Some(label_start) = label_start {
emit_label_line_break(state, label_start, options);
} else if in_code && !in_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 in_pre {
emit_pre_line_break(state, options.br_in_tables);
} else if state.in_table_cell() {
emit_cell_line_break(state, options.br_in_tables, in_code);
} else if in_code {
state.cell_or_output_mut().push('\n');
} else {
emit_regular_line_break(state, options);
}
Ok(())
}
fn emit_label_line_break(state: &mut Tier1State, label_start: usize, options: &ConversionOptions) {
let in_heading = state.escape_ctx.contains(EscapeCtx::HEADING);
let in_table_cell = state.in_table_cell();
let dest = state.cell_or_output_mut();
let label_start = clamp_to_char_boundary(dest, label_start);
if in_heading {
crate::converter::main_helpers::trim_trailing_whitespace(dest);
dest.push(' ');
} else if in_table_cell {
if dest.len() == label_start && !options.br_in_tables {
dest.push(' ');
} else {
crate::converter::main_helpers::trim_trailing_whitespace(dest);
if options.br_in_tables {
dest.push_str("<br>");
} else if dest.len() > label_start {
dest.push(' ');
}
}
} else {
dest.push_str(crate::converter::main_helpers::hard_break_marker(options));
}
}
fn emit_pre_line_break(state: &mut Tier1State, br_in_tables: bool) {
if state.in_table_cell() && br_in_tables {
let offset = state.cell_or_output_mut().len();
state.pre_cell_break_offsets.push(offset);
}
state.cell_or_output_mut().push('\n');
}
fn emit_cell_line_break(state: &mut Tier1State, br_in_tables: bool, in_code: bool) {
let emit_literal_br = br_in_tables && !in_code;
let cell_is_empty = state.cell_or_output_mut().is_empty();
with_cell_scratch(state, |dest| {
crate::converter::main_helpers::trim_trailing_whitespace(dest);
if emit_literal_br {
dest.push_str("<br>");
} else if !cell_is_empty {
dest.push(' ');
}
});
}
fn emit_regular_line_break(state: &mut Tier1State, options: &ConversionOptions) {
let paragraph_start = state
.stack
.iter()
.rev()
.find(|frame| matches!(frame.spec.kind, TagKind::Paragraph))
.map(|frame| frame.content_start);
let buffer_len = state.cell_or_output_mut().len();
let starts_in_inline_buffer = inline_break_starts_paragraph(state, buffer_len);
let dest = state.cell_or_output_mut();
if paragraph_start != Some(dest.len()) && !starts_in_inline_buffer {
crate::converter::main_helpers::trim_trailing_whitespace(dest);
dest.push_str(crate::converter::main_helpers::hard_break_marker(options));
}
}
fn emit_image(
state: &mut Tier1State,
attrs: &[(&[u8], Option<&[u8]>)],
html: &str,
options: &ConversionOptions,
) -> Result<(), BailReason> {
if options.skip_images {
state.last_emitted_was_img = true;
return Ok(());
}
let raw_src = find_attr(attrs, b"src").unwrap_or_default();
if image_uses_lazy_source(attrs, raw_src) {
return Err(BailReason::ImageLazyLoadSrc);
}
let src = decode_attr(raw_src)?;
if src.trim().is_empty() {
return Err(BailReason::ImageMissingSource);
}
let src = state.resolve_url(&src).unwrap_or(src);
let alt = decode_attr(find_attr(attrs, b"alt").unwrap_or_default())?;
if state.escape_ctx.contains(EscapeCtx::CODE) {
return emit_image_in_code(state, &src, &alt, options);
}
let title = find_attr(attrs, b"title")
.map(decode_attr)
.transpose()?
.map(|title| crate::converter::inline::link::escape_markdown_title(&title).into_owned());
let keep_as_markdown = should_keep_image_as_markdown(html, &state.stack, options);
let dest = state.cell_or_output_mut();
let written_from = dest.len();
if keep_as_markdown {
emit_markdown_image(dest, &src, &alt, title.as_deref(), options);
} else {
dest.push_str(&alt);
}
state.end_document_start_if_written(written_from);
state.last_emitted_was_img = true;
Ok(())
}
fn emit_image_in_code(
state: &mut Tier1State,
src: &str,
alt: &str,
options: &ConversionOptions,
) -> Result<(), BailReason> {
if alt.contains(['\n', '\r']) {
return Err(BailReason::Classifier);
}
let treatment = crate::converter::media::inline_data_treatment(options.inline_data_media, src);
if !options.skip_images && treatment != crate::options::InlineDataMedia::DropElement && !alt.is_empty() {
let dest = state.cell_or_output_mut();
let written_from = dest.len();
dest.push_str(alt);
state.end_document_start_if_written(written_from);
}
state.last_emitted_was_img = true;
Ok(())
}
fn image_uses_lazy_source(attrs: &[(&[u8], Option<&[u8]>)], src: &[u8]) -> bool {
let trimmed = src.trim_ascii();
let is_placeholder = trimmed.is_empty()
|| trimmed
.get(..5)
.is_some_and(|scheme| scheme.eq_ignore_ascii_case(b"data:"));
is_placeholder
&& [
b"data-src".as_slice(),
b"data-lazy-src",
b"data-original",
b"data-srcset",
b"srcset",
]
.iter()
.any(|name| find_attr(attrs, name).is_some())
}
fn emit_markdown_image(dest: &mut String, src: &str, alt: &str, title: Option<&str>, options: &ConversionOptions) {
let escaped_alt = crate::converter::utility::escaping::escape_image_alt(alt);
dest.push_str(";
crate::converter::inline::link::append_url_destination(dest, src, options.url_escape_style, title.is_some());
if let Some(title) = title {
dest.push_str(" \"");
dest.push_str(title);
dest.push('"');
}
dest.push(')');
}
#[inline]
#[allow(clippy::missing_const_for_fn)]
fn should_keep_image_as_markdown(html: &str, stack: &[OpenTag], options: &ConversionOptions) -> bool {
keep_inline_image_for_ancestors(html.as_bytes(), stack, &options.keep_inline_images_in)
}
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
}
const 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<Option<TextPlace>, 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);
if matches!(spec.kind, TagKind::DefinitionTerm | TagKind::DefinitionDescription) {
trim_start_of_cell_content(state, frame.content_start)?;
}
check_whitespace_led_block_in_cell(state, &frame)?;
dispatch_close(state, spec, &frame, name_lower, options, table_probes)?;
let breaks_after = is_block_tag(name_lower) && !(matches!(spec.kind, TagKind::Table) && state.in_table_cell());
state.last_closed_block = breaks_after || (state.last_closed_block && is_inline_tag(name_lower));
let holds_text = state.text_count > frame.texts_before;
let in_text_flow =
std::str::from_utf8(name_lower).is_ok_and(crate::converter::utility::white_space::writes_in_text_flow);
Ok((holds_text && !in_text_flow).then_some(TextPlace::AfterText))
}
fn dispatch_close(
state: &mut Tier1State,
spec: &TagSpec,
frame: &OpenTag,
name_lower: &[u8],
options: &ConversionOptions,
table_probes: &mut Vec<TableLayoutProbe>,
) -> Result<(), BailReason> {
match spec.kind {
TagKind::Paragraph => close_paragraph(state),
TagKind::Heading(n) => close_heading(state, frame, n, false, options)?,
TagKind::Blockquote => close_blockquote(state, frame, options),
TagKind::Pre => close_pre(state, frame, options),
TagKind::Strong if suppress_close_marker(state, EscapeCtx::STRONG, true) => {}
TagKind::Strong => close_inline_marker(state, frame, "**")?,
TagKind::Emphasis if suppress_close_marker(state, EscapeCtx::empty(), false) => {}
TagKind::Emphasis => close_inline_marker(state, frame, "*")?,
TagKind::Strikethrough if suppress_close_marker(state, EscapeCtx::empty(), false) => {}
TagKind::Strikethrough => close_inline_marker(state, frame, "~~")?,
TagKind::Inserted if suppress_close_marker(state, EscapeCtx::empty(), false) => {}
TagKind::Inserted => {}
TagKind::Code => close_code(state, frame, matches!(name_lower, b"kbd" | b"samp" | b"tt"), options)?,
TagKind::Link => close_link(state, frame, options)?,
TagKind::List(ListKind::Definition) => close_dl(state, frame, options),
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::Table => close_table(state, options, table_probes)?,
TagKind::TableHead => close_table_head(state),
TagKind::TableBody => close_table_body(state),
TagKind::TableRow => close_table_row(state),
TagKind::TableCell { .. } => close_table_cell(state, false, options)?,
TagKind::TableCaption => close_table_caption(state),
TagKind::Block if figure_joins_an_image(state, frame, name_lower) => return Err(BailReason::Classifier),
TagKind::Block => close_block_container(state, frame, name_lower, options),
TagKind::Summary => close_summary(state, frame),
TagKind::Figcaption => close_figcaption(state, frame),
TagKind::LineEndControl => close_line_end_control(state, frame),
TagKind::Inline if name_lower == b"abbr" => close_abbreviation(state, frame),
TagKind::Hr
| TagKind::TableFoot
| TagKind::Inline
| TagKind::LineBreak
| TagKind::Image
| TagKind::Media
| TagKind::RawText(_)
| TagKind::Ignored => {}
}
Ok(())
}
fn suppress_close_marker(state: &Tier1State, nested_context: EscapeCtx, suppress_in_summary: bool) -> bool {
(suppress_in_summary && state.summary_at_top())
|| (!nested_context.is_empty() && state.escape_ctx.contains(nested_context))
|| state.escape_ctx.intersects(EscapeCtx::CODE | EscapeCtx::PRE)
}
fn close_abbreviation(state: &mut Tier1State, frame: &OpenTag) {
let Some(Some(title)) = state.abbr_titles.pop() else {
return;
};
let dest = state.cell_or_output_mut();
let content_start = clamp_to_char_boundary(dest, frame.content_start);
let trailing_start = content_start + dest[content_start..].trim_end().len();
let trailing = dest[trailing_start..].to_owned();
dest.truncate(trailing_start);
dest.push_str(" (");
dest.push_str(&title);
dest.push(')');
dest.push_str(&trailing);
}
fn figure_joins_an_image(state: &mut Tier1State, frame: &OpenTag, name_lower: &[u8]) -> bool {
name_lower == b"figure"
&& state
.cell_or_output_mut()
.get(frame.content_start..)
.is_some_and(|content| content.contains(" ![") || content.contains("\n!["))
}
fn close_block_container(state: &mut Tier1State, frame: &OpenTag, name_lower: &[u8], options: &ConversionOptions) {
if block_container_is_passthrough(name_lower) || is_transparent_plain_form(name_lower, options) {
return;
}
if state.in_table_cell() || start_line_in_pre(state, name_lower) {
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");
}
}
if trimmed.contains("\n\n") && crate::converter::inline::wrapped::block_runs_are_plain(trimmed) {
dest.push_str(&crate::converter::inline::wrapped::wrap_block_runs(trimmed, "**", "**"));
} else {
dest.push_str("**");
dest.push_str(trimmed);
dest.push_str("**");
}
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_line_end_control(state: &mut Tier1State, frame: &OpenTag) {
let dest = state.cell_or_output_mut();
let content_start = clamp_to_char_boundary(dest, frame.content_start);
crate::converter::form::spacing::ControlStart::at(dest, content_start).finish(dest);
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 indent_width = state.list_continuation_indent_width();
let buf = state.cell_or_output_mut();
let mut content_start = clamp_to_char_boundary(buf, frame.content_start);
normalize_marker_leading_break(buf, &mut content_start, marker, indent_width);
if handle_empty_inline_body(buf, content_start, frame, marker)? {
return Ok(());
}
if wrap_inline_block_runs(buf, content_start, marker) {
return Ok(());
}
content_start = migrate_leading_inline_whitespace(buf, content_start, marker);
if migrate_trailing_inline_whitespace(buf, content_start, marker) {
return Ok(());
}
buf.push_str(marker);
Ok(())
}
fn normalize_marker_leading_break(buf: &mut String, content_start: &mut usize, marker: &str, indent_width: usize) {
if !buf[*content_start..].starts_with(" \n") {
return;
}
let marker_start = clamp_to_char_boundary(buf, content_start.saturating_sub(marker.len()));
let indent = std::iter::repeat_n(' ', indent_width).collect::<String>();
let mut replacement = String::with_capacity(marker.len() + indent.len() * 2 + 3);
if buf[..marker_start].ends_with('\n') {
replacement.push_str(&indent);
replacement.push_str("\\\n");
} else {
replacement.push_str(" \n");
}
replacement.push_str(&indent);
replacement.push_str(marker);
let mut body_start = *content_start + 3;
while matches!(buf.as_bytes().get(body_start), Some(b' ' | b'\t')) {
body_start += 1;
}
buf.replace_range(marker_start..body_start, &replacement);
*content_start = marker_start + replacement.len();
}
fn handle_empty_inline_body(
buf: &mut String,
content_start: usize,
frame: &OpenTag,
marker: &str,
) -> Result<bool, BailReason> {
let content_absent = buf.len() <= content_start;
let is_marks = matches!(marker, "**" | "*" | "~~" | "==");
let whitespace_only = !content_absent
&& if is_marks {
buf[content_start..]
.trim_start_matches([' ', '\t'])
.trim_start_matches("\\\n")
.trim_end_matches(" \n")
.trim_end_matches("\\\n")
.chars()
.all(char::is_whitespace)
} else {
buf[content_start..]
.bytes()
.all(|byte| matches!(byte, b' ' | b'\t' | b'\n' | b'\r'))
};
if !content_absent && !whitespace_only {
return Ok(false);
}
let was_whitespace_only = whitespace_only || (content_absent && frame.dropped_whitespace_only_text);
if was_whitespace_only && is_marks {
return Err(BailReason::WhitespaceOnlyInlineEmphasis);
}
let marker_start = clamp_to_char_boundary(buf, content_start.saturating_sub(marker.len()));
buf.truncate(marker_start);
Ok(true)
}
fn wrap_inline_block_runs(buf: &mut String, content_start: usize, marker: &str) -> bool {
let content = &buf[content_start..];
if !content.contains("\n\n") || !crate::converter::inline::wrapped::block_runs_are_plain(content) {
return false;
}
let content = content.to_owned();
let marker_start = clamp_to_char_boundary(buf, content_start.saturating_sub(marker.len()));
buf.truncate(marker_start);
buf.push_str(&crate::converter::inline::wrapped::wrap_block_runs(
&content, marker, marker,
));
true
}
fn migrate_leading_inline_whitespace(buf: &mut String, content_start: usize, marker: &str) -> usize {
let content = &buf[content_start..];
let leading_len = content.len() - content.trim_start().len();
if leading_len == 0 {
return content_start;
}
let leading = content[..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()));
let owed = crate::converter::utility::white_space::owed_part(&buf[..marker_start], &leading).len();
buf.insert_str(marker_start, &leading[leading_len - owed..]);
content_start + owed
}
fn migrate_trailing_inline_whitespace(buf: &mut String, content_start: usize, marker: &str) -> bool {
let content = &buf[content_start..];
let trailing_len = content.len() - content.trim_end().len();
if trailing_len == 0 {
return false;
}
let trailing_start = buf.len() - trailing_len;
let trailing = buf[trailing_start..].to_owned();
buf.truncate(trailing_start);
buf.push_str(marker);
buf.push_str(&trailing);
true
}