{#id x=](image)} ", "!["),
] {
let expected = visible.len();
assert_eq!(heading_text_end(text), expected, "{text:?}");
}
}
#[test]
fn test_kramdown_format_extraction() {
// Simple kramdown format
let (text, id) = extract_header_id("# Header {#simple}");
assert_eq!(text, "# Header");
assert_eq!(id, Some("simple".to_string()));
let (text, id) = extract_header_id("## Section {#section-id}");
assert_eq!(text, "## Section");
assert_eq!(id, Some("section-id".to_string()));
}
#[test]
fn test_python_markdown_attr_list_extraction() {
// Python-markdown formats
let (text, id) = extract_header_id("# Header {:#colon-id}");
assert_eq!(text, "# Header");
assert_eq!(id, Some("colon-id".to_string()));
let (text, id) = extract_header_id("# Header {: #spaced-id }");
assert_eq!(text, "# Header");
assert_eq!(id, Some("spaced-id".to_string()));
}
#[test]
fn test_extended_attr_list_extraction() {
// ID with single class
let (text, id) = extract_header_id("# Header {: #with-class .highlight }");
assert_eq!(text, "# Header");
assert_eq!(id, Some("with-class".to_string()));
// ID with multiple classes
let (text, id) = extract_header_id("## Section {: #multi .class1 .class2 }");
assert_eq!(text, "## Section");
assert_eq!(id, Some("multi".to_string()));
// ID with key-value attributes
let (text, id) = extract_header_id("### Subsection {: #with-attrs data-test=\"value\" style=\"color: red\" }");
assert_eq!(text, "### Subsection");
assert_eq!(id, Some("with-attrs".to_string()));
// Complex combination
let (text, id) = extract_header_id("#### Complex {: #complex .highlight data-role=\"button\" title=\"Test\" }");
assert_eq!(text, "#### Complex");
assert_eq!(id, Some("complex".to_string()));
// ID with quotes in attributes
let (text, id) = extract_header_id("##### Quotes {: #quotes title=\"Has \\\"nested\\\" quotes\" }");
assert_eq!(text, "##### Quotes");
assert_eq!(id, Some("quotes".to_string()));
}
#[test]
fn test_attr_list_detection_edge_cases() {
// Attr-list without ID should not match
let (text, id) = extract_header_id("# Header {: .class-only }");
assert_eq!(text, "# Header {: .class-only }");
assert_eq!(id, None);
// Malformed attr-list should not match
let (text, id) = extract_header_id("# Header { no-hash }");
assert_eq!(text, "# Header { no-hash }");
assert_eq!(id, None);
// Empty ID should not match
let (text, id) = extract_header_id("# Header {: # }");
assert_eq!(text, "# Header {: # }");
assert_eq!(id, None);
// ID in middle (not at end) should not match
let (text, id) = extract_header_id("# Header {: #middle } with more text");
assert_eq!(text, "# Header {: #middle } with more text");
assert_eq!(id, None);
}
#[test]
fn test_standalone_attr_list_detection() {
// Simple ID formats
assert!(is_standalone_attr_list("{#custom-id}"));
assert!(is_standalone_attr_list("{ #spaced-id }"));
assert!(is_standalone_attr_list("{:#colon-id}"));
assert!(is_standalone_attr_list("{: #full-format }"));
// With classes and attributes
assert!(is_standalone_attr_list("{: #with-class .highlight }"));
assert!(is_standalone_attr_list("{: #multi .class1 .class2 }"));
assert!(is_standalone_attr_list("{: #complex .highlight data-test=\"value\" }"));
// Should not match
assert!(!is_standalone_attr_list("Some text {#not-standalone}"));
assert!(!is_standalone_attr_list("Text before {#id}"));
assert!(!is_standalone_attr_list("{#id} text after"));
assert!(!is_standalone_attr_list(""));
assert!(!is_standalone_attr_list(" ")); // just spaces
assert!(!is_standalone_attr_list("{: .class-only }")); // no ID
}
#[test]
fn test_standalone_attr_list_id_extraction() {
// Basic formats
assert_eq!(extract_standalone_attr_list_id("{#simple}"), Some("simple".to_string()));
assert_eq!(
extract_standalone_attr_list_id("{ #spaced }"),
Some("spaced".to_string())
);
assert_eq!(extract_standalone_attr_list_id("{:#colon}"), Some("colon".to_string()));
assert_eq!(extract_standalone_attr_list_id("{: #full }"), Some("full".to_string()));
// With additional attributes
assert_eq!(
extract_standalone_attr_list_id("{: #with-class .highlight }"),
Some("with-class".to_string())
);
assert_eq!(
extract_standalone_attr_list_id("{: #complex .class1 .class2 data=\"value\" }"),
Some("complex".to_string())
);
// Should return None
assert_eq!(extract_standalone_attr_list_id("Not an attr-list"), None);
assert_eq!(extract_standalone_attr_list_id("Text {#not-standalone}"), None);
assert_eq!(extract_standalone_attr_list_id("{: .class-only }"), None);
assert_eq!(extract_standalone_attr_list_id(""), None);
}
#[test]
fn test_backward_compatibility() {
// Ensure all original kramdown formats still work
let test_cases = vec![
("# Header {#a}", "# Header", Some("a".to_string())),
("# Header {#simple-id}", "# Header", Some("simple-id".to_string())),
("## Heading {#heading-2}", "## Heading", Some("heading-2".to_string())),
(
"### With-Hyphens {#with-hyphens}",
"### With-Hyphens",
Some("with-hyphens".to_string()),
),
];
for (input, expected_text, expected_id) in test_cases {
let (text, id) = extract_header_id(input);
assert_eq!(text, expected_text, "Text mismatch for input: {input}");
assert_eq!(id, expected_id, "ID mismatch for input: {input}");
}
}
#[test]
fn test_invalid_id_with_dots() {
// IDs with dots should not be extracted (dots are not valid ID characters)
let (text, id) = extract_header_id("## Another. {#id.with.dots}");
assert_eq!(text, "## Another. {#id.with.dots}"); // Should not strip invalid ID
assert_eq!(id, None); // Should not extract invalid ID
// Test that only the part before the dot would be extracted if it was valid standalone
// But since it's in an invalid format, the whole thing should be rejected
let (text, id) = extract_header_id("## Another. {#id.more.dots}");
assert_eq!(text, "## Another. {#id.more.dots}");
assert_eq!(id, None);
}
#[test]
fn test_html_anchor_stripping() {
// HTML anchor elements should be stripped from heading text
// This is used by some authors for custom anchors
// Basic pattern
let (text, id) = extract_header_id("Cheat Sheets");
assert_eq!(text, "Cheat Sheets");
assert_eq!(id, None);
// pattern
let (text, id) = extract_header_id("Tools and session management");
assert_eq!(text, "Tools and session management");
assert_eq!(id, None);
// With spaces around the anchor
let (text, id) = extract_header_id(" Heading with space");
assert_eq!(text, "Heading with space");
assert_eq!(id, None);
// Combined with kramdown custom ID
let (text, id) = extract_header_id("My Section {#my-custom-id}");
assert_eq!(text, "My Section");
assert_eq!(id, Some("my-custom-id".to_string()));
}
#[test]
fn test_html_anchor_ids_are_read_from_empty_anchor_elements_in_order() {
assert_eq!(extract_html_anchor_ids(r#"Heading"#), ["target"]);
assert_eq!(
extract_html_anchor_ids(r#"Heading"#),
["preferred"]
);
assert_eq!(
extract_html_anchor_ids(r#"Heading"#),
["legacy", "newer"]
);
assert_eq!(extract_html_anchor_ids("Heading"), ["plain"]);
assert!(extract_html_anchor_ids(r##"Heading"##).is_empty());
assert!(extract_html_anchor_ids(r#"Heading"#).is_empty());
assert!(extract_html_anchor_ids(r#"textHeading"#).is_empty());
}
#[test]
fn test_an_attribute_merely_ending_in_id_or_name_is_not_an_anchor() {
assert!(extract_html_anchor_ids(r#"Foo"#).is_empty());
assert!(extract_html_anchor_ids(r#"Foo"#).is_empty());
}
#[test]
fn test_a_quoted_attribute_value_may_contain_a_closing_angle_bracket() {
let raw = r#"Heading"#;
assert_eq!(extract_html_anchor_ids(raw), ["target"]);
assert_eq!(extract_header_id(raw), ("Heading".to_string(), None));
}
#[test]
fn test_anchor_markup_inside_a_code_span_is_heading_text() {
let raw = "Showing `` syntax";
assert!(extract_html_anchor_ids(raw).is_empty());
assert_eq!(extract_header_id(raw), (raw.to_string(), None));
// A real anchor beside the code span is still found and stripped.
let raw = "Showing `` syntax";
assert_eq!(extract_html_anchor_ids(raw), ["real"]);
assert_eq!(extract_header_id(raw).0, "Showing `` syntax");
}
#[test]
fn test_anchor_markup_inside_an_html_comment_is_not_a_target() {
let raw = "Foo ";
assert!(extract_html_anchor_ids(raw).is_empty());
assert_eq!(extract_header_id(raw), (raw.to_string(), None));
}
#[test]
fn test_html_tag_attribute_matches_whole_names_case_insensitively() {
assert_eq!(html_tag_attribute(r#"