1use crate::ast::*;
2use crate::carrier_markers::{
3 carrier_line, carrier_line_at, spell_carrier_markers, CarrierMarkers,
4};
5use crate::extension::Options;
6use crate::parse::unwrap_nested_anchors;
7use crate::render_loss::{DeniedSink, MarkdownLoss, RawNodeType};
8use crate::render_text::{strip_high_controls as strip_control_chars, trim_non_nbsp};
9use std::collections::HashMap;
10use std::collections::HashSet;
11use std::fmt::Write as _;
12use std::rc::Rc;
13
14use crate::render::MAX_RENDER_DEPTH;
15
16#[derive(Debug, Clone, PartialEq, Eq)]
18pub enum MarkdownRenderError {
19 Depth(crate::RenderDepthError),
21 SourceUnspellable(crate::SourceUnspellable),
23}
24
25impl std::fmt::Display for MarkdownRenderError {
26 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27 match self {
28 Self::Depth(error) => error.fmt(f),
29 Self::SourceUnspellable(error) => error.fmt(f),
30 }
31 }
32}
33
34impl std::error::Error for MarkdownRenderError {}
35
36impl From<crate::RenderDepthError> for MarkdownRenderError {
37 fn from(error: crate::RenderDepthError) -> Self {
38 Self::Depth(error)
39 }
40}
41
42impl From<crate::RenderCarveError> for MarkdownRenderError {
43 fn from(error: crate::RenderCarveError) -> Self {
44 match error {
45 crate::RenderCarveError::Depth(error) => Self::Depth(error),
46 crate::RenderCarveError::SourceUnspellable(error) => Self::SourceUnspellable(error),
47 }
48 }
49}
50
51impl From<MarkdownRenderError> for crate::RenderCarveError {
52 fn from(error: MarkdownRenderError) -> Self {
53 match error {
54 MarkdownRenderError::Depth(error) => Self::Depth(error),
55 MarkdownRenderError::SourceUnspellable(error) => Self::SourceUnspellable(error),
56 }
57 }
58}
59
60impl From<MarkdownRenderError> for crate::CarveWriteError {
61 fn from(error: MarkdownRenderError) -> Self {
62 Self::Render(error.into())
63 }
64}
65
66fn trim_block_output(s: &str) -> &str {
67 s.trim_matches(|c| c == '\n' || c == ' ')
68}
69
70thread_local! {
71 static SMART_TYPOGRAPHY: std::cell::Cell<crate::extension::SmartTypographyMode> =
75 const { std::cell::Cell::new(crate::extension::SmartTypographyMode::Glyph) };
76}
77
78fn smart_punctuation_text(node: &crate::ast::SmartPunctuation) -> &str {
79 if SMART_TYPOGRAPHY
80 .with(std::cell::Cell::get)
81 .uses_source(&node.kind)
82 {
83 return &node.value;
84 }
85
86 smart_punctuation_glyph(node)
87}
88
89pub fn render_markdown_with_options(
96 doc: &Document,
97 options: &Options<'_>,
98) -> Result<String, MarkdownRenderError> {
99 crate::render_depth::refuse_if_too_deep(doc, "markdown")?;
100 let watch = crate::render_depth::RenderDepthWatch::new();
101 let previous_error = CARRIER_WRITE_ERROR.with(|cell| cell.replace(None));
102 let output = render_markdown_inner(
103 doc,
104 options.smart_typography,
105 options.heading_id_options(),
106 options.roundtrip,
107 );
108 let carrier_error = CARRIER_WRITE_ERROR.with(|cell| cell.replace(previous_error));
109 let output = watch.into_result(output)?;
110 carrier_error.map_or(Ok(output), |error| Err(error.into()))
111}
112
113pub fn render_markdown(doc: &Document) -> Result<String, crate::RenderDepthError> {
120 crate::render_depth::refuse_if_too_deep(doc, "markdown")?;
121 let watch = crate::render_depth::RenderDepthWatch::new();
122 watch.into_result(render_markdown_inner(
123 doc,
124 crate::extension::SmartTypographyMode::Glyph,
125 Options::default().heading_id_options(),
126 false,
127 ))
128}
129
130fn render_markdown_inner(
147 doc: &Document,
148 smart_typography: crate::extension::SmartTypographyMode,
149 id_opts: crate::extension::HeadingIdOptions,
150 roundtrip: bool,
151) -> String {
152 let _carriers = CarrierGuard::install_defaults();
153 let losses = crate::render_loss::checkpoint();
154 let first = render_markdown_once(doc, smart_typography, id_opts, roundtrip);
155 let inserted = INSERTED.with(std::cell::Cell::get);
156 let seen = SEEN.with(std::cell::Cell::get);
157 if (0..CARRIER_COUNT).all(|slot| seen[slot] <= inserted[slot]) {
158 return first;
159 }
160
161 if OCCUPIED.with(|occupied| marker_pool_needs_tokens(&occupied.borrow())) {
162 OCCUPIED_TOKEN_NONCES.with(|nonces| {
163 crate::sentinel_run::collect_marker_nonce_cores(&first, &mut nonces.borrow_mut());
164 });
165 }
166 let picked = OCCUPIED.with(|occupied| {
167 let occupied = occupied.borrow();
168 let chars: [char; CARRIER_COUNT] =
169 crate::sentinel_run::pick_sentinel_run(&occupied, u32::from(CARRIER_DEFAULTS[0]));
170 if chars
171 .iter()
172 .all(|marker| !occupied.contains(&u32::from(*marker)))
173 {
174 return chars.map(|marker| Rc::new(marker.to_string()));
175 }
176 let nonce = OCCUPIED_TOKEN_NONCES.with(|nonces| {
180 let nonces = nonces.borrow();
181 (0..=nonces.len())
182 .find(|nonce| !nonces.contains(nonce))
183 .unwrap()
184 });
185 std::array::from_fn(|index| Rc::new(format!("§m{nonce}:{index}§")))
186 });
187 let _picked_carriers = CarrierGuard::install(picked);
188 losses.restore();
189 render_markdown_once(doc, smart_typography, id_opts, roundtrip)
190}
191
192fn render_markdown_once(
193 doc: &Document,
194 smart_typography: crate::extension::SmartTypographyMode,
195 id_opts: crate::extension::HeadingIdOptions,
196 roundtrip: bool,
197) -> String {
198 INLINE_CARRIER_PATTERN.with(|pattern| {
199 *pattern.borrow_mut() = None;
200 });
201 if roundtrip {
202 let authored = crate::ast_json::to_json(doc);
203 let mut nonce = 0;
204 let prefix = loop {
205 let prefix = format!("§carve-inline-{nonce}§");
206 if !authored.contains(&prefix) {
207 break prefix;
208 }
209 nonce += 1;
210 };
211 INLINE_CARRIER_PATTERN.with(|pattern| {
212 *pattern.borrow_mut() = Some(
213 regex::Regex::new(&format!(
214 "{}(<!--[\\s\\S]*?-->){}",
215 regex::escape(&prefix),
216 regex::escape(&prefix)
217 ))
218 .unwrap(),
219 );
220 });
221 INLINE_CARRIER_PREFIX.with(|current| {
222 *current.borrow_mut() = prefix;
223 });
224 }
225 INSERTED.with(|counts| counts.set([0; CARRIER_COUNT]));
226 SEEN.with(|counts| counts.set([0; CARRIER_COUNT]));
227 IN_LINK_TEXT.with(|d| d.set(0));
228 OCCUPIED.with(|occupied| occupied.borrow_mut().clear());
229 OCCUPIED_TOKEN_NONCES.with(|nonces| nonces.borrow_mut().clear());
230 SMART_TYPOGRAPHY.with(|cell| cell.set(smart_typography));
231 let _abbr_guard = crate::abbr_budget::AbbrBudgetGuard::for_document(doc);
232 let crossref_index = crate::parse::crossref_index_for_document(doc, id_opts);
233 let mut explicit_ids = HashSet::new();
239 let mut explicit_pass = |block: &BlockNode, _: Option<&[InlineNode]>| {
240 if let BlockNode::Heading(heading) = block {
241 if let Some(id) = heading.attrs.as_ref().and_then(|attrs| attrs.id.as_ref()) {
242 explicit_ids.insert(id.clone());
243 }
244 }
245 };
246 walk_blocks(&doc.children, 0, &mut explicit_pass);
247 for body in doc.footnote_defs.values() {
248 walk_blocks(body, 0, &mut explicit_pass);
249 }
250
251 let mut id_counts = std::collections::BTreeMap::new();
252 let mut slugger = GfmSlugger::default();
253 let mut heading_slugs: HashMap<String, String> = HashMap::new();
254 let mut heading_pass = |heading: &Heading, written: bool| {
255 let id = next_heading_id(heading, &mut id_counts, &explicit_ids);
256 if written {
257 let slug = slugger.slug(&gfm_heading_text(&heading.children));
258 heading_slugs.entry(id).or_insert(slug);
259 }
260 };
261 walk_headings(&doc.children, 0, true, &mut heading_pass);
262 for (_, body) in crate::ast_json::footnote_defs_in_source_order(doc) {
263 walk_headings(body, 0, true, &mut heading_pass);
264 }
265
266 let referenced_links = referenced_link_labels(doc);
267 let native_references = if roundtrip {
268 native_reference_definitions(doc)
269 } else {
270 HashMap::new()
271 };
272 let mut ctx = MarkdownContext {
273 referenced_links,
274 native_references,
275 suppress_automatic_abbreviation: false,
276 heading_slugs,
277 list_depth: 0,
278 thematic_glued: false,
279 in_footnote: false,
280 footnote_allowed_lists: HashSet::new(),
281 list_item_opening_list: None,
282 defined_footnotes: doc.footnote_defs.keys().cloned().collect(),
283 crossref_index,
284 link_depth: 0,
285 single_line_depth: 0,
286 previous_list: None,
287 roundtrip,
288 carrier_depth: 0,
289 definition_depth: 0,
290 delimiter_ancestors: Vec::new(),
291 rendered_children: HashMap::new(),
292 };
293 let out: String =
294 crate::render_text::blocks_with_link_definitions_in_source_order(&doc.children)
295 .into_iter()
296 .map(|block| render_block(block, &mut ctx, 0))
297 .collect();
298 let footnotes = render_footnote_defs(doc, &mut ctx);
299 let body = normalize(&format!("{out}{footnotes}"));
300 let body = INLINE_CARRIER_PATTERN.with(|pattern| {
301 pattern
302 .borrow()
303 .as_ref()
304 .map(|pattern| pattern.replace_all(&body, "$1").into_owned())
305 .unwrap_or(body)
306 });
307 match render_frontmatter(doc) {
308 None => body,
309 Some(frontmatter) if body == "\n" => format!("{frontmatter}\n"),
312 Some(frontmatter) => format!("{frontmatter}\n\n{body}"),
313 }
314}
315
316fn render_frontmatter(doc: &Document) -> Option<String> {
327 let strip = |text: &str| -> String {
328 text.chars()
329 .filter(|c| !crate::escape::is_bidi_control(*c))
330 .collect()
331 };
332 if let Some(raw) = &doc.frontmatter_raw {
333 let format = if raw.format == "yaml" {
334 ""
335 } else {
336 raw.format.as_str()
337 };
338 return Some(format!("---{format}\n{}\n---", strip(&raw.content)));
339 }
340 if doc.frontmatter.is_empty() {
341 return None;
342 }
343 let mut out = String::from("---");
344 for (key, value) in &doc.frontmatter {
345 out.push('\n');
346 out.push_str(&strip(key));
347 out.push_str(": ");
348 out.push_str(&strip(value));
349 }
350 out.push_str("\n---");
351 Some(out)
352}
353
354pub(crate) fn referenced_link_labels(doc: &Document) -> HashSet<String> {
355 use crate::include_walk::{visit_block_children, visit_inline_children, SubtreeVisitor};
356 struct Labels(HashSet<String>);
357 impl SubtreeVisitor for Labels {
358 fn blocks(&mut self, blocks: &mut Vec<BlockNode>) {
359 for block in blocks {
360 visit_block_children(block, self);
361 }
362 }
363 fn inlines(&mut self, inlines: &mut Vec<InlineNode>) {
364 for inline in inlines {
365 if let InlineNode::Link(link) = inline {
366 if !crate::parse::is_unresolved_reference(link) {
367 if let Some(label) = &link.ref_label {
368 self.0.insert(crate::parse::label_key(label));
369 }
370 }
371 }
372 visit_inline_children(inline, self);
373 }
374 }
375 fn image(&mut self, image: &mut Image) {
376 if !crate::parse::is_unresolved_image(image) {
377 if let Some(label) = &image.ref_label {
378 self.0.insert(crate::parse::label_key(label));
379 }
380 }
381 }
382 }
383 if !doc
384 .children
385 .iter()
386 .any(|block| matches!(block, BlockNode::LinkReferenceDefinition(_)))
387 {
388 return HashSet::new();
389 }
390 let mut labels = Labels(HashSet::new());
391 let mut document = doc.clone();
392 labels.blocks(&mut document.children);
393 for body in document.footnote_defs.values_mut() {
394 labels.blocks(body);
395 }
396 labels.0
397}
398
399fn native_reference_definitions(
400 doc: &Document,
401) -> HashMap<String, crate::ast::LinkReferenceDefinition> {
402 let mut groups: HashMap<String, Vec<&crate::ast::LinkReferenceDefinition>> = HashMap::new();
403 for block in &doc.children {
404 if let BlockNode::LinkReferenceDefinition(def) = block {
405 groups
406 .entry(crate::parse::label_key(&def.label).to_uppercase())
407 .or_default()
408 .push(def);
409 }
410 }
411 groups
412 .into_values()
413 .filter_map(|group| {
414 let def = *group.first()?;
415 let label = &def.label;
416 (group.len() == 1
417 && label.chars().any(|ch| !ch.is_whitespace())
418 && label.len() <= 999
419 && !label.contains(['[', ']', '\\', '\n', '\r']))
420 .then(|| (crate::parse::label_key(label), def.clone()))
421 })
422 .collect()
423}
424
425struct MarkdownContext {
426 referenced_links: HashSet<String>,
427 native_references: HashMap<String, crate::ast::LinkReferenceDefinition>,
428 suppress_automatic_abbreviation: bool,
435 heading_slugs: HashMap<String, String>,
438 list_depth: usize,
439 thematic_glued: bool,
440 in_footnote: bool,
441 footnote_allowed_lists: HashSet<usize>,
442 list_item_opening_list: Option<usize>,
443 defined_footnotes: std::collections::BTreeSet<String>,
449 crossref_index: crate::parse::CrossrefIndex,
450 link_depth: usize,
456 single_line_depth: usize,
458 previous_list: Option<(bool, char)>,
461 roundtrip: bool,
464 carrier_depth: usize,
467 definition_depth: usize,
468 delimiter_ancestors: Vec<&'static str>,
469 rendered_children: HashMap<usize, Vec<String>>,
470}
471
472fn carrier_markers(node: &BlockNode, ctx: &MarkdownContext) -> Option<CarrierMarkers> {
475 if !ctx.roundtrip || ctx.single_line_depth > 0 {
476 return None;
477 }
478 spell_carrier_markers(node, ctx.carrier_depth)
479}
480
481fn carried_children(
483 children: &[BlockNode],
484 markers: Option<&CarrierMarkers>,
485 ctx: &mut MarkdownContext,
486 depth: usize,
487) -> String {
488 if markers.is_none() {
489 return render_blocks(children, ctx, depth);
490 }
491 ctx.carrier_depth += 1;
492 let out = render_blocks(children, ctx, depth);
493 ctx.carrier_depth -= 1;
494
495 out
496}
497
498fn carried(markers: Option<&CarrierMarkers>, body: String) -> String {
503 let Some(markers) = markers else { return body };
504 let mut out = String::new();
505 for payload in markers
506 .prelude
507 .iter()
508 .chain(std::iter::once(&markers.opener))
509 {
510 out.push_str(&carrier_line(payload));
511 out.push('\n');
512 }
513 out.push_str(&body);
514 out.push_str(&carrier_line(&markers.closer));
515 out.push('\n');
516
517 out
518}
519
520fn render_block_inlines(nodes: &[InlineNode], ctx: &mut MarkdownContext) -> String {
521 render_inlines(nodes, ctx, 0)
522}
523
524fn render_wrapped_inlines(
525 nodes: &[InlineNode],
526 ctx: &mut MarkdownContext,
527 delimiter: &'static str,
528 open_tag: &str,
529 close_tag: &str,
530) -> String {
531 let owner = &nodes as *const &[InlineNode] as usize;
532 ctx.delimiter_ancestors.push(delimiter);
533 let inner = render_recorded_inlines(nodes, ctx, 0, Some(owner));
534 ctx.delimiter_ancestors.pop();
535 let parts = ctx.rendered_children.remove(&owner);
536 let (marked_lead, marked_core, marked_trail) = inline_padding(&inner, char::is_whitespace);
537 let visible_core = visible_inline(&marked_core);
538 let core = visible_core.as_ref();
539 let rendered =
540 if !core.is_empty() && nested_edge_is_invalid(core, delimiter, nodes, parts.as_ref()) {
541 format!("{marked_lead}{open_tag}{marked_core}{close_tag}{marked_trail}")
542 } else {
543 pad_outside(inner, delimiter, open_tag, close_tag)
544 };
545 trim_non_nbsp(&rendered).to_string()
546}
547
548fn render_title_inlines(nodes: &[InlineNode], ctx: &mut MarkdownContext) -> String {
549 let nodes = inline_nodes_without_strong(nodes);
550 render_wrapped_inlines(&nodes, ctx, "**", "<strong>", "</strong>")
551}
552
553fn render_blocks(blocks: &[BlockNode], ctx: &mut MarkdownContext, depth: usize) -> String {
554 if depth > MAX_RENDER_DEPTH {
555 crate::render_depth::record("markdown");
556 return String::new();
557 }
558 blocks
559 .iter()
560 .map(|block| render_block(block, ctx, depth))
561 .collect()
562}
563
564fn render_block(node: &BlockNode, ctx: &mut MarkdownContext, depth: usize) -> String {
568 if !ctx.roundtrip && markdown_dropped_attributes(node) {
569 crate::render_loss::record_markdown_loss(
570 MarkdownLoss::Attributes,
571 RawNodeType::Block,
572 crate::ast_json::block_pos(node),
573 );
574 }
575 let mut out = render_block_kind(node, ctx, depth);
576 if ctx.roundtrip && ctx.single_line_depth == 0 && !out.is_empty() {
577 if let Some(attributes) = dropped_attribute_line(node)
578 .filter(|_| !matches!(node, BlockNode::DefinitionList(_)) || ctx.definition_depth == 0)
579 {
580 out = format!("{}\n{out}", carrier_line(&attributes));
581 }
582 }
583 let transparent = matches!(
584 node,
585 BlockNode::List(_)
586 | BlockNode::Div(_)
587 | BlockNode::Section(_)
588 | BlockNode::LineBlock(_)
589 | BlockNode::Admonition(_)
590 | BlockNode::Directive(_)
591 | BlockNode::ExtensionCarrier(_)
592 | BlockNode::BlockExtension(_)
593 | BlockNode::DefinitionList(_)
594 );
595 if !transparent && !out.trim().is_empty() {
596 ctx.previous_list = None;
597 }
598 out
599}
600
601fn markdown_dropped_attributes(node: &BlockNode) -> bool {
602 let additional = match node {
603 BlockNode::CodeBlock(n) => {
604 return !crate::render::render_attrs_without_keys(&n.attrs, &["title"]).is_empty();
605 }
606 BlockNode::Admonition(n) if list_table_outer(n).is_some() => {
607 return !crate::render::render_attrs_without_keys(
608 &n.attrs,
609 &["header-rows", "header-cols", "aligns"],
610 )
611 .is_empty();
612 }
613 BlockNode::Admonition(n) => &n.attrs,
614 BlockNode::Directive(n) => &n.attrs,
615 BlockNode::Div(n) => &n.attrs,
616 BlockNode::LineBlock(n) => &n.attrs,
617 BlockNode::FigureGroup(n) => &n.attrs,
618 BlockNode::BlockExtension(n) => &n.attrs,
619 BlockNode::ExtensionCarrier(n) => &n.attrs,
620 BlockNode::LinkReferenceDefinition(n) => &n.attrs,
621 _ => return dropped_attribute_line(node).is_some(),
622 };
623 nonempty_attributes(additional).is_some()
624}
625
626fn dropped_attribute_line(node: &BlockNode) -> Option<String> {
627 let attrs = match node {
628 BlockNode::Heading(n) => {
629 let mut attrs = n.attrs.clone();
630 if let Some(attrs) = &mut attrs {
631 if !attrs.order.iter().any(|slot| matches!(slot, AttrSlot::Id)) {
632 attrs.id = None;
633 }
634 }
635 return nonempty_attributes(&attrs);
636 }
637 BlockNode::Paragraph(n) => &n.attrs,
638 BlockNode::BlockQuote(n) => &n.attrs,
639 BlockNode::List(n) => &n.attrs,
640 BlockNode::DefinitionList(n) => &n.attrs,
641 BlockNode::Table(n) => &n.attrs,
642 BlockNode::CodeBlock(n) => {
643 let mut attrs = n.attrs.clone();
644 if let (Some(title), Some(attrs)) = (&n.title, &mut attrs) {
645 if attrs.key_values.get("title") == Some(title) {
646 attrs.key_values.remove("title");
647 attrs
648 .order
649 .retain(|slot| !matches!(slot, AttrSlot::Key(key) if key == "title"));
650 }
651 }
652 return nonempty_attributes(&attrs);
653 }
654 BlockNode::ThematicBreak(n) => &n.attrs,
655 BlockNode::Figure(n) => &n.attrs,
656 BlockNode::BlockImage(n) => &n.attrs,
657 _ => return None,
658 };
659 nonempty_attributes(attrs)
660}
661
662fn nonempty_attributes(attrs: &Option<Attrs>) -> Option<String> {
663 let line = crate::render_carve::render_attrs(attrs);
664 (!line.is_empty()).then_some(line)
665}
666
667fn inline_carrier(prefix: &str, nodes: &[InlineNode]) -> String {
668 inline_carrier_at(prefix, nodes, 1)
669}
670
671fn inline_carrier_at(prefix: &str, nodes: &[InlineNode], depth: usize) -> String {
672 let mut doc = crate::parse::parse("");
673 let protect_term_tab = prefix == "::"
677 && matches!(nodes.first(), Some(InlineNode::Text(text)) if text.value.starts_with('\t'));
678 let mut children = nodes.to_vec();
679 if protect_term_tab {
680 children.insert(0, InlineNode::Text(Text::from("a")));
681 }
682 doc.children.push(BlockNode::Paragraph(Paragraph {
683 children,
684 ..Paragraph::default()
685 }));
686 let text = match crate::render_carve::render_carve(&doc) {
687 Ok(text) => text,
688 Err(error) => {
689 CARRIER_WRITE_ERROR.with(|cell| {
690 let mut first = cell.borrow_mut();
691 if first.is_none() {
692 *first = Some(error);
693 }
694 });
695 return String::new();
696 }
697 };
698 let text = if protect_term_tab {
699 text.strip_prefix('a').expect("temporary carrier prefix")
700 } else {
701 &text
702 };
703 carrier_line_at(&format!("{prefix} {}", text.trim_end()), depth)
704}
705
706fn caption_carrier(nodes: &[InlineNode], ctx: &MarkdownContext) -> String {
707 if ctx.roundtrip && ctx.single_line_depth == 0 {
708 format!("{}\n", inline_carrier("^", nodes))
709 } else {
710 String::new()
711 }
712}
713
714fn render_block_kind(node: &BlockNode, ctx: &mut MarkdownContext, depth: usize) -> String {
715 if depth > MAX_RENDER_DEPTH {
716 crate::render_depth::record("markdown");
717 return String::new();
718 }
719 match node {
720 BlockNode::LinkReferenceDefinition(definition) => {
722 if !ctx.roundtrip
723 && ctx
724 .referenced_links
725 .contains(&crate::parse::label_key(&definition.label))
726 {
727 return String::new();
728 }
729 let safe = crate::escape::sanitize_destination(
730 &definition.href,
731 definition.pos.as_ref(),
732 crate::render_loss::DeniedSink::Destination,
733 );
734 let destination = if safe.is_empty() {
735 "<>".to_string()
736 } else {
737 neutralize_char_refs(
738 &strip_control_chars(&safe)
739 .replace('\\', "\\\\")
740 .replace('(', "\\(")
741 .replace(')', "\\)")
742 .replace('<', "\\<")
743 .replace('>', "\\>"),
744 )
745 };
746 let title = definition
747 .title
748 .as_ref()
749 .map(|title| format!(" \"{}\"", escape_md_title(title)))
750 .unwrap_or_default();
751 format!("[{}]: {destination}{title}\n\n", definition.label)
752 }
753 BlockNode::CitationDefinition(_) => String::new(),
754 BlockNode::Heading(heading) => {
757 ctx.single_line_depth += 1;
758 let text = flatten_heading_text(&render_block_inlines(&heading.children, ctx));
759 ctx.single_line_depth -= 1;
760 format!("{} {text}\n\n", "#".repeat(heading.level as usize))
761 }
762 BlockNode::Paragraph(paragraph) => {
763 let text = protect_paragraph_list_markers(
764 &render_block_inlines(¶graph.children, ctx),
765 ctx.definition_depth > 0,
766 );
767 if text.is_empty() {
768 String::new()
769 } else {
770 format!("{text}\n\n")
771 }
772 }
773 BlockNode::CodeBlock(code) => {
774 let content = resolve_nbsp(&strip_controls(&code.content));
775 let mut info = code
776 .lang
777 .as_deref()
778 .map(sanitize_code_lang)
779 .filter(|lang| !lang.is_empty())
780 .unwrap_or_default();
781 let effective_title = code
788 .attrs
789 .as_ref()
790 .and_then(|attrs| attrs.key_values.get("title"))
791 .or(code.title.as_ref());
792 if effective_title.is_some() && info.is_empty() && !ctx.roundtrip {
793 crate::render_loss::record_markdown_loss(
794 MarkdownLoss::FenceTitle,
795 RawNodeType::Block,
796 code.pos.as_ref(),
797 );
798 }
799 if let Some(title) = effective_title.filter(|_| !info.is_empty()) {
809 info.push_str(&format!(" \"{}\"", escape_code_title(title)));
810 }
811 if let Some(label) = &code.label {
816 if !label.is_empty() {
817 let cleaned: String =
818 label.chars().filter(|c| !matches!(c, '[' | ']')).collect();
819 info.push_str(&format!(" [{cleaned}]"));
820 }
821 }
822 let fence =
823 safe_fence_with_marker(&content, 3, if info.contains('`') { '~' } else { '`' });
824 let ending = if !content.is_empty() && !content.ends_with('\n') {
825 "\n"
826 } else {
827 ""
828 };
829 let info_separator = if fence.starts_with('~') && info.starts_with('~') {
830 " "
831 } else {
832 ""
833 };
834 note_inserted(C_PAYLOAD_BOUNDARY);
835 note_inserted(C_PAYLOAD_BOUNDARY);
836 let boundary = carrier(C_PAYLOAD_BOUNDARY);
837 let body = format!(
838 "{fence}{info_separator}{info}{boundary}\n{content}{boundary}{ending}{fence}\n\n"
839 );
840 if ctx.roundtrip {
841 if let Some(marker) = crate::carrier_markers::code_fence_marker(code) {
842 format!("{marker}\n{body}")
843 } else {
844 body
845 }
846 } else {
847 body
848 }
849 }
850 BlockNode::BlockQuote(quote) => {
851 let outer = ctx.previous_list.take();
852 let lines = render_blocks("e.children, ctx, depth + 1);
853 ctx.previous_list = outer;
854 let body = trim_block_output(&lines).to_string();
855 let quoted = body
856 .split('\n')
857 .map(|line| {
858 if line.is_empty() {
859 ">".to_string()
860 } else {
861 format!("> {line}")
862 }
863 })
864 .collect::<Vec<_>>()
865 .join("\n");
866 format!("{quoted}\n\n")
867 }
868 BlockNode::List(list) => {
869 let style_carrier = ctx.roundtrip
870 && list.ordered
871 && list.ol_type.is_some()
872 && ctx.list_item_opening_list != Some(list as *const List as usize)
873 && (!ctx.in_footnote
874 || ctx
875 .footnote_allowed_lists
876 .contains(&(list as *const List as usize)));
877 if let Some(style) = list.ol_type.filter(|_| !style_carrier) {
878 crate::render_loss::record_list_style_dropped(style, list.pos.as_ref());
879 }
880 let usual = if list.ordered {
883 if list.delim == Some(')') {
884 ')'
885 } else {
886 '.'
887 }
888 } else {
889 list.bullet_char.unwrap_or('-')
890 };
891 let marker = match ctx.previous_list.filter(|_| !style_carrier) {
892 Some((ordered, previous)) if ordered == list.ordered && previous == usual => {
893 match usual {
894 '.' => ')',
895 ')' => '.',
896 '-' => '*',
897 _ => '-',
898 }
899 }
900 _ => usual,
901 };
902 if list.ordered && marker != usual {
903 crate::render_loss::record_list_delimiter_changed(usual, marker, list.pos.as_ref());
904 }
905 let mut out = render_list(list, ctx, depth + 1, marker);
906 if style_carrier {
907 let start = list.start.unwrap_or(1);
908 let style = list.ol_type;
909 let first = crate::render_carve::ordered_marker(start, style);
910 let second = if first.len() == 1
911 && first.bytes().all(|ch| b"ivxlcdmIVXLCDM".contains(&ch))
912 && list.items.len() > 1
913 {
914 format!(
915 " {}{}",
916 crate::render_carve::ordered_marker(start + 1, style),
917 usual
918 )
919 } else {
920 String::new()
921 };
922 out = format!(
923 "{}\n{out}",
924 carrier_line(&format!("{first}{usual}{second}"))
925 );
926 }
927 if !out.trim().is_empty() {
928 ctx.previous_list = Some((list.ordered, marker));
929 }
930 out
931 }
932 BlockNode::ThematicBreak(_) => if ctx.thematic_glued {
933 "***\n\n"
934 } else {
935 "---\n\n"
936 }
937 .to_string(),
938 BlockNode::Table(table) => render_table(table, ctx),
939 BlockNode::Admonition(admonition) => {
940 if let Some(table) = list_table_as_table(admonition) {
941 let table = render_table(&table, ctx);
942 return match admonition.label.as_deref().filter(|l| !l.is_empty()) {
943 Some(label) => {
944 ctx.previous_list = None;
945 prepend_label(table, Some(label))
946 }
947 None => table,
948 };
949 }
950 let title = admonition
954 .title
955 .as_ref()
956 .map(|title| render_title_inlines(title, ctx));
957 if title.as_ref().is_some_and(|t| !t.is_empty())
958 || admonition.label.as_deref().is_some_and(|l| !l.is_empty())
959 {
960 ctx.previous_list = None;
961 }
962 let markers = carrier_markers(node, ctx);
963 let body = carried_children(&admonition.children, markers.as_ref(), ctx, depth + 1);
964 let body = prepend_label(body, admonition.label.as_deref());
968 let body = match title {
969 Some(t) if !t.is_empty() => format!("{t}\n\n{body}"),
970 _ => body,
971 };
972 carried(markers.as_ref(), body)
973 }
974 BlockNode::LineBlock(lb) => {
975 let markers = carrier_markers(node, ctx);
976 let body = carried_children(&lb.children, markers.as_ref(), ctx, depth + 1);
977 carried(markers.as_ref(), body)
978 }
979 BlockNode::Directive(d) => {
980 let title = d
981 .title
982 .as_ref()
983 .map(|title| render_title_inlines(title, ctx));
984 if title.as_ref().is_some_and(|t| !t.is_empty())
985 || d.label.as_deref().is_some_and(|l| !l.is_empty())
986 {
987 ctx.previous_list = None;
988 }
989 let markers = carrier_markers(node, ctx);
990 let body = carried_children(&d.children, markers.as_ref(), ctx, depth + 1);
991 let body = prepend_label(body, d.label.as_deref());
992 let body = match title {
993 Some(t) if !t.is_empty() => format!("{t}\n\n{body}"),
994 _ => body,
995 };
996 carried(markers.as_ref(), body)
997 }
998 BlockNode::Div(div) => {
999 if !ctx.roundtrip {
1000 crate::render_loss::record_markdown_loss(
1001 MarkdownLoss::Container,
1002 RawNodeType::Block,
1003 div.pos.as_ref(),
1004 );
1005 }
1006 if div.label.as_deref().is_some_and(|l| !l.is_empty()) {
1007 ctx.previous_list = None;
1008 }
1009 let markers = carrier_markers(node, ctx);
1010 let body = carried_children(&div.children, markers.as_ref(), ctx, depth + 1);
1011 carried(markers.as_ref(), prepend_label(body, div.label.as_deref()))
1012 }
1013 BlockNode::Section(section) => render_blocks(§ion.children, ctx, depth + 1),
1014 BlockNode::DefinitionList(list) => {
1015 if ctx.roundtrip && ctx.single_line_depth > 0 {
1016 crate::render_loss::record_definition_list_flattened(list.pos.as_ref());
1017 }
1018 render_definition_list(&list.items, ctx, depth + 1)
1019 }
1020 BlockNode::Figure(figure) => render_figure(figure, ctx, depth + 1),
1021 BlockNode::FigureGroup(group) => render_figure_group(group, ctx, depth + 1),
1022 BlockNode::BlockImage(image) => format!("{}\n\n", render_image(image)),
1026 BlockNode::RawBlock(raw) => {
1027 if raw.format == "html" {
1028 crate::render_loss::record_markdown_loss(
1029 MarkdownLoss::RawHtml,
1030 RawNodeType::Block,
1031 raw.pos.as_ref(),
1032 );
1033 format!(
1036 "{}\n\n",
1037 escape_md_html(&strip_controls(raw.target_content()))
1038 )
1039 } else {
1040 crate::render_loss::record_raw_drop(
1041 &raw.format,
1042 crate::RawNodeType::Block,
1043 raw.pos.clone(),
1044 );
1045 String::new()
1046 }
1047 }
1048 BlockNode::BlockExtension(n) => render_block(&n.fallback, ctx, depth),
1049 BlockNode::ExtensionCarrier(extension) => {
1050 render_blocks(&extension.children, ctx, depth + 1)
1051 }
1052 BlockNode::AbbreviationDef(def) => format!(
1063 "*[{}]: {}\n\n",
1064 escape_md_html(&strip_controls(&def.abbr)),
1065 escape_md_html(&strip_controls(&def.expansion))
1066 ),
1067 BlockNode::Comment(_) => String::new(),
1068 }
1069}
1070
1071fn protect_paragraph_list_markers(text: &str, in_definition: bool) -> String {
1076 if !inline_marker_ranges(text).is_empty() {
1077 let mut code_fence = 0;
1078 return text
1079 .split('\n')
1080 .map(|line| {
1081 let visible = visible_inline(line);
1082 let inside_code = code_fence != 0;
1083 code_fence = code_span_state_after(&visible, code_fence);
1084 if inside_code {
1085 return line.to_owned();
1086 }
1087 let core = inline_padding(line, |ch| ch == ' ' || ch == '\t').1;
1088 let visible = visible_inline(&core);
1089 let protected = protect_paragraph_list_markers(&visible, in_definition);
1090 restore_inline_change(&core, &visible, &protected)
1091 })
1092 .collect::<Vec<_>>()
1093 .join("\n");
1094 }
1095 let lines: Vec<&str> = text.split('\n').collect();
1096 let last = lines.len() - 1;
1097 let mut output = String::with_capacity(text.len());
1098 let mut code_fence = 0usize;
1099 let mut written = 0usize;
1100 for (line_index, source_line) in lines.iter().enumerate() {
1101 let open_at_start = code_fence != 0;
1102 code_fence = code_span_state_after(source_line, code_fence);
1103 let mut line = if open_at_start {
1104 (*source_line).to_string()
1105 } else {
1106 source_line.trim_start_matches([' ', '\t']).to_string()
1107 };
1108 if code_fence == 0 {
1109 let backslashes = line.chars().rev().take_while(|c| *c == '\\').count();
1110 line = if backslashes % 2 == 1 && line_index < last {
1111 let body = &line[..line.len() - 1];
1112 format!("{}\\", body.trim_end_matches([' ', '\t']))
1113 } else {
1114 line.trim_end_matches([' ', '\t']).to_string()
1115 };
1116 }
1117 if line.is_empty() {
1119 continue;
1120 }
1121 if !open_at_start {
1122 let underscore = carrier(C_UNDERSCORE);
1126 let dot = carrier(C_WWW_DOT);
1127 let view = if line.contains(underscore.as_str()) || line.contains(dot.as_str()) {
1128 std::borrow::Cow::Owned(
1129 line.replace(underscore.as_str(), "_")
1130 .replace(dot.as_str(), "."),
1131 )
1132 } else {
1133 std::borrow::Cow::Borrowed(line.as_str())
1134 };
1135 if let Some(at) = paragraph_marker_escape(&view, written == 0, in_definition) {
1136 if line[at..].starts_with(underscore.as_str()) {
1137 note_assembled(&line[at..at + underscore.len()]);
1138 line.replace_range(at..at + underscore.len(), "\\_");
1139 } else if line[at..].starts_with(dot.as_str()) {
1140 note_assembled(&line[at..at + dot.len()]);
1141 line.replace_range(at..at + dot.len(), "\\.");
1142 } else {
1143 line.insert(at, '\\');
1144 }
1145 }
1146 }
1147 if written > 0 {
1148 output.push('\n');
1149 }
1150 output.push_str(&line);
1151 written += 1;
1152 }
1153 output
1154}
1155
1156fn paragraph_marker_escape(line: &str, first: bool, in_definition: bool) -> Option<usize> {
1159 let bytes = line.as_bytes();
1160 let first_byte = *bytes.first()?;
1161 if matches!(first_byte, b'-' | b'+')
1163 && (bytes.len() == 1 && first || matches!(bytes.get(1), Some(b' ' | b'\t')))
1164 {
1165 return Some(0);
1166 }
1167 if first_byte == b':'
1169 && ((in_definition && matches!(bytes.get(1), None | Some(b' ' | b'\t')))
1170 || (bytes.get(1) == Some(&b':') && matches!(bytes.get(2), Some(b' ' | b'\t'))))
1171 {
1172 return Some(0);
1173 }
1174 let digits = bytes
1175 .iter()
1176 .take(10)
1177 .take_while(|b| b.is_ascii_digit())
1178 .count();
1179 if (1..=9).contains(&digits) && matches!(bytes.get(digits), Some(b'.' | b')')) {
1180 let after = bytes.get(digits + 1);
1181 if matches!(after, Some(b' ' | b'\t')) || after.is_none() && first {
1182 return Some(digits);
1183 }
1184 }
1185 if first_byte == b'>' {
1187 return Some(0);
1188 }
1189 if !first && matches!(first_byte, b'=' | b'-') && bytes.iter().all(|b| *b == first_byte) {
1191 return Some(0);
1192 }
1193 if matches!(first_byte, b'-' | b'_' | b'*')
1195 && bytes
1196 .iter()
1197 .all(|b| *b == first_byte || matches!(b, b' ' | b'\t'))
1198 && bytes.iter().filter(|b| **b == first_byte).count() >= 3
1199 {
1200 return Some(0);
1201 }
1202 None
1203}
1204
1205fn code_span_state_after(line: &str, mut code_fence: usize) -> usize {
1208 let bytes = line.as_bytes();
1209 let mut i = 0usize;
1210 while i < bytes.len() {
1211 if bytes[i] != b'`' {
1212 i += 1;
1213 continue;
1214 }
1215 let mut backslashes = 0usize;
1216 let mut before = i;
1217 while before > 0 && bytes[before - 1] == b'\\' {
1218 backslashes += 1;
1219 before -= 1;
1220 }
1221 let mut run = 1usize;
1222 while i + run < bytes.len() && bytes[i + run] == b'`' {
1223 run += 1;
1224 }
1225 if backslashes % 2 == 0 {
1226 if code_fence == 0 {
1227 code_fence = run;
1228 } else if code_fence == run {
1229 code_fence = 0;
1230 }
1231 }
1232 i += run;
1233 }
1234 code_fence
1235}
1236
1237fn render_list(node: &List, ctx: &mut MarkdownContext, depth: usize, marker: char) -> String {
1238 if depth > MAX_RENDER_DEPTH {
1239 crate::render_depth::record("markdown");
1240 return String::new();
1241 }
1242 ctx.list_depth += 1;
1243 let tight = node.effective_tight();
1244 let mut out = String::new();
1245 let mut counter = node.start.unwrap_or(1);
1246 let bullet = marker;
1250 let delim = marker;
1251 let last = node.items.len().saturating_sub(1);
1252 for (index, item) in node.items.iter().enumerate() {
1253 let (prefix, pad) = if node.ordered {
1259 let prefix = format!("{counter}{delim} ");
1260 counter += 1;
1261 let pad = prefix.len();
1262 (prefix, pad)
1263 } else if let Some(checked) = item.checked {
1264 let box_mark = if checked { 'x' } else { ' ' };
1265 (format!("{bullet} [{box_mark}] "), bullet.len_utf8() + 1)
1266 } else {
1267 let prefix = format!("{bullet} ");
1268 let pad = prefix.len();
1269 (prefix, pad)
1270 };
1271 let outer = ctx.previous_list.take();
1272 let content = trim_block_output(&render_list_item(item, tight, ctx, depth + 1)).to_string();
1273 ctx.previous_list = outer;
1274 let mut lines = content.split('\n');
1275 let first = lines.next().unwrap_or_default();
1276 if first.is_empty() {
1277 out.push_str(&format!("{}\n", prefix.trim_end()));
1278 } else {
1279 out.push_str(&format!("{prefix}{first}\n"));
1280 }
1281 render_continuation_lines(&mut out, lines, pad);
1282 if !tight && index < last {
1286 out.push('\n');
1287 }
1288 }
1289 ctx.list_depth -= 1;
1290 if ctx.list_depth == 0 {
1291 out.push('\n');
1292 }
1293 out
1294}
1295
1296fn render_list_item(
1309 item: &ListItem,
1310 tight: bool,
1311 ctx: &mut MarkdownContext,
1312 depth: usize,
1313) -> String {
1314 if depth > MAX_RENDER_DEPTH {
1315 crate::render_depth::record("markdown");
1316 return String::new();
1317 }
1318 let mut out = String::new();
1319 for (index, child) in item.children.iter().enumerate() {
1320 let outer_opening = ctx.list_item_opening_list;
1321 if out.is_empty() {
1322 ctx.list_item_opening_list = match child {
1323 BlockNode::List(list) => Some(list as *const List as usize),
1324 _ => None,
1325 };
1326 }
1327 let non_setext = tight
1328 && matches!(opening_block(child), BlockNode::ThematicBreak(_))
1329 && out.ends_with("\n\n");
1330 let previous_thematic = std::mem::replace(&mut ctx.thematic_glued, non_setext);
1331 let rendered = render_block(child, ctx, depth);
1332 ctx.thematic_glued = previous_thematic;
1333 ctx.list_item_opening_list = outer_opening;
1334 let opener = opening_block(child);
1336 if tight
1337 && out.ends_with("\n\n")
1338 && (interrupts_a_paragraph(opener, &rendered)
1339 || matches!(opener, BlockNode::ThematicBreak(_))
1340 || matches!(opener, BlockNode::Paragraph(_))
1341 && index > 0
1342 && matches!(
1343 closing_block(&item.children[index - 1]),
1344 BlockNode::CodeBlock(_)
1345 ))
1346 && !absorbs_below(&out, opener)
1347 {
1348 out.pop();
1349 } else if out.ends_with('\n')
1350 && !out.ends_with("\n\n")
1351 && swallowed_by_a_lazy_line(&out, &rendered)
1352 {
1353 out.push('\n');
1354 }
1355 out.push_str(&rendered);
1356 }
1357 out
1358}
1359
1360fn swallowed_by_a_lazy_line(above_text: &str, rendered: &str) -> bool {
1376 let first = rendered.split('\n').next().unwrap_or_default();
1380 if first.trim().is_empty() || thematic_break_line(first) || opens_an_interrupter(first) {
1381 return false;
1382 }
1383 let tail = above_text
1384 .lines()
1385 .rev()
1386 .find(|line| !line.trim().is_empty())
1387 .unwrap_or_default()
1388 .trim_start_matches([' ', '\t']);
1389 let boundary = carrier(C_PAYLOAD_BOUNDARY);
1390 let tail = tail.strip_prefix(boundary.as_str()).unwrap_or(tail);
1391 if bare_marker_line(tail) || thematic_break_line(tail) {
1392 return false;
1393 }
1394 let text = marker_content(tail);
1395 !text.is_empty() && !text.starts_with(['#', '>', '|', '`', '~', '='])
1396}
1397
1398fn opens_an_interrupter(line: &str) -> bool {
1400 let hashes = line.len() - line.trim_start_matches('#').len();
1401 let bullet = line
1402 .strip_prefix(['-', '*', '+'])
1403 .or_else(|| line.strip_prefix("1.").or_else(|| line.strip_prefix("1)")));
1404 line.starts_with('>')
1405 || ((1..=6).contains(&hashes)
1406 && (line.len() == hashes || line[hashes..].starts_with([' ', '\t'])))
1407 || line.starts_with("```")
1408 || line.starts_with("~~~")
1409 || bullet.is_some_and(|rest| {
1410 rest.starts_with([' ', '\t']) && !rest.trim_start_matches([' ', '\t']).is_empty()
1411 })
1412 || (line.starts_with('*')
1413 && line.matches('*').count() >= 3
1414 && line.chars().all(|c| matches!(c, '*' | ' ' | '\t')))
1415}
1416
1417fn marker_content(line: &str) -> &str {
1420 if let Some(rest) = line.strip_prefix(['-', '*', '+']) {
1421 if let Some(rest) = rest.strip_prefix(' ') {
1422 return rest.trim_start_matches(' ');
1423 }
1424 }
1425 let digits = line.trim_start_matches(|c: char| c.is_ascii_digit());
1426 if digits.len() < line.len() {
1427 if let Some(rest) = digits.strip_prefix(['.', ')']) {
1428 if let Some(rest) = rest.strip_prefix(' ') {
1429 return rest.trim_start_matches(' ');
1430 }
1431 }
1432 }
1433 line
1434}
1435
1436fn thematic_break_line(line: &str) -> bool {
1439 let line = line.trim_end_matches([' ', '\t']);
1440 let Some(first) = line.chars().next() else {
1441 return false;
1442 };
1443 matches!(first, '-' | '*' | '_') && line.len() >= 3 && line.chars().all(|c| c == first)
1444}
1445
1446fn absorbs_below(above_text: &str, below: &BlockNode) -> bool {
1462 let tail = above_text
1463 .lines()
1464 .rev()
1465 .find(|line| !line.trim().is_empty())
1466 .unwrap_or_default()
1467 .trim_start_matches([' ', '\t']);
1468 match below {
1469 BlockNode::BlockQuote(_) => tail.starts_with('>'),
1470 BlockNode::Table(_) => tail.starts_with(['>', '|']),
1471 BlockNode::Figure(figure) => match &*figure.target {
1472 FigureTarget::BlockQuote(_) => tail.starts_with('>'),
1473 FigureTarget::Table(_) => tail.starts_with(['>', '|']),
1474 _ => false,
1475 },
1476 _ => false,
1477 }
1478}
1479
1480fn interrupts_a_paragraph(node: &BlockNode, rendered: &str) -> bool {
1483 if rendered.starts_with("<!--") {
1486 return true;
1487 }
1488 match node {
1489 BlockNode::BlockQuote(_) => true,
1491 BlockNode::Heading(_) | BlockNode::CodeBlock(_) => true,
1498 BlockNode::Table(_) => has_delimiter_row(rendered),
1503 BlockNode::List(list) => {
1504 if list.ordered && list.start.unwrap_or(1) != 1 {
1508 return false;
1509 }
1510 !bare_marker_line(rendered.split('\n').next().unwrap_or_default())
1511 }
1512 BlockNode::Figure(figure) => {
1516 let first = rendered.split('\n').next().unwrap_or_default();
1517 match &*figure.target {
1518 FigureTarget::BlockQuote(_) => first.starts_with('>'),
1519 FigureTarget::CodeBlock(_) => first.starts_with("```") || first.starts_with("~~~"),
1520 FigureTarget::Table(_) => first.starts_with('|') && has_delimiter_row(rendered),
1521 FigureTarget::Image(_) | FigureTarget::Paragraph(_) => false,
1522 }
1523 }
1524 _ => false,
1525 }
1526}
1527
1528fn opening_block(node: &BlockNode) -> &BlockNode {
1533 let mut node = node;
1534 loop {
1535 let children = match node {
1536 BlockNode::Div(div) if div.label.as_deref().unwrap_or_default().is_empty() => {
1537 &div.children
1538 }
1539 BlockNode::Admonition(admonition)
1540 if admonition.label.as_deref().unwrap_or_default().is_empty()
1541 && admonition.title.as_ref().map_or(true, Vec::is_empty) =>
1542 {
1543 &admonition.children
1544 }
1545 BlockNode::LineBlock(line) => &line.children,
1546 _ => break,
1547 };
1548 match children.first() {
1549 Some(first) => node = first,
1550 None => break,
1551 }
1552 }
1553 node
1554}
1555
1556fn closing_block(mut node: &BlockNode) -> &BlockNode {
1557 while let BlockNode::Div(div) = node {
1558 match div.children.last() {
1559 Some(last) => node = last,
1560 None => break,
1561 }
1562 }
1563 node
1564}
1565
1566fn has_delimiter_row(rendered: &str) -> bool {
1569 let Some(second) = rendered.split('\n').nth(1) else {
1570 return false;
1571 };
1572 second.contains('-')
1573 && second
1574 .chars()
1575 .all(|c| matches!(c, '|' | '-' | ':' | ' ' | '\t'))
1576}
1577
1578fn bare_marker_line(line: &str) -> bool {
1580 let line = line.trim_end_matches(' ');
1581 if let Some(rest) = line.strip_prefix(['-', '*', '+']) {
1582 return rest.is_empty();
1583 }
1584 let rest = line.trim_start_matches(|c: char| c.is_ascii_digit());
1585 rest.len() < line.len() && matches!(rest, "." | ")")
1586}
1587
1588fn render_definition_list(
1592 items: &[DefinitionItem],
1593 ctx: &mut MarkdownContext,
1594 depth: usize,
1595) -> String {
1596 if depth > MAX_RENDER_DEPTH {
1597 crate::render_depth::record("markdown");
1598 return String::new();
1599 }
1600 let carried = ctx.roundtrip && ctx.single_line_depth == 0;
1601 ctx.definition_depth += 1;
1602 let carrier_depth = ctx.definition_depth;
1603 let mut out = String::new();
1604 for item in items {
1605 for term in &item.terms {
1606 if carried {
1607 out.push_str(&inline_carrier_at("::", term, carrier_depth));
1608 out.push('\n');
1609 }
1610 let term = render_wrapped_inlines(term, ctx, "**", "<strong>", "</strong>");
1611 if !term.is_empty() {
1612 ctx.previous_list = None;
1613 out.push_str(&term);
1614 out.push_str("\n\n");
1615 }
1616 }
1617 for definition in &item.definitions {
1618 if carried {
1619 out.push_str(&carrier_line_at(":", carrier_depth));
1620 out.push('\n');
1621 }
1622 out.push_str(&render_blocks(definition, ctx, depth + 1));
1623 }
1624 }
1625 if carried {
1626 out.push_str(&carrier_line_at("", carrier_depth));
1627 out.push_str("\n\n");
1628 }
1629 ctx.definition_depth -= 1;
1630 out
1631}
1632
1633fn list_table_count(value: Option<&String>) -> usize {
1635 match value {
1636 None => 0,
1637 Some(v) if v.trim().is_empty() => 1,
1638 Some(v) => {
1639 let digits: String = v
1640 .trim_start()
1641 .chars()
1642 .enumerate()
1643 .take_while(|(i, c)| c.is_ascii_digit() || (*i == 0 && (*c == '-' || *c == '+')))
1644 .map(|(_, c)| c)
1645 .collect();
1646 digits.parse::<i64>().map_or(0, |n| n.max(0) as usize)
1647 }
1648 }
1649}
1650
1651fn list_table_outer(node: &Admonition) -> Option<&List> {
1654 if node.kind != "list-table" {
1655 return None;
1656 }
1657 let [BlockNode::List(outer)] = node.children.as_slice() else {
1658 return None;
1659 };
1660 if outer
1661 .items
1662 .iter()
1663 .any(|row| !matches!(row.children.first(), Some(BlockNode::List(_))))
1664 {
1665 return None;
1666 }
1667 Some(outer)
1668}
1669
1670fn list_table_as_table(node: &Admonition) -> Option<Table> {
1671 let outer = list_table_outer(node)?;
1672 let empty = std::collections::BTreeMap::new();
1673 let kv = node.attrs.as_ref().map_or(&empty, |a| &a.key_values);
1674 let header_rows = list_table_count(kv.get("header-rows"));
1675 let header_cols = list_table_count(kv.get("header-cols"));
1676 let aligns: Vec<&str> = kv
1677 .get("aligns")
1678 .map(|v| v.split(',').map(str::trim).collect())
1679 .unwrap_or_default();
1680 let align_of = |value: &str| match value {
1681 "left" => Some(TableAlign::Left),
1682 "right" => Some(TableAlign::Right),
1683 "center" => Some(TableAlign::Center),
1684 _ => None,
1685 };
1686 let cell = |header: bool, span: Option<TableCellSpan>, align: Option<TableAlign>| TableCell {
1687 header,
1688 span,
1689 colspan: None,
1690 rowspan: None,
1691 align,
1692 valign: None,
1693 attrs: None,
1694 children: Vec::new(),
1695 blocks: None,
1696 pos: None,
1697 };
1698 let mut rows = Vec::with_capacity(outer.items.len());
1699 for (r, row) in outer.items.iter().enumerate() {
1700 let mut entries: Vec<(&ListItem, Vec<BlockNode>)> = Vec::new();
1702 for block in &row.children {
1703 match block {
1704 BlockNode::List(inner) => {
1705 entries.extend(inner.items.iter().map(|item| (item, item.children.clone())))
1706 }
1707 other => entries.last_mut()?.1.push(other.clone()),
1708 }
1709 }
1710 let header_row = r < header_rows
1711 || entries
1712 .first()
1713 .and_then(|(item, _)| item.attrs.as_ref())
1714 .is_some_and(|a| a.key_values.contains_key("header-row"));
1715 let mut cells = Vec::with_capacity(entries.len());
1716 for (c, (item, blocks)) in entries.into_iter().enumerate() {
1717 if item.attrs.is_none() {
1718 if let [BlockNode::Paragraph(p)] = blocks.as_slice() {
1719 if let (None, [InlineNode::Text(t)]) = (&p.attrs, p.children.as_slice()) {
1720 match t.value.trim() {
1721 "^" => {
1722 cells.push(cell(false, Some(TableCellSpan::Rowspan), None));
1723 continue;
1724 }
1725 "<" => {
1726 cells.push(cell(false, Some(TableCellSpan::Colspan), None));
1727 continue;
1728 }
1729 _ => {}
1730 }
1731 }
1732 }
1733 }
1734 let own = item.attrs.as_ref().map(|a| &a.key_values);
1735 let align = own
1736 .and_then(|kv| kv.get("align"))
1737 .and_then(|v| align_of(v))
1738 .or_else(|| aligns.get(c).and_then(|v| align_of(v)));
1739 let header =
1740 header_row || c < header_cols || own.is_some_and(|kv| kv.contains_key("header"));
1741 let mut built = cell(header, None, align);
1742 match blocks.as_slice() {
1743 [BlockNode::Paragraph(p)] => built.children = p.children.clone(),
1744 [] => {}
1745 _ => built.blocks = Some(blocks),
1746 }
1747 cells.push(built);
1748 }
1749 rows.push(TableRow {
1750 cells,
1751 attrs: None,
1752 continuation_lines: None,
1753 pos: None,
1754 });
1755 }
1756 Some(Table {
1757 source_format: None,
1758 attrs: None,
1759 caption: node.title.clone().filter(|t| !t.is_empty()),
1760 short_caption: None,
1761 columns: Vec::new(),
1762 rows,
1763 row_groups: None,
1764 separator_widths: None,
1765 pos: None,
1766 })
1767}
1768
1769fn render_table(node: &Table, ctx: &mut MarkdownContext) -> String {
1770 let mut header = None;
1771 let mut header_columns = 0usize;
1772 let mut rows = Vec::new();
1773 let mut aligns: Vec<Option<TableAlign>> = Vec::new();
1774 let take_aligns = |aligns: &mut Vec<Option<TableAlign>>, row: &TableRow| {
1775 for (i, cell) in row.cells.iter().enumerate() {
1776 if aligns.len() <= i {
1777 aligns.resize(i + 1, None);
1778 }
1779 if aligns[i].is_none() {
1780 aligns[i] = cell.align;
1781 }
1782 }
1783 };
1784 for (row_index, row) in node.rows.iter().enumerate() {
1785 let is_header = row
1786 .cells
1787 .iter()
1788 .all(|cell| cell.header || cell.span.is_some())
1789 && row.cells.iter().any(|cell| cell.header);
1790 if is_header && header.is_some() {
1791 crate::render_loss::record_table_header_row(row.pos.as_ref());
1792 } else if is_header && row_index > 0 {
1793 crate::render_loss::record_table_header_row_hoisted(row.pos.as_ref());
1795 }
1796 let cells = row
1797 .cells
1798 .iter()
1799 .map(|cell| {
1800 if !is_header && cell.header && cell.span.is_none() {
1801 crate::render_loss::record_table_header_cell(cell.pos.as_ref());
1802 }
1803 crate::render_loss::record_table_span(cell);
1804 let inlines = match &cell.blocks {
1805 Some(blocks) => {
1806 crate::render_plain::flatten_cell_block_inlines_with(blocks, true)
1807 }
1808 None => crate::render_plain::flatten_cell_inlines(
1809 crate::ast::visible_table_cell_children(&cell.children),
1810 true,
1811 ),
1812 };
1813 ctx.single_line_depth += 1;
1814 let roundtrip = ctx.roundtrip;
1815 ctx.roundtrip = false;
1816 let content = trim_non_nbsp(&render_block_inlines(&inlines, ctx)).to_string();
1817 ctx.roundtrip = roundtrip;
1818 ctx.single_line_depth -= 1;
1819 escape_cell_pipes(&content)
1820 })
1821 .map(|content| content.replace(['\r', '\n'], " "))
1822 .collect::<Vec<_>>();
1823 let rendered = format!("| {} |", cells.join(" | "));
1824 if is_header {
1826 if header.is_none() {
1827 aligns.clear();
1828 }
1829 for (i, cell) in row.cells.iter().enumerate() {
1833 if aligns.len() <= i {
1834 aligns.resize(i + 1, None);
1835 }
1836 if cell.align.is_some() {
1837 aligns[i] = cell.align;
1838 }
1839 }
1840 if header.is_none() {
1841 header_columns = cells.len();
1842 header = Some(rendered);
1843 } else {
1844 rows.push(rendered);
1847 }
1848 } else {
1849 rows.push(rendered);
1850 if header.is_none() {
1853 take_aligns(&mut aligns, row);
1854 }
1855 }
1856 }
1857 let mut out = String::new();
1858 let widest = node.rows.iter().map(|row| row.cells.len()).max();
1862 let header = header.or_else(|| {
1863 let width = widest?;
1864 header_columns = width;
1865 Some(format!("| {} |", vec![""; width].join(" | ")))
1866 });
1867 if let Some(mut header) = header {
1868 while header_columns < widest.unwrap_or(0) {
1869 header.push_str(" |");
1870 header_columns += 1;
1871 }
1872 out.push_str(&header);
1873 out.push('\n');
1874 let sep = (0..header_columns)
1877 .map(|i| match aligns.get(i).copied().flatten() {
1878 Some(TableAlign::Left) => ":---",
1879 Some(TableAlign::Center) => ":---:",
1880 Some(TableAlign::Right) => "---:",
1881 None => "---",
1882 })
1883 .collect::<Vec<_>>()
1884 .join(" | ");
1885 out.push_str(&format!("| {sep} |\n"));
1886 }
1887 out.push_str(&rows.join("\n"));
1888 out.push('\n');
1889 if let Some(caption) = &node.caption {
1902 if !ctx.roundtrip && !caption.is_empty() {
1903 crate::render_loss::record_markdown_loss(
1904 MarkdownLoss::Caption,
1905 RawNodeType::Block,
1906 node.pos.as_ref(),
1907 );
1908 }
1909 let text = render_inlines(caption, ctx, 0);
1910 let text = text.trim();
1911 if !text.is_empty() {
1912 out.push('\n');
1913 out.push_str(&caption_carrier(caption, ctx));
1914 out.push_str(text);
1915 out.push('\n');
1916 }
1917 }
1918 out.push('\n');
1919 out
1920}
1921
1922fn escape_cell_pipes(content: &str) -> String {
1933 if !content.contains('|') {
1934 return content.to_owned();
1935 }
1936 let bytes = content.as_bytes();
1937 let mut runs = Vec::new();
1938 let mut at = 0;
1939 while at < bytes.len() {
1940 if bytes[at] != b'`' {
1941 at += 1;
1942 continue;
1943 }
1944 let start = at;
1945 while bytes.get(at) == Some(&b'`') {
1946 at += 1;
1947 }
1948 runs.push((start, at - start));
1949 }
1950 let mut ends = vec![usize::MAX; bytes.len()];
1951 let mut next = std::collections::HashMap::new();
1952 for (start, width) in runs.into_iter().rev() {
1953 for offset in 0..width {
1954 ends[start + offset] = next.get(&(width - offset)).copied().unwrap_or(usize::MAX);
1955 }
1956 next.insert(width, start + width);
1957 }
1958 let mut out = Vec::with_capacity(bytes.len());
1959 let mut i = 0;
1960 while i < bytes.len() {
1961 if bytes[i] == b'\\' {
1962 let begin = i;
1963 while bytes.get(i) == Some(&b'\\') {
1964 i += 1;
1965 }
1966 out.extend_from_slice(&bytes[begin..i]);
1967 if bytes.get(i) == Some(&b'|') {
1968 if (i - begin) % 2 == 0 {
1969 out.push(b'\\');
1970 }
1971 out.push(b'|');
1972 i += 1;
1973 continue;
1974 }
1975 if (i - begin) % 2 != 0 && i < bytes.len() {
1976 out.push(bytes[i]);
1977 i += 1;
1978 }
1979 continue;
1980 }
1981 let end = if bytes[i] == b'`' {
1982 ends[i]
1983 } else {
1984 usize::MAX
1985 };
1986 if end != usize::MAX {
1987 out.extend_from_slice(content[i..end].replace('|', "\\|").as_bytes());
1988 i = end;
1989 continue;
1990 }
1991 if bytes[i..].starts_with(b"[^") {
1992 let mut close = i + 2;
1993 while close < bytes.len() && !matches!(bytes[close], b'[' | b']' | b'\n') {
1994 close += 1;
1995 }
1996 if bytes.get(close) == Some(&b']') {
1997 out.extend_from_slice(content[i..=close].replace('|', "\\|").as_bytes());
1998 i = close + 1;
1999 continue;
2000 }
2001 }
2002 if bytes[i] == b'|' {
2003 out.push(b'\\');
2004 }
2005 out.push(bytes[i]);
2006 i += 1;
2007 }
2008 String::from_utf8(out).expect("pipe escapes preserve UTF-8")
2009}
2010
2011fn render_figure_group(node: &FigureGroup, ctx: &mut MarkdownContext, depth: usize) -> String {
2012 if depth > MAX_RENDER_DEPTH {
2013 crate::render_depth::record("markdown");
2014 return String::new();
2015 }
2016 let markers = carrier_markers(&BlockNode::FigureGroup(node.clone()), ctx);
2017 if markers.is_some() {
2018 ctx.carrier_depth += 1;
2019 }
2020 let mut out = String::new();
2021 for child in &node.children {
2022 match child {
2023 BlockNode::Figure(figure) => {
2024 if !ctx.roundtrip {
2025 crate::render_loss::record_markdown_loss(
2026 MarkdownLoss::Caption,
2027 RawNodeType::Block,
2028 figure.pos.as_ref(),
2029 );
2030 }
2031 let target = render_figure_target(figure, ctx, depth);
2032 let caption = render_wrapped_inlines(&figure.caption, ctx, "*", "<em>", "</em>");
2033 out.push_str(&format!(
2034 "{target}\n\n{}{caption}\n\n",
2035 caption_carrier(&figure.caption, ctx)
2036 ));
2037 }
2038 other => out.push_str(&render_block(other, ctx, depth)),
2039 }
2040 }
2041 if markers.is_some() {
2042 ctx.carrier_depth -= 1;
2043 }
2044 out = carried(markers.as_ref(), out);
2045 if let Some(caption) = &node.caption {
2047 if !ctx.roundtrip {
2048 crate::render_loss::record_markdown_loss(
2049 MarkdownLoss::Caption,
2050 RawNodeType::Block,
2051 node.pos.as_ref(),
2052 );
2053 }
2054 out.push_str(&caption_carrier(caption, ctx));
2055 let caption = render_wrapped_inlines(caption, ctx, "**", "<strong>", "</strong>");
2056 out.push_str(&caption);
2057 out.push_str("\n\n");
2058 }
2059 out
2060}
2061
2062fn render_figure(node: &Figure, ctx: &mut MarkdownContext, depth: usize) -> String {
2063 if depth > MAX_RENDER_DEPTH {
2064 crate::render_depth::record("markdown");
2065 return String::new();
2066 }
2067 if !ctx.roundtrip {
2068 crate::render_loss::record_markdown_loss(
2069 MarkdownLoss::Caption,
2070 RawNodeType::Block,
2071 node.pos.as_ref(),
2072 );
2073 }
2074 let target = render_figure_target(node, ctx, depth);
2075 let sep = match &*node.target {
2076 FigureTarget::BlockQuote(_) | FigureTarget::Table(_) => "\n\n",
2077 _ => "\n",
2078 };
2079 format!(
2082 "{target}{sep}{}{}\n\n",
2083 caption_carrier(&node.caption, ctx),
2084 render_block_inlines(&node.caption, ctx)
2085 )
2086}
2087
2088fn render_figure_target(node: &Figure, ctx: &mut MarkdownContext, depth: usize) -> String {
2092 match &*node.target {
2093 FigureTarget::Image(image) => render_image(image),
2094 FigureTarget::Table(table) => render_table(table, ctx).trim().to_string(),
2095 FigureTarget::BlockQuote(quote) => {
2096 render_block(&BlockNode::BlockQuote(quote.clone()), ctx, depth + 1)
2097 .trim()
2098 .to_string()
2099 }
2100 FigureTarget::CodeBlock(cb) => {
2101 render_block(&BlockNode::CodeBlock(cb.clone()), ctx, depth + 1)
2102 .trim()
2103 .to_string()
2104 }
2105 FigureTarget::Paragraph(p) => {
2106 render_block(&BlockNode::Paragraph(p.clone()), ctx, depth + 1)
2107 .trim()
2108 .to_string()
2109 }
2110 }
2111}
2112
2113fn render_continuation_lines<'a>(
2114 out: &mut String,
2115 lines: impl Iterator<Item = &'a str>,
2116 pad: usize,
2117) {
2118 let continuation = " ".repeat(pad);
2119 for line in lines {
2120 if !line.is_empty() {
2122 out.push_str(&continuation);
2123 out.push_str(line);
2124 }
2125 out.push('\n');
2126 }
2127}
2128
2129fn render_footnote_defs(doc: &Document, ctx: &mut MarkdownContext) -> String {
2130 let mut out = String::new();
2131 for (label, blocks) in crate::ast_json::footnote_defs_in_source_order(doc) {
2133 let outer = ctx.previous_list.take();
2134 ctx.in_footnote = true;
2135 ctx.footnote_allowed_lists = blocks
2136 .iter()
2137 .skip(1)
2138 .filter_map(|block| match block {
2139 BlockNode::List(list) => Some(list as *const List as usize),
2140 _ => None,
2141 })
2142 .collect();
2143 let body = render_blocks(blocks, ctx, 0);
2144 ctx.in_footnote = false;
2145 ctx.footnote_allowed_lists.clear();
2146 ctx.previous_list = outer;
2147 let mut lines = trim_non_nbsp(&body).split('\n');
2148 let first = lines.next().unwrap_or_default();
2149 out.push_str(&format!("[^{}]:", escape_md_html(&strip_controls(label))));
2150 if !first.is_empty() {
2151 out.push(' ');
2152 out.push_str(first);
2153 }
2154 out.push('\n');
2155 render_continuation_lines(&mut out, lines, 4);
2156 }
2157 out
2158}
2159
2160fn ends_escaped(core: &str) -> bool {
2164 core.chars().rev().take_while(|c| *c == '\\').count() % 2 == 1
2165}
2166
2167fn pad_outside_on_its_own_line(
2169 inner: String,
2170 delimiter: &str,
2171 open_tag: &str,
2172 close_tag: &str,
2173) -> String {
2174 trim_non_nbsp(&pad_outside(inner, delimiter, open_tag, close_tag)).to_string()
2175}
2176
2177fn pad_outside(inner: String, delimiter: &str, open_tag: &str, close_tag: &str) -> String {
2185 if inline_marker_ranges(&inner).is_empty() {
2186 return pad_outside_plain(inner, delimiter, open_tag, close_tag);
2187 }
2188 let (lead, core, trail) = inline_padding(&inner, char::is_whitespace);
2189 let visible = visible_inline(&core);
2190 if visible.is_empty() {
2191 return core;
2192 }
2193 if ends_escaped(&visible) || content_grows_the_run(&visible, delimiter) {
2194 return format!("{open_tag}{inner}{close_tag}");
2195 }
2196 format!("{lead}{delimiter}{core}{delimiter}{trail}")
2197}
2198
2199fn pad_outside_plain(inner: String, delimiter: &str, open_tag: &str, close_tag: &str) -> String {
2200 let core = inner.trim();
2206 if core.is_empty() || ends_escaped(core) || content_grows_the_run(core, delimiter) {
2207 if inner.is_empty() {
2208 return String::new();
2209 }
2210 let lead = &inner[..inner.len() - inner.trim_start().len()];
2211 if ends_escaped(core) && lead.contains('\n') {
2212 return format!("{lead}{open_tag}{}{close_tag}", &inner[lead.len()..]);
2213 }
2214 return format!("{open_tag}{inner}{close_tag}");
2215 }
2216 let lead = &inner[..inner.len() - inner.trim_start().len()];
2217 let trail = &inner[inner.trim_end().len()..];
2218 format!("{lead}{delimiter}{core}{delimiter}{trail}")
2219}
2220
2221fn nested_edge_is_invalid(
2222 core: &str,
2223 delimiter: &str,
2224 children: &[InlineNode],
2225 parts: Option<&Vec<String>>,
2226) -> bool {
2227 let ch = delimiter.chars().next().unwrap();
2228 let lead = core.chars().take_while(|c| *c == ch).count();
2229 let mut invalid_edge = ch == '*' && lead > 0 && delimiter.len() + lead != 3;
2230 if ch == '*' && live_run_at_end(core, ch) > 0 {
2231 if let Some(parts) = parts {
2232 if let Some(i) = parts.iter().rposition(|part| !part.is_empty()) {
2233 if let Some((child_delimiter, _, _)) = delimiter_run(&children[i]) {
2234 if child_delimiter.starts_with(ch) {
2235 invalid_edge |= child_delimiter == delimiter;
2236 if let Some(child) = split_run(&parts[i], child_delimiter) {
2237 let before = child
2238 .lead
2239 .chars()
2240 .next_back()
2241 .or_else(|| neighbour_before(parts, i));
2242 let inside = child.core.chars().find(|c| *c != ch);
2243 let closes = before.is_some_and(|c| !flank_space(flank_character(c)))
2244 && flanks(before, inside);
2245 let opening = child_delimiter.len()
2246 + child.core.chars().take_while(|c| *c == ch).count();
2247 let closing = delimiter.len() + live_run_at_end(core, ch);
2248 invalid_edge |= closes && !rule_of_three_allows(opening, closing);
2249 }
2250 }
2251 }
2252 }
2253 }
2254 }
2255 invalid_edge
2256}
2257
2258fn pad_nested_outside(
2259 body: String,
2260 delimiter: &str,
2261 open_tag: &str,
2262 close_tag: &str,
2263 node: &InlineNode,
2264 ctx: &MarkdownContext,
2265) -> String {
2266 let core = body.trim();
2267 let children = match node {
2268 InlineNode::Emphasis(emphasis) => emphasis.children.as_slice(),
2269 _ => &[],
2270 };
2271 let invalid_edge = nested_edge_is_invalid(
2272 core,
2273 delimiter,
2274 children,
2275 ctx.rendered_children
2276 .get(&(node as *const InlineNode as usize)),
2277 );
2278 if invalid_edge && !core.is_empty() {
2279 let piece = RunPiece {
2280 lead: body[..body.len() - body.trim_start().len()].to_string(),
2281 core: core.to_string(),
2282 trail: body[body.trim_end().len()..].to_string(),
2283 };
2284 return spell_nested_html(node, piece, open_tag, close_tag, ctx);
2285 }
2286 pad_outside(body, delimiter, open_tag, close_tag)
2287}
2288
2289fn content_grows_the_run(core: &str, delimiter: &str) -> bool {
2303 let Some(ch) = delimiter.chars().next() else {
2304 return false;
2305 };
2306 if ch == '~' {
2307 return core.starts_with('~') || live_run_at_end(core, '~') > 0;
2308 }
2309 let lead = core.chars().take_while(|c| *c == ch).count();
2311 let trail = live_run_at_end(core, ch);
2312 (lead > 0 && delimiter.len() + lead == 2) || (trail > 0 && delimiter.len() + trail == 2)
2313}
2314
2315fn live_run_at_end(core: &str, ch: char) -> usize {
2318 let run = core.chars().rev().take_while(|c| *c == ch).count();
2319 if run == 0 {
2320 return 0;
2321 }
2322 let slashes = core
2323 .chars()
2324 .rev()
2325 .skip(run)
2326 .take_while(|c| *c == '\\')
2327 .count();
2328 if slashes % 2 == 1 {
2329 run - 1
2330 } else {
2331 run
2332 }
2333}
2334
2335fn render_inlines(nodes: &[InlineNode], ctx: &mut MarkdownContext, depth: usize) -> String {
2336 render_recorded_inlines(nodes, ctx, depth, None)
2337}
2338
2339fn render_recorded_inlines(
2340 nodes: &[InlineNode],
2341 ctx: &mut MarkdownContext,
2342 depth: usize,
2343 owner: Option<usize>,
2344) -> String {
2345 if depth > MAX_RENDER_DEPTH {
2346 crate::render_depth::record("markdown");
2347 return String::new();
2348 }
2349 let mut parts: Vec<String> = Vec::with_capacity(nodes.len());
2350 for node in nodes {
2351 parts.push(render_inline(node, ctx, depth));
2352 }
2353 let has_content = |part: &str| {
2354 visible_inline(part)
2355 .bytes()
2356 .any(|byte| !matches!(byte, b' ' | b'\t' | b'\r' | b'\n'))
2357 };
2358 for i in (0..parts.len()).rev() {
2359 if matches!(nodes[i], InlineNode::HardBreak(_)) && parts[i] == "\\\n" {
2360 let preceding_content = parts[..i].iter().any(|part| has_content(part));
2361 parts[i] = if preceding_content {
2362 "<br>"
2363 } else {
2364 "<br><!---->"
2365 }
2366 .to_owned();
2367 break;
2368 }
2369 if has_content(&parts[i]) {
2370 break;
2371 }
2372 }
2373 repair_nested_runs(nodes, &mut parts, ctx, owner.is_some());
2374 if nodes.len() > 1 {
2375 reflank_runs(nodes, &mut parts, ctx);
2376 unmerge_runs(nodes, &mut parts, ctx);
2377 }
2378 let result = parts.concat();
2379 if !ctx.rendered_children.is_empty() {
2380 for child in nodes {
2381 ctx.rendered_children
2382 .remove(&(child as *const InlineNode as usize));
2383 }
2384 }
2385 if let Some(owner) = owner {
2386 if nodes.iter().any(|node| delimiter_run(node).is_some()) {
2387 ctx.rendered_children.insert(owner, parts);
2388 }
2389 }
2390 result
2391}
2392
2393fn delimiter_run(node: &InlineNode) -> Option<(&'static str, &'static str, &'static str)> {
2397 let InlineNode::Emphasis(emphasis) = node else {
2398 return None;
2399 };
2400 match emphasis.kind {
2401 EmphasisKind::Italic => Some(("*", "<em>", "</em>")),
2402 EmphasisKind::Strong => Some(("**", "<strong>", "</strong>")),
2403 EmphasisKind::Strike => Some(("~~", "<del>", "</del>")),
2404 EmphasisKind::BoldItalic => Some(("***", "<strong><em>", "</em></strong>")),
2405 _ => None,
2406 }
2407}
2408
2409fn flank_punct(ch: char) -> bool {
2416 static PUNCT: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();
2417 PUNCT
2418 .get_or_init(|| regex::Regex::new(r"^[\p{P}\p{S}]$").expect("the flanking class is valid"))
2419 .is_match(ch.encode_utf8(&mut [0u8; 4]))
2420}
2421
2422fn flank_space(ch: char) -> bool {
2426 ch.is_whitespace() || ch == crate::NBSP_PLACEHOLDER
2427}
2428
2429fn flank_character(ch: char) -> char {
2434 CARRIERS.with(|slot| match carrier_slot(&slot.borrow(), ch) {
2435 Some(index) => carried_character(index),
2436 None => ch,
2437 })
2438}
2439
2440fn flanks(inside: Option<char>, outside: Option<char>) -> bool {
2452 let Some(inner) = inside else {
2453 return true;
2454 };
2455 if !flank_punct(flank_character(inner)) {
2456 return true;
2457 }
2458 let Some(outer) = outside else {
2459 return true;
2460 };
2461 let outer = flank_character(outer);
2462
2463 flank_space(outer) || flank_punct(outer)
2464}
2465
2466struct RunPiece {
2468 lead: String,
2469 core: String,
2470 trail: String,
2471}
2472
2473fn split_run(part: &str, delimiter: &str) -> Option<RunPiece> {
2477 let body = part.trim();
2478 if body.len() <= delimiter.len() * 2 {
2479 return None;
2480 }
2481 if !body.starts_with(delimiter) || !body.ends_with(delimiter) {
2482 return None;
2483 }
2484
2485 Some(RunPiece {
2486 lead: part[..part.len() - part.trim_start().len()].to_string(),
2487 core: body[delimiter.len()..body.len() - delimiter.len()].to_string(),
2488 trail: part[part.trim_end().len()..].to_string(),
2489 })
2490}
2491
2492fn spell_nested_html(
2493 node: &InlineNode,
2494 piece: RunPiece,
2495 open_tag: &str,
2496 close_tag: &str,
2497 ctx: &MarkdownContext,
2498) -> String {
2499 let mut core = piece.core;
2500 let parent = ctx.delimiter_ancestors.last().copied();
2501 if matches!(parent, Some("**" | "***")) {
2502 if let (InlineNode::Emphasis(emphasis), Some(parts)) = (
2503 node,
2504 ctx.rendered_children
2505 .get(&(node as *const InlineNode as usize)),
2506 ) {
2507 let body = parts.concat();
2508 if body.trim() == core && parts.len() == emphasis.children.len() {
2509 let repaired = parts
2510 .iter()
2511 .zip(&emphasis.children)
2512 .map(|(part, child)| {
2513 if let InlineNode::Emphasis(child) = child {
2514 if child.kind == EmphasisKind::Strong {
2515 if let Some(nested) = split_run(part, "**") {
2516 return format!(
2517 "{}<strong>{}</strong>{}",
2518 nested.lead, nested.core, nested.trail
2519 );
2520 }
2521 }
2522 }
2523 part.clone()
2524 })
2525 .collect::<Vec<_>>()
2526 .concat();
2527 let lead = body.len() - body.trim_start().len();
2528 let trail = body.len() - body.trim_end().len();
2529 core = repaired[lead..repaired.len() - trail].to_string();
2530 }
2531 }
2532 }
2533 format!("{}{open_tag}{core}{close_tag}{}", piece.lead, piece.trail)
2534}
2535
2536fn repair_nested_runs(
2537 nodes: &[InlineNode],
2538 parts: &mut [String],
2539 ctx: &MarkdownContext,
2540 direct_parent: bool,
2541) {
2542 let ancestors = &ctx.delimiter_ancestors;
2543 if ancestors.is_empty() {
2544 return;
2545 }
2546 let parent = ancestors.last().copied();
2547 for i in 0..parts.len() {
2548 let Some((delimiter, open_tag, close_tag)) = delimiter_run(&nodes[i]) else {
2549 continue;
2550 };
2551 let Some(piece) = split_run(&parts[i], delimiter) else {
2552 continue;
2553 };
2554 let edge_separated = direct_parent
2555 && parent == Some(delimiter)
2556 && ancestors.iter().filter(|run| Some(**run) == parent).count() == 1
2557 && !ancestors.contains(&"***")
2558 && (neighbour_before(parts, i).is_none() || neighbour_after(parts, i).is_none());
2559 if edge_separated {
2560 continue;
2561 }
2562 let ch = delimiter.chars().next().unwrap();
2563 let before = piece
2564 .lead
2565 .chars()
2566 .next_back()
2567 .or_else(|| neighbour_before(parts, i));
2568 let inside = piece.core.chars().find(|c| *c != ch);
2569 let closes =
2570 before.is_some_and(|c| !flank_space(flank_character(c))) && flanks(before, inside);
2571 let opening = delimiter.len() + piece.core.chars().take_while(|c| *c == ch).count();
2572 let repeated_strong =
2573 delimiter == "**" && (parent == Some("**") || ancestors.contains(&"***"));
2574 let steals = closes
2575 && ancestors.iter().any(|run| {
2576 run.starts_with(ch) && (ch == '~' || rule_of_three_allows(run.len(), opening))
2577 });
2578 if repeated_strong || steals {
2579 parts[i] = spell_nested_html(&nodes[i], piece, open_tag, close_tag, ctx);
2580 }
2581 }
2582}
2583
2584fn reflank_runs(nodes: &[InlineNode], parts: &mut [String], ctx: &MarkdownContext) {
2597 for i in 0..parts.len() {
2598 let Some((delimiter, open_tag, close_tag)) = delimiter_run(&nodes[i]) else {
2599 continue;
2600 };
2601 let Some(piece) = split_run(&parts[i], delimiter) else {
2602 continue;
2603 };
2604 let before = piece
2605 .lead
2606 .chars()
2607 .next_back()
2608 .or_else(|| neighbour_before(parts, i));
2609 let after = piece
2610 .trail
2611 .chars()
2612 .next()
2613 .or_else(|| neighbour_after(parts, i));
2614 let (outer_kind, inner_kind, nested) = if delimiter == "*" {
2618 (EmphasisKind::Italic, EmphasisKind::Strong, "**")
2619 } else {
2620 (EmphasisKind::Strong, EmphasisKind::Italic, "*")
2621 };
2622 if (delimiter == "*" || delimiter == "**")
2623 && ctx.delimiter_ancestors.is_empty()
2624 && matches!(&nodes[i], InlineNode::Emphasis(emphasis)
2625 if emphasis.kind == outer_kind
2626 && emphasis.children.len() == 1
2627 && matches!(&emphasis.children[0], InlineNode::Emphasis(child)
2628 if child.kind == inner_kind))
2629 && piece.core.len() > nested.len() * 2
2630 && piece.core.starts_with(nested)
2631 && piece.core.ends_with(nested)
2632 {
2633 let inner = &piece.core[nested.len()..piece.core.len() - nested.len()];
2634 if flanks(inner.chars().next(), before) && flanks(inner.chars().next_back(), after) {
2635 continue;
2636 }
2637 }
2638 if flanks(piece.core.chars().next(), before)
2639 && flanks(piece.core.chars().next_back(), after)
2640 {
2641 continue;
2642 }
2643 parts[i] = spell_nested_html(&nodes[i], piece, open_tag, close_tag, ctx);
2644 }
2645}
2646
2647fn neighbour_before(parts: &[String], i: usize) -> Option<char> {
2648 parts[..i]
2649 .iter()
2650 .rev()
2651 .find_map(|p| visible_inline(p).chars().next_back())
2652}
2653
2654fn neighbour_after(parts: &[String], i: usize) -> Option<char> {
2655 parts[i + 1..]
2656 .iter()
2657 .find_map(|p| visible_inline(p).chars().next())
2658}
2659
2660fn is_run_character(ch: char) -> bool {
2664 ch == '*' || ch == '~'
2665}
2666
2667fn rule_of_three_allows(opener: usize, closer: usize) -> bool {
2671 (opener + closer) % 3 != 0 || (opener % 3 == 0 && closer % 3 == 0)
2672}
2673
2674fn merged_run_reads_back(
2688 ch: char,
2689 m: usize,
2690 n: usize,
2691 inner_left: Option<char>,
2692 inner_right: Option<char>,
2693) -> bool {
2694 if !flanks(inner_left, inner_right) || !flanks(inner_right, inner_left) {
2695 return false;
2696 }
2697 if ch != '*' {
2698 return false;
2699 }
2700 let summed = m + n;
2701
2702 rule_of_three_allows(m, summed) && rule_of_three_allows(summed, n)
2703}
2704
2705fn trailing_run(part: &str, ch: char) -> (usize, Option<char>) {
2707 let head = part.trim_end_matches(ch);
2708 let run = part.chars().count() - head.chars().count();
2709 let escaped = head.chars().rev().take_while(|c| *c == '\\').count() % 2 == 1;
2713 (run - usize::from(escaped), head.chars().next_back())
2714}
2715
2716fn leading_run(part: &str, ch: char) -> (usize, Option<char>) {
2718 let tail = part.trim_start_matches(ch);
2719
2720 (
2721 part.chars().count() - tail.chars().count(),
2722 tail.chars().next(),
2723 )
2724}
2725
2726fn respell(nodes: &[InlineNode], parts: &mut [String], at: usize, ctx: &MarkdownContext) -> bool {
2728 let Some((delimiter, open_tag, close_tag)) = delimiter_run(&nodes[at]) else {
2729 return false;
2730 };
2731 let Some(piece) = split_run(&parts[at], delimiter) else {
2732 return false;
2733 };
2734 parts[at] = spell_nested_html(&nodes[at], piece, open_tag, close_tag, ctx);
2735
2736 true
2737}
2738
2739fn unmerge_runs(nodes: &[InlineNode], parts: &mut [String], ctx: &MarkdownContext) {
2756 let filled: Vec<usize> = (0..parts.len()).filter(|i| !parts[*i].is_empty()).collect();
2757 for pair in filled.windows(2) {
2758 let (left, right) = (pair[0], pair[1]);
2759 let Some(ch) = parts[left].chars().next_back() else {
2760 continue;
2761 };
2762 if !is_run_character(ch) || !parts[right].starts_with(ch) {
2763 continue;
2764 }
2765 let (m, before) = trailing_run(&parts[left], ch);
2766 if m == 0 {
2771 continue;
2772 }
2773 let (n, after) = leading_run(&parts[right], ch);
2774 let inner_left = before.or_else(|| neighbour_before(parts, left));
2775 let inner_right = after.or_else(|| neighbour_after(parts, right));
2776 if merged_run_reads_back(ch, m, n, inner_left, inner_right) {
2777 continue;
2778 }
2779 if !respell(nodes, parts, right, ctx) {
2780 respell(nodes, parts, left, ctx);
2781 }
2782 }
2783}
2784
2785fn render_attributed_emphasis(
2786 emphasis: &Emphasis,
2787 ctx: &mut MarkdownContext,
2788 depth: usize,
2789) -> Option<String> {
2790 let (open, close) = match emphasis.kind {
2791 EmphasisKind::Italic => ("<em", "</em>"),
2792 EmphasisKind::Strong | EmphasisKind::BoldItalic => ("<strong", "</strong>"),
2793 EmphasisKind::Strike => ("<del", "</del>"),
2794 _ => return None,
2795 };
2796 emphasis.attrs.as_ref()?;
2797 let attrs = crate::render::render_attrs(&emphasis.attrs);
2798 if attrs.is_empty() {
2799 return None;
2800 }
2801 let inner = if emphasis.kind == EmphasisKind::BoldItalic {
2804 let italic = InlineNode::Emphasis(Emphasis {
2805 attrs: None,
2806 kind: EmphasisKind::Italic,
2807 children: emphasis.children.clone(),
2808 pos: None,
2809 });
2810 render_inlines(std::slice::from_ref(&italic), ctx, depth + 1)
2811 } else {
2812 render_inlines(&emphasis.children, ctx, depth + 1)
2813 };
2814 Some(format!("{open}{attrs}>{inner}{close}"))
2815}
2816
2817thread_local! {
2818 static CARRIER_WRITE_ERROR: std::cell::RefCell<Option<crate::RenderCarveError>> = const { std::cell::RefCell::new(None) };
2819 static INLINE_CARRIER_PATTERN: std::cell::RefCell<Option<regex::Regex>> = const { std::cell::RefCell::new(None) };
2820 static INLINE_CARRIER_PREFIX: std::cell::RefCell<String> = const { std::cell::RefCell::new(String::new()) };
2821}
2822
2823fn inline_carrier_line(payload: &str) -> String {
2824 INLINE_CARRIER_PREFIX.with(|prefix| {
2825 let prefix = prefix.borrow();
2826 format!("{prefix}{}{prefix}", carrier_line(payload))
2827 })
2828}
2829
2830fn inline_marker_ranges(text: &str) -> Vec<std::ops::Range<usize>> {
2831 INLINE_CARRIER_PATTERN.with(|pattern| {
2832 pattern
2833 .borrow()
2834 .as_ref()
2835 .map(|pattern| pattern.find_iter(text).map(|found| found.range()).collect())
2836 .unwrap_or_default()
2837 })
2838}
2839
2840fn visible_inline(text: &str) -> std::borrow::Cow<'_, str> {
2841 INLINE_CARRIER_PATTERN.with(|pattern| match pattern.borrow().as_ref() {
2842 Some(pattern) => pattern.replace_all(text, ""),
2843 None => std::borrow::Cow::Borrowed(text),
2844 })
2845}
2846
2847fn inline_padding(text: &str, space: impl Fn(char) -> bool) -> (String, String, String) {
2848 let visible = visible_inline(text);
2849 let start = visible.len() - visible.trim_start_matches(&space).len();
2850 let end = visible.trim_end_matches(&space).len().max(start);
2851 let mut core = String::new();
2852 let mut cursor = 0;
2853 let mut position = 0;
2854 for range in inline_marker_ranges(text) {
2855 let chunk = &text[cursor..range.start];
2856 let from = start.saturating_sub(position).min(chunk.len());
2857 let to = end.saturating_sub(position).min(chunk.len()).max(from);
2858 core.push_str(&chunk[from..to]);
2859 core.push_str(&text[range.clone()]);
2860 position += chunk.len();
2861 cursor = range.end;
2862 }
2863 let chunk = &text[cursor..];
2864 let from = start.saturating_sub(position).min(chunk.len());
2865 let to = end.saturating_sub(position).min(chunk.len()).max(from);
2866 core.push_str(&chunk[from..to]);
2867 (visible[..start].to_owned(), core, visible[end..].to_owned())
2868}
2869
2870fn restore_inline_change(marked: &str, before: &str, after: &str) -> String {
2871 if before == after {
2872 return marked.to_owned();
2873 }
2874 let start = before
2875 .chars()
2876 .zip(after.chars())
2877 .take_while(|(a, b)| a == b)
2878 .map(|(ch, _)| ch.len_utf8())
2879 .sum::<usize>();
2880 let suffix = before[start..]
2881 .chars()
2882 .rev()
2883 .zip(after[start..].chars().rev())
2884 .take_while(|(a, b)| a == b)
2885 .map(|(ch, _)| ch.len_utf8())
2886 .sum::<usize>();
2887 let end = before.len() - suffix;
2888 let ranges = inline_marker_ranges(marked);
2889 let offset = |position: usize| {
2890 let mut at = position;
2891 let mut removed = 0;
2892 for range in &ranges {
2893 if range.start - removed > position {
2894 break;
2895 }
2896 at += range.len();
2897 removed += range.len();
2898 }
2899 at
2900 };
2901 let from = offset(start);
2902 let to = offset(end);
2903 let mut out = marked[..from].to_owned();
2904 for range in ranges
2905 .iter()
2906 .filter(|range| range.start >= from && range.end <= to)
2907 {
2908 out.push_str(&marked[range.clone()]);
2909 }
2910 out.push_str(&after[start..after.len() - suffix]);
2911 out.push_str(&marked[to..]);
2912 out
2913}
2914
2915fn render_inline(node: &InlineNode, ctx: &mut MarkdownContext, depth: usize) -> String {
2916 let body = render_inline_body(node, ctx, depth);
2917 if !ctx.roundtrip {
2918 return body;
2919 }
2920 let attributes = |attrs: &Option<Attrs>| {
2921 let spelled = crate::render_carve::render_carrier_attrs(attrs);
2922 if spelled.is_empty() {
2923 "{}".to_owned()
2924 } else {
2925 spelled
2926 }
2927 };
2928 match node {
2929 InlineNode::Link(link)
2930 if ctx.link_depth == 0 && !crate::parse::is_unresolved_reference(link) =>
2931 {
2932 let attrs = attributes(&link.attrs);
2933 if attrs == "{}" {
2934 body
2935 } else {
2936 format!("{body}{}", inline_carrier_line(&attrs))
2937 }
2938 }
2939 InlineNode::Image(image) if !crate::parse::is_unresolved_image(image) => {
2940 let attrs = attributes(&image.attrs);
2941 if attrs == "{}" {
2942 body
2943 } else {
2944 format!("{body}{}", inline_carrier_line(&attrs))
2945 }
2946 }
2947 InlineNode::Span(span) => format!(
2948 "{}{body}{}",
2949 inline_carrier_line("["),
2950 inline_carrier_line(&format!("]{}", attributes(&span.attrs)))
2951 ),
2952 InlineNode::CrossRef(reference) if ctx.link_depth == 0 && body.starts_with('[') => {
2953 let mut document = crate::parse::parse("x");
2954 if let BlockNode::Paragraph(paragraph) = &mut document.children[0] {
2955 paragraph.children = vec![InlineNode::CrossRef(CrossRef {
2956 target: reference.target.clone(),
2957 href: None,
2958 pos: None,
2959 })];
2960 }
2961 let payload = crate::render_carve::render_carve(&document).unwrap_or_default();
2962 format!(
2963 "{}{body}",
2964 inline_carrier_line(payload.trim_end_matches('\n'))
2965 )
2966 }
2967 _ => body,
2968 }
2969}
2970
2971fn render_inline_body(node: &InlineNode, ctx: &mut MarkdownContext, depth: usize) -> String {
2972 if depth > MAX_RENDER_DEPTH {
2973 crate::render_depth::record("markdown");
2974 return String::new();
2975 }
2976 match node {
2977 InlineNode::EscapedText(text) => {
2994 let mut chars = text.value.chars();
2995 match (chars.next(), chars.next()) {
2996 (Some(ch), None) if AUTHORED_INERT.contains(&ch) => ch.to_string(),
2997 (Some(ch), None) if authored_defers_to_the_line(ch) => {
2998 positional_sentinel().to_string()
2999 }
3000 _ => format!("\\{}", text.value),
3001 }
3002 }
3003 InlineNode::Text(text) => {
3004 if is_literal_crossref(&text.value) {
3005 strip_controls(&text.value)
3006 } else {
3007 escape_text(&resolve_nbsp(&strip_controls(&text.value)))
3008 }
3009 }
3010 InlineNode::SmartPunctuation(s) => strip_controls(smart_punctuation_text(s)),
3016 InlineNode::Emphasis(emphasis) => {
3017 if let Some(rendered) = render_attributed_emphasis(emphasis, ctx, depth) {
3018 return rendered;
3019 }
3020 match emphasis.kind {
3021 EmphasisKind::Italic => {
3022 ctx.delimiter_ancestors.push("*");
3023 let body = render_recorded_inlines(
3024 &emphasis.children,
3025 ctx,
3026 depth + 1,
3027 Some(node as *const InlineNode as usize),
3028 );
3029 ctx.delimiter_ancestors.pop();
3030 pad_nested_outside(body, "*", "<em>", "</em>", node, ctx)
3031 }
3032 EmphasisKind::Strong => {
3033 ctx.delimiter_ancestors.push(if emphasis.children.len() == 1 && matches!(&emphasis.children[0], InlineNode::Emphasis(child) if child.kind == EmphasisKind::Italic) { "***" } else { "**" });
3034 let body = render_recorded_inlines(
3035 &emphasis.children,
3036 ctx,
3037 depth + 1,
3038 Some(node as *const InlineNode as usize),
3039 );
3040 ctx.delimiter_ancestors.pop();
3041 pad_nested_outside(body, "**", "<strong>", "</strong>", node, ctx)
3042 }
3043 EmphasisKind::Underline => {
3044 format!(
3045 "<u{}>{}</u>",
3046 crate::render::render_attrs(&emphasis.attrs),
3047 render_inlines(&emphasis.children, ctx, depth + 1)
3048 )
3049 }
3050 EmphasisKind::SmallCaps => {
3055 format!(
3056 "<span{}>{}</span>",
3057 crate::render::render_attrs_with_base_class(&emphasis.attrs, "smallcaps"),
3058 render_inlines(&emphasis.children, ctx, depth + 1)
3059 )
3060 }
3061 EmphasisKind::Strike => {
3062 ctx.delimiter_ancestors.push("~~");
3063 let body = render_recorded_inlines(
3064 &emphasis.children,
3065 ctx,
3066 depth + 1,
3067 Some(node as *const InlineNode as usize),
3068 );
3069 ctx.delimiter_ancestors.pop();
3070 pad_nested_outside(body, "~~", "<del>", "</del>", node, ctx)
3071 }
3072 EmphasisKind::Sub => {
3073 format!(
3074 "<sub{}>{}</sub>",
3075 crate::render::render_attrs(&emphasis.attrs),
3076 render_inlines(&emphasis.children, ctx, depth + 1)
3077 )
3078 }
3079 EmphasisKind::Super => {
3080 format!(
3081 "<sup{}>{}</sup>",
3082 crate::render::render_attrs(&emphasis.attrs),
3083 render_inlines(&emphasis.children, ctx, depth + 1)
3084 )
3085 }
3086 EmphasisKind::Highlight => {
3087 format!(
3088 "<mark{}>{}</mark>",
3089 crate::render::render_attrs(&emphasis.attrs),
3090 render_inlines(&emphasis.children, ctx, depth + 1)
3091 )
3092 }
3093 EmphasisKind::BoldItalic => {
3094 ctx.delimiter_ancestors.push("***");
3095 let body = render_recorded_inlines(
3096 &emphasis.children,
3097 ctx,
3098 depth + 1,
3099 Some(node as *const InlineNode as usize),
3100 );
3101 ctx.delimiter_ancestors.pop();
3102 pad_nested_outside(body, "***", "<strong><em>", "</em></strong>", node, ctx)
3103 }
3104 }
3105 }
3106 InlineNode::Code(code) => render_code(&resolve_nbsp(&strip_controls(&code.value))),
3107 InlineNode::Link(link) => render_link(link, ctx, depth + 1),
3108 InlineNode::Image(image) => render_image(image),
3109 InlineNode::Span(span) => {
3110 let authored = span
3116 .attrs
3117 .as_ref()
3118 .and_then(|a| a.key_values.get("abbr"))
3119 .cloned();
3120 let Some(authored) = authored else {
3121 return render_inlines(&span.children, ctx, depth + 1);
3122 };
3123 let previous = ctx.suppress_automatic_abbreviation;
3124 ctx.suppress_automatic_abbreviation = true;
3125 let inner = render_inlines(&span.children, ctx, depth + 1);
3126 ctx.suppress_automatic_abbreviation = previous;
3127 if authored.is_empty() || !crate::abbr_budget::try_spend(authored.len()) {
3128 return inner;
3129 }
3130 let title = escape_md_html(&strip_controls(&authored)).replace('"', """);
3131 format!("<abbr title=\"{title}\">{inner}</abbr>")
3132 }
3133 InlineNode::Ruby(r) => {
3134 let mut out = format!("<ruby{}>", crate::render::render_attrs(&r.attrs));
3135 for pair in &r.pairs {
3136 out.push_str(&render_inlines(&pair.base, ctx, depth + 1));
3137 out.push_str("<rp>(</rp><rt>");
3138 out.push_str(&render_inlines(&pair.annotation, ctx, depth + 1));
3139 out.push_str("</rt><rp>)</rp>");
3140 }
3141 out.push_str("</ruby>");
3142 out
3143 }
3144 InlineNode::Math(math) => {
3145 let content = escape_md_html(&strip_controls(&math.content));
3150 let spelled = if math.display {
3151 format!("$${content}$$")
3152 } else {
3153 format!("${content}$")
3154 };
3155 match (&math.label, math.number) {
3158 (Some(label), Some(number)) => format!(
3159 "{spelled} {} {number}",
3160 escape_md_html(&strip_controls(label))
3161 ),
3162 _ => spelled,
3163 }
3164 }
3165 InlineNode::RawInline(raw) => {
3166 if raw.format == "html" {
3167 crate::render_loss::record_markdown_loss(
3168 MarkdownLoss::RawHtml,
3169 RawNodeType::Inline,
3170 raw.pos.as_ref(),
3171 );
3172 escape_md_html(&strip_controls(&raw.content))
3173 } else {
3174 crate::render_loss::record_raw_drop(
3175 &raw.format,
3176 crate::RawNodeType::Inline,
3177 raw.pos.clone(),
3178 );
3179 String::new()
3180 }
3181 }
3182 InlineNode::LiteralInline(lit) => {
3183 escape_text(&resolve_nbsp(&strip_controls(&lit.content)))
3187 }
3188 InlineNode::Symbol(symbol) => format!(":{}:", symbol.name),
3189 InlineNode::AutoLink(link) => {
3190 let raw_label = strip_controls(&link.text);
3191 let destination =
3192 encode_markdown_destination(&link.href, link.pos.as_ref(), DeniedSink::Destination);
3193 let title_value = link
3194 .attrs
3195 .as_ref()
3196 .and_then(|attrs| attrs.key_values.get("title"));
3197 if title_value.is_none()
3198 && markdown_angle_autolink(&raw_label, &link.href, &destination)
3199 {
3200 return format!("<{raw_label}>");
3201 }
3202 IN_LINK_TEXT.with(|d| d.set(d.get() + 1));
3203
3204 let label = escape_text(&raw_label);
3205
3206 IN_LINK_TEXT.with(|d| d.set(d.get() - 1));
3207 let title = title_value
3208 .map(|title| format!(" \"{}\"", escape_md_title(&strip_controls(title))))
3209 .unwrap_or_default();
3210 format!("[{label}]({destination}{title})")
3211 }
3212 InlineNode::Mention(mention) => format!("@{}", strip_controls(&mention.user)),
3213 InlineNode::Tag(tag) => escape_text(&format!("#{}", strip_controls(&tag.name))),
3214 InlineNode::Extension(extension) => render_inlines(&extension.children, ctx, depth + 1),
3215 InlineNode::Abbreviation(abbr) => {
3216 let text = escape_md_html(&strip_controls(&abbr.abbr));
3220 if ctx.suppress_automatic_abbreviation {
3223 return text;
3224 }
3225 if crate::abbr_budget::try_spend(abbr.expansion.len()) {
3228 let title = escape_md_html(&strip_controls(&abbr.expansion)).replace('"', """);
3231 format!("<abbr title=\"{title}\">{text}</abbr>")
3232 } else {
3233 text
3234 }
3235 }
3236 InlineNode::Footnote(footnote) => {
3237 if let Some(inline) = &footnote.inline {
3238 let outer = std::mem::replace(&mut ctx.link_depth, 0);
3243 let rendered = render_inlines(inline, ctx, depth + 1);
3244 ctx.link_depth = outer;
3245 format!("^[{rendered}]")
3246 } else {
3247 let id = strip_controls(footnote.id.as_deref().unwrap_or(""));
3248 if ctx.defined_footnotes.contains(&id) {
3249 format!("[^{}]", escape_md_html(&id))
3254 } else {
3255 format!("\\[^{}\\]", escape_md_html(&id).replace("\\", "\\\\"))
3263 }
3264 }
3265 }
3266 InlineNode::NonBreakingSpace(_) => "\u{00a0}".to_string(),
3267 InlineNode::SoftBreak(_) => "\n".to_string(),
3268 InlineNode::CellWrap(_) => " ".to_string(),
3269 InlineNode::HardBreak(_) if ctx.single_line_depth > 0 => "<br>".to_string(),
3276 InlineNode::HardBreak(_) => "\\\n".to_string(),
3277 InlineNode::CriticInsert(insert) => {
3278 format!(
3279 "<ins>{}</ins>",
3280 render_inlines(&insert.children, ctx, depth + 1)
3281 )
3282 }
3283 InlineNode::CriticDelete(delete) => {
3284 format!(
3288 "<del class=\"critic-delete\">{}</del>",
3289 render_inlines(&delete.children, ctx, depth + 1)
3290 )
3291 }
3292 InlineNode::CriticSubstitute(sub) => format!(
3293 "<del class=\"critic-delete\">{}</del><ins>{}</ins>",
3294 render_inlines(&sub.old, ctx, depth + 1),
3295 render_inlines(&sub.new, ctx, depth + 1)
3296 ),
3297 InlineNode::Comment(_) => String::new(),
3307 InlineNode::CriticComment(c) => format!(
3308 "<span class=\"critic-comment\">{}</span>",
3309 escape_text(&strip_controls(&c.text))
3310 ),
3311 InlineNode::CrossRef(crossref) => {
3323 let resolved = ctx
3329 .crossref_index
3330 .resolve(&crossref.target)
3331 .map(|(id, title)| {
3332 (
3333 id.to_string(),
3334 if crate::abbr_budget::label_rejected(&crossref.target) {
3335 String::new()
3336 } else {
3337 title.to_string()
3338 },
3339 )
3340 });
3341 match resolved {
3342 None => {
3349 let target = escape_md_html(&strip_controls(&crossref.target));
3350 let target = if ctx.link_depth > 0 {
3353 target.replace(']', "\\]")
3354 } else {
3355 target
3356 };
3357 format!("</#{target}>")
3358 }
3359 Some((id, title)) => {
3360 let rejected = crate::abbr_budget::label_rejected(&crossref.target);
3361 let label = if rejected {
3362 None
3363 } else {
3364 ctx.crossref_index.label(&id)
3365 };
3366 let links = ctx.link_depth == 0 && ctx.heading_slugs.contains_key(&id);
3368 if links {
3369 IN_LINK_TEXT.with(|d| d.set(d.get() + 1));
3370 }
3371 let text = match &label {
3372 Some(nodes) => render_inlines(nodes, ctx, depth + 1),
3373 None => escape_text(&strip_controls(if rejected {
3374 &crossref.target
3375 } else {
3376 &title
3377 })),
3378 };
3379 let text = if crate::abbr_budget::try_spend_label(
3383 resolved_len(&text),
3384 &crossref.target,
3385 ) {
3386 text
3387 } else {
3388 escape_text(&strip_controls(&crossref.target))
3389 };
3390 if links {
3391 IN_LINK_TEXT.with(|d| d.set(d.get() - 1));
3392 }
3393 match ctx.heading_slugs.get(&id) {
3398 Some(slug) if ctx.link_depth == 0 => format!("[{text}](#{slug})"),
3399 _ => text,
3400 }
3401 }
3402 }
3403 }
3404 InlineNode::CitationGroup(group) => strip_controls(&group.raw),
3406 InlineNode::CaptionNumber(number) => number
3407 .number
3408 .map(|n| n.to_string())
3409 .unwrap_or_else(|| "#".to_string()),
3410 }
3411}
3412
3413fn render_link(node: &Link, ctx: &mut MarkdownContext, depth: usize) -> String {
3414 if crate::parse::is_unresolved_reference(node) {
3415 return escape_text(&strip_controls(node.raw_ref.as_deref().unwrap_or_default()));
3416 }
3417 ctx.link_depth += 1;
3418 IN_LINK_TEXT.with(|d| d.set(d.get() + 1));
3419
3420 let children = unwrap_nested_anchors(&node.children);
3422 let ancestors = std::mem::take(&mut ctx.delimiter_ancestors);
3423 let text = render_inlines(children.as_ref(), ctx, depth);
3424 ctx.delimiter_ancestors = ancestors;
3425 IN_LINK_TEXT.with(|d| d.set(d.get() - 1));
3426
3427 ctx.link_depth -= 1;
3428 if ctx.roundtrip
3429 && node.attrs.is_none()
3430 && ctx.single_line_depth == 0
3431 && !fragment_id(&node.href).is_some_and(|id| ctx.heading_slugs.contains_key(id))
3432 {
3433 if let Some(label) = &node.ref_label {
3434 if let Some(def) = ctx.native_references.get(&crate::parse::label_key(label)) {
3435 if def.href == node.href && def.title == node.title {
3436 if node
3437 .raw_ref
3438 .as_deref()
3439 .is_some_and(|raw| raw.ends_with("[]"))
3440 && text == def.label
3441 {
3442 return format!("[{text}][]");
3443 }
3444 return format!("[{text}][{}]", def.label);
3445 }
3446 }
3447 }
3448 }
3449 if let Some(slug) = fragment_id(&node.href).and_then(|id| ctx.heading_slugs.get(id)) {
3453 let destination = format!("#{slug}");
3454 if let Some(title) = node.title.as_ref().or_else(|| {
3455 node.attrs
3456 .as_ref()
3457 .and_then(|attrs| attrs.key_values.get("title"))
3458 }) {
3459 format!(
3460 "[{text}]({destination} \"{}\")",
3461 escape_md_title(&strip_controls(title))
3462 )
3463 } else {
3464 format!("[{text}]({destination})")
3465 }
3466 } else {
3467 let mut href =
3468 encode_markdown_destination(&node.href, node.pos.as_ref(), DeniedSink::Destination);
3469 if node.href.is_empty() {
3470 href = "<>".to_owned();
3471 }
3472 if let Some(title) = node.title.as_ref().or_else(|| {
3473 node.attrs
3474 .as_ref()
3475 .and_then(|attrs| attrs.key_values.get("title"))
3476 }) {
3477 format!(
3478 "[{text}]({href} \"{}\")",
3479 escape_md_title(&strip_controls(title))
3480 )
3481 } else {
3482 format!("[{text}]({href})")
3483 }
3484 }
3485}
3486
3487fn render_image(node: &Image) -> String {
3488 if crate::parse::is_unresolved_image(node) {
3489 return escape_text(&strip_controls(node.raw_ref.as_deref().unwrap_or_default()));
3490 }
3491 let mut src =
3492 encode_markdown_destination(&node.src, node.pos.as_ref(), DeniedSink::ImageSource);
3493 if node.src.is_empty() {
3494 src = "<>".to_owned();
3495 }
3496 let alt = escape_md_label(&strip_controls(&node.alt));
3497 if let Some(title) = node.title.as_ref().or_else(|| {
3498 node.attrs
3499 .as_ref()
3500 .and_then(|attrs| attrs.key_values.get("title"))
3501 }) {
3502 format!(
3503 "",
3504 alt,
3505 src,
3506 escape_md_title(&strip_controls(title))
3507 )
3508 } else {
3509 format!("", alt, src)
3510 }
3511}
3512
3513fn escape_md_title(title: &str) -> String {
3514 neutralize_char_refs(&title.replace('\\', "\\\\").replace('"', "\\\""))
3515}
3516
3517fn escape_md_label(label: &str) -> String {
3518 IN_LINK_TEXT.with(|depth| depth.set(depth.get() + 1));
3519 let escaped = escape_text(label);
3520 IN_LINK_TEXT.with(|depth| depth.set(depth.get() - 1));
3521 escaped
3522}
3523
3524fn sanitize_code_lang(lang: &str) -> String {
3525 let token = strip_controls(lang)
3528 .split_whitespace()
3529 .next()
3530 .unwrap_or("")
3531 .to_string();
3532 if token.contains('`') {
3533 String::new()
3534 } else {
3535 token
3536 }
3537}
3538
3539fn escape_code_title(title: &str) -> String {
3540 strip_controls(title)
3541 .replace('\\', "\\\\")
3542 .replace('"', "\\\"")
3543}
3544
3545fn safe_fence(content: &str, min: usize) -> String {
3546 safe_fence_with_marker(content, min, '`')
3547}
3548
3549fn safe_fence_with_marker(content: &str, min: usize, marker: char) -> String {
3550 let mut longest = 0usize;
3551 let mut current = 0usize;
3552 for ch in content.chars() {
3553 if ch == marker {
3554 current += 1;
3555 longest = longest.max(current);
3556 } else {
3557 current = 0;
3558 }
3559 }
3560 marker.to_string().repeat(min.max(longest + 1))
3561}
3562
3563fn render_code(content: &str) -> String {
3564 if content.is_empty() {
3565 return "<code></code>".to_owned();
3566 }
3567 if content.contains('\n') || content.contains('\t') {
3568 let mut escaped = String::with_capacity(content.len());
3569 for character in content.chars() {
3570 if character == '\n' {
3571 escaped.push_str("<!----> <!---->");
3572 } else if matches!(character, ' ' | '\t') || character.is_ascii_punctuation() {
3573 write!(&mut escaped, "&#{};", u32::from(character)).expect("writing to a String");
3574 if character == '@' {
3575 escaped.push_str("<!---->");
3576 }
3577 } else {
3578 escaped.push(character);
3579 }
3580 }
3581 return format!("<code>{escaped}</code>");
3582 }
3583 let fence = safe_fence(content, 1);
3584 let needs_padding = content.starts_with('`')
3585 || content.ends_with('`')
3586 || (content.starts_with(' ')
3587 && content.ends_with(' ')
3588 && content.chars().any(|ch| ch != ' '));
3589 if needs_padding {
3590 format!("{fence} {content} {fence}")
3591 } else {
3592 format!("{fence}{content}{fence}")
3593 }
3594}
3595
3596fn fragment_id(href: &str) -> Option<&str> {
3597 href.strip_prefix('#')
3598}
3599
3600fn prepend_label(body: String, label: Option<&str>) -> String {
3604 match label {
3605 Some(label) if !label.is_empty() => {
3606 let l = pad_outside_on_its_own_line(escape_text(label), "**", "<strong>", "</strong>");
3607 if body.is_empty() {
3608 format!("{l}\n\n")
3609 } else {
3610 format!("{l}\n\n{body}")
3611 }
3612 }
3613 _ => body,
3614 }
3615}
3616
3617fn escape_text(text: &str) -> String {
3618 CARRIERS.with(|slot| {
3619 let mut out = String::with_capacity(text.len());
3620 let carriers = slot.borrow();
3624 let mut inserted = [0usize; CARRIER_COUNT];
3625 let mut carry = |slot: usize, out: &mut String| {
3626 inserted[slot] += 1;
3627 out.push_str(&carriers[slot]);
3628 };
3629 let autolinks = IN_LINK_TEXT.with(std::cell::Cell::get) == 0;
3630 let bytes = text.as_bytes();
3634 for (at, ch) in text.char_indices() {
3635 if autolinks && ch == ':' && may_be_scheme_colon(bytes, at) {
3638 carry(C_SCHEME_COLON, &mut out);
3639 continue;
3640 }
3641 if autolinks && ch == '.' && may_be_www_dot(bytes, at) {
3642 carry(C_WWW_DOT, &mut out);
3643 continue;
3644 }
3645 if autolinks && ch == '@' {
3649 carry(C_EMAIL_AT, &mut out);
3650 continue;
3651 }
3652 match ch {
3653 '<' => {
3689 carry(C_ANGLE, &mut out);
3690 continue;
3691 }
3692 '&' => {
3693 carry(C_AMPERSAND, &mut out);
3694 continue;
3695 }
3696 '!' => {
3697 carry(C_BANG, &mut out);
3698 continue;
3699 }
3700 '_' => {
3704 carry(C_UNDERSCORE, &mut out);
3705 continue;
3706 }
3707 '#' => {
3708 carry(C_POSITIONAL_HASH, &mut out);
3709 continue;
3710 }
3711 '[' => {
3712 if !autolinks {
3713 out.push_str("\\[");
3714 } else {
3715 carry(C_BRACKET, &mut out);
3716 }
3717 continue;
3718 }
3719 '\\' | '`' | '*' | '~' | ']' => out.push('\\'),
3730 _ => {}
3731 }
3732 out.push(ch);
3733 }
3734 INSERTED.with(|counts| {
3735 let mut current = counts.get();
3736 for (slot, count) in inserted.iter().enumerate() {
3737 current[slot] += count;
3738 }
3739 counts.set(current);
3740 });
3741 out
3742 })
3743}
3744
3745fn may_be_scheme_colon(bytes: &[u8], at: usize) -> bool {
3749 let rest = &bytes[at + 1..];
3750 let slashes = rest.iter().take(2).take_while(|b| **b == b'/').count();
3751 if slashes < rest.len().min(2) {
3752 return false;
3753 }
3754 let run = bytes[..at]
3755 .iter()
3756 .rev()
3757 .take_while(|b| b.is_ascii_alphabetic())
3758 .count();
3759 run == at || scheme_ends_at(at, |k| bytes.get(k).map(|b| *b as char))
3760}
3761
3762fn may_be_www_dot(bytes: &[u8], at: usize) -> bool {
3765 let run = bytes[..at].iter().rev().take_while(|b| **b == b'w').count();
3766 run >= 3 || run == at
3767}
3768
3769fn scheme_ends_at(at: usize, char_at: impl Fn(usize) -> Option<char>) -> bool {
3774 ["https", "http", "ftp"].iter().any(|scheme| {
3775 let len = scheme.len();
3776 at >= len
3777 && scheme
3778 .chars()
3779 .enumerate()
3780 .all(|(k, s)| char_at(at - len + k).is_some_and(|c| c.eq_ignore_ascii_case(&s)))
3781 && !(at > len && char_at(at - len - 1).is_some_and(|c| c.is_ascii_alphabetic()))
3782 })
3783}
3784
3785const CARRIER_DEFAULTS: [char; 10] = [
3788 '\u{E004}', '\u{E005}', '\u{E006}', '\u{E007}', '\u{E008}', '\u{E009}', '\u{E00A}', '\u{E00B}',
3789 '\u{E00C}', '\u{E00D}',
3790];
3791const CARRIER_COUNT: usize = CARRIER_DEFAULTS.len();
3792
3793const C_UNDERSCORE: usize = 0;
3799const C_BRACKET: usize = 1;
3800const C_POSITIONAL_HASH: usize = 2;
3801const C_ANGLE: usize = 3;
3802const C_AMPERSAND: usize = 4;
3803const C_BANG: usize = 5;
3804const C_SCHEME_COLON: usize = 6;
3806const C_WWW_DOT: usize = 7;
3807const C_EMAIL_AT: usize = 8;
3811const C_PAYLOAD_BOUNDARY: usize = 9;
3812
3813thread_local! {
3814 static DEFAULT_CARRIER_TOKENS: [Rc<String>; CARRIER_COUNT] =
3820 CARRIER_DEFAULTS.map(|marker| Rc::new(marker.to_string()));
3821 static CARRIERS: std::cell::RefCell<[Rc<String>; CARRIER_COUNT]> =
3822 std::cell::RefCell::new(DEFAULT_CARRIER_TOKENS.with(Clone::clone));
3823 static OCCUPIED_TOKEN_NONCES: std::cell::RefCell<std::collections::BTreeSet<usize>> =
3824 const { std::cell::RefCell::new(std::collections::BTreeSet::new()) };
3825 static INSERTED: std::cell::Cell<[usize; CARRIER_COUNT]> =
3827 const { std::cell::Cell::new([0; CARRIER_COUNT]) };
3828 static SEEN: std::cell::Cell<[usize; CARRIER_COUNT]> =
3832 const { std::cell::Cell::new([0; CARRIER_COUNT]) };
3833 static OCCUPIED: std::cell::RefCell<std::collections::BTreeSet<u32>> =
3836 const { std::cell::RefCell::new(std::collections::BTreeSet::new()) };
3837 static IN_LINK_TEXT: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
3840}
3841
3842struct CarrierGuard(Option<[Rc<String>; CARRIER_COUNT]>);
3845
3846impl CarrierGuard {
3847 fn install_defaults() -> CarrierGuard {
3848 DEFAULT_CARRIER_TOKENS.with(|defaults| {
3849 let installed = CARRIERS.with(|slot| {
3850 slot.borrow()
3851 .iter()
3852 .zip(defaults)
3853 .all(|(current, default)| Rc::ptr_eq(current, default))
3854 });
3855 if installed {
3856 CarrierGuard(None)
3857 } else {
3858 CarrierGuard::install(defaults.clone())
3859 }
3860 })
3861 }
3862
3863 fn install(chars: [Rc<String>; CARRIER_COUNT]) -> CarrierGuard {
3864 CarrierGuard(Some(CARRIERS.with(|slot| slot.replace(chars))))
3865 }
3866}
3867
3868impl Drop for CarrierGuard {
3869 fn drop(&mut self) {
3870 if let Some(markers) = self.0.take() {
3871 CARRIERS.with(|slot| *slot.borrow_mut() = markers);
3872 }
3873 }
3874}
3875
3876fn resolved_len(text: &str) -> usize {
3879 CARRIERS.with(|slot| {
3880 carrier_tokens(text, &slot.borrow())
3881 .map(|token| {
3882 if token.slot == Some(C_PAYLOAD_BOUNDARY) {
3883 0
3884 } else if token.slot.is_some() {
3885 1
3886 } else {
3887 token.character.len_utf8()
3888 }
3889 })
3890 .sum()
3891 })
3892}
3893
3894struct CarrierToken<'a> {
3895 raw: &'a str,
3896 character: char,
3897 slot: Option<usize>,
3898}
3899
3900fn carrier_tokens<'a>(
3901 text: &'a str,
3902 markers: &'a [Rc<String>; CARRIER_COUNT],
3903) -> impl Iterator<Item = CarrierToken<'a>> + 'a {
3904 let mut offset = 0;
3905 let first_characters: [char; CARRIER_COUNT] =
3906 std::array::from_fn(|index| markers[index].chars().next().unwrap());
3907 let single_characters = markers
3908 .iter()
3909 .zip(first_characters)
3910 .all(|(marker, character)| marker.len() == character.len_utf8());
3911 debug_assert!(first_characters
3912 .iter()
3913 .all(|character| !character.is_ascii()));
3914 std::iter::from_fn(move || {
3915 if offset == text.len() {
3916 return None;
3917 }
3918 let rest = &text[offset..];
3919 let authored = rest.chars().next().unwrap();
3920 let slot = if authored.is_ascii() {
3921 None
3922 } else if single_characters {
3923 first_characters
3924 .iter()
3925 .position(|character| *character == authored)
3926 } else {
3927 markers
3928 .iter()
3929 .position(|marker| rest.starts_with(marker.as_str()))
3930 };
3931 let (character, width) = match slot {
3932 Some(index) => (carried_character(index), markers[index].len()),
3933 None => (authored, authored.len_utf8()),
3934 };
3935 let raw = &text[offset..offset + width];
3936 offset += width;
3937 Some(CarrierToken {
3938 raw,
3939 character,
3940 slot,
3941 })
3942 })
3943}
3944
3945fn carrier(which: usize) -> Rc<String> {
3946 CARRIERS.with(|slot| slot.borrow()[which].clone())
3947}
3948
3949fn note_inserted(which: usize) {
3952 INSERTED.with(|counts| {
3953 let mut current = counts.get();
3954 current[which] += 1;
3955 counts.set(current);
3956 });
3957}
3958
3959fn marker_pool_needs_tokens(occupied: &std::collections::BTreeSet<u32>) -> bool {
3960 let pool_size = (crate::sentinel_run::POOL_LAST - crate::sentinel_run::POOL_FIRST + 1) as usize;
3961 if occupied.len() < pool_size / CARRIER_COUNT {
3962 return false;
3963 }
3964 let picked: [char; CARRIER_COUNT] =
3965 crate::sentinel_run::pick_sentinel_run(occupied, u32::from(CARRIER_DEFAULTS[0]));
3966 picked
3967 .iter()
3968 .any(|marker| occupied.contains(&u32::from(*marker)))
3969}
3970
3971fn note_assembled(text: &str) {
3978 if text.is_ascii() {
3979 return;
3980 }
3981 let mut seen = SEEN.with(std::cell::Cell::get);
3982 let needs_nonces = OCCUPIED.with(|occupied| {
3983 let mut occupied = occupied.borrow_mut();
3984 crate::sentinel_run::collect_private_use(text, &mut occupied);
3985 marker_pool_needs_tokens(&occupied)
3986 });
3987 if needs_nonces {
3988 OCCUPIED_TOKEN_NONCES.with(|nonces| {
3989 crate::sentinel_run::collect_marker_nonce_cores(text, &mut nonces.borrow_mut());
3990 });
3991 }
3992 CARRIERS.with(|slot| {
3993 let markers = slot.borrow();
3994 let characters: [char; CARRIER_COUNT] =
3995 std::array::from_fn(|index| markers[index].chars().next().unwrap());
3996 if markers
3997 .iter()
3998 .zip(characters)
3999 .all(|(marker, character)| marker.len() == character.len_utf8())
4000 {
4001 for character in text.chars().filter(|character| !character.is_ascii()) {
4002 if let Some(index) = characters.iter().position(|marker| *marker == character) {
4003 seen[index] += 1;
4004 }
4005 }
4006 } else {
4007 for token in carrier_tokens(text, &markers) {
4008 if let Some(index) = token.slot {
4009 seen[index] += 1;
4010 }
4011 }
4012 }
4013 });
4014 SEEN.with(|counts| counts.set(seen));
4015}
4016
4017const AUTHORED_INERT: [char; 8] = ['{', '}', '^', ',', '%', ':', '/', '@'];
4029
4030fn authored_defers_to_the_line(ch: char) -> bool {
4036 ch == '#'
4037}
4038
4039fn positional_sentinel() -> Rc<String> {
4041 note_inserted(C_POSITIONAL_HASH);
4042 carrier(C_POSITIONAL_HASH)
4043}
4044
4045fn carrier_slot(carriers: &[Rc<String>; CARRIER_COUNT], ch: char) -> Option<usize> {
4051 carriers.iter().position(|current| current.chars().eq([ch]))
4052}
4053
4054fn carried_character(slot: usize) -> char {
4056 match slot {
4057 C_UNDERSCORE => '_',
4058 C_BRACKET => '[',
4059 C_ANGLE => '<',
4060 C_AMPERSAND => '&',
4061 C_BANG => '!',
4062 C_SCHEME_COLON => ':',
4063 C_WWW_DOT => '.',
4064 C_EMAIL_AT => '@',
4065 _ => '#',
4066 }
4067}
4068
4069fn strip_controls(input: &str) -> String {
4085 strip_control_chars(input)
4086}
4087
4088fn resolve_nbsp(text: &str) -> String {
4090 text.replace(crate::NBSP_PLACEHOLDER, "\u{00a0}")
4091}
4092
4093fn escape_md_html(text: &str) -> String {
4095 text.replace('&', "&")
4096 .replace('<', "<")
4097 .replace('>', ">")
4098}
4099
4100fn sanitize_md_url(url: &str, pos: Option<&Pos>, sink: DeniedSink) -> String {
4111 crate::escape::sanitize_destination(url, pos, sink).into_owned()
4116}
4117
4118fn markdown_angle_autolink(label: &str, href: &str, destination: &str) -> bool {
4119 if destination == label {
4120 if let Some((scheme, body)) = label.split_once(':') {
4121 let scheme = scheme.as_bytes();
4122 if (2..=32).contains(&scheme.len())
4123 && scheme[0].is_ascii_alphabetic()
4124 && scheme[1..]
4125 .iter()
4126 .all(|b| b.is_ascii_alphanumeric() || matches!(*b, b'+' | b'.' | b'-'))
4127 && !body.is_empty()
4128 && body.chars().all(|ch| {
4129 !ch.is_whitespace() && !ch.is_control() && !matches!(ch, '<' | '>' | '\\')
4130 })
4131 {
4132 return true;
4133 }
4134 }
4135 }
4136 if href.strip_prefix("mailto:") != Some(label) || destination != href {
4137 return false;
4138 }
4139 let Some((local, domain)) = label.split_once('@') else {
4140 return false;
4141 };
4142 let local_valid = !local.is_empty()
4143 && local
4144 .chars()
4145 .all(|ch| ch.is_ascii_alphanumeric() || ".!#$%&'*+/=?^_`{|}~-".contains(ch));
4146 let labels: Vec<&str> = domain.split('.').collect();
4147 local_valid
4148 && labels.len() >= 2
4149 && labels.iter().all(|part| {
4150 let bytes = part.as_bytes();
4151 (1..=63).contains(&bytes.len())
4152 && bytes[0].is_ascii_alphanumeric()
4153 && bytes[bytes.len() - 1].is_ascii_alphanumeric()
4154 && bytes
4155 .iter()
4156 .all(|b| b.is_ascii_alphanumeric() || *b == b'-')
4157 })
4158}
4159
4160fn encode_markdown_destination(url: &str, pos: Option<&Pos>, sink: DeniedSink) -> String {
4169 let sanitized = sanitize_md_url(&strip_controls(url), pos, sink);
4175 let mut out = String::new();
4176 let pipe_destination = sanitized.contains('|');
4177 for ch in sanitized.chars() {
4178 match ch {
4179 '\\' if pipe_destination => out.push_str("%5C"),
4180 '|' => out.push_str("%7C"),
4181 ' ' => out.push_str("%20"),
4182 '(' => out.push_str("\\("),
4183 ')' => out.push_str("\\)"),
4184 '<' => out.push_str("%3C"),
4185 '>' => out.push_str("%3E"),
4186 _ => out.push(ch),
4187 }
4188 }
4189 neutralize_char_refs(&out)
4192}
4193
4194fn neutralize_char_refs(url: &str) -> String {
4209 if !url.contains('&') {
4210 return url.to_string();
4211 }
4212 let mut out = String::with_capacity(url.len() + 8);
4213 for (i, ch) in url.char_indices() {
4214 if ch == '&' && opens_char_ref(&url[i + ch.len_utf8()..]) {
4215 out.push_str("&");
4216 } else {
4217 out.push(ch);
4218 }
4219 }
4220 out
4221}
4222
4223fn opens_char_ref(rest: &str) -> bool {
4230 let mut chars = rest.chars();
4231 match chars.next() {
4232 Some('#') => {
4233 let digits = chars.as_str();
4234 let (digits, hex) = match digits.strip_prefix(['x', 'X']) {
4235 Some(after) => (after, true),
4236 None => (digits, false),
4237 };
4238 for (seen, c) in digits.chars().enumerate() {
4239 if c == ';' {
4240 return seen > 0;
4241 }
4242 let is_digit = if hex {
4243 c.is_ascii_hexdigit()
4244 } else {
4245 c.is_ascii_digit()
4246 };
4247 if !is_digit || seen >= 8 {
4248 return false;
4249 }
4250 }
4251 false
4252 }
4253 Some(c) if c.is_ascii_alphabetic() => {
4254 for (seen, c) in (1usize..).zip(chars) {
4255 if c == ';' {
4256 return true;
4257 }
4258 if !c.is_ascii_alphanumeric() || seen >= 32 {
4259 return false;
4260 }
4261 }
4262 false
4263 }
4264 _ => false,
4265 }
4266}
4267
4268fn normalize(text: &str) -> String {
4269 let carriers = CARRIERS.with(|slot| slot.borrow().clone());
4270 let mut out = String::with_capacity(text.len());
4271 let mut newlines = 0usize;
4272 for (index, chunk) in text
4273 .split(carriers[C_PAYLOAD_BOUNDARY].as_str())
4274 .enumerate()
4275 {
4276 if index > 0 {
4277 out.push_str(&carriers[C_PAYLOAD_BOUNDARY]);
4278 }
4279 if index % 2 == 1 {
4280 out.push_str(chunk);
4281 let trailing = chunk
4282 .bytes()
4283 .rev()
4284 .take_while(|byte| *byte == b'\n')
4285 .count();
4286 newlines = if trailing == chunk.len() {
4287 newlines + trailing
4288 } else {
4289 trailing
4290 };
4291 continue;
4292 }
4293 let mut cursor = 0;
4294 for (at, _) in chunk.match_indices('\n') {
4295 if at > cursor {
4296 out.push_str(&chunk[cursor..at]);
4297 newlines = 0;
4298 }
4299 newlines += 1;
4300 if newlines <= 2 {
4301 out.push('\n');
4302 }
4303 cursor = at + 1;
4304 }
4305 if cursor < chunk.len() {
4306 out.push_str(&chunk[cursor..]);
4307 newlines = 0;
4308 }
4309 }
4310 let collapsed = format!("{}\n", out.trim_matches(|c| c == '\n' || c == ' '));
4311
4312 note_assembled(&collapsed);
4317 resolve_narrowed_escapes(&collapsed)
4318}
4319
4320fn adjacent_to_live_delimiter(line: &[char], i: usize, ch: char) -> bool {
4334 if line.get(i + 1) == Some(&ch) {
4335 return true;
4336 }
4337 if i == 0 || line[i - 1] != ch {
4338 return false;
4339 }
4340 let mut backslashes = 0usize;
4341 let mut j = i.checked_sub(2);
4342 while let Some(k) = j {
4343 if line[k] != '\\' {
4344 break;
4345 }
4346 backslashes += 1;
4347 j = k.checked_sub(1);
4348 }
4349 backslashes % 2 == 0
4350}
4351
4352fn underscores_that_could_pair(text: &[char], slots: &[u8]) -> Vec<bool> {
4360 let mut escape = vec![false; text.len()];
4361 for block in inline_blocks(text) {
4362 let flags: Vec<(usize, bool, bool)> = block
4363 .iter()
4364 .flat_map(|&(start, end)| (start..end).map(move |i| (start, end, i)))
4365 .filter(|&(_, _, i)| slots[i] == C_UNDERSCORE as u8)
4366 .map(|(start, end, i)| {
4367 let before = if i == start { ' ' } else { text[i - 1] };
4369 let after = if i + 1 < end { text[i + 1] } else { ' ' };
4370 let left = !flank_space(after)
4371 && (!flank_punct(after) || flank_space(before) || flank_punct(before));
4372 let right = !flank_space(before)
4373 && (!flank_punct(before) || flank_space(after) || flank_punct(after));
4374 let opens = left && (!right || flank_punct(before));
4375 let closes = right && (!left || flank_punct(after));
4376 (i, opens, closes)
4377 })
4378 .collect();
4379 let mut opener_before = false;
4380 for &(i, opens, closes) in &flags {
4381 if closes && opener_before {
4382 escape[i] = true;
4383 }
4384 opener_before |= opens;
4385 }
4386 let mut closer_after = false;
4387 for &(i, opens, closes) in flags.iter().rev() {
4388 if opens && closer_after {
4389 escape[i] = true;
4390 }
4391 closer_after |= closes;
4392 }
4393 }
4394 escape
4395}
4396
4397fn inline_blocks(text: &[char]) -> Vec<Vec<(usize, usize)>> {
4402 let mut blocks = Vec::new();
4403 let mut current: Vec<(usize, usize)> = Vec::new();
4404 let mut line_start = 0usize;
4405 while line_start <= text.len() {
4406 let line_end = text[line_start..]
4407 .iter()
4408 .position(|c| *c == '\n')
4409 .map_or(text.len(), |at| line_start + at);
4410 let content = content_position(text, line_start).min(line_end);
4411 let body = &text[content..line_end];
4412 if body.iter().all(|c| *c == ' ') {
4413 if !current.is_empty() {
4414 blocks.push(std::mem::take(&mut current));
4415 }
4416 } else if body[0] == '|' {
4417 if !current.is_empty() {
4418 blocks.push(std::mem::take(&mut current));
4419 }
4420 let mut cell = content + 1;
4421 let mut i = cell;
4422 while i < line_end {
4423 if text[i] == '\\' {
4424 i += 2;
4425 continue;
4426 }
4427 if text[i] == '|' {
4428 blocks.push(vec![(cell, i)]);
4429 cell = i + 1;
4430 }
4431 i += 1;
4432 }
4433 if cell < line_end {
4434 blocks.push(vec![(cell, line_end)]);
4435 }
4436 } else if starts_a_list_item(text, line_start) {
4437 if !current.is_empty() {
4438 blocks.push(std::mem::take(&mut current));
4439 }
4440 current.push((content, line_end));
4441 } else {
4442 current.push((content, line_end));
4443 }
4444 line_start = line_end + 1;
4445 }
4446 if !current.is_empty() {
4447 blocks.push(current);
4448 }
4449 blocks
4450}
4451
4452fn starts_a_list_item(text: &[char], line_start: usize) -> bool {
4454 let mut at = line_start;
4455 loop {
4456 while text.get(at) == Some(&' ') {
4457 at += 1;
4458 }
4459 match text.get(at) {
4460 Some('>') => match container_prefix_end(text, at) {
4461 Some(end) => at = end,
4462 None => return false,
4463 },
4464 Some(_) => return container_prefix_end(text, at).is_some(),
4465 None => return false,
4466 }
4467 }
4468}
4469
4470fn resolve_narrowed_escapes(text: &str) -> String {
4472 if text.is_ascii() {
4473 return text.to_string();
4474 }
4475 let carriers = CARRIERS.with(|slot| slot.borrow().clone());
4479 if !carriers[..C_PAYLOAD_BOUNDARY]
4480 .iter()
4481 .any(|marker| text.contains(marker.as_str()))
4482 {
4483 return text.replace(carriers[C_PAYLOAD_BOUNDARY].as_str(), "");
4484 }
4485 let mut line = Vec::new();
4486 let mut raw_chars = Vec::new();
4487 let mut slots = Vec::new();
4488 const NO_SLOT: u8 = u8::MAX;
4489 const _: () = assert!(CARRIER_COUNT < u8::MAX as usize);
4490 let mut markers = std::collections::BTreeMap::<usize, String>::new();
4491 let mut cursor = 0;
4492 for range in inline_marker_ranges(text)
4493 .into_iter()
4494 .chain(std::iter::once(text.len()..text.len()))
4495 {
4496 for token in carrier_tokens(&text[cursor..range.start], &carriers)
4497 .filter(|token| token.slot != Some(C_PAYLOAD_BOUNDARY))
4498 {
4499 line.push(token.character);
4500 raw_chars.push(token.raw.chars().next().unwrap());
4501 slots.push(token.slot.map_or(NO_SLOT, |slot| slot as u8));
4502 }
4503 if !range.is_empty() {
4504 markers
4505 .entry(line.len())
4506 .or_default()
4507 .push_str(&text[range.clone()]);
4508 }
4509 cursor = range.end;
4510 }
4511 let pairs = underscores_that_could_pair(&line, &slots);
4512 let mut out = String::with_capacity(text.len());
4513 let mut content_start = content_position(&line, 0);
4522 for (i, (&raw, &slot)) in raw_chars.iter().zip(&slots).enumerate() {
4523 if let Some(marker) = markers.get(&i) {
4524 out.push_str(marker);
4525 }
4526 if raw == '\n' {
4527 content_start = content_position(&line, i + 1);
4528 out.push(raw);
4529 continue;
4530 }
4531 let keep = (slot != NO_SLOT).then(|| {
4535 let slot = usize::from(slot);
4536 let ch = carried_character(slot);
4537 if slot == C_POSITIONAL_HASH {
4538 (
4539 ch,
4540 opens_an_atx_heading(&line, i, content_start)
4541 || closes_an_atx_heading(&line, &raw_chars, i, content_start),
4542 )
4543 } else if slot == C_ANGLE {
4544 let next = line.get(i + 1).copied().unwrap_or('\n');
4546 (
4547 ch,
4548 next.is_ascii_alphabetic() || matches!(next, '/' | '!' | '?'),
4549 )
4550 } else if slot == C_AMPERSAND {
4551 let rest: String = line[i + 1..].iter().take(40).collect();
4552 (ch, opens_char_ref(&rest))
4553 } else if slot == C_BANG {
4554 (ch, raw_chars.get(i + 1) == Some(&'['))
4556 } else if slot == C_SCHEME_COLON {
4557 let slashes = line.get(i + 1) == Some(&'/') && line.get(i + 2) == Some(&'/');
4558 (ch, slashes && scheme_ends_at(i, |k| line.get(k).copied()))
4559 } else if slot == C_WWW_DOT {
4560 let www = i >= 3 && line[i - 3..i] == ['w', 'w', 'w'];
4561 (ch, www && !(i > 3 && line[i - 4].is_ascii_alphanumeric()))
4562 } else if slot == C_EMAIL_AT {
4563 (ch, gfm_email_at(&line, i))
4564 } else {
4565 (ch, adjacent_to_live_delimiter(&line, i, ch) || pairs[i])
4566 }
4567 });
4568
4569 match keep {
4570 Some(('@', true)) => {
4571 out.push_str("<!---->@");
4574 }
4575 Some((ch, true)) => {
4576 out.push('\\');
4577 out.push(ch);
4578 }
4579 Some((ch, false)) => out.push(ch),
4580 None => out.push(raw),
4581 }
4582 }
4583 if let Some(marker) = markers.get(&line.len()) {
4584 out.push_str(marker);
4585 }
4586 out
4587}
4588
4589fn gfm_email_at(line: &[char], at: usize) -> bool {
4596 let mut start = at;
4597 let mut xmpp = false;
4598 while start > 0 {
4599 let ch = line[start - 1];
4600 if ch.is_ascii_alphanumeric() || matches!(ch, '.' | '+' | '-' | '_') {
4601 start -= 1;
4602 continue;
4603 }
4604 if ch == ':' {
4605 let mut took_protocol = false;
4606 for protocol in ["mailto:", "xmpp:"] {
4607 let spelled: Vec<char> = protocol.chars().collect();
4608 if start < spelled.len() || line[start - spelled.len()..start] != spelled[..] {
4609 continue;
4610 }
4611 let before = start - spelled.len();
4612 if before > 0 && line[before - 1].is_ascii_alphanumeric() {
4613 continue;
4614 }
4615 xmpp = xmpp || protocol == "xmpp:";
4616 start -= 1;
4617 took_protocol = true;
4618 break;
4619 }
4620 if took_protocol {
4621 continue;
4622 }
4623 }
4624 break;
4625 }
4626 if start == at {
4627 return false;
4628 }
4629 let mut dots = 0usize;
4630 let mut end = at + 1;
4631 while end < line.len() {
4632 let ch = line[end];
4633 if ch.is_ascii_alphanumeric() || ch == '-' || ch == '_' || (xmpp && ch == '/') {
4634 end += 1;
4635 continue;
4636 }
4637 if ch == '@' {
4639 return false;
4640 }
4641 if ch == '.' && line.get(end + 1).is_some_and(char::is_ascii_alphanumeric) {
4642 dots += 1;
4643 end += 1;
4644 continue;
4645 }
4646 break;
4647 }
4648 dots > 0 && line[end - 1].is_ascii_alphabetic()
4652}
4653
4654fn closes_an_atx_heading(line: &[char], raw: &[char], index: usize, content_start: usize) -> bool {
4663 if index == 0 || !matches!(line.get(index - 1), Some(' ' | '\t')) {
4664 return false;
4665 }
4666 if !emitted_as_a_heading(raw, content_start) {
4667 return false;
4668 }
4669 line[index..]
4670 .iter()
4671 .take_while(|c| **c != '\n')
4672 .all(|c| *c == '#')
4673}
4674
4675fn emitted_as_a_heading(raw: &[char], content_start: usize) -> bool {
4678 let run = raw[content_start..]
4679 .iter()
4680 .take_while(|c| **c == '#')
4681 .take(7)
4682 .count();
4683 (1..=6).contains(&run) && matches!(raw.get(content_start + run), Some(' '))
4684}
4685
4686fn opens_an_atx_heading(line: &[char], index: usize, content_start: usize) -> bool {
4688 if index != content_start {
4689 return false;
4690 }
4691
4692 let run = line[index..]
4693 .iter()
4694 .take_while(|c| **c == '#')
4695 .take(7)
4696 .count();
4697 if run > 6 {
4698 return false;
4699 }
4700
4701 match line.get(index + run) {
4702 Some(' ' | '\t' | '\n') | None => true,
4703 Some(_) => false,
4704 }
4705}
4706
4707fn content_position(line: &[char], line_start: usize) -> usize {
4711 let mut at = line_start;
4712 loop {
4713 while line.get(at) == Some(&' ') {
4714 at += 1;
4715 }
4716 match container_prefix_end(line, at) {
4717 Some(end) => at = end,
4718 None => return at,
4719 }
4720 }
4721}
4722
4723fn container_prefix_end(line: &[char], at: usize) -> Option<usize> {
4725 let ch = *line.get(at)?;
4726
4727 if ch == '>' {
4730 return Some(if line.get(at + 1) == Some(&' ') {
4731 at + 2
4732 } else {
4733 at + 1
4734 });
4735 }
4736
4737 if matches!(ch, '-' | '*' | '+') && line.get(at + 1) == Some(&' ') {
4741 return Some(at + 2);
4742 }
4743
4744 if ch.is_ascii_digit() {
4748 let mut end = at;
4749 while end - at < 9 && line.get(end).is_some_and(char::is_ascii_digit) {
4750 end += 1;
4751 }
4752 if matches!(line.get(end), Some('.' | ')')) && line.get(end + 1) == Some(&' ') {
4753 return Some(end + 2);
4754 }
4755 return None;
4756 }
4757
4758 if ch == '[' && line.get(at + 1) == Some(&'^') {
4762 let mut end = at + 2;
4763 while !matches!(line.get(end), None | Some('\n') | Some(']')) {
4764 end += 1;
4765 }
4766 if line.get(end) == Some(&']')
4767 && line.get(end + 1) == Some(&':')
4768 && line.get(end + 2) == Some(&' ')
4769 {
4770 return Some(end + 3);
4771 }
4772 }
4773
4774 None
4775}
4776
4777fn flatten_heading_text(text: &str) -> String {
4778 let layout = |c: char| c == ' ' || c == '\t' || c == '\r';
4787
4788 text.split('\n')
4789 .map(|part| inline_padding(part, layout).1)
4790 .filter(|part| !part.is_empty())
4791 .collect::<Vec<_>>()
4792 .join(" ")
4793}
4794
4795fn next_heading_id(
4802 heading: &Heading,
4803 counts: &mut std::collections::BTreeMap<String, usize>,
4804 explicit_ids: &HashSet<String>,
4805) -> String {
4806 let explicit = heading.attrs.as_ref().and_then(|attrs| attrs.id.clone());
4807 let has_explicit = explicit.is_some();
4808 let base = explicit.unwrap_or_else(|| slugify(&plain_inlines(&heading.children)));
4809
4810 crate::document_ids::allocate_heading_id(base, has_explicit, counts, |id| {
4811 explicit_ids.contains(id)
4812 })
4813}
4814
4815fn plain_inlines(nodes: &[InlineNode]) -> String {
4819 plain_inlines_with(nodes, ' ', " ")
4820}
4821
4822fn plain_inlines_with(nodes: &[InlineNode], nbsp: char, hard_break_text: &str) -> String {
4824 let mut out = String::new();
4825 for node in nodes {
4826 match node {
4827 InlineNode::NonBreakingSpace(_) => out.push('\u{00a0}'),
4828 InlineNode::Text(text) => out.push_str(
4829 &text
4830 .value
4831 .replace(crate::NBSP_PLACEHOLDER, nbsp.encode_utf8(&mut [0u8; 4])),
4832 ),
4833 InlineNode::EscapedText(escaped) => out.push_str(&escaped.value),
4839 InlineNode::SmartPunctuation(s) => out.push_str(smart_punctuation_text(s)),
4840 InlineNode::Emphasis(emphasis) => out.push_str(&plain_inlines_with(
4841 &emphasis.children,
4842 nbsp,
4843 hard_break_text,
4844 )),
4845 InlineNode::Span(span) if !span.injected => {
4846 out.push_str(&plain_inlines_with(&span.children, nbsp, hard_break_text))
4847 }
4848 InlineNode::Code(code) => out.push_str(&code.value),
4849 InlineNode::LiteralInline(lit) => out.push_str(&lit.content),
4852 InlineNode::Math(math) => out.push_str(&math.content),
4857 InlineNode::Link(link) if link.from_crossref => {}
4863 InlineNode::Link(link) => {
4864 out.push_str(&plain_inlines_with(&link.children, nbsp, hard_break_text))
4865 }
4866 InlineNode::Ruby(r) => {
4867 out.push_str(&plain_inlines_with(&r.flattened(), nbsp, hard_break_text))
4868 }
4869 InlineNode::Image(image) => out.push_str(&image.alt),
4870 InlineNode::AutoLink(autolink) => out.push_str(&autolink.text),
4871 InlineNode::Extension(extension) => out.push_str(&plain_inlines_with(
4872 &extension.children,
4873 nbsp,
4874 hard_break_text,
4875 )),
4876 InlineNode::CitationGroup(group) => out.push_str(&group.raw),
4877 InlineNode::Abbreviation(abbr) => out.push_str(&abbr.abbr),
4878 InlineNode::Mention(mention) => out.push_str(&mention.user),
4879 InlineNode::Tag(tag) => out.push_str(&tag.name),
4880 InlineNode::CaptionNumber(number) => {
4881 if let Some(number) = number.number {
4882 out.push_str(&number.to_string());
4883 }
4884 }
4885 InlineNode::SoftBreak(_) => out.push(' '),
4886 InlineNode::HardBreak(_) => out.push_str(hard_break_text),
4887 _ => {}
4888 }
4889 }
4890 out
4891}
4892
4893fn slugify(text: &str) -> String {
4894 crate::parse::slugify_parse(text, crate::extension::HeadingIdOptions::PLAIN)
4899}
4900
4901#[derive(Default)]
4904struct GfmSlugger {
4905 counts: HashMap<String, usize>,
4906}
4907
4908impl GfmSlugger {
4909 fn slug(&mut self, text: &str) -> String {
4910 static DROP: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();
4911 let drop = DROP.get_or_init(|| {
4912 regex::Regex::new(r"[^\p{L}\p{M}\p{N}\p{Pc} \-]").expect("the slug class is valid")
4913 });
4914 let base = drop.replace_all(&text.to_lowercase(), "").replace(' ', "-");
4915 let mut slug = base.clone();
4916 while self.counts.contains_key(&slug) {
4917 let count = self.counts.entry(base.clone()).or_insert(0);
4918 *count += 1;
4919 slug = format!("{base}-{count}");
4920 }
4921 self.counts.insert(slug.clone(), 0);
4922 slug
4923 }
4924}
4925
4926fn gfm_heading_text(nodes: &[InlineNode]) -> String {
4929 use unicode_normalization::UnicodeNormalization;
4930 let text = plain_inlines_with(nodes, '\u{00a0}', "");
4931 text.trim_matches(|c: char| c.is_ascii_whitespace())
4932 .nfc()
4933 .collect()
4934}
4935
4936fn walk_headings<F>(blocks: &[BlockNode], depth: usize, written: bool, visit: &mut F)
4939where
4940 F: FnMut(&Heading, bool),
4941{
4942 if depth > MAX_RENDER_DEPTH {
4943 return;
4944 }
4945 let next = depth + 1;
4946 for block in blocks {
4947 match block {
4948 BlockNode::Heading(heading) => visit(heading, written),
4949 BlockNode::BlockQuote(quote) => walk_headings("e.children, next, written, visit),
4950 BlockNode::Admonition(admonition) => match list_table_as_table(admonition) {
4951 Some(table) => walk_headings(&[BlockNode::Table(table)], depth, written, visit),
4952 None => walk_headings(&admonition.children, next, written, visit),
4953 },
4954 BlockNode::Directive(directive) => {
4955 walk_headings(&directive.children, next, written, visit)
4956 }
4957 BlockNode::Div(div) => walk_headings(&div.children, next, written, visit),
4958 BlockNode::Section(section) => walk_headings(§ion.children, next, written, visit),
4959 BlockNode::LineBlock(lb) => walk_headings(&lb.children, next, written, visit),
4960 BlockNode::List(list) => {
4961 for item in &list.items {
4962 walk_headings(&item.children, next, written, visit);
4963 }
4964 }
4965 BlockNode::DefinitionList(list) => {
4966 for item in &list.items {
4967 for definition in &item.definitions {
4968 walk_headings(definition, next, written, visit);
4969 }
4970 }
4971 }
4972 BlockNode::Table(table) => {
4973 for row in &table.rows {
4974 for cell in &row.cells {
4975 if let Some(blocks) = &cell.blocks {
4976 walk_headings(blocks, next, false, visit);
4977 }
4978 }
4979 }
4980 }
4981 BlockNode::Figure(figure) => {
4982 if let FigureTarget::BlockQuote(quote) = &*figure.target {
4983 walk_headings("e.children, next, written, visit);
4984 }
4985 }
4986 BlockNode::FigureGroup(group) => walk_headings(&group.children, next, written, visit),
4987 BlockNode::ExtensionCarrier(extension) => {
4988 walk_headings(&extension.children, next, written, visit)
4989 }
4990 BlockNode::BlockExtension(extension) => walk_headings(
4991 std::slice::from_ref(extension.fallback.as_ref()),
4992 next,
4993 written,
4994 visit,
4995 ),
4996 _ => {}
4997 }
4998 }
4999}
5000
5001fn is_literal_crossref(text: &str) -> bool {
5002 text.starts_with("</#") && text.ends_with('>') && !text[3..text.len() - 1].contains('>')
5003}
5004
5005fn walk_blocks<F>(blocks: &[BlockNode], depth: usize, visit: &mut F)
5006where
5007 F: FnMut(&BlockNode, Option<&[InlineNode]>),
5008{
5009 if depth > MAX_RENDER_DEPTH {
5010 crate::render_depth::record("markdown");
5011 return;
5012 }
5013 for block in blocks {
5014 visit(block, None);
5015 match block {
5016 BlockNode::Heading(heading) => visit(block, Some(&heading.children)),
5017 BlockNode::Paragraph(paragraph) => visit(block, Some(¶graph.children)),
5018 BlockNode::BlockQuote(quote) => walk_blocks("e.children, depth + 1, visit),
5019 BlockNode::Admonition(admonition) if list_table_as_table(admonition).is_some() => {
5020 if let Some(table) = list_table_as_table(admonition) {
5021 walk_blocks(&[BlockNode::Table(table)], depth, visit);
5022 }
5023 }
5024 BlockNode::Admonition(admonition) => {
5025 if let Some(title) = &admonition.title {
5028 visit(block, Some(title));
5029 }
5030 walk_blocks(&admonition.children, depth + 1, visit);
5031 }
5032 BlockNode::Directive(directive) => {
5033 if let Some(title) = &directive.title {
5034 visit(block, Some(title));
5035 }
5036 walk_blocks(&directive.children, depth + 1, visit);
5037 }
5038 BlockNode::Div(div) => walk_blocks(&div.children, depth + 1, visit),
5039 BlockNode::Section(section) => walk_blocks(§ion.children, depth + 1, visit),
5040 BlockNode::LineBlock(lb) => walk_blocks(&lb.children, depth + 1, visit),
5041 BlockNode::List(list) => {
5042 for item in &list.items {
5043 walk_blocks(&item.children, depth + 1, visit);
5044 }
5045 }
5046 BlockNode::DefinitionList(list) => {
5047 for item in &list.items {
5048 for term in &item.terms {
5049 visit(block, Some(term));
5050 }
5051 for definition in &item.definitions {
5052 walk_blocks(definition, depth + 1, visit);
5053 }
5054 }
5055 }
5056 BlockNode::Table(table) => {
5057 if let Some(caption) = &table.caption {
5058 visit(block, Some(caption));
5059 }
5060 for row in &table.rows {
5061 for cell in &row.cells {
5062 visit(block, Some(&cell.children));
5063 if let Some(blocks) = &cell.blocks {
5064 walk_blocks(blocks, depth + 1, visit);
5065 }
5066 }
5067 }
5068 }
5069 BlockNode::Figure(figure) => {
5070 visit(block, Some(&figure.caption));
5071 match &*figure.target {
5072 FigureTarget::BlockQuote(quote) => {
5073 walk_blocks("e.children, depth + 1, visit)
5074 }
5075 FigureTarget::Table(table) => {
5076 walk_blocks(&[BlockNode::Table(table.clone())], depth + 1, visit);
5077 }
5078 FigureTarget::Paragraph(p) => {
5079 walk_blocks(&[BlockNode::Paragraph(p.clone())], depth + 1, visit);
5080 }
5081 FigureTarget::Image(_) | FigureTarget::CodeBlock(_) => {}
5082 }
5083 }
5084 BlockNode::FigureGroup(group) => {
5085 if let Some(caption) = &group.caption {
5086 visit(block, Some(caption));
5087 }
5088 walk_blocks(&group.children, depth + 1, visit);
5089 }
5090 BlockNode::ExtensionCarrier(extension) => {
5091 walk_blocks(&extension.children, depth + 1, visit)
5092 }
5093 _ => {}
5094 }
5095 }
5096}