use super::*; mod secondary_parse_plan { use super::*; const FLAVORS: [MarkdownFlavor; 12] = crate::format_proof::ALL_FLAVORS; fn verify_context(ctx: &LintContext<'_>) { // The oracle always performs the independent no-math parse when the // old query did, regardless of the primary parser's recorded facts. let legacy = if ctx.content.contains('$') { crate::utils::parser_options::code_span_ranges_without_math(ctx.content) } else { Vec::new() }; for byte in 0..=ctx.content.len() + 1 { let expected = ctx.is_in_code_span_byte(byte) || legacy.iter().any(|&(start, end)| start <= byte && byte < end); assert_eq!( ctx.is_in_code_span_byte_with_or_without_math(byte), expected, "{:?} at {byte}: {:?}", ctx.flavor, ctx.content ); } if !ctx.parser_has_math && ctx.flavor != MarkdownFlavor::MDX { assert!(ctx.code_spans_without_math_cache.get().is_none()); } } #[test] fn secondary_parse_plan_preserves_every_reading_and_flavor() { for content in [ "$a`b\n", "`$`$`\n", "$$unclosed\n", "| left | right |\n| --- | --- |\n| $x ` y | z` |\n", "Costs $10 and `code` with é.\n", "A $x `y$ and z` end.\n\nThen `w` code.\n", "$$a `b`$$ and `c`\n", "`$` and `$unclosed`, and escaped \\$ text\n", "\n\n
\n> Title\n> ===\n",
"- \n Title\n ===\n",
"- \n Title\n ===\n",
"[^a]: \n Title\n ===\n\nRef[^a]\n",
] {
assert!(
setext_headings(content, MarkdownFlavor::Standard).is_empty(),
"{content:?}"
);
}
for (content, line, text, depth) in [
// Text after the tag on its line makes it inline content.
(" x\n===\n", 1, " x", 0),
// A blank line ends the block.
("\n\nTitle\n===\n", 3, "Title", 0),
("> \n\n> Title\n> ===\n", 3, "Title", 1),
// The text of an HTML block opens no list item, so the heading below the
// block sits at the top level rather than in a lazy item paragraph.
("\n- a\n\n Title\n===\n", 4, "Title", 0),
// Front matter is not Markdown, so a tag written in it opens no block
// over the lines after it ends.
("---\nhtml: |\n\n \n---\nTitle\n===\n", 6, "Title", 0),
// A block opening on the line after another one ends can open containers
// of its own, so the item holds the heading written inside it.
("\n
\n10. \n\n Title\n ===\n", 5, "Title", 0),
// A tag naming no block-level element reads like paragraph text, yet it
// opens an HTML block, which ends with the container holding it. So the
// text below the container starts a paragraph of its own rather than
// continuing one lazily.
("> \nTitle\n===\n", 2, "Title", 0),
("- \nTitle\n===\n", 2, "Title", 0),
("[^a]: \nTitle\n===\n\nRef[^a]\n", 2, "Title", 0),
] {
assert_eq!(
setext_headings(content, MarkdownFlavor::Standard),
vec![(line, text.to_string(), 1, depth)],
"{content:?}"
);
}
// A tag cannot interrupt a paragraph, so the paragraph runs on through it
// into the heading.
assert_eq!(
setext_headings("Intro\n\n===\n", MarkdownFlavor::Standard).len(),
1
);
// An Obsidian comment is not Markdown either, while elsewhere `%%` is text
// and the tag below it opens a block running through the underline.
let content = "%%\n\n\n%%\nTitle\n===\n";
assert_eq!(
setext_headings(content, MarkdownFlavor::Obsidian),
vec![(5, "Title".to_string(), 1, 0)]
);
assert!(setext_headings(content, MarkdownFlavor::Standard).is_empty());
// A flavor's own fence holds code, so a tag written inside it opens no
// block over the lines below the fence. Where the same bytes carry no
// fence, the tag is Markdown and opens a block running through the
// underline.
let content = "::: note\n\n\n:::\nTitle\n===\n";
assert_eq!(
setext_headings(content, MarkdownFlavor::AzureDevOps),
vec![(5, "Title".to_string(), 1, 0)]
);
assert!(setext_headings(content, MarkdownFlavor::Standard).is_empty());
}
/// A list item's text is not the paragraph a run below the item could
/// underline: the run is a lazy continuation of the item's paragraph.
#[test]
fn setext_underline_below_a_list_item_is_paragraph_text() {
for content in [
"- item\n===\n",
"* * x\n===\n",
"1) item\n===\n",
"Intro\n1. foo\n===\n",
] {
assert!(
setext_headings(content, MarkdownFlavor::Standard).is_empty(),
"{content:?}"
);
}
}
/// CommonMark 5.2: a list item starting with a blank line cannot interrupt a
/// paragraph, so a bare `-` under prose is the paragraph's underline and a bare
/// `*`, `+`, `1.` or `1)` is its text. Each row's expectation is pulldown-cmark's,
/// and pandoc's CommonMark reader agrees.
#[test]
fn empty_list_item_under_a_paragraph_opens_nothing() {
for (content, line, depth) in [
("Title\n- \n", 1, 0),
("Title\n-\t\n", 1, 0),
("Title\n - \n", 1, 0),
("Title\n- \n x\n", 1, 0),
("> Title\n> - \n", 1, 1),
("- a\n\n Title\n - \n", 3, 0),
("[^a]: intro\n\n Title\n - \n\nRef[^a]\n", 3, 0),
] {
assert_eq!(
setext_headings(content, MarkdownFlavor::Standard),
vec![(line, "Title".to_string(), 2, depth)],
"{content:?}"
);
}
for content in [
"Title\n* \n",
"Title\n+ \n",
"Title\n1. \n",
"Title\n1) \n",
// The first `-` has content, an empty item of its own, so it opens a list.
"Title\n- - \n",
// A blank line ends the paragraph, so the item below it opens.
"Title\n\n- \n",
// A sibling item interrupts no paragraph, and sits outside the first.
"- Title\n- \n",
] {
assert!(
setext_headings(content, MarkdownFlavor::Standard).is_empty(),
"{content:?}"
);
}
// The bare `*` continues the paragraph, and the underline below makes a
// heading of both lines, recorded on the line the underline sits under.
assert_eq!(
setext_headings("Title\n* \n===\n", MarkdownFlavor::Standard),
vec![(2, "Title *".to_string(), 1, 0)]
);
}
/// A setext underline makes a heading of the whole paragraph above it. The
/// heading is recorded on the paragraph's last line with the text of every
/// line, joined at the soft breaks by the space each renders as, and every text
/// line is marked as holding heading text.
#[test]
fn setext_heading_text_is_the_whole_paragraph() {
let content = "Intro.\n\nFirst line\nsecond line\n===\n\nBody.\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let headings: Vec<_> = ctx.headings().collect();
assert_eq!(headings.len(), 1);
let heading = &headings[0];
assert_eq!((heading.first_line_num(), heading.line_num), (3, 4));
assert_eq!(heading.heading.text_lines, 2);
assert_eq!(heading.heading.text, "First line second line");
assert_eq!(heading.heading.raw_text, "First line second line");
assert_eq!(heading.heading.slug_text, "First line second line");
assert_eq!(heading.heading.content_column, 0);
assert_eq!(heading.first_line_info().byte_offset, 8);
assert_eq!(heading.line_info.byte_offset, 19);
assert_eq!(&content[heading.text_byte_range(content)], "First line\nsecond line");
let text_lines: Vec = ctx.lines.iter().map(|line| line.is_setext_heading_text).collect();
assert_eq!(text_lines, vec![false, false, true, true, false, false, false]);
assert!(
ctx.lines[2].heading.is_none(),
"the heading is recorded on the last line"
);
assert!(ctx.heading_on_line(3).is_none());
assert_eq!(ctx.heading_on_line(4).map(|heading| heading.first_line_num()), Some(3));
let valid: Vec<_> = ctx
.valid_headings()
.map(|heading| {
(
heading.first_line_num(),
heading.line_num,
heading.first_line_info().byte_offset,
)
})
.collect();
assert_eq!(valid, vec![(3, 4, 8)]);
// Three lines, a level 2 underline.
assert_eq!(
setext_headings("One\ntwo\nthree\n---\n", MarkdownFlavor::Standard),
vec![(3, "One two three".to_string(), 2, 0)]
);
// A hard line break renders as a break, not as text: the backslash goes
// with the line ending, and two trailing spaces are trimmed with it. An
// escaped backslash pair is text and stays.
assert_eq!(
setext_headings("Foo\\\nBar\n===\n", MarkdownFlavor::Standard),
vec![(2, "Foo Bar".to_string(), 1, 0)]
);
assert_eq!(
setext_headings("Foo \nBar\n===\n", MarkdownFlavor::Standard),
vec![(2, "Foo Bar".to_string(), 1, 0)]
);
assert_eq!(
setext_headings("Foo\\\\\nBar\n===\n", MarkdownFlavor::Standard),
vec![(2, "Foo\\\\ Bar".to_string(), 1, 0)]
);
// A lazy continuation line is part of the paragraph a list item holds, and
// the underline indented into the item makes a heading of the whole of it.
assert_eq!(
setext_headings("1. a\n\n First\nsecond\n ===\n", MarkdownFlavor::Standard),
vec![(4, "First second".to_string(), 1, 0)]
);
// The text starts where the first line's does and ends before the
// attribute list a custom ID sits in.
let content = " First\nsecond {#custom}\n===\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let heading = ctx.heading_on_line(2).expect("a heading on the last text line");
assert_eq!(heading.heading.text, "First second");
assert_eq!(heading.heading.custom_id.as_deref(), Some("custom"));
assert_eq!(heading.heading.content_column, 2);
assert_eq!(&content[heading.text_byte_range(content)], "First\nsecond");
}
/// The link reference definitions a paragraph opens with are not its text
/// (CommonMark 4.7), so the heading an underline makes starts below them, and a
/// run under definitions alone underlines nothing: it is the text of the
/// paragraph they open, however it reads. Each row's expectation is the
/// CommonMark reference renderer's.
#[test]
fn link_reference_definitions_above_the_text_stay_outside_the_heading() {
let content = "[ref]: /url\nHeading\n=======\n\n[ref]\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let headings: Vec<_> = ctx.headings().collect();
assert_eq!(headings.len(), 1);
assert_eq!((headings[0].first_line_num(), headings[0].line_num), (2, 2));
assert_eq!(headings[0].heading.text, "Heading");
assert_eq!(headings[0].heading.text_lines, 1);
assert_eq!(&content[headings[0].text_byte_range(content)], "Heading");
let text_lines: Vec = ctx.lines.iter().map(|line| line.is_setext_heading_text).collect();
assert_eq!(text_lines, vec![false, true, false, false, false]);
for (content, expected) in [
// Several definitions: one with a title, one indented.
(
"[a]: /a\n[b]: /b \"t\"\nHeading\n===\n",
vec![(3, "Heading".to_string(), 1, 0)],
),
(
"[a]: /a\n [b]: /b\nHeading\n===\n",
vec![(3, "Heading".to_string(), 1, 0)],
),
// A title on the line below a definition belongs to it; a line that
// merely starts with one is paragraph text.
(
"[foo]: /url\n\"title\"\nHeading\n===\n",
vec![(3, "Heading".to_string(), 1, 0)],
),
(
"[foo]: /url\n\"title\" ok\nHeading\n===\n",
vec![(3, "\"title\" ok Heading".to_string(), 1, 0)],
),
// The destination, and the title after it, may follow the label on
// the lines below it, indented or not. A label whose destination line
// reads as no destination defines nothing, and it all stays text.
("[foo]:\n/url\nTitle\n===\n", vec![(3, "Title".to_string(), 1, 0)]),
("[foo]:\n/url \"t\"\nTitle\n===\n", vec![(3, "Title".to_string(), 1, 0)]),
(
"[foo]:\n/url\n\"t\"\nTitle\n===\n",
vec![(4, "Title".to_string(), 1, 0)],
),
(
"[foo]:\n /url\n \"t\"\nTitle\n===\n",
vec![(4, "Title".to_string(), 1, 0)],
),
("[foo]:\n\nTitle\n===\n", vec![(3, "Title".to_string(), 1, 0)]),
("[a]: /a\n[b]:\n/b\nTitle\n===\n", vec![(4, "Title".to_string(), 1, 0)]),
(
"[foo]:\n/url\n\"t\" ok\nTitle\n===\n",
vec![(4, "\"t\" ok Title".to_string(), 1, 0)],
),
(
"[foo]:\n/url \"t\" ok\nTitle\n===\n",
vec![(3, "[foo]: /url \"t\" ok Title".to_string(), 1, 0)],
),
("[foo]:\n===\n", vec![(1, "[foo]:".to_string(), 1, 0)]),
("[foo]:\n/url\n\"t\"\n===\n", vec![]),
("[foo]:\n/url\n===\nBar\n---\n", vec![(4, "=== Bar".to_string(), 2, 0)]),
// Definitions written inside a list item or a blockquote.
(
"- [foo]: /url\n Heading\n ===\n",
vec![(2, "Heading".to_string(), 1, 0)],
),
(
"> [ref]: /url\n> Heading\n> ===\n",
vec![(2, "Heading".to_string(), 1, 1)],
),
// A run under definitions alone is paragraph text, whatever it reads,
// and the paragraph runs on below it.
("[foo]: /url\n===\n[foo]\n", vec![]),
("[foo]: /url\n---\n", vec![]),
("[foo]: /url\n===\nBar\n---\n", vec![(3, "=== Bar".to_string(), 2, 0)]),
("[foo]: /url\n---\nBar\n===\n", vec![(3, "--- Bar".to_string(), 1, 0)]),
// A footnote definition holds its body, and its label line is not
// the text of a heading.
("[^a]: note\nHeading\n===\n", vec![]),
] {
assert_eq!(
setext_headings(content, MarkdownFlavor::Standard),
expected,
"{content:?}"
);
}
}
/// A backslash ending a line before the last is a hard line break, which
/// renders as the break rather than as text, so it goes with the line ending
/// it marks. A backslash inside a code span is code, and a backslash followed
/// by a space is text, since the space is what ends the line. Each row's
/// expectation is the CommonMark reference renderer's.
#[test]
fn a_hard_break_backslash_goes_with_the_line_ending_it_marks() {
for (content, text) in [
("Foo\\\nBar\n===\n", "Foo Bar"),
("Foo\\\r\nBar\r\n===\r\n", "Foo Bar"),
("Foo `a\\\nb` tail\n===\n", "Foo `a\\ b` tail"),
("Foo\\ \nBar\n===\n", "Foo\\ Bar"),
] {
assert_eq!(
setext_headings(content, MarkdownFlavor::Standard),
vec![(2, text.to_string(), 1, 0)],
"{content:?}"
);
}
}
/// The text range of a heading ends where its display text ends: before the
/// attribute list a custom ID sits in and before an anchor element, on an
/// earlier line when the last line holds nothing else, and for an ATX heading
/// before its closing sequence. An anchor element written inside the text keeps
/// the range on the source, so it ends on a character boundary whatever the
/// anchor holds.
#[test]
fn the_text_range_ends_where_the_display_text_ends() {
for (content, text, source_text) in [
("Title.\n{#id}\n===\n", "Title.", "Title."),
("Title.\n\n===\n", "Title.", "Title."),
("Title.\nmore {#id}\n===\n", "Title. more", "Title.\nmore"),
("> Title.\n> {#id}\n> ===\n", "Title.", "Title."),
("# Foo. ##\n", "Foo.", "Foo."),
("# Foo ## {#id}\n", "Foo", "Foo"),
("# Title. \n", "Title.", "Title."),
("# Foo ##\n", "Foo", "Foo"),
("# Foo\n", "Foo", "Foo"),
(
"# Foo bar.\n",
"Foo bar.",
"Foo bar.",
),
] {
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let heading = ctx.headings().next().expect("a heading");
assert_eq!(heading.heading.text, text, "{content:?}");
assert_eq!(&content[heading.text_byte_range(content)], source_text, "{content:?}");
}
}
/// A code block, an HTML block or indented code written inside a list item, a
/// footnote or a blockquote leaves the container open below it, so an underline
/// indented to the container's edge makes a heading there. Each row's
/// expectation is pulldown-cmark's.
#[test]
fn setext_headings_below_a_block_inside_a_container() {
for (content, line, depth) in [
("10. item\n\n ```\n code\n ```\n\n Title\n ===\n", 7, 0),
("10. item\n ```\n code\n ```\n Title\n ===\n", 5, 0),
("10. ```\n code\n ```\n\n Title\n ===\n", 5, 0),
("10. item\n\n code\n\n Title\n ===\n", 5, 0),
("10. item\n\n \n x\n \n\n Title\n ===\n", 7, 0),
("- item\n\n ```\n code\n ```\n\n Title\n ===\n", 7, 0),
(
"[^a]: intro\n\n ```\n code\n ```\n\n Title\n ===\n\nRef[^a]\n",
7,
0,
),
(
"> 10. a\n>\n> ```\n> c\n> ```\n>\n> Title\n> ===\n",
7,
1,
),
// The text of a code block opens no list item, so the heading below the
// block sits at the top level rather than in a lazy item paragraph.
("```\n- a\n ```\n\n Title\n===\n", 5, 0),
// A code block starting on the line after another one ends can open
// containers of its own, so the item holds the heading written inside it.
("~~~\nx\n~~~\n10. ```\n c\n ```\n\n Title\n ===\n", 8, 0),
("~~~\nx\n~~~\n10. ```\n - a\n ```\n\n Title\n ===\n", 8, 0),
(" x\n10. ```\n c\n ```\n\n Title\n ===\n", 6, 0),
(
"~~~\nx\n~~~\n> 10. ```\n> c\n> ```\n>\n> Title\n> ===\n",
8,
1,
),
] {
assert_eq!(
setext_headings(content, MarkdownFlavor::Standard),
vec![(line, "Title".to_string(), 1, depth)],
"{content:?}"
);
}
for content in [
// An underline outside the item is lazy.
"10. item\n\n ```\n code\n ```\n\n Title\n===\n",
"~~~\nx\n~~~\n- ```\n c\n ```\n\n Title\n===\n",
// Four columns past the item's edge are indented code.
"10. item\n\n ```\n code\n ```\n\n Title\n ===\n",
] {
assert!(
setext_headings(content, MarkdownFlavor::Standard).is_empty(),
"{content:?}"
);
}
}
/// CommonMark 4.3 lets a setext underline be indented three spaces. A fourth
/// makes it paragraph text, except where indentation means something else.
#[test]
fn setext_underline_indent_is_limited_to_three_spaces() {
let heading = vec![(1, "Foo".to_string(), 1, 0)];
assert_eq!(setext_headings("Foo\n ===\n", MarkdownFlavor::Standard), heading);
assert!(setext_headings("Foo\n ===\n", MarkdownFlavor::Standard).is_empty());
// The tab reaches column eight, six past the item's content.
assert!(setext_headings("- item\n\n Heading\n\t ===\n", MarkdownFlavor::Standard).is_empty());
// MDX has no indented code, and markdown-rs lifts the limit with it.
assert_eq!(setext_headings("Foo\n ===\n", MarkdownFlavor::MDX), heading);
// An admonition body is indented four columns, and its headings with it.
assert_eq!(
setext_headings("!!! note\n Foo\n ===\n", MarkdownFlavor::MkDocs),
vec![(2, "Foo".to_string(), 1, 0)]
);
}
/// A container's marker line is structure, so the paragraph below it is the
/// container's own, and a heading made of that paragraph holds no marker text.
/// Each row's expectation is the flavor's own renderer's reading: the opener of
/// a MkDocs admonition or content tab, a PyMdown block fence and a MyST colon
/// fence are no part of the body they hold, like a Pandoc div fence.
#[test]
fn container_marker_lines_hold_no_setext_text() {
for (content, flavor, line, text, text_lines) in [
("!!! note\n Foo\n ===\n", MarkdownFlavor::MkDocs, 2, "Foo", 1),
(
"!!! note\n First\n second\n ===\n",
MarkdownFlavor::MkDocs,
3,
"First second",
2,
),
(
"!!! note\n !!! tip\n Foo\n ===\n",
MarkdownFlavor::MkDocs,
3,
"Foo",
1,
),
("=== \"Tab\"\n Foo\n ===\n", MarkdownFlavor::MkDocs, 2, "Foo", 1),
("/// note\nFoo\n===\n///\n", MarkdownFlavor::MkDocs, 2, "Foo", 1),
(
"/// outer\n/// inner\nFoo\n///\nBar\n===\n///\n",
MarkdownFlavor::MkDocs,
5,
"Bar",
1,
),
(":::{note}\nFoo\n===\n:::\n", MarkdownFlavor::MyST, 2, "Foo", 1),
(
"::::{note}\n:::{tip}\nFoo\n:::\nBar\n===\n::::\n",
MarkdownFlavor::MyST,
5,
"Bar",
1,
),
("::: {.note}\nFoo\n===\n:::\n", MarkdownFlavor::Pandoc, 2, "Foo", 1),
] {
let ctx = LintContext::new(content, flavor, None);
let headings: Vec<_> = ctx
.headings()
.filter(super::types::ParsedHeading::is_setext)
.map(|heading| {
(
heading.line_num,
heading.heading.text.clone(),
heading.heading.text_lines,
)
})
.collect();
assert_eq!(headings, vec![(line, text.to_string(), text_lines)], "{content:?}");
}
for (content, flavor, markers) in [
(
"!!! note\n Foo\n ===\n",
MarkdownFlavor::MkDocs,
vec![true, false, false],
),
("=== \"Tab\"\n Foo\n", MarkdownFlavor::MkDocs, vec![true, false]),
("/// note\nFoo\n///\n", MarkdownFlavor::MkDocs, vec![true, false, true]),
(":::{note}\nFoo\n:::\n", MarkdownFlavor::MyST, vec![true, false, true]),
// A fence inside a code block is code, and a `:::` div fence is
// `is_div_marker`.
(
"/// note\n```\n///\n```\n",
MarkdownFlavor::MkDocs,
vec![true, false, false, false],
),
(
"::: {.note}\nFoo\n:::\n",
MarkdownFlavor::Pandoc,
vec![false, false, false],
),
("!!! note\n Foo\n", MarkdownFlavor::Standard, vec![false, false]),
] {
let ctx = LintContext::new(content, flavor, None);
let flags: Vec = ctx.lines.iter().map(|line| line.is_container_marker).collect();
assert_eq!(flags, markers, "{content:?}");
}
}
/// A footnote definition's body is a container whose edge sits four columns
/// past the container holding the definition, re-entered by indentation. Each
/// row's expectation is pulldown-cmark's, and pandoc's CommonMark reader agrees.
#[test]
fn setext_headings_inside_footnote_definitions() {
for (content, line, level) in [
("[^a]: intro\n\n Heading\n ===\n", 3, 1),
("[^a]: intro\n\n Heading\n ===\n", 3, 1),
("[^a]: intro\n\n Heading\n ---\n", 3, 2),
("[^a]: intro\n\n\tHeading\n\t===\n", 3, 1),
// A tab reaches the next multiple of four, which is the body's edge.
("[^a]: intro\n\n Heading\n\t===\n", 3, 1),
// The edge is measured from the definition's container, not its label.
(" [^a]: intro\n\n Heading\n ===\n", 3, 1),
("- [^a]: intro\n\n Heading\n ===\n", 3, 1),
// A definition interrupts a paragraph.
("Para\n[^a]: intro\n\n Heading\n ===\n", 4, 1),
// A label written inside a body starts a body with the same edge.
("[^a]: intro\n\n [^b]: x\n\n Heading\n ===\n", 5, 1),
] {
assert_eq!(
setext_headings(content, MarkdownFlavor::Standard),
vec![(line, "Heading".to_string(), level, 0)],
"{content:?}"
);
}
// A label cannot hold an unescaped bracket, so this line opens no definition
// and is the text of the heading.
assert_eq!(
setext_headings("[^a[b]: Heading\n===\n", MarkdownFlavor::Standard),
vec![(1, "[^a[b]: Heading".to_string(), 1, 0)]
);
// The `>` takes one column of the tab after it, so a body opened in this
// quote starts four columns past the quote's edge, at column 6.
for content in [
">\t[^a]: intro\n>\n>\t Heading\n>\t ===\n",
">\t[^a]: intro\n>\n>\t Heading\n>\t ===\n",
] {
assert_eq!(
setext_headings(content, MarkdownFlavor::Standard),
vec![(3, "Heading".to_string(), 1, 1)],
"{content:?}"
);
}
for content in [
// A heading starting on the label line sits inside the body the label
// opens, where no ATX marker can be written, so none is recorded.
"[^a]: Heading\n ===\n",
"> [^a]: Heading\n> ===\n",
// An underline that does not reach the body's edge is lazy.
"[^a]: Heading\n===\n",
"[^a]: intro\n\n Heading\n===\n",
"[^a]: intro\n\n Heading\n ===\n",
" [^a]: intro\n\n Heading\n ===\n",
// Four columns past the body's edge make it paragraph text.
"[^a]: intro\n\n Heading\n ===\n",
"[^a]: intro\n\n\tHeading\n\t ===\n",
// Four columns past the edge of a body opened in a tab-indented quote
// are indented code.
">\t[^a]: intro\n>\n>\t Heading\n>\t ===\n",
// The body of an indented label shares the edge of the body holding it,
// so four columns further in are paragraph text or indented code.
"[^a]: intro\n\n [^b]: x\n Heading\n ===\n",
"[^a]: intro\n\n [^b]: x\n\n Heading\n ===\n",
// An unindented line closes the body.
"[^a]: intro\n\nHeading\n ===\n",
] {
assert!(
setext_headings(content, MarkdownFlavor::Standard).is_empty(),
"{content:?}"
);
}
}
/// A `>` alone is a blank line inside its blockquote: it closes the blockquotes
/// it does not re-enter and leaves the list items and footnotes around it open,
/// which the reference renderer and pandoc agree on.
#[test]
fn setext_headings_after_a_blank_line_inside_a_blockquote() {
for (content, line, depth) in [
("> - intro\n>\n> Heading\n> ===\n", 3, 1),
("> [^a]: intro\n>\n> Heading\n> ===\n", 3, 1),
// The blank line closes the blockquote nested in the item, which holds on.
("> - a\n> > b\n>\n> Heading\n> ===\n", 4, 1),
("> - a\n> > b\n>\n> > Heading\n> > ===\n", 4, 2),
] {
assert_eq!(
setext_headings(content, MarkdownFlavor::Standard),
vec![(line, "Heading".to_string(), 1, depth)],
"{content:?}"
);
}
// Four columns past the item's edge are indented code.
assert!(
setext_headings(
"> - intro\n>\n> Heading\n> ===\n",
MarkdownFlavor::Standard
)
.is_empty()
);
}
/// A setext heading inside a blockquote is reported at its depth, with columns
/// that locate its text on the quoted line.
#[test]
fn setext_headings_inside_blockquotes() {
let content = "> Title\n> ===\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let headings: Vec<_> = ctx.headings().collect();
assert_eq!(headings.len(), 1);
let heading = &headings[0];
assert_eq!((heading.line_num, heading.blockquote_depth), (1, 1));
assert_eq!(heading.heading.style, HeadingStyle::Setext1);
assert_eq!(heading.heading.text, "Title");
assert_eq!(heading.heading.marker, "===");
assert_eq!((heading.heading.marker_column, heading.heading.content_column), (2, 2));
assert_eq!(heading.text_byte_range(content), 2..7);
assert!(
ctx.lines[0].heading.is_none(),
"a quoted heading is not a top-level one"
);
// The heading is the whole quoted paragraph, a lazy continuation line
// included, recorded on its last line at the paragraph's depth.
assert_eq!(
setext_headings("> First\n> second\n> ===\n", MarkdownFlavor::Standard),
vec![(2, "First second".to_string(), 1, 1)]
);
assert_eq!(
setext_headings("> First\nmiddle\n> last\n> ===\n", MarkdownFlavor::Standard),
vec![(3, "First middle last".to_string(), 1, 1)]
);
let content = "> First\n> second\n> ===\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let heading = ctx.heading_on_line(2).expect("a heading on the last text line");
assert_eq!((heading.first_line_num(), heading.heading.text_lines), (1, 2));
assert_eq!(&content[heading.text_byte_range(content)], "First\n> second");
assert!(
ctx.lines.iter().all(|line| !line.is_setext_heading_text),
"a quoted heading is reported through headings(), not per line"
);
// A heading whose first line carries a list marker is recorded at the
// depth its text sits at.
assert_eq!(
setext_headings("> - item\n> ---\n", MarkdownFlavor::Standard),
vec![(1, "item".to_string(), 2, 1)]
);
assert_eq!(
setext_headings("- First\n second\n ===\n", MarkdownFlavor::Standard),
vec![(2, "First second".to_string(), 1, 0)]
);
assert_eq!(
setext_headings("> > Deep\n> > ---\n", MarkdownFlavor::Standard),
vec![(1, "Deep".to_string(), 2, 2)]
);
assert_eq!(
setext_headings("> Title\n> ===\n", MarkdownFlavor::Standard),
vec![(1, "Title".to_string(), 1, 1)]
);
// The `>` takes one column of a tab after it as its space, and the rest of
// the tab indents the text, so these underlines sit three columns or fewer
// past the quote's edge.
for content in [">\tTitle\n>\t ===\n", " > Title\n >\t ===\n"] {
assert_eq!(
setext_headings(content, MarkdownFlavor::Standard),
vec![(1, "Title".to_string(), 1, 1)],
"{content:?}"
);
}
assert_eq!(
setext_headings("> > Title\n> >\t ===\n", MarkdownFlavor::Standard),
vec![(1, "Title".to_string(), 1, 2)]
);
for content in [
// The two columns of the tab the `>` leaves over, and the two spaces,
// put the underline four columns past the quote's edge.
"> Title\n>\t ===\n",
">\t> Title\n>\t>\t ===\n",
"> Title\n>\t\t===\n",
// `> ---` re-enters only the outer quote, so it is a break there.
"> > Deep\n> ---\n",
// An underline cannot be a lazy continuation line.
"> Quoted\n===\n",
"> Quoted\n---\n",
// Four spaces past the quote's edge make it paragraph text.
"> Title\n> ===\n",
// Indented code is no paragraph to underline.
"> Foo\n> ===\n",
// These are headings in CommonMark, and none is modeled: a lazy text
// line carries fewer `>` than the paragraph it continues, so its own
// depth is not the heading's.
"> First\nsecond\n> ===\n",
"> > First\n> second\n> > ===\n",
"> - a\nb\n> ---\n",
"- > a\n b\n > ---\n",
] {
assert!(
setext_headings(content, MarkdownFlavor::Standard).is_empty(),
"{content:?}"
);
}
}
/// A JSX flow element ends the paragraph above it; a JSX text element is part
/// of the paragraph holding it.
#[test]
fn setext_underline_and_mdx_jsx_elements() {
assert!(setext_headings("Text\n \n===\n", MarkdownFlavor::MDX).is_empty());
assert_eq!(
setext_headings("Para\n \nText\n===\n", MarkdownFlavor::MDX),
vec![(3, "Text".to_string(), 1, 0)]
);
assert_eq!(
setext_headings("Heading new \n---\n", MarkdownFlavor::MDX),
vec![(1, "Heading new ".to_string(), 2, 0)]
);
// A flow element's children are Markdown blocks of their own, while its tag
// lines end the paragraph beside them. Each row's expectation is markdown-rs's,
// and remark-mdx agrees.
assert_eq!(
setext_headings("\ntext\n===\n \n", MarkdownFlavor::MDX),
vec![(2, "text".to_string(), 1, 0)]
);
assert_eq!(
setext_headings("\ntext\n===\n \n", MarkdownFlavor::MDX),
vec![(4, "text".to_string(), 1, 0)]
);
for content in [
"{x}\ntext\n===\n \n",
" \ntext\n===\n \n",
] {
assert_eq!(
setext_headings(content, MarkdownFlavor::MDX),
vec![(2, "text".to_string(), 1, 0)],
"{content:?}"
);
}
for content in [
"\n\ntext\n \n===\n \n",
"\ntext\n \n===\n \n",
"{x}\n===\n \n",
// The element ends no container, so an underline outside the item is a
// lazy continuation of the item's text.
"- \n text\n===\n",
] {
assert!(setext_headings(content, MarkdownFlavor::MDX).is_empty(), "{content:?}");
}
assert_eq!(
setext_headings("- \n text\n ===\n", MarkdownFlavor::MDX),
vec![(2, "text".to_string(), 1, 0)]
);
}
/// An expression alone on its lines is a flow block of its own in MDX, so the
/// `===` below it opens a paragraph, while an expression sharing its line with
/// text is inline content of the heading. Each row's expectation is
/// remark-mdx's.
#[test]
fn setext_underline_below_an_mdx_flow_expression() {
for content in [
"{x}\n===\n",
" {x}\n===\n",
"Intro\n\n{\n x\n}\n===\n",
"{[\n 1,\n]}\n===\n",
"{/* c */}\n===\n",
"text\n{x}\n===\n",
"> {x}\n> ===\n",
"\n{x}\n===\n \n",
"{x} \n===\n",
// The expression ends no container, so an underline outside the item is
// a lazy continuation of the item's text.
"- {x}\n text\n===\n",
] {
assert!(setext_headings(content, MarkdownFlavor::MDX).is_empty(), "{content:?}");
}
for (content, line, text) in [
("{x}\ntext\n===\n", 2, "text"),
("\n{x}\ntext\n===\n \n", 3, "text"),
("{x}\n{y} \ntext\n===\n", 3, "text"),
("- {x}\n text\n ===\n", 2, "text"),
("{x} and text\n===\n", 1, "{x} and text"),
("{x} {y}\n===\n", 1, "{x} {y}"),
] {
assert_eq!(
setext_headings(content, MarkdownFlavor::MDX),
vec![(line, text.to_string(), 1, 0)],
"{content:?}"
);
}
}
/// `:::` opens a div in Pandoc, and is paragraph text where nothing gives it
/// a meaning.
#[test]
fn setext_underline_below_a_colon_fence_depends_on_the_flavor() {
let content = "::: note\n---\n\n# H\n";
assert!(setext_headings(content, MarkdownFlavor::Pandoc).is_empty());
assert_eq!(
setext_headings(content, MarkdownFlavor::Standard),
vec![(1, "::: note".to_string(), 2, 0)]
);
}
#[test]
fn test_multi_line() {
let content = "# Title\n\nSecond line\nThird line";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.line_offsets, vec![0, 8, 9, 21]);
// Test offset to line/col
assert_eq!(ctx.offset_to_line_col(0), (1, 1)); // start
assert_eq!(ctx.offset_to_line_col(8), (2, 1)); // start of blank line
assert_eq!(ctx.offset_to_line_col(9), (3, 1)); // start of 'Second line'
assert_eq!(ctx.offset_to_line_col(15), (3, 7)); // middle of 'Second line'
assert_eq!(ctx.offset_to_line_col(21), (4, 1)); // start of 'Third line'
}
#[test]
fn test_is_blank_follows_commonmark_blank_line() {
// A blank line holds only spaces and tabs. Unicode spaces are paragraph text.
let content = "a\n \t \n\u{a0}\n\u{2003}\n> \n> \u{a0}\n\u{3000}b\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let blank: Vec = ctx.lines.iter().map(|l| l.is_blank).collect();
assert_eq!(blank[..7], [false, true, false, false, true, false, false]);
}
#[test]
fn test_line_info() {
let content = "# Title\n indented\n\ncode:\n```rust\nfn main() {}\n```";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Test line info
assert_eq!(ctx.lines.len(), 7);
// Line 1: "# Title"
let line1 = &ctx.lines[0];
assert_eq!(line1.content(ctx.content), "# Title");
assert_eq!(line1.byte_offset, 0);
assert_eq!(line1.indent, 0);
assert!(!line1.is_blank);
assert!(!line1.in_code_block);
assert!(line1.list_item.is_none());
// Line 2: " indented"
let line2 = &ctx.lines[1];
assert_eq!(line2.content(ctx.content), " indented");
assert_eq!(line2.byte_offset, 8);
assert_eq!(line2.indent, 4);
assert!(!line2.is_blank);
// Line 3: "" (blank)
let line3 = &ctx.lines[2];
assert_eq!(line3.content(ctx.content), "");
assert!(line3.is_blank);
// Test helper methods
assert_eq!(ctx.line_info(1).map(|l| l.indent), Some(0));
assert_eq!(ctx.line_info(2).map(|l| l.indent), Some(4));
assert_eq!(ctx.line_info(1).map(|l| l.byte_offset), Some(0));
assert_eq!(ctx.line_info(2).map(|l| l.byte_offset), Some(8));
}
#[test]
fn test_list_item_detection() {
let content = "- Unordered item\n * Nested item\n1. Ordered item\n 2) Nested ordered\n\nNot a list";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Line 1: "- Unordered item"
let line1 = &ctx.lines[0];
assert!(line1.list_item.is_some());
let list1 = line1.list_item.as_ref().unwrap();
assert_eq!(list1.marker, "-");
assert!(!list1.is_ordered);
assert_eq!(list1.marker_column, 0);
assert_eq!(list1.content_column, 2);
// Line 2: " * Nested item"
let line2 = &ctx.lines[1];
assert!(line2.list_item.is_some());
let list2 = line2.list_item.as_ref().unwrap();
assert_eq!(list2.marker, "*");
assert_eq!(list2.marker_column, 2);
// Line 3: "1. Ordered item"
let line3 = &ctx.lines[2];
assert!(line3.list_item.is_some());
let list3 = line3.list_item.as_ref().unwrap();
assert_eq!(list3.marker, "1.");
assert!(list3.is_ordered);
assert_eq!(list3.number, Some(1));
// Line 6: "Not a list"
let line6 = &ctx.lines[5];
assert!(line6.list_item.is_none());
}
#[test]
fn parsed_list_views_preserve_existing_cache_semantics() {
let fixtures = [
(
MarkdownFlavor::Standard,
"- root\n 1. ordered child\n continuation\n * sibling\n\n> 10. quoted\n> - nested\n\nparagraph\n\t- tabbed\n\n - indented code\n",
),
(
MarkdownFlavor::MkDocs,
"!!! note\n - item\n + nested\n\n=== tab\n 1. ordered\n - child\n",
),
(
MarkdownFlavor::Kramdown,
"{::nomarkdown}\n- hidden\n{:/nomarkdown}\n\n- visible\n - child\n",
),
(MarkdownFlavor::AzureDevOps, ":::python\n- code\n:::\n\n> - visible\n"),
];
for (flavor, content) in fixtures {
let ctx = LintContext::new(content, flavor, None);
let raw_items: Vec<_> = ctx
.lines
.iter()
.enumerate()
.filter_map(|(idx, line)| {
line.list_item.as_deref().map(|item| {
(
idx + 1,
item.marker.as_str(),
item.is_ordered,
item.number,
item.marker_column,
item.content_column,
line.blockquote.as_ref().map_or(0, |bq| bq.nesting_level),
line.blockquote.as_ref().map_or(0, |bq| bq.prefix.len()),
)
})
})
.collect();
let parsed_items: Vec<_> = ctx
.list_items()
.map(|item| {
(
item.line_num(),
item.marker(),
item.is_ordered(),
item.number(),
item.marker_column(),
item.content_column(),
item.blockquote_depth(),
item.blockquote_prefix_len(),
)
})
.collect();
assert_eq!(parsed_items, raw_items, "list item view drifted for {flavor:?}");
for line_num in 0..=ctx.lines.len() + 1 {
let raw = line_num
.checked_sub(1)
.and_then(|idx| ctx.lines.get(idx))
.and_then(|line| line.list_item.as_deref());
let parsed = ctx.list_item_on_line(line_num);
assert_eq!(parsed.is_some(), raw.is_some(), "lookup drifted at line {line_num}");
if let (Some(parsed), Some(raw)) = (parsed, raw) {
assert_eq!(parsed.marker(), raw.marker);
assert_eq!(
parsed.marker_byte_offset(),
parsed.line_info().byte_offset + raw.marker_column
);
}
}
let parsed_blocks = ctx.parsed_list_blocks();
assert_eq!(parsed_blocks.len(), ctx.list_blocks.len());
assert_eq!(parsed_blocks.is_empty(), ctx.list_blocks.is_empty());
for (index, (parsed, raw)) in parsed_blocks.into_iter().zip(&ctx.list_blocks).enumerate() {
assert_eq!(
parsed_blocks.get(index).map(ParsedListBlock::start_line),
Some(raw.start_line)
);
assert_eq!(parsed.start_line(), raw.start_line);
assert_eq!(parsed.end_line(), raw.end_line);
assert_eq!(parsed.is_ordered(), raw.is_ordered);
assert_eq!(parsed.marker(), raw.marker.as_deref());
assert_eq!(parsed.blockquote_prefix(), raw.blockquote_prefix);
assert_eq!(parsed.nesting_level(), raw.nesting_level);
assert_eq!(parsed.max_marker_width(), raw.max_marker_width);
assert_eq!(
parsed.items().map(ParsedListItem::line_num).collect::>(),
raw.item_lines
.iter()
.copied()
.filter(|line| ctx.list_item_on_line(*line).is_some())
.collect::>()
);
}
assert!(parsed_blocks.get(parsed_blocks.len()).is_none());
assert_eq!(ctx.has_list_items(), !raw_items.is_empty());
assert_eq!(ctx.has_unordered_list_items(), raw_items.iter().any(|item| !item.2));
}
}
#[test]
fn parsed_list_block_items_keep_source_order_and_container_depth() {
let content = "- root\n - child\n> 1. quoted\n> + nested\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(
ctx.list_items()
.map(|item| (item.line_num(), item.marker(), item.blockquote_depth()))
.collect::>(),
vec![(1, "-", 0), (2, "-", 0), (3, "1.", 1), (4, "+", 1)]
);
let block_lines: Vec> = ctx
.parsed_list_blocks()
.into_iter()
.map(|block| block.items().map(ParsedListItem::line_num).collect())
.collect();
assert_eq!(block_lines, vec![vec![1, 2, 3, 4]]);
}
#[test]
fn list_block_item_groups_split_a_block_into_its_lists() {
// One block holds every level; the groups are the lists inside it, one
// per run of items at a level, closed by the next shallower item, so the
// children of `a` and the children of `b` are different lists even though
// they sit at the same level. A tab and two spaces put `b1x` on column
// 6, level 3, as six spaces would, and the level skipped on the way down
// still closes on the way back up.
let content = "- a\n - a1\n - a2\n- b\n - b1\n\t - b1x\n - b2\n- c\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.list_blocks.len(), 1, "{:?}", ctx.list_blocks);
let groups: Vec> = ctx
.list_blocks
.iter()
.flat_map(|block| ctx.list_block_item_groups(block))
.collect();
assert_eq!(groups, vec![vec![1, 4, 8], vec![2, 3], vec![5, 7], vec![6]]);
}
#[test]
fn list_block_item_groups_split_on_a_marker_type_change() {
// Two items at one level are in the same list only when their markers
// are of one type: the same bullet character, or ordered markers with
// the same delimiter. A change starts a new list at that level, nested
// or not, even though the tracker keeps consecutive items of both kinds
// in one block. Ordered markers may count up freely.
let groups = |content: &str| -> Vec> {
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.list_blocks.len(), 1, "{content:?}: {:?}", ctx.list_blocks);
ctx.list_blocks
.iter()
.flat_map(|block| ctx.list_block_item_groups(block))
.collect()
};
for content in [
"- p\n - a\n - b\n * c\n\n * d\n",
"- p\n - a\n - b\n + c\n\n + d\n",
"- p\n 1. a\n 2. b\n 1) c\n\n 2) d\n",
"- p\n - a\n - b\n 1. c\n\n 2. d\n",
"- p\n 1. a\n 2. b\n - c\n\n - d\n",
] {
assert_eq!(groups(content), vec![vec![1], vec![2, 3], vec![4, 6]], "{content:?}");
}
assert_eq!(groups("- a\n- b\n* c\n\n* d\n"), vec![vec![1, 2], vec![3, 5]]);
assert_eq!(groups("1. a\n2. b\n1) c\n\n2) d\n"), vec![vec![1, 2], vec![3, 5]]);
// Controls: one type throughout is one list.
for content in [
"- p\n - a\n - b\n - c\n\n - d\n",
"- p\n 1. a\n 2. b\n 7. c\n\n 4. d\n",
"- p\n 1) a\n 2) b\n 3) c\n\n 4) d\n",
] {
assert_eq!(groups(content), vec![vec![1], vec![2, 3, 4, 6]], "{content:?}");
}
assert_eq!(groups("- a\n- b\n- c\n\n- d\n"), vec![vec![1, 2, 3, 5]]);
}
#[test]
fn list_block_item_groups_read_ancestry_from_columns() {
// An item is nested in the open list's latest item when it starts at or
// right of that item's content column, and a sibling when it starts left
// of it but not left of the column the list lives in. Siblings may sit at
// different indents, so a sibling and a nested list can share a marker
// column and still be different lists; the level is not the column.
let groups = |content: &str| -> Vec> {
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.list_blocks.len(), 1, "{content:?}: {:?}", ctx.list_blocks);
ctx.list_blocks
.iter()
.flat_map(|block| ctx.list_block_item_groups(block))
.collect()
};
// The siblings at column 2 sit left of the parent's content column 3, so
// they are the parent's siblings, not more items of the child list at
// column 3.
assert_eq!(
groups(" - parent\n - child a\n - child b\n\n - sibling a\n\n - sibling b\n"),
vec![vec![1, 5, 7], vec![2, 3]]
);
// Siblings at indents 0 to 3 are one list, at the top and nested.
assert_eq!(groups(" - first\n- second\n"), vec![vec![1, 2]]);
assert_eq!(groups("- a\n - b\n - c\n - d\n"), vec![vec![1, 2, 3, 4]]);
assert_eq!(groups("- a\n - b\n - c\n"), vec![vec![1], vec![2, 3]]);
// Ordered markers of different widths are siblings when the wider one
// starts left of the content column.
assert_eq!(groups("9. a\n10. b\n\n11. c\n"), vec![vec![1, 2, 4]]);
// An item at or right of the content column is nested, however deep.
assert_eq!(groups("- a\n\n - b\n"), vec![vec![1], vec![3]]);
assert_eq!(
groups("* parent\n - child a\n - child b\n * sibling\n"),
vec![vec![1], vec![2, 3], vec![4]]
);
// A blockquote starting inside the item's content nests its list there;
// one starting left of the content column ends the list, and an item
// after it starts another.
assert_eq!(groups("- a\n > - b\n > - c\n- d\n"), vec![vec![1, 4], vec![2, 3]]);
assert_eq!(groups("> - a\n> > - b\n> - c\n"), vec![vec![1, 3], vec![2]]);
assert_eq!(groups("1. a\n> - b\n2. c\n"), vec![vec![1], vec![2], vec![3]]);
assert_eq!(groups("> - a\n>> - b\n"), vec![vec![1], vec![2]]);
// Columns inside a blockquote count from the quote's content, so the
// indent before the `>` and the space after it change nothing: the child
// items at quote-relative column 2 are nested in a parent whose content
// starts at 2, and an item at column 1 is the parent's sibling.
assert_eq!(
groups(" > - parent\n> - child a\n>\n> - child b\n"),
vec![vec![1], vec![2, 4]]
);
assert_eq!(groups(" > - a\n> - b\n> - c\n"), vec![vec![1, 3], vec![2]]);
assert_eq!(groups(">- a\n> - b\n"), vec![vec![1, 2]]);
assert_eq!(groups("> - a\n>- b\n"), vec![vec![1, 2]]);
assert_eq!(groups("> - a\n>\t- b\n"), vec![vec![1], vec![2]]);
assert_eq!(groups("- a\n > - b\n > - c\n- e\n"), vec![vec![1, 4], vec![2, 3]]);
}
#[test]
fn list_block_item_groups_close_a_nested_list_on_ancestor_content() {
// Content that belongs to the parent item, sitting left of the nested
// items' content column, ends the nested list; a later nested list under
// the same parent is a different list. A paragraph line directly under
// an item continues it lazily wherever it starts, so it closes nothing.
let groups = |content: &str| -> Vec> {
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.list_blocks.len(), 1, "{content:?}: {:?}", ctx.list_blocks);
ctx.list_blocks
.iter()
.flat_map(|block| ctx.list_block_item_groups(block))
.collect()
};
// Paragraph at the parent's content column between two nested lists.
assert_eq!(
groups("- p\n - a\n - b\n\n With:\n\n - c\n\n - d\n"),
vec![vec![1], vec![2, 3], vec![7, 9]]
);
// Content indented to the nested items' content column continues them.
assert_eq!(
groups("- p\n - a\n\n More about a.\n\n - b\n"),
vec![vec![1], vec![2, 6]]
);
// A lazy continuation line starts wherever it likes.
assert_eq!(groups("- p\n - a\nlazy tail of a\n - b\n"), vec![vec![1], vec![2, 4]]);
// Ancestor content also closes every list deeper than the one it ends.
assert_eq!(
groups("- p\n - a\n - a1\n\n With:\n\n - b\n - b1\n"),
vec![vec![1], vec![2], vec![3], vec![7], vec![8]]
);
// Inside a blockquote the same columns are measured after the marker.
assert_eq!(
groups("> - p\n> - a\n> - b\n>\n> With:\n>\n> - c\n> - d\n"),
vec![vec![1], vec![2, 3], vec![7, 8]]
);
// What interrupts a paragraph needs no blank line before it to end the
// nested list: an HTML comment or instruction opener, and a blockquote
// (a `>` with nothing after it opens one under an unquoted list; it is
// a blank line only to a list at its own quote depth).
assert_eq!(
groups("- p\n - a\n - b\n \n - c\n\n - d\n"),
vec![vec![1], vec![2, 3], vec![5, 7]]
);
assert_eq!(
groups("- p\n - a\n - b\n \n - c\n\n - d\n"),
vec![vec![1], vec![2, 3], vec![5, 7]]
);
assert_eq!(
groups("- p\n - a\n - b\n >\n - c\n\n - d\n"),
vec![vec![1], vec![2, 3], vec![5, 7]]
);
assert_eq!(
groups("> - p\n> - a\n> - b\n> >\n> - c\n>\n> - d\n"),
vec![vec![1], vec![2, 3], vec![5, 7]]
);
// A comment that opens inside an item's paragraph continues that
// paragraph across the lines it covers; only an opener at line start
// is a block.
assert_eq!(
groups("- p\n - a\n - b \n - c\n\n - d\n"),
vec![vec![1], vec![2, 3, 6, 8]]
);
// Lazy continuation needs an open paragraph. A bare `>` inside the
// nested item and an empty item leave none, so the dedented text after
// them is the parent's and ends the nested list; a quoted paragraph is
// one, and the text continues it.
assert_eq!(
groups("- p\n - a\n >\n parent\n - c\n\n - d\n"),
vec![vec![1], vec![2], vec![5, 7]]
);
assert_eq!(
groups("- p\n - a\n -\n text\n - c\n\n - d\n"),
vec![vec![1], vec![2, 3], vec![5, 7]]
);
assert_eq!(
groups("- p\n - a\n > quote\n parent\n - c\n\n - d\n"),
vec![vec![1], vec![2, 5, 7]]
);
// An item whose own text opens a block (a fence, a heading, a thematic
// break, an HTML block, indented code) opens no paragraph either, so
// dedented text after it ends the nested list; a nested item's text is a
// paragraph and continues.
for (content, second_list) in [
("- p\n - a\n - ```\n more\n - c\n\n - d\n", vec![5, 7]),
("- p\n - a\n - # h\n more\n - c\n\n - d\n", vec![5, 7]),
("- p\n - a\n - ***\n more\n - c\n\n - d\n", vec![5, 7]),
("- p\n - a\n - code\n more\n - c\n\n - d\n", vec![5, 7]),
("- p\n - a\n - \n more\n - c\n\n - d\n", vec![5, 7]),
("- p\n - a\n - - # h\n more\n - c\n\n - d\n", vec![5, 7]),
("- p\n - a\n - -\n more\n - c\n\n - d\n", vec![5, 7]),
("- p\n - a\n - >\n more\n - c\n\n - d\n", vec![5, 7]),
("- p\n - a\n - > code\n more\n - c\n\n - d\n", vec![5, 7]),
("- p\n - a\n - 1. ```\n more\n - c\n\n - d\n", vec![5, 7]),
(
"- p\n - a\n - | h |\n | --- |\n more\n - c\n\n - d\n",
vec![6, 8],
),
("- p\n - a\n - \n - c\n\n - d\n", vec![6, 8]),
] {
assert_eq!(groups(content), vec![vec![1], vec![2, 3], second_list], "{content:?}");
}
for content in [
"- p\n - a\n - - b1\n more\n - c\n\n - d\n",
"- p\n - a\n - text\n more\n - c\n\n - d\n",
"- p\n - a\n - #tag\n more\n - c\n\n - d\n",
"- p\n - a\n - ```lang`bad\n more\n - c\n\n - d\n",
"- p\n - a\n - > text\n more\n - c\n\n - d\n",
"- p\n - a\n - - text\n more\n - c\n\n - d\n",
"- p\n - a\n - 1234567890. x\n more\n - c\n\n - d\n",
] {
assert_eq!(groups(content), vec![vec![1], vec![2, 3, 5, 7]], "{content:?}");
}
}
#[test]
fn list_block_item_groups_end_with_the_item_that_holds_their_blockquote() {
// A list inside a blockquote inside an item lives in that item: a line
// whose `>` sits left of the item's content is not in the item, so it
// ends the item and every list nested in it, however the columns inside
// the quote compare. A line with fewer quote markers than the list ends
// its blockquote unless it lazily continues a paragraph, a blank line
// included, and items in the next blockquote are another list. A blank
// line or bare `>` at the list's own depth is only the spacing between
// its items.
let groups = |content: &str| -> Vec> {
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.list_blocks.len(), 1, "{content:?}: {:?}", ctx.list_blocks);
ctx.list_blocks
.iter()
.flat_map(|block| ctx.list_block_item_groups(block))
.collect()
};
// The `>` at column 0 leaves `p`; the quote inside `p` measured its
// items from column 0 too, so the columns alone would read `c` as a
// sibling of `b2`.
assert_eq!(
groups("- p\n >- b1\n >\n >- b2\n>- c\n>- d\n"),
vec![vec![1], vec![2, 4], vec![5, 6]]
);
assert_eq!(groups("- p\n >- b x\n>- c x\n"), vec![vec![1], vec![2], vec![3]]);
assert_eq!(groups("- p\n > - b1\n>\n > - b2\n"), vec![vec![1], vec![2], vec![4]]);
// A blank line ends the blockquote; a bare `>` at its depth does not.
assert_eq!(groups("- p\n > - b1\n\n > - b2\n"), vec![vec![1], vec![2], vec![4]]);
assert_eq!(
groups("- p\n > - b1\n > - b2\n\n > - b3\n"),
vec![vec![1], vec![2, 3], vec![5]]
);
assert_eq!(
groups("> - a\n> - b\n> - c\n\n> - d\n"),
vec![vec![1], vec![2, 3], vec![5]]
);
assert_eq!(groups("> - a\n> - b\n>\n> - c\n"), vec![vec![1], vec![2, 4]]);
assert_eq!(
groups("- p\n > > - x\n >\n > > - y\n"),
vec![vec![1], vec![2], vec![4]]
);
assert_eq!(groups("- p\n > > - x\n > >\n > > - y\n"), vec![vec![1], vec![2, 4]]);
// Paragraph text with fewer markers continues the quoted item lazily.
assert_eq!(groups("- p\n > - b1\n lazy\n > - b2\n"), vec![vec![1], vec![2, 4]]);
assert_eq!(groups("- p\n >- b1\ntext\n >- b2\n"), vec![vec![1], vec![2, 4]]);
// After the blockquote, an item at the holding item's content column
// starts a new list in that item; a deeper `>` left of the quoted item's
// content starts a blockquote beside it, and its list is nested in `p`.
assert_eq!(groups("- p\n > - b1\n - q\n"), vec![vec![1], vec![2], vec![3]]);
assert_eq!(groups("- p\n > - a\n >> - b\n"), vec![vec![1], vec![2], vec![3]]);
}
#[test]
fn list_block_item_groups_close_the_list_at_the_block_level_too() {
// A fence, an HTML block or a blank line that ends a blockquote closes
// the list at the block's own level the way it closes a nested one; the
// tracker keeps the items on both sides in one block, so a later item
// starts a new list at that level instead of nesting in the item the
// line ended, and an item that would nest in the ended item is a
// sibling of the item after it. A lazy paragraph line and content at
// the item's content column continue the item.
let groups = |content: &str| -> Vec> {
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.list_blocks.len(), 1, "{content:?}: {:?}", ctx.list_blocks);
ctx.list_blocks
.iter()
.flat_map(|block| ctx.list_block_item_groups(block))
.collect()
};
assert_eq!(groups("- p\n```\n```\n- q\n"), vec![vec![1], vec![4]]);
assert_eq!(groups("- p\n\n- q\n"), vec![vec![1], vec![3]]);
assert_eq!(groups("- p\n```\n```\n - b\n- c\n"), vec![vec![1], vec![4, 5]]);
assert_eq!(groups("- p\n\n - b\n- c\n"), vec![vec![1], vec![3, 4]]);
assert_eq!(
groups("- p\n```\n```\n > - b\n lazy\n> - c\n"),
vec![vec![1], vec![4, 6]]
);
assert_eq!(groups("> - a\n> - b\n\n> - c\n"), vec![vec![1, 2], vec![4]]);
// Controls: the item continues through a lazy line and indented content.
assert_eq!(groups("- p\nlazy\n- q\n"), vec![vec![1, 3]]);
assert_eq!(groups("- p\n\n more\n- q\n"), vec![vec![1, 4]]);
assert_eq!(groups("- p\n\n ```\n ```\n- q\n"), vec![vec![1, 5]]);
}
#[test]
fn commonmark_ordered_lists_preserve_raw_membership_and_start_values() {
let fixtures = [
(
MarkdownFlavor::Standard,
"11. outer\n 1. nested\n 2. nested\n12. outer\n\nparagraph\n\n1. restart\n2. restart\n\n> 7. quoted\n> 8. quoted\n",
),
(
MarkdownFlavor::MkDocs,
"1. outer\n 9. nested\n 10. nested\n2. outer\n\n!!! note\n 4. inside\n 5. inside\n",
),
(
MarkdownFlavor::Pandoc,
"(@example) example marker\n\n3) parenthesized\n4) parenthesized\n\n1. ordinary\n",
),
];
for (flavor, content) in fixtures {
let ctx = LintContext::new(content, flavor, None);
assert!(ctx.commonmark_ordered_lists_cache.get().is_none());
let raw = crate::utils::code_block_utils::CodeBlockUtils::detect_code_blocks_and_spans(content);
let mut groups: std::collections::HashMap> = std::collections::HashMap::new();
for line_num in 1..=ctx.lines.len() {
if ctx.list_item_on_line(line_num).is_some_and(ParsedListItem::is_ordered)
&& let Some(&list_id) = raw.line_to_list.get(&line_num)
{
groups.entry(list_id).or_default().push(line_num);
}
}
let mut expected: Vec<_> = groups
.into_iter()
.map(|(list_id, mut item_lines)| {
item_lines.sort_unstable();
(raw.list_start_values.get(&list_id).copied().unwrap_or(1), item_lines)
})
.collect();
expected.sort_by_key(|(_, lines)| lines.first().copied().unwrap_or(0));
let lists = ctx.commonmark_ordered_lists();
assert!(ctx.commonmark_ordered_lists_cache.get().is_some());
let actual: Vec<_> = lists
.into_iter()
.map(|list| {
(
list.start_value(),
list.items().map(ParsedListItem::line_num).collect::>(),
)
})
.collect();
assert_eq!(actual, expected, "CommonMark grouping drifted for {flavor:?}");
assert_eq!(lists.len(), expected.len());
assert_eq!(lists.is_empty(), expected.is_empty());
for (index, expected_list) in expected.iter().enumerate() {
let list = lists.get(index).expect("index comes from expected list collection");
assert_eq!(list.start_value(), expected_list.0);
}
assert!(lists.get(lists.len()).is_none());
}
}
#[test]
fn commonmark_ordered_lists_keep_nested_groups_source_ordered() {
let content = "11. outer\n 1. nested\n 2. nested\n12. outer\n\ntext\n\n1. restart\n2. restart\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let groups: Vec<_> = ctx
.commonmark_ordered_lists()
.into_iter()
.map(|list| {
(
list.start_value(),
list.items().map(ParsedListItem::line_num).collect::>(),
)
})
.collect();
assert_eq!(groups, vec![(11, vec![1, 4]), (1, vec![2, 3]), (1, vec![8, 9])]);
}
#[test]
fn test_offset_to_line_col_edge_cases() {
let content = "a\nb\nc";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// line_offsets: [0, 2, 4]
assert_eq!(ctx.offset_to_line_col(0), (1, 1)); // 'a'
assert_eq!(ctx.offset_to_line_col(1), (1, 2)); // after 'a'
assert_eq!(ctx.offset_to_line_col(2), (2, 1)); // 'b'
assert_eq!(ctx.offset_to_line_col(3), (2, 2)); // after 'b'
assert_eq!(ctx.offset_to_line_col(4), (3, 1)); // 'c'
assert_eq!(ctx.offset_to_line_col(5), (3, 2)); // after 'c'
}
#[test]
fn test_offset_to_line_col_non_ascii() {
// Issue #670: the column is a character offset, not a byte offset.
// "你好x": 你=bytes 0-2, 好=bytes 3-5, x=byte 6.
let content = "你好x";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.offset_to_line_col(0), (1, 1)); // 你 -> char column 1
assert_eq!(ctx.offset_to_line_col(3), (1, 2)); // 好 -> char column 2
assert_eq!(ctx.offset_to_line_col(6), (1, 3)); // x -> char column 3 (not byte 7)
}
#[test]
fn source_location_queries_define_utf8_crlf_and_eof_boundaries() {
let content = "éx\r\n中\nlast";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.line_start_byte(1), Some(0));
assert_eq!(ctx.line_start_byte(2), Some(5));
assert_eq!(ctx.line_start_byte(3), Some(9));
assert_eq!(ctx.line_start_byte(0), None);
assert_eq!(ctx.line_start_byte(4), None);
assert_eq!(ctx.line_column_byte_range(1, 2), 2..2);
assert_eq!(ctx.line_column_byte_range_with_length(1, 1, 2), 0..3);
assert_eq!(ctx.line_text_byte_range(2, 1, 2), 5..8);
assert_eq!(ctx.line_content_byte_range(1), 0..3);
assert_eq!(ctx.whole_line_byte_range(1), 0..5);
assert_eq!(ctx.line_span_byte_range(1, 2), 0..9);
assert_eq!(ctx.line_content_byte_range(3), 9..13);
assert_eq!(ctx.whole_line_byte_range(3), 9..13);
let eof = content.len();
assert_eq!(ctx.line_column_byte_range(99, 99), eof..eof);
assert_eq!(ctx.line_content_byte_range(99), eof..eof);
}
#[test]
fn test_mdx_esm_blocks() {
let content = r##"import {Chart} from './snowfall.js'
export const year = 2023
# Last year's snowfall
In {year}, the snowfall was above average.
It was followed by a warm spring which caused
flood conditions in many of the nearby rivers.
"##;
let ctx = LintContext::new(content, MarkdownFlavor::MDX, None);
// Check that lines 1 and 2 are marked as ESM blocks
assert_eq!(ctx.lines.len(), 10);
assert!(ctx.lines[0].in_esm_block, "Line 1 (import) should be in_esm_block");
assert!(ctx.lines[1].in_esm_block, "Line 2 (export) should be in_esm_block");
assert!(!ctx.lines[2].in_esm_block, "Line 3 (blank) should NOT be in_esm_block");
assert!(
!ctx.lines[3].in_esm_block,
"Line 4 (heading) should NOT be in_esm_block"
);
assert!(!ctx.lines[4].in_esm_block, "Line 5 (blank) should NOT be in_esm_block");
assert!(!ctx.lines[5].in_esm_block, "Line 6 (text) should NOT be in_esm_block");
}
#[test]
fn test_mdx_esm_blocks_not_detected_in_standard_flavor() {
let content = r#"import {Chart} from './snowfall.js'
export const year = 2023
# Last year's snowfall
"#;
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// ESM blocks should NOT be detected in Standard flavor
assert!(
!ctx.lines[0].in_esm_block,
"Line 1 should NOT be in_esm_block in Standard flavor"
);
assert!(
!ctx.lines[1].in_esm_block,
"Line 2 should NOT be in_esm_block in Standard flavor"
);
}
#[test]
fn test_blockquote_with_indented_content() {
// Lines with `>` followed by heavily-indented content should be detected as blockquotes.
// The content inside the blockquote may also be detected as a code block (which is correct),
// but for MD046 purposes, we need to know the line is inside a blockquote.
let content = r#"# Heading
> -S socket-path
> More text
"#;
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Line 3 (index 2) should be detected as blockquote
assert!(
ctx.lines.get(2).is_some_and(|l| l.blockquote.is_some()),
"Line 3 should be a blockquote"
);
// Line 4 (index 3) should also be blockquote
assert!(
ctx.lines.get(3).is_some_and(|l| l.blockquote.is_some()),
"Line 4 should be a blockquote"
);
// Verify blockquote content is correctly parsed
// Note: spaces_after includes the spaces between `>` and content
let bq3 = ctx.lines.get(2).unwrap().blockquote.as_ref().unwrap();
assert_eq!(bq3.content, "-S socket-path");
assert_eq!(bq3.nesting_level, 1);
// 6 spaces after the `>` marker
assert!(bq3.has_multiple_spaces_after_marker);
let bq4 = ctx.lines.get(3).unwrap().blockquote.as_ref().unwrap();
assert_eq!(bq4.content, "More text");
assert_eq!(bq4.nesting_level, 1);
}
#[test]
fn test_blockquote_spaced_nested_markers_are_detected() {
let content = r#"> > Nested quote content
> > Additional line
"#;
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let bq1 = ctx.lines.first().unwrap().blockquote.as_ref().unwrap();
assert_eq!(bq1.nesting_level, 2);
assert_eq!(bq1.prefix, "> > ");
assert_eq!(bq1.content, "Nested quote content");
let bq2 = ctx.lines.get(1).unwrap().blockquote.as_ref().unwrap();
assert_eq!(bq2.nesting_level, 2);
assert_eq!(bq2.prefix, "> > ");
assert_eq!(bq2.content, "Additional line");
}
#[test]
fn test_ref_def_with_angle_bracket_destination_containing_space() {
// CommonMark §6.6 admits <...>-form destinations that contain spaces.
// Without this, the auto-fix output `[id]: <./has space.md>` (which
// `format_url_destination` chooses for whitespace-bearing URLs) silently
// disappears from `ctx.reference_definitions()` on the next parse, breaking
// dedup in MD054 and ref-def discovery in MD053/MD057.
let content = "[docs]: <./has space.md>\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(
ctx.reference_definitions().len(),
1,
"angle-bracket destination must parse"
);
assert_eq!(ctx.reference_definitions()[0].id, "docs");
assert_eq!(
ctx.reference_definitions()[0].url,
"./has space.md",
"URL should be the destination content, not the angle-bracketed form"
);
assert_eq!(ctx.reference_definitions()[0].title, None);
}
#[test]
fn test_ref_def_with_angle_bracket_destination_and_title() {
// The optional title still parses after an angle-bracket destination.
let content = "[docs]: <./has space.md> \"Help me\"\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.reference_definitions().len(), 1);
assert_eq!(ctx.reference_definitions()[0].url, "./has space.md");
assert_eq!(ctx.reference_definitions()[0].title.as_deref(), Some("Help me"));
}
#[test]
fn test_ref_def_multiline_title_on_next_line() {
// CommonMark §4.7: a reference definition's title may sit on the line
// immediately after the destination. The whole definition - including the
// title line - is one reference definition.
let content = "[ref]: https://example.com\n \"the title\"\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.reference_definitions().len(), 1);
assert_eq!(ctx.reference_definitions()[0].title.as_deref(), Some("the title"));
// The byte range and is_in_reference_def cover the continuation title line.
let quote = content.find('"').unwrap();
assert!(
ctx.is_in_reference_def(quote),
"title line should be inside the ref def"
);
// The title byte range spans the delimiters exactly, and byte_end reaches
// the end of the continuation line (the byte before its newline).
let def = &ctx.reference_definitions()[0];
assert_eq!(
def.title_byte_start,
Some(quote),
"title_byte_start is the opening quote"
);
assert_eq!(
def.title_byte_end,
Some(quote + "\"the title\"".len()),
"title_byte_end is one past the closing quote"
);
assert_eq!(
def.byte_end,
content.trim_end_matches('\n').len(),
"byte_end reaches end of title line"
);
}
#[test]
fn test_ref_def_single_quote_and_paren_title_on_next_line() {
for content in [
"[ref]: https://example.com\n 'the title'\n",
"[ref]: https://example.com\n (the title)\n",
] {
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.reference_definitions().len(), 1, "input: {content:?}");
assert_eq!(
ctx.reference_definitions()[0].title.as_deref(),
Some("the title"),
"input: {content:?}"
);
}
}
#[test]
fn test_ref_def_blank_line_breaks_multiline_title() {
// A blank line between the destination and a quoted line means the quoted
// line is a separate paragraph, not the definition's title.
let content = "[ref]: https://example.com\n\n\"not a title\"\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.reference_definitions().len(), 1);
assert_eq!(ctx.reference_definitions()[0].title, None);
let quote = content.find('"').unwrap();
assert!(
!ctx.is_in_reference_def(quote),
"a blank-separated quoted line is not part of the ref def"
);
}
#[test]
fn test_ref_def_non_title_next_line_not_consumed() {
// The line after the destination is only a title if it is *only* a quoted
// or parenthesised title; ordinary prose is a separate paragraph.
let content = "[ref]: https://example.com\nordinary paragraph text\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.reference_definitions().len(), 1);
assert_eq!(ctx.reference_definitions()[0].title, None);
let para = content.find("ordinary").unwrap();
assert!(
!ctx.is_in_reference_def(para),
"a following paragraph is not part of the ref def"
);
}
#[test]
fn test_ref_def_inline_title_unaffected_by_lookahead() {
// A definition that already has its title on the destination line is
// unchanged; the next line is not pulled in.
let content = "[ref]: https://example.com \"inline\"\n\"separate\"\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.reference_definitions().len(), 1);
assert_eq!(ctx.reference_definitions()[0].title.as_deref(), Some("inline"));
let sep = content.find("\"separate\"").unwrap();
assert!(
!ctx.is_in_reference_def(sep),
"the inline-title def must not absorb the next line"
);
}
#[test]
fn test_ref_def_multiline_title_in_blockquote() {
// A reference definition inside a blockquote may also carry its title on
// the following (still blockquoted) line.
let content = "> [ref]: https://example.com\n> \"the title\"\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.reference_definitions().len(), 1);
assert_eq!(ctx.reference_definitions()[0].title.as_deref(), Some("the title"));
}
#[test]
fn test_ref_def_paren_title_with_escaped_parens() {
// CommonMark §4.7 paren-form titles may contain `(`/`)` only when
// backslash-escaped. Both pulldown-cmark and the rumdl ref-def regex
// unescape the captured title (per CommonMark §6.1) so downstream rules
// see the same value regardless of which path produced it.
let content = "[docs]: https://example.com (title \\(x\\))\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.reference_definitions().len(), 1);
assert_eq!(ctx.reference_definitions()[0].url, "https://example.com");
assert_eq!(
ctx.reference_definitions()[0].title.as_deref(),
Some("title (x)"),
"title must be unescaped to match pulldown-cmark's parsed value"
);
}
#[test]
fn test_mkdocs_admonition_link_with_paren_title() {
// pulldown-cmark treats indented MkDocs admonition content as a code block,
// so the inline link is recovered by the regex fallback in
// `parse_links_images_pulldown`. The fallback must recognize all three
// CommonMark §6.7 title delimiter forms — including `(...)` — otherwise
// a link like `[doc](url (title))` is parsed with title=None and MD054
// auto-fix silently strips the title when rewriting the link.
let content = "!!! note\n See [doc](https://example.com (paren title)) here.\n";
let ctx = LintContext::new(content, MarkdownFlavor::MkDocs, None);
let link = ctx
.links()
.iter()
.find(|l| l.url == "https://example.com")
.expect("MkDocs fallback must surface the link");
assert_eq!(
link.title.as_deref(),
Some("paren title"),
"paren-form title must be captured by the MkDocs link fallback"
);
}
#[test]
fn test_mkdocs_admonition_image_with_paren_title() {
// Mirror of the link test for images.
let content = "!!! note\n See ) here.\n";
let ctx = LintContext::new(content, MarkdownFlavor::MkDocs, None);
let img = ctx
.images()
.iter()
.find(|i| i.url == "https://example.com/x.png")
.expect("MkDocs fallback must surface the image");
assert_eq!(
img.title.as_deref(),
Some("paren title"),
"paren-form title must be captured by the MkDocs image fallback"
);
}
#[test]
fn parsed_link_queries_preserve_document_order_and_half_open_ranges() {
let content = "[one](a)\n and [two](c)\n\n[Docs]: ./guide.md\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(
ctx.links_on_line(1)
.iter()
.map(|link| link.text.as_ref())
.collect::>(),
["one"]
);
assert_eq!(
ctx.links_on_line(2)
.iter()
.map(|link| link.text.as_ref())
.collect::>(),
["two"]
);
assert!(ctx.links_on_line(3).is_empty());
assert_eq!(
ctx.images_on_line(2)
.iter()
.map(|image| image.alt_text.as_ref())
.collect::>(),
["pic"]
);
let first_link_start = content.find("[one]").unwrap();
let second_link_start = content.find("[two]").unwrap();
let image_start = content.find("![pic]").unwrap();
let first_link = ctx.link_starting_at(first_link_start).unwrap();
assert!(std::ptr::eq(ctx.link_containing(first_link_start).unwrap(), first_link));
assert!(std::ptr::eq(
ctx.link_containing(first_link.byte_end - 1).unwrap(),
first_link
));
assert!(ctx.link_containing(first_link.byte_end).is_none());
assert_eq!(ctx.links_starting_before_or_at(second_link_start).len(), 2);
let image = ctx.image_starting_at(image_start).unwrap();
assert!(std::ptr::eq(ctx.image_containing(image_start).unwrap(), image));
assert!(std::ptr::eq(ctx.image_containing(image.byte_end - 1).unwrap(), image));
assert!(ctx.image_containing(image.byte_end).is_none());
let definition = ctx.reference_definition("DOCS").unwrap();
assert_eq!(definition.url, "./guide.md");
assert_eq!(ctx.get_reference_url("docs"), Some("./guide.md"));
}
#[test]
fn parsed_links_remain_ordered_after_regex_fallbacks() {
// Undefined reference links are appended after the pulldown-cmark pass.
// Queries rely on the final collections being restored to document order.
let content = "\
# Document
See [undefined-ref] for details.
Some text with [another-undef-ref] here.
Short text [link](https://example.com/a-very-long-destination).
";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert!(
ctx.links()
.windows(2)
.all(|pair| { (pair[0].line, pair[0].byte_offset) <= (pair[1].line, pair[1].byte_offset) })
);
assert!(
ctx.images()
.windows(2)
.all(|pair| { (pair[0].line, pair[0].byte_offset) <= (pair[1].line, pair[1].byte_offset) })
);
}
#[test]
fn test_wiki_embed_has_no_alt_text() {
// A wiki embed has no alt-text slot: `![[note]]` transcludes the target and
// the pipe in `![[image.png|300]]` sets the rendered dimensions. Reading the
// pipe portion as alt text made MD045 accept `![[a.png|300]]` and report
// `![[a.png]]`, neither of which the author can act on.
for (content, url) in [
("![[image.png]]\n", "image.png"),
("![[image.png|300]]\n", "image.png"),
("![[image.png|Some description]]\n", "image.png"),
("![[subfolder/image.png|640x480]]\n", "subfolder/image.png"),
] {
for flavor in [MarkdownFlavor::Obsidian, MarkdownFlavor::Standard] {
let ctx = LintContext::new(content, flavor, None);
assert_eq!(ctx.images().len(), 1, "{flavor:?}: {content:?}");
assert_eq!(ctx.images()[0].url, url, "{flavor:?}: {content:?}");
assert_eq!(
ctx.images()[0].alt_text,
"",
"{flavor:?}: {content:?} has no alt-text slot"
);
}
}
}
/// The text of the first link in `content`, parsed with the standard flavor.
fn first_link_text(content: &str) -> String {
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
ctx.links()
.first()
.unwrap_or_else(|| panic!("the link must be parsed: {content:?}"))
.text
.to_string()
}
#[test]
fn test_label_holding_an_unmatched_backtick_reads_to_its_bracket() {
// One backtick opens nothing, because no second run closes it. A scan that
// toggles a code-span flag per backtick reads the rest of the link as code
// and finds no bracket at all.
let content = "[literal `](missing.md \"See ](./existing.md)\")\n";
assert_eq!(first_link_text(content), "literal `");
}
#[test]
fn test_label_holding_inline_html_reads_to_its_bracket() {
// The bracket inside the attribute is HTML, not the opener of a nested
// label, so it must not raise the depth a bracket scan counts.
let content = "[text](missing.md)\n";
assert_eq!(first_link_text(content), "text");
}
#[test]
fn test_label_holding_a_two_backtick_code_span_reads_to_its_bracket() {
// The code span holds a bracket and a paren of its own, and it wraps onto
// another line. Neither ends the label.
let content = "[``a\n](./exists.md)``](exists.md)\n";
assert_eq!(first_link_text(content), "``a\n](./exists.md)``");
}
#[test]
fn test_label_holding_an_escaped_bracket_reads_to_its_bracket() {
// An escaped bracket is text. The label ends at the next unescaped one, and
// the text keeps the backslash as written.
let content = "[a\\]b](url)\n";
assert_eq!(first_link_text(content), "a\\]b");
}
#[test]
fn test_label_holding_a_nested_image_reads_to_its_bracket() {
let content = "[](y.md)\n";
assert_eq!(first_link_text(content), "");
}
#[test]
fn test_label_holding_a_collapsed_reference_image_reads_to_its_bracket() {
// The parse's range for a collapsed image stops at the bracket closing its
// description and leaves out the `[]` after it, so the label's own bracket
// lies past where the image's events reach.
let content = "[![alt][]](target1.md)\n\n[alt]: exists.png\n";
assert_eq!(first_link_text(content), "![alt][]");
}
#[test]
fn test_label_holding_a_collapsed_reference_image_between_text_reads_to_its_bracket() {
// A control for test_label_holding_a_collapsed_reference_image_reads_to_its_bracket:
// the text after the image reaches past the image's own range, so the
// label's end is found from that text and holds whatever the collapsed
// image's range covers. A failure here is in the bracket scan rather than
// in how far a collapsed image reaches.
let content = "[before ![alt][] after](target4.md)\n\n[alt]: exists.png\n";
assert_eq!(first_link_text(content), "before ![alt][] after");
}
#[test]
fn test_label_ending_in_whitespace_after_a_collapsed_reference_image_reads_to_its_bracket() {
// A control for test_label_holding_a_collapsed_reference_image_reads_to_its_bracket:
// the space after the image is text that reaches past the image's own
// range, so the label's end is found from it and holds whatever the
// collapsed image's range covers. A failure here is in the bracket scan
// rather than in how far a collapsed image reaches.
let content = "[![alt][] ](t.md)\n\n[alt]: exists.png\n";
assert_eq!(first_link_text(content), "![alt][] ");
}
#[test]
fn test_reference_label_holding_a_collapsed_reference_image_reads_to_its_bracket() {
let content = "[![alt][]][ref]\n\n[alt]: exists.png\n[ref]: target5.md\n";
assert_eq!(first_link_text(content), "![alt][]");
}
#[test]
fn test_an_empty_label_reads_as_empty() {
let content = "[](url)\n";
assert_eq!(first_link_text(content), "");
}
#[test]
fn test_a_label_wrapping_onto_an_indented_line_keeps_the_break_and_the_indent() {
// The text is the source between the brackets, so the newline and the
// continuation line's indent are part of it.
let content = "- [wrapped\n text](./gone.md)\n";
assert_eq!(first_link_text(content), "wrapped\n text");
}
#[test]
fn test_a_label_ending_in_whitespace_reads_to_its_bracket() {
// The inline events stop at the last content, so the trailing spaces on the
// continuation line lie between the last event and the bracket.
let content = "[ \n ](url)\n";
assert_eq!(first_link_text(content), " \n ");
}
#[test]
fn test_a_label_wrapping_inside_a_blockquote_keeps_the_marker() {
// The continuation line's blockquote marker sits in the label's source span
// and no event covers it.
let content = "> [a\n> ](url)\n";
assert_eq!(first_link_text(content), "a\n> ");
}
#[test]
fn test_wikilink_text_survives_a_nested_image() {
// A wikilink is the only link whose text is the concatenation of its parse
// events rather than the source between its brackets, so the nested image's
// events must leave the text accumulated so far in place.
let content = "[[Target|Display  more]]\n";
let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
let link = ctx
.links()
.iter()
.find(|l| l.url == "Target")
.expect("the wikilink must be parsed");
assert_eq!(link.text, "Display alt more");
}
#[test]
fn test_ref_def_angle_bracket_destination_with_escaped_brackets() {
// CommonMark §6.6 angle-bracket destinations admit `\<` and `\>` so the
// round-trip from `format_url_destination` (which emits `c>` when
// a URL contains `<` or `>`) is recovered on the next parse instead of
// silently dropping the def out of `ctx.reference_definitions()`.
let content = "[id]: c>\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(
ctx.reference_definitions().len(),
1,
"escaped angle-bracket destination must round-trip through the regex"
);
assert_eq!(ctx.reference_definitions()[0].id, "id");
assert_eq!(ctx.reference_definitions()[0].title, None);
}
#[test]
fn test_ref_def_label_with_escaped_closing_bracket() {
// CommonMark ends a link label at the first right bracket that is *not*
// backslash-escaped, so `ref1\[\]` and `ref2\]` are single labels. A label
// scan that stops at any `]` drops these definitions entirely, which makes
// MD034 lint the destination as prose and hides them from MD053.
//
// The stored id keeps its backslashes: label normalization case-folds and
// collapses whitespace but does not unescape, and pulldown-cmark reports
// the raw id, so both sides must agree for reference matching to work.
let content = "[ref1\\[\\]]: https://example.com/ref1\n[ref2\\]]: https://example.com/ref2\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let ids: Vec<&str> = ctx.reference_definitions().iter().map(|d| d.id.as_str()).collect();
assert_eq!(
ids,
vec!["ref1\\[\\]", "ref2\\]"],
"an escaped `]` must not terminate the label scan"
);
assert_eq!(ctx.reference_definitions()[0].url, "https://example.com/ref1");
assert_eq!(ctx.reference_definitions()[1].url, "https://example.com/ref2");
}
#[test]
fn test_ref_def_double_quoted_title_with_escaped_quote() {
// Title delimiter `"` may appear inside the title only when backslash-escaped;
// `format_title` produces this form whenever the unescaped title contains `"`,
// so the regex must accept it or the freshly generated def disappears from
// the next pass and MD053/MD057/dedup all break. The captured title is
// unescaped (CommonMark §6.1) so it matches pulldown-cmark's parsed value.
let content = "[id]: https://example.com \"he said \\\"hi\\\"\"\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.reference_definitions().len(), 1);
assert_eq!(ctx.reference_definitions()[0].url, "https://example.com");
assert_eq!(
ctx.reference_definitions()[0].title.as_deref(),
Some("he said \"hi\""),
"title must be unescaped to match pulldown-cmark's parsed value"
);
}
#[test]
fn test_ref_def_single_quoted_title_with_escaped_quote() {
let content = "[id]: https://example.com 'it\\'s fine'\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.reference_definitions().len(), 1);
assert_eq!(ctx.reference_definitions()[0].url, "https://example.com");
assert_eq!(
ctx.reference_definitions()[0].title.as_deref(),
Some("it's fine"),
"title must be unescaped to match pulldown-cmark's parsed value"
);
}
#[test]
fn test_ref_def_url_unescapes_backslash_escapes() {
// CommonMark §6.1: a backslash before any ASCII punctuation character
// produces the literal character; the backslash itself is removed. The
// rumdl regex fallback must apply this transform so `ctx.reference_definitions()[i].url`
// matches what pulldown-cmark exposes via `Tag::Link`/`Tag::Image`. Without
// this, MD053/MD054/MD057 would see `https://e.com/path\(1\)` while the
// parser sees `https://e.com/path(1)`, and any rule that copies the value
// back into the document would corrupt it.
let content = "[id]: https://e.com/path\\(1\\)\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.reference_definitions().len(), 1);
assert_eq!(
ctx.reference_definitions()[0].url,
"https://e.com/path(1)",
"URL must be unescaped per CommonMark §6.1"
);
}
#[test]
fn test_ref_def_unescape_preserves_non_punctuation_backslash() {
// CommonMark §6.1 explicitly limits the escape to ASCII punctuation. A
// backslash followed by a letter, digit, or whitespace is preserved
// verbatim (the backslash stays in the output). Verifying this guards
// against an over-eager unescape that would silently drop backslashes
// from URL paths and titles.
let content = "[id]: https://e.com/p\\ath \"a\\b c\"\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.reference_definitions().len(), 1);
assert_eq!(
ctx.reference_definitions()[0].url,
"https://e.com/p\\ath",
"backslash before non-punctuation must remain in URL"
);
assert_eq!(
ctx.reference_definitions()[0].title.as_deref(),
Some("a\\b c"),
"backslash before non-punctuation must remain in title"
);
}
#[test]
fn test_footnote_definitions_not_parsed_as_reference_defs() {
// Footnote definitions use [^id]: syntax and should NOT be parsed as reference definitions
let content = r#"# Title
A footnote[^1].
[^1]: This is the footnote content.
[^note]: Another footnote with [link](https://example.com).
[regular]: ./path.md "A real reference definition"
"#;
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Should only have one reference definition (the regular one)
assert_eq!(
ctx.reference_definitions().len(),
1,
"Footnotes should not be parsed as reference definitions"
);
// The only reference def should be the regular one
assert_eq!(ctx.reference_definitions()[0].id, "regular");
assert_eq!(ctx.reference_definitions()[0].url, "./path.md");
assert_eq!(
ctx.reference_definitions()[0].title,
Some("A real reference definition".to_string())
);
}
#[test]
fn test_footnote_with_inline_link_not_misidentified() {
// Regression test for issue #286: footnote containing an inline link
// was incorrectly parsed as a reference definition with URL "[link](url)"
let content = r#"# Title
A footnote[^1].
[^1]: [link](https://www.google.com).
"#;
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Should have no reference definitions
assert!(
ctx.reference_definitions().is_empty(),
"Footnote with inline link should not create a reference definition"
);
}
#[test]
fn test_various_footnote_formats_excluded() {
// Test various footnote ID formats are all excluded
let content = r#"[^1]: Numeric footnote
[^note]: Named footnote
[^a]: Single char footnote
[^long-footnote-name]: Long named footnote
[^123abc]: Mixed alphanumeric
[ref1]: ./file1.md
[ref2]: ./file2.md
"#;
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Should only have the two regular reference definitions
assert_eq!(
ctx.reference_definitions().len(),
2,
"Only regular reference definitions should be parsed"
);
let ids: Vec<&str> = ctx.reference_definitions().iter().map(|r| r.id.as_str()).collect();
assert!(ids.contains(&"ref1"));
assert!(ids.contains(&"ref2"));
assert!(!ids.iter().any(|id| id.starts_with('^')));
}
// ==================================================================
// Tests for has_char and char_count methods
// =========================================================================
#[test]
fn test_has_char_tracked_characters() {
// Test all 12 tracked characters
let content =
"# Heading\n* list item\n_emphasis_ and -hyphen-\n+ plus\n> quote\n| table |\n[link]\n`code`\n\n!image";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// All tracked characters should be detected
assert!(ctx.has_char('#'), "Should detect hash");
assert!(ctx.has_char('*'), "Should detect asterisk");
assert!(ctx.has_char('_'), "Should detect underscore");
assert!(ctx.has_char('-'), "Should detect hyphen");
assert!(ctx.has_char('+'), "Should detect plus");
assert!(ctx.has_char('>'), "Should detect gt");
assert!(ctx.has_char('|'), "Should detect pipe");
assert!(ctx.has_char('['), "Should detect bracket");
assert!(ctx.has_char('`'), "Should detect backtick");
assert!(ctx.has_char('<'), "Should detect lt");
assert!(ctx.has_char('!'), "Should detect exclamation");
assert!(ctx.has_char('\n'), "Should detect newline");
}
#[test]
fn test_has_char_absent_characters() {
let content = "Simple text without special chars";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// None of the tracked characters should be present
assert!(!ctx.has_char('#'), "Should not detect hash");
assert!(!ctx.has_char('*'), "Should not detect asterisk");
assert!(!ctx.has_char('_'), "Should not detect underscore");
assert!(!ctx.has_char('-'), "Should not detect hyphen");
assert!(!ctx.has_char('+'), "Should not detect plus");
assert!(!ctx.has_char('>'), "Should not detect gt");
assert!(!ctx.has_char('|'), "Should not detect pipe");
assert!(!ctx.has_char('['), "Should not detect bracket");
assert!(!ctx.has_char('`'), "Should not detect backtick");
assert!(!ctx.has_char('<'), "Should not detect lt");
assert!(!ctx.has_char('!'), "Should not detect exclamation");
// Note: single line content has no newlines
assert!(!ctx.has_char('\n'), "Should not detect newline in single line");
}
#[test]
fn test_has_char_fallback_for_untracked() {
let content = "Text with @mention and $dollar and %percent";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Untracked characters should fall back to content.contains()
assert!(ctx.has_char('@'), "Should detect @ via fallback");
assert!(ctx.has_char('$'), "Should detect $ via fallback");
assert!(ctx.has_char('%'), "Should detect % via fallback");
assert!(!ctx.has_char('^'), "Should not detect absent ^ via fallback");
}
#[test]
fn test_char_count_tracked_characters() {
let content =
"## Heading ##\n***bold***\n__emphasis__\n---\n+++\n>> nested\n|| table ||\n[[link]]\n``code``\n<>\n!!";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Count each tracked character
assert_eq!(ctx.char_count('#'), 4, "Should count 4 hashes");
assert_eq!(ctx.char_count('*'), 6, "Should count 6 asterisks");
assert_eq!(ctx.char_count('_'), 4, "Should count 4 underscores");
assert_eq!(ctx.char_count('-'), 3, "Should count 3 hyphens");
assert_eq!(ctx.char_count('+'), 3, "Should count 3 pluses");
assert_eq!(ctx.char_count('>'), 4, "Should count 4 gt (2 nested + 2 in <>)");
assert_eq!(ctx.char_count('|'), 4, "Should count 4 pipes");
assert_eq!(ctx.char_count('['), 2, "Should count 2 brackets");
assert_eq!(ctx.char_count('`'), 4, "Should count 4 backticks");
assert_eq!(ctx.char_count('<'), 2, "Should count 2 lt");
assert_eq!(ctx.char_count('!'), 2, "Should count 2 exclamations");
assert_eq!(ctx.char_count('\n'), 10, "Should count 10 newlines");
}
#[test]
fn test_char_count_zero_for_absent() {
let content = "Plain text";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.char_count('#'), 0);
assert_eq!(ctx.char_count('*'), 0);
assert_eq!(ctx.char_count('_'), 0);
assert_eq!(ctx.char_count('\n'), 0);
}
#[test]
fn test_char_count_fallback_for_untracked() {
let content = "@@@ $$ %%%";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.char_count('@'), 3, "Should count 3 @ via fallback");
assert_eq!(ctx.char_count('$'), 2, "Should count 2 $ via fallback");
assert_eq!(ctx.char_count('%'), 3, "Should count 3 % via fallback");
assert_eq!(ctx.char_count('^'), 0, "Should count 0 for absent char");
}
#[test]
fn test_char_count_empty_content() {
let ctx = LintContext::new("", MarkdownFlavor::Standard, None);
assert_eq!(ctx.char_count('#'), 0);
assert_eq!(ctx.char_count('*'), 0);
assert_eq!(ctx.char_count('@'), 0);
assert!(!ctx.has_char('#'));
assert!(!ctx.has_char('@'));
}
// =========================================================================
// Tests for is_in_html_tag method
// =========================================================================
#[test]
fn test_is_in_html_tag_simple() {
let content = "content";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Inside opening tag
assert!(ctx.is_in_html_tag(0), "Position 0 (<) should be in tag");
assert!(ctx.is_in_html_tag(1), "Position 1 (d) should be in tag");
assert!(ctx.is_in_html_tag(4), "Position 4 (>) should be in tag");
// Outside tag (in content)
assert!(!ctx.is_in_html_tag(5), "Position 5 (c) should not be in tag");
assert!(!ctx.is_in_html_tag(10), "Position 10 (t) should not be in tag");
// Inside closing tag
assert!(ctx.is_in_html_tag(12), "Position 12 (<) should be in tag");
assert!(ctx.is_in_html_tag(17), "Position 17 (>) should be in tag");
}
#[test]
fn test_is_in_html_tag_self_closing() {
let content = "Text
more text";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Before tag
assert!(!ctx.is_in_html_tag(0), "Position 0 should not be in tag");
assert!(!ctx.is_in_html_tag(4), "Position 4 (space) should not be in tag");
// Inside self-closing tag
assert!(ctx.is_in_html_tag(5), "Position 5 (<) should be in tag");
assert!(ctx.is_in_html_tag(8), "Position 8 (/) should be in tag");
assert!(ctx.is_in_html_tag(9), "Position 9 (>) should be in tag");
// After tag
assert!(!ctx.is_in_html_tag(10), "Position 10 (space) should not be in tag");
}
#[test]
fn test_is_in_html_tag_with_attributes() {
let content = r#"text"#;
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// All positions inside opening tag with attributes
assert!(ctx.is_in_html_tag(0), "Start of tag");
assert!(ctx.is_in_html_tag(10), "Inside href attribute");
assert!(ctx.is_in_html_tag(20), "Inside class attribute");
assert!(ctx.is_in_html_tag(26), "End of opening tag");
// Content between tags
assert!(!ctx.is_in_html_tag(27), "Start of content");
assert!(!ctx.is_in_html_tag(30), "End of content");
// Closing tag
assert!(ctx.is_in_html_tag(31), "Start of closing tag");
}
#[test]
fn test_is_in_html_tag_multiline() {
let content = "\ncontent\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Opening tag spans multiple lines
assert!(ctx.is_in_html_tag(0), "Start of multiline tag");
assert!(ctx.is_in_html_tag(5), "After first newline in tag");
assert!(ctx.is_in_html_tag(15), "Inside attribute");
// After closing > of opening tag
let closing_bracket_pos = content.find(">\n").unwrap();
assert!(!ctx.is_in_html_tag(closing_bracket_pos + 2), "Content after tag");
}
#[test]
fn test_is_in_html_tag_with_url_attributes() {
// Tags with URLs in attributes contain '/' which must not be treated as self-closing
let content = r#""#;
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let tags = ctx.html_tags();
assert_eq!(tags.len(), 1, "Should detect one HTML tag");
assert_eq!(tags[0].tag_name, "input");
assert!(!tags[0].is_self_closing);
assert!(ctx.is_in_html_tag(35), "URL position should be inside HTML tag");
}
#[test]
fn test_is_in_html_tag_self_closing_with_slash() {
let content = "
";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let tags = ctx.html_tags();
assert_eq!(tags.len(), 1);
assert_eq!(tags[0].tag_name, "br");
assert!(tags[0].is_self_closing);
}
#[test]
fn test_html_tags_respect_backslash_escaped_openers() {
// CommonMark §6.1: an odd number of backslashes escapes the `<`, while an
// even number escapes in pairs and leaves it active as markup.
for content in [r"\", r"\\\"] {
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert!(ctx.html_tags().is_empty(), "escaped opener in {content:?}");
}
for content in [r"", r"\\", r"\\\\"] {
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let tags = ctx.html_tags();
assert_eq!(tags.len(), 1, "active opener in {content:?}");
assert_eq!(tags[0].tag_name, "x-keyboard");
}
}
#[test]
fn test_is_in_html_tag_nested_angle_brackets() {
// Hugo shortcodes: contain nested '<'
let content = r#"}}">"#;
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let tags = ctx.html_tags();
// The regex handles nested '<' by matching the shortest valid tag
assert!(!tags.is_empty(), "Should detect at least one tag fragment");
}
#[test]
fn test_html_tag_window_ends_inside_multibyte_char() {
// Issue #757: an unterminated HTML-like tag (``) whose
// 4096-byte search window ends inside a multi-byte UTF-8 character must not panic.
// `' beyond the window boundary, still landing inside a multi-byte char.
let content2 = format!("", "b".repeat(5000));
let ctx2 = LintContext::new(&content2, MarkdownFlavor::Standard, None);
let _ = ctx2.html_tags(); // must not panic
}
#[test]
fn test_is_in_html_tag_no_tags() {
let content = "Plain text without any HTML";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// No position should be in an HTML tag
for i in 0..content.len() {
assert!(!ctx.is_in_html_tag(i), "Position {i} should not be in tag");
}
}
// =========================================================================
// Tests for is_in_jinja_range method
// =========================================================================
#[test]
fn test_is_in_jinja_range_expression() {
let content = "Hello {{ name }}!";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Before Jinja
assert!(!ctx.is_in_jinja_range(0), "H should not be in Jinja");
assert!(!ctx.is_in_jinja_range(5), "Space before Jinja should not be in Jinja");
// Inside Jinja expression (positions 6-15 for "{{ name }}")
assert!(ctx.is_in_jinja_range(6), "First brace should be in Jinja");
assert!(ctx.is_in_jinja_range(7), "Second brace should be in Jinja");
assert!(ctx.is_in_jinja_range(10), "name should be in Jinja");
assert!(ctx.is_in_jinja_range(14), "Closing brace should be in Jinja");
assert!(ctx.is_in_jinja_range(15), "Second closing brace should be in Jinja");
// After Jinja
assert!(!ctx.is_in_jinja_range(16), "! should not be in Jinja");
}
#[test]
fn test_is_in_jinja_range_statement() {
let content = "{% if condition %}content{% endif %}";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Inside opening statement
assert!(ctx.is_in_jinja_range(0), "Start of Jinja statement");
assert!(ctx.is_in_jinja_range(5), "condition should be in Jinja");
assert!(ctx.is_in_jinja_range(17), "End of opening statement");
// Content between
assert!(!ctx.is_in_jinja_range(18), "content should not be in Jinja");
// Inside closing statement
assert!(ctx.is_in_jinja_range(25), "Start of endif");
assert!(ctx.is_in_jinja_range(32), "endif should be in Jinja");
}
#[test]
fn test_is_in_jinja_range_multiple() {
let content = "{{ a }} and {{ b }}";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// First Jinja expression
assert!(ctx.is_in_jinja_range(0));
assert!(ctx.is_in_jinja_range(3));
assert!(ctx.is_in_jinja_range(6));
// Between expressions
assert!(!ctx.is_in_jinja_range(8));
assert!(!ctx.is_in_jinja_range(11));
// Second Jinja expression
assert!(ctx.is_in_jinja_range(12));
assert!(ctx.is_in_jinja_range(15));
assert!(ctx.is_in_jinja_range(18));
}
#[test]
fn test_is_in_jinja_range_no_jinja() {
let content = "Plain text with single braces but not Jinja";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// No position should be in Jinja
for i in 0..content.len() {
assert!(!ctx.is_in_jinja_range(i), "Position {i} should not be in Jinja");
}
}
// =========================================================================
// Tests for is_in_link_title method
// =========================================================================
#[test]
fn test_is_in_link_title_with_title() {
let content = r#"[ref]: https://example.com "Title text"
Some content."#;
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Verify we have a reference def with title
assert_eq!(ctx.reference_definitions().len(), 1);
let def = &ctx.reference_definitions()[0];
assert!(def.title_byte_start.is_some());
assert!(def.title_byte_end.is_some());
let title_start = def.title_byte_start.unwrap();
let title_end = def.title_byte_end.unwrap();
// Before title (in URL)
assert!(!ctx.is_in_link_title(10), "URL should not be in title");
// Inside title
assert!(ctx.is_in_link_title(title_start), "Title start should be in title");
assert!(
ctx.is_in_link_title(title_start + 5),
"Middle of title should be in title"
);
assert!(ctx.is_in_link_title(title_end - 1), "End of title should be in title");
// After title
assert!(
!ctx.is_in_link_title(title_end),
"After title end should not be in title"
);
}
#[test]
fn test_is_in_link_title_without_title() {
let content = "[ref]: https://example.com\n\nSome content.";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Reference def without title
assert_eq!(ctx.reference_definitions().len(), 1);
let def = &ctx.reference_definitions()[0];
assert!(def.title_byte_start.is_none());
assert!(def.title_byte_end.is_none());
// No position should be in a title
for i in 0..content.len() {
assert!(!ctx.is_in_link_title(i), "Position {i} should not be in title");
}
}
#[test]
fn test_is_in_link_title_multiple_refs() {
let content = r#"[ref1]: /url1 "Title One"
[ref2]: /url2
[ref3]: /url3 "Title Three"
"#;
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Should have 3 reference defs
assert_eq!(ctx.reference_definitions().len(), 3);
// ref1 has title
let ref1 = ctx.reference_definitions().iter().find(|r| r.id == "ref1").unwrap();
assert!(ref1.title_byte_start.is_some());
// ref2 has no title
let ref2 = ctx.reference_definitions().iter().find(|r| r.id == "ref2").unwrap();
assert!(ref2.title_byte_start.is_none());
// ref3 has title
let ref3 = ctx.reference_definitions().iter().find(|r| r.id == "ref3").unwrap();
assert!(ref3.title_byte_start.is_some());
// Check positions in ref1's title
if let (Some(start), Some(end)) = (ref1.title_byte_start, ref1.title_byte_end) {
assert!(ctx.is_in_link_title(start + 1));
assert!(!ctx.is_in_link_title(end + 5));
}
// Check positions in ref3's title
if let (Some(start), Some(_end)) = (ref3.title_byte_start, ref3.title_byte_end) {
assert!(ctx.is_in_link_title(start + 1));
}
}
#[test]
fn test_is_in_link_title_single_quotes() {
let content = "[ref]: /url 'Single quoted title'\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.reference_definitions().len(), 1);
let def = &ctx.reference_definitions()[0];
if let (Some(start), Some(end)) = (def.title_byte_start, def.title_byte_end) {
assert!(ctx.is_in_link_title(start));
assert!(ctx.is_in_link_title(start + 5));
assert!(!ctx.is_in_link_title(end));
}
}
#[test]
fn test_is_in_link_title_parentheses() {
// Note: The reference def parser may not support parenthesized titles
// This test verifies the is_in_link_title method works when titles exist
let content = "[ref]: /url (Parenthesized title)\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Parser behavior: may or may not parse parenthesized titles
// We test that is_in_link_title correctly reflects whatever was parsed
if ctx.reference_definitions().is_empty() {
// Parser didn't recognize this as a reference def
for i in 0..content.len() {
assert!(!ctx.is_in_link_title(i));
}
} else {
let def = &ctx.reference_definitions()[0];
if let (Some(start), Some(end)) = (def.title_byte_start, def.title_byte_end) {
assert!(ctx.is_in_link_title(start));
assert!(ctx.is_in_link_title(start + 5));
assert!(!ctx.is_in_link_title(end));
} else {
// Title wasn't parsed, so no position should be in title
for i in 0..content.len() {
assert!(!ctx.is_in_link_title(i));
}
}
}
}
#[test]
fn test_is_in_link_title_no_refs() {
let content = "Just plain text without any reference definitions.";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert!(ctx.reference_definitions().is_empty());
for i in 0..content.len() {
assert!(!ctx.is_in_link_title(i));
}
}
// =========================================================================
// Math span tests (Issue #289)
// =========================================================================
#[test]
fn test_math_spans_inline() {
let content = "Text with inline math $[f](x)$ in it.";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let math_spans = ctx.math_spans();
assert_eq!(math_spans.len(), 1, "Should detect one inline math span");
let span = &math_spans[0];
assert!(!span.is_display, "Should be inline math, not display");
assert_eq!(span.content, "[f](x)", "Content should be extracted correctly");
}
#[test]
fn test_math_spans_display_single_line() {
let content = "$$X(\\zeta) = \\mathcal Z [x](\\zeta)$$";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let math_spans = ctx.math_spans();
assert_eq!(math_spans.len(), 1, "Should detect one display math span");
let span = &math_spans[0];
assert!(span.is_display, "Should be display math");
assert!(
span.content.contains("[x](\\zeta)"),
"Content should contain the link-like pattern"
);
}
#[test]
fn test_math_spans_display_multiline() {
let content = "Before\n\n$$\n[x](\\zeta) = \\sum_k x(k)\n$$\n\nAfter";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let math_spans = ctx.math_spans();
assert_eq!(math_spans.len(), 1, "Should detect one display math span");
let span = &math_spans[0];
assert!(span.is_display, "Should be display math");
}
#[test]
fn test_is_in_math_span() {
let content = "Text $[f](x)$ more text";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Position inside the math span
let math_start = content.find('$').unwrap();
let math_end = content.rfind('$').unwrap() + 1;
assert!(
ctx.is_in_math_span(math_start + 1),
"Position inside math span should return true"
);
assert!(
ctx.is_in_math_span(math_start + 3),
"Position inside math span should return true"
);
// Position outside the math span
assert!(!ctx.is_in_math_span(0), "Position before math span should return false");
assert!(
!ctx.is_in_math_span(math_end + 1),
"Position after math span should return false"
);
}
#[test]
fn test_math_spans_mixed_with_code() {
let content = "Math $[f](x)$ and code `[g](y)` mixed";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let math_spans = ctx.math_spans();
let code_spans = ctx.code_spans();
assert_eq!(math_spans.len(), 1, "Should have one math span");
assert_eq!(code_spans.len(), 1, "Should have one code span");
// Verify math span content
assert_eq!(math_spans[0].content, "[f](x)");
// Verify code span content
assert_eq!(code_spans[0].content, "[g](y)");
}
#[test]
fn test_math_spans_no_math() {
let content = "Regular text without any math at all.";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let math_spans = ctx.math_spans();
assert!(math_spans.is_empty(), "Should have no math spans");
}
#[test]
fn test_math_spans_multiple() {
let content = "First $a$ and second $b$ and display $$c$$";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let math_spans = ctx.math_spans();
assert_eq!(math_spans.len(), 3, "Should detect three math spans");
// Two inline, one display
let inline_count = math_spans.iter().filter(|s| !s.is_display).count();
let display_count = math_spans.iter().filter(|s| s.is_display).count();
assert_eq!(inline_count, 2, "Should have two inline math spans");
assert_eq!(display_count, 1, "Should have one display math span");
}
#[test]
fn test_is_in_math_span_boundary_positions() {
// Test exact boundary positions: $[f](x)$
// Byte positions: 0123456789
let content = "$[f](x)$";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let math_spans = ctx.math_spans();
assert_eq!(math_spans.len(), 1, "Should have one math span");
let span = &math_spans[0];
// Position at opening $ should be in span (byte 0)
assert!(
ctx.is_in_math_span(span.byte_offset),
"Start position should be in span"
);
// Position just inside should be in span
assert!(
ctx.is_in_math_span(span.byte_offset + 1),
"Position after start should be in span"
);
// Position at closing $ should be in span (exclusive end means we check byte_end - 1)
assert!(
ctx.is_in_math_span(span.byte_end - 1),
"Position at end-1 should be in span"
);
// Position at byte_end should NOT be in span (exclusive end)
assert!(
!ctx.is_in_math_span(span.byte_end),
"Position at byte_end should NOT be in span (exclusive)"
);
}
#[test]
fn test_math_spans_at_document_start() {
let content = "$x$ text";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let math_spans = ctx.math_spans();
assert_eq!(math_spans.len(), 1);
assert_eq!(math_spans[0].byte_offset, 0, "Math should start at byte 0");
}
#[test]
fn test_math_spans_at_document_end() {
let content = "text $x$";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let math_spans = ctx.math_spans();
assert_eq!(math_spans.len(), 1);
assert_eq!(math_spans[0].byte_end, content.len(), "Math should end at document end");
}
#[test]
fn test_math_spans_consecutive() {
let content = "$a$$b$";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let math_spans = ctx.math_spans();
// pulldown-cmark should parse these as separate spans
assert!(!math_spans.is_empty(), "Should detect at least one math span");
// All positions should be in some math span
for i in 0..content.len() {
assert!(ctx.is_in_math_span(i), "Position {i} should be in a math span");
}
}
#[test]
fn test_math_spans_currency_not_math() {
// Unbalanced $ should not create math spans
let content = "Price is $100";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let math_spans = ctx.math_spans();
// pulldown-cmark requires balanced delimiters for math
// $100 alone is not math
assert!(
math_spans.is_empty() || !math_spans.iter().any(|s| s.content.contains("100")),
"Unbalanced $ should not create math span containing 100"
);
}
// =========================================================================
// Tests for O(1) reference definition lookups via HashMap
// =========================================================================
#[test]
fn test_reference_lookup_o1_basic() {
let content = r#"[ref1]: /url1
[REF2]: /url2 "Title"
[Ref3]: /url3
Use [link][ref1] and [link][REF2]."#;
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
// Verify we have 3 reference defs
assert_eq!(ctx.reference_definitions().len(), 3);
// Test get_reference_url with various cases
assert_eq!(ctx.get_reference_url("ref1"), Some("/url1"));
assert_eq!(ctx.get_reference_url("REF1"), Some("/url1")); // case insensitive
assert_eq!(ctx.get_reference_url("Ref1"), Some("/url1")); // case insensitive
assert_eq!(ctx.get_reference_url("ref2"), Some("/url2"));
assert_eq!(ctx.get_reference_url("REF2"), Some("/url2"));
assert_eq!(ctx.get_reference_url("ref3"), Some("/url3"));
assert_eq!(ctx.get_reference_url("nonexistent"), None);
}
#[test]
fn test_reference_lookup_o1_empty_content() {
let content = "No references here.";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert!(ctx.reference_definitions().is_empty());
assert_eq!(ctx.get_reference_url("anything"), None);
}
#[test]
fn test_reference_lookup_o1_special_characters_in_id() {
let content = r#"[ref-with-dash]: /url1
[ref_with_underscore]: /url2
[ref.with.dots]: /url3
"#;
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.get_reference_url("ref-with-dash"), Some("/url1"));
assert_eq!(ctx.get_reference_url("ref_with_underscore"), Some("/url2"));
assert_eq!(ctx.get_reference_url("ref.with.dots"), Some("/url3"));
}
#[test]
fn test_reference_lookup_o1_unicode_id() {
let content = r#"[日本語]: /japanese
[émoji]: /emoji
"#;
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.get_reference_url("日本語"), Some("/japanese"));
assert_eq!(ctx.get_reference_url("émoji"), Some("/emoji"));
assert_eq!(ctx.get_reference_url("ÉMOJI"), Some("/emoji")); // uppercase
}
#[test]
fn test_is_in_link_title_multiple_ranges_binary_search() {
// Three reference defs with titles — verifies binary search works across all three
let content = "[a]: /url1 \"Title A\"\n[b]: /url2 \"Title B\"\n[c]: /url3 \"Title C\"\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert_eq!(ctx.reference_definitions().len(), 3, "Should have 3 reference defs");
// Position inside first title should return true
if let (Some(start), Some(end)) = (
ctx.reference_definitions()[0].title_byte_start,
ctx.reference_definitions()[0].title_byte_end,
) {
assert!(ctx.is_in_link_title(start + 1), "Inside first title should return true");
// Position at exclusive end should return false
assert!(!ctx.is_in_link_title(end), "At exclusive end should return false");
}
// Position between titles (in URL area of def B, before its title) should return false
if let (Some(end_a), Some(start_b)) = (
ctx.reference_definitions()[0].title_byte_end,
ctx.reference_definitions()[1].title_byte_start,
) && end_a + 1 < start_b
{
assert!(!ctx.is_in_link_title(end_a + 1), "Between titles should return false");
}
// Position inside third title should return true
if let Some(start) = ctx.reference_definitions()[2].title_byte_start {
assert!(ctx.is_in_link_title(start + 1), "Inside third title should return true");
}
}
#[test]
fn test_is_in_math_span_between_two_spans() {
// Position in text between two math spans should return false
let content = "$a$ text $b$";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let math_spans = ctx.math_spans();
if math_spans.len() >= 2 {
let between = math_spans[0].byte_end + 1;
assert!(
!ctx.is_in_math_span(between),
"Position between math spans should return false"
);
}
}
// =========================================================================
// Tests for code span and HTML tag detection at boundaries
// =========================================================================
#[test]
fn test_code_span_at_line_start() {
let content = "Line one\n`code` end\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let spans = ctx.code_spans();
let line2_spans: Vec<_> = spans.iter().filter(|s| s.line == 2).collect();
assert!(!line2_spans.is_empty(), "Should detect code span on line 2");
assert_eq!(line2_spans[0].start_col, 0, "Code span should start at column 0");
}
#[test]
fn test_html_tag_at_byte_zero() {
let content = "
text";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
let tags = ctx.html_tags();
assert!(!tags.is_empty(), "Should detect HTML tag at byte 0");
assert_eq!(tags[0].line, 1, "Tag at byte 0 should be on line 1");
}
// =========================================================================
// HTML block detection: CommonMark Type-1 blank-line handling
// =========================================================================
//
// Per CommonMark §4.6, Type-1 HTML blocks open with , ) terminate
// at the first blank line.
#[test]
fn test_html_block_pre_with_blank_line_marks_all_inner_lines() {
// Reproduces issue #578: a
containing a blank line.
let content = "# Heading\n\n\n\nhello world\n
\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert!(ctx.is_in_html_block(3), "line 3 (``) should be in html block");
assert!(
ctx.is_in_html_block(4),
"line 4 (blank inside pre) should be in html block"
);
assert!(
ctx.is_in_html_block(5),
"line 5 (`hello world`) should be in html block"
);
assert!(ctx.is_in_html_block(6), "line 6 (``) should be in html block");
}
#[test]
fn test_html_block_textarea_with_blank_line_marks_all_inner_lines() {
let content = "\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert!(ctx.is_in_html_block(1), "line 1 (``) should be in html block"
);
}
#[test]
fn test_html_block_long_pre_exceeds_arbitrary_line_cap() {
// A with 120 inner lines (no blanks) must mark every inner line,
// regardless of any internal line cap.
let mut content = String::from("\n");
for i in 0..120 {
content.push_str(&format!("inner line {i}\n"));
}
content.push_str("\n");
let ctx = LintContext::new(&content, MarkdownFlavor::Standard, None);
// 1-indexed: line 1 = , lines 2..=121 = inner, line 122 =
.
for line_num in 1..=122 {
assert!(
ctx.is_in_html_block(line_num),
"line {line_num} of a 122-line block should be marked in_html_block",
);
}
}
#[test]
fn test_html_block_div_still_terminates_on_blank_line() {
// Type-6 guardrail: is not Type-1 and must terminate at blank line.
let content = "\ninner\n\nafter blank\n\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert!(ctx.is_in_html_block(1), "line 1 (``) should be in html block");
assert!(ctx.is_in_html_block(2), "line 2 (inner) should be in html block");
assert!(
!ctx.is_in_html_block(4),
"line 4 (`after blank`) must NOT be in html block"
);
}
#[test]
fn test_html_block_gt_line_is_html_text_outside_a_blockquote() {
// A tag broken across lines leaves a line that starts with `>`. Outside a
// blockquote that is HTML text: it neither ends the block nor opens a quote.
let content = "\n \n a\n text after\n \n
\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
for line in 1..=8 {
let info = ctx.line_info(line).unwrap();
assert!(info.in_html_block, "line {line} should be in the html block");
assert!(info.blockquote.is_none(), "line {line} must not be a blockquote");
assert!(!info.is_blank, "line {line} must not be blank");
}
}
#[test]
fn test_html_block_gt_line_below_the_opener_column_is_still_a_blockquote() {
// The `>` line is indented less than the item's HTML, so it leaves the list
// item and the HTML block with it, and opens a blockquote.
let content = "- item\n\n \n inner\n> quote\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert!(ctx.is_in_html_block(4), "line 4 should be in the html block");
assert!(ctx.line_info(5).unwrap().blockquote.is_some(), "line 5 is a blockquote");
}
#[test]
fn test_html_block_closed_on_its_opening_line_runs_to_a_blank_line() {
// A Type-6 block ends only at a blank line, wherever its tag closes.
let content = "a\ntext\n\nAfter\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert!(ctx.is_in_html_block(1));
assert!(ctx.is_in_html_block(2), "the line under the opener is HTML text");
assert!(!ctx.is_in_html_block(4), "the blank line ends the block");
// A Type-1 block ends at the line holding its end tag.
let ctx = LintContext::new("a
\ntext\n", MarkdownFlavor::Standard, None);
assert!(ctx.is_in_html_block(1));
assert!(!ctx.is_in_html_block(2));
}
#[test]
fn test_html_block_opened_by_a_closing_tag_runs_to_a_blank_line() {
// A closing tag starts a block like an opening one, which ends at a blank line.
let content = "Intro\n\n# text\n\nAfter\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
assert!(!ctx.is_in_html_block(1));
assert!(ctx.is_in_html_block(2));
assert!(ctx.is_in_html_block(3), "the line under the closing tag is HTML text");
assert!(!ctx.is_in_html_block(5), "the blank line ends the block");
// The closing tag of a Type-1 element opens no block running to its end tag.
let ctx = LintContext::new("\ntext\n\nAfter\n", MarkdownFlavor::Standard, None);
assert!(ctx.is_in_html_block(2));
assert!(!ctx.is_in_html_block(4), "the blank line ends the block");
}
#[test]
fn test_html_block_complete_tag_alone_on_a_line_opens_a_block() {
// CommonMark start condition 7: a tag alone on its line opens an HTML block
// running to the next blank line, whatever element it names.
let content = "Intro\n\n\n\nAfter\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
for line in 3..=5 {
assert!(ctx.is_in_html_block(line), "line {line} should be in the html block");
}
assert!(!ctx.is_in_html_block(7), "the blank line ends the block");
// Such a tag cannot interrupt a paragraph.
let ctx = LintContext::new("Intro\n\ntext\n", MarkdownFlavor::Standard, None);
assert!(!ctx.is_in_html_block(2));
// Front matter is not Markdown, so a tag in it opens no block.
let ctx = LintContext::new("---\nx: 1\n\n\n---\ntext\n", MarkdownFlavor::Standard, None);
assert!(!ctx.is_in_html_block(4));
assert!(!ctx.is_in_html_block(6));
// A block opened after a list marker leaves the item's line to the list.
let ctx = LintContext::new("- \n text\n", MarkdownFlavor::Standard, None);
assert!(!ctx.is_in_html_block(1));
assert!(ctx.line_info(1).unwrap().list_item.is_some());
// MDX reads the same line as JSX.
let ctx = LintContext::new("\ntext\n", MarkdownFlavor::MDX, None);
assert!(!ctx.is_in_html_block(2));
}
#[test]
fn test_html_block_unclosed_pre_extends_to_eof() {
// Per CommonMark, an unclosed Type-1 block extends to end of document.
let content = "\nline a\n\nline b\nline c\n";
let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
for line_num in 1..=5 {
assert!(
ctx.is_in_html_block(line_num),
"line {line_num} of an unclosed should extend to EOF",
);
}
}
#[test]
fn test_html_block_nested_pre_with_split_closing_tag_ends_with_outer_block() {
// Prettier wraps a long closing tag as ``, which no single-line
// search for `` can match. Inside an already-open