Skip to main content

rumdl_lib/rules/
md037_spaces_around_emphasis.rs

1/// Rule MD037: No spaces around emphasis markers
2///
3/// See [docs/md037.md](../../docs/md037.md) for full documentation, configuration, and examples.
4use crate::filtered_lines::FilteredLinesExt;
5use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
6use crate::utils::emphasis_utils::{
7    EmphasisSpan, find_emphasis_markers, find_emphasis_spans, has_doc_patterns, replace_inline_code,
8    replace_inline_math,
9};
10use crate::utils::kramdown_utils::has_span_ial;
11use crate::utils::regex_cache::UNORDERED_LIST_MARKER_REGEX;
12use crate::utils::skip_context::{
13    is_in_html_comment, is_in_inline_html_code, is_in_jsx_expression, is_in_math_context, is_in_mdx_comment,
14    is_in_mkdocs_markup, is_in_table_cell,
15};
16
17/// Check if an emphasis span has spacing issues that should be flagged
18#[inline]
19fn has_spacing_issues(span: &EmphasisSpan) -> bool {
20    span.has_leading_space || span.has_trailing_space
21}
22
23/// Truncate long text for display in warning messages
24/// Shows first ~30 and last ~30 chars with ellipsis in middle for readability
25#[inline]
26fn truncate_for_display(text: &str, max_len: usize) -> String {
27    if text.len() <= max_len {
28        return text.to_string();
29    }
30
31    let prefix_len = max_len / 2 - 2; // -2 for "..."
32    let suffix_len = max_len / 2 - 2;
33
34    // Use floor_char_boundary to safely find UTF-8 character boundaries
35    let prefix_end = text.floor_char_boundary(prefix_len.min(text.len()));
36    let suffix_start = text.floor_char_boundary(text.len().saturating_sub(suffix_len));
37
38    format!("{}...{}", &text[..prefix_end], &text[suffix_start..])
39}
40
41/// Rule MD037: Spaces inside emphasis markers
42#[derive(Clone)]
43pub struct MD037NoSpaceInEmphasis;
44
45impl Default for MD037NoSpaceInEmphasis {
46    fn default() -> Self {
47        Self
48    }
49}
50
51impl MD037NoSpaceInEmphasis {
52    /// Check if a byte position is within a link (inline links, reference links, or reference definitions)
53    fn is_in_link(&self, ctx: &crate::lint_context::LintContext, byte_pos: usize) -> bool {
54        // Check inline and reference links
55        for link in &ctx.links {
56            if link.byte_offset <= byte_pos && byte_pos < link.byte_end {
57                return true;
58            }
59        }
60
61        // Check images (which use similar syntax)
62        for image in &ctx.images {
63            if image.byte_offset <= byte_pos && byte_pos < image.byte_end {
64                return true;
65            }
66        }
67
68        // Check reference definitions [ref]: url "title" using pre-computed data (O(1) vs O(n))
69        ctx.is_in_reference_def(byte_pos)
70    }
71}
72
73impl Rule for MD037NoSpaceInEmphasis {
74    fn name(&self) -> &'static str {
75        "MD037"
76    }
77
78    fn description(&self) -> &'static str {
79        "Spaces inside emphasis markers"
80    }
81
82    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
83        let content = ctx.content;
84        let _timer = crate::profiling::ScopedTimer::new("MD037_check");
85
86        // Early return: if no emphasis markers at all, skip processing
87        if !content.contains('*') && !content.contains('_') {
88            return Ok(vec![]);
89        }
90
91        // Create LineIndex for correct byte position calculations across all line ending types
92        let line_index = &ctx.line_index;
93
94        let mut warnings = Vec::new();
95
96        // Process content lines, automatically skipping front matter, code blocks, math blocks,
97        // and Obsidian comments (when in Obsidian flavor)
98        // Math blocks contain LaTeX syntax where _ and * have special meaning
99        for line in ctx
100            .filtered_lines()
101            .skip_front_matter()
102            .skip_code_blocks()
103            .skip_math_blocks()
104            .skip_html_blocks()
105            .skip_jsx_expressions()
106            .skip_mdx_comments()
107            .skip_obsidian_comments()
108            .skip_mkdocstrings()
109        {
110            // Skip if the line doesn't contain any emphasis markers
111            if !line.content.contains('*') && !line.content.contains('_') {
112                continue;
113            }
114
115            // Check for emphasis issues on the original line
116            self.check_line_for_emphasis_issues_fast(line.content, line.line_num, &mut warnings);
117        }
118
119        // Filter out warnings for emphasis markers that are inside links, HTML comments, math, or MkDocs markup
120        let mut filtered_warnings = Vec::new();
121        let lines = ctx.raw_lines();
122
123        for (line_idx, line) in lines.iter().enumerate() {
124            let line_num = line_idx + 1;
125            let line_start_pos = line_index.get_line_start_byte(line_num).unwrap_or(0);
126
127            // Find warnings for this line
128            for warning in &warnings {
129                if warning.line == line_num {
130                    // Calculate byte position of the warning
131                    let byte_pos = line_start_pos + (warning.column - 1);
132                    // Calculate position within the line (0-indexed)
133                    let line_pos = warning.column - 1;
134
135                    // Skip if inside links, HTML comments, math contexts, tables, code spans, MDX constructs, or MkDocs markup
136                    // Note: is_in_code_span uses pulldown-cmark and correctly handles multi-line spans
137                    // Pandoc bracketed spans `[text]{.class}` may contain spaced
138                    // emphasis markers as literal content; suppress MD037 there.
139                    // Subscripts/superscripts cannot contain whitespace per the
140                    // detector grammar, so MD037's spaced-emphasis warnings can
141                    // never land inside one.
142                    let in_pandoc_construct = ctx.flavor.is_pandoc_compatible() && ctx.is_in_bracketed_span(byte_pos);
143                    if !in_pandoc_construct
144                        && !self.is_in_link(ctx, byte_pos)
145                        && !is_in_html_comment(content, byte_pos)
146                        && !is_in_math_context(ctx, byte_pos)
147                        && !is_in_table_cell(ctx, line_num, warning.column)
148                        && !ctx.is_in_code_span(line_num, warning.column)
149                        && !is_in_inline_html_code(line, line_pos)
150                        && !is_in_jsx_expression(ctx, byte_pos)
151                        && !is_in_mdx_comment(ctx, byte_pos)
152                        && !is_in_mkdocs_markup(line, line_pos, ctx.flavor)
153                        && !ctx.is_position_in_obsidian_comment(line_num, warning.column)
154                    {
155                        let mut adjusted_warning = warning.clone();
156                        if let Some(fix) = &mut adjusted_warning.fix {
157                            // Convert line-relative range to absolute range
158                            let abs_start = line_start_pos + fix.range.start;
159                            let abs_end = line_start_pos + fix.range.end;
160                            fix.range = abs_start..abs_end;
161                        }
162                        filtered_warnings.push(adjusted_warning);
163                    }
164                }
165            }
166        }
167
168        Ok(filtered_warnings)
169    }
170
171    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
172        let content = ctx.content;
173        let _timer = crate::profiling::ScopedTimer::new("MD037_fix");
174
175        // Fast path: if no emphasis markers, return unchanged
176        if !content.contains('*') && !content.contains('_') {
177            return Ok(content.to_string());
178        }
179
180        // First check for issues and get all warnings with fixes
181        let warnings = self.check(ctx)?;
182        let warnings =
183            crate::utils::fix_utils::filter_warnings_by_inline_config(warnings, ctx.inline_config(), self.name());
184
185        // If no warnings, return original content
186        if warnings.is_empty() {
187            return Ok(content.to_string());
188        }
189
190        // Apply fixes
191        let mut result = content.to_string();
192        let mut offset: isize = 0;
193
194        // Sort warnings by position to apply fixes in the correct order
195        let mut sorted_warnings: Vec<_> = warnings.iter().filter(|w| w.fix.is_some()).collect();
196        sorted_warnings.sort_by_key(|w| (w.line, w.column));
197
198        for warning in sorted_warnings {
199            if let Some(fix) = &warning.fix {
200                // Apply fix with offset adjustment
201                let actual_start = (fix.range.start as isize + offset) as usize;
202                let actual_end = (fix.range.end as isize + offset) as usize;
203
204                // Make sure we're not out of bounds
205                if actual_start < result.len() && actual_end <= result.len() {
206                    // Replace the text
207                    result.replace_range(actual_start..actual_end, &fix.replacement);
208                    // Update offset for future replacements
209                    offset += fix.replacement.len() as isize - (fix.range.end - fix.range.start) as isize;
210                }
211            }
212        }
213
214        Ok(result)
215    }
216
217    /// Get the category of this rule for selective processing
218    fn category(&self) -> RuleCategory {
219        RuleCategory::Emphasis
220    }
221
222    /// Check if this rule should be skipped
223    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
224        ctx.content.is_empty() || !ctx.likely_has_emphasis()
225    }
226
227    fn as_any(&self) -> &dyn std::any::Any {
228        self
229    }
230
231    fn from_config(_config: &crate::config::Config) -> Box<dyn Rule>
232    where
233        Self: Sized,
234    {
235        Box::new(MD037NoSpaceInEmphasis)
236    }
237}
238
239impl MD037NoSpaceInEmphasis {
240    /// Optimized line checking for emphasis spacing issues
241    #[inline]
242    fn check_line_for_emphasis_issues_fast(&self, line: &str, line_num: usize, warnings: &mut Vec<LintWarning>) {
243        // Quick documentation pattern checks
244        if has_doc_patterns(line) {
245            return;
246        }
247
248        // Optimized list detection with fast path
249        // When a list marker is detected, ALWAYS check only the content after the marker,
250        // never the full line. This prevents the list marker (* + -) from being mistaken
251        // for emphasis markers.
252        if (line.starts_with(' ') || line.starts_with('*') || line.starts_with('+') || line.starts_with('-'))
253            && UNORDERED_LIST_MARKER_REGEX.is_match(line)
254        {
255            if let Some(caps) = UNORDERED_LIST_MARKER_REGEX.captures(line)
256                && let Some(full_match) = caps.get(0)
257            {
258                let list_marker_end = full_match.end();
259                if list_marker_end < line.len() {
260                    let remaining_content = &line[list_marker_end..];
261
262                    // Always check just the remaining content (after the list marker).
263                    // The list marker itself is never emphasis.
264                    self.check_line_content_for_emphasis_fast(remaining_content, line_num, list_marker_end, warnings);
265                }
266            }
267            return;
268        }
269
270        // Check the entire line
271        self.check_line_content_for_emphasis_fast(line, line_num, 0, warnings);
272    }
273
274    /// Optimized line content checking for emphasis issues
275    fn check_line_content_for_emphasis_fast(
276        &self,
277        content: &str,
278        line_num: usize,
279        offset: usize,
280        warnings: &mut Vec<LintWarning>,
281    ) {
282        // Replace inline code and inline math to avoid false positives
283        // with emphasis markers inside backticks or dollar signs
284        let processed_content = replace_inline_code(content);
285        let processed_content = replace_inline_math(&processed_content);
286
287        // Find all emphasis markers using optimized parsing
288        let markers = find_emphasis_markers(&processed_content);
289        if markers.is_empty() {
290            return;
291        }
292
293        // Find valid emphasis spans
294        let spans = find_emphasis_spans(&processed_content, &markers);
295
296        // Check each span for spacing issues
297        for span in spans {
298            if has_spacing_issues(&span) {
299                // Calculate the full span including markers
300                let full_start = span.opening.start_pos;
301                let full_end = span.closing.end_pos();
302                let full_text = &content[full_start..full_end];
303
304                // Skip if this emphasis has a Kramdown span IAL immediately after it
305                // (no space between emphasis and IAL)
306                if full_end < content.len() {
307                    let remaining = &content[full_end..];
308                    // Check if IAL starts immediately after the emphasis (no whitespace)
309                    if remaining.starts_with('{') && has_span_ial(remaining.split_whitespace().next().unwrap_or("")) {
310                        continue;
311                    }
312                }
313
314                // Create the marker string efficiently
315                let marker_char = span.opening.as_char();
316                let marker_str = if span.opening.count == 1 {
317                    marker_char.to_string()
318                } else {
319                    format!("{marker_char}{marker_char}")
320                };
321
322                // Create the fixed version by trimming spaces from content
323                let trimmed_content = span.content.trim();
324                let fixed_text = format!("{marker_str}{trimmed_content}{marker_str}");
325
326                // Truncate long emphasis spans for readable warning messages
327                let display_text = truncate_for_display(full_text, 60);
328
329                let warning = LintWarning {
330                    rule_name: Some(self.name().to_string()),
331                    message: format!("Spaces inside emphasis markers: {display_text:?}"),
332                    line: line_num,
333                    column: offset + full_start + 1, // +1 because columns are 1-indexed
334                    end_line: line_num,
335                    end_column: offset + full_end + 1,
336                    severity: Severity::Warning,
337                    fix: Some(Fix::new((offset + full_start)..(offset + full_end), fixed_text)),
338                };
339
340                warnings.push(warning);
341            }
342        }
343    }
344}
345
346#[cfg(test)]
347mod tests {
348    use super::*;
349    use crate::lint_context::LintContext;
350
351    #[test]
352    fn test_emphasis_marker_parsing() {
353        let markers = find_emphasis_markers("This has *single* and **double** emphasis");
354        assert_eq!(markers.len(), 4); // *, *, **, **
355
356        let markers = find_emphasis_markers("*start* and *end*");
357        assert_eq!(markers.len(), 4); // *, *, *, *
358    }
359
360    #[test]
361    fn test_emphasis_span_detection() {
362        let markers = find_emphasis_markers("This has *valid* emphasis");
363        let spans = find_emphasis_spans("This has *valid* emphasis", &markers);
364        assert_eq!(spans.len(), 1);
365        assert_eq!(spans[0].content, "valid");
366        assert!(!spans[0].has_leading_space);
367        assert!(!spans[0].has_trailing_space);
368
369        let markers = find_emphasis_markers("This has * invalid * emphasis");
370        let spans = find_emphasis_spans("This has * invalid * emphasis", &markers);
371        assert_eq!(spans.len(), 1);
372        assert_eq!(spans[0].content, " invalid ");
373        assert!(spans[0].has_leading_space);
374        assert!(spans[0].has_trailing_space);
375    }
376
377    #[test]
378    fn test_with_document_structure() {
379        let rule = MD037NoSpaceInEmphasis;
380
381        // Test with no spaces inside emphasis - should pass
382        let content = "This is *correct* emphasis and **strong emphasis**";
383        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
384        let result = rule.check(&ctx).unwrap();
385        assert!(result.is_empty(), "No warnings expected for correct emphasis");
386
387        // Test with actual spaces inside emphasis - use content that should warn
388        let content = "This is * text with spaces * and more content";
389        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
390        let result = rule.check(&ctx).unwrap();
391        assert!(!result.is_empty(), "Expected warnings for spaces in emphasis");
392
393        // Test with code blocks - emphasis in code should be ignored
394        let content = "This is *correct* emphasis\n```\n* incorrect * in code block\n```\nOutside block with * spaces in emphasis *";
395        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
396        let result = rule.check(&ctx).unwrap();
397        assert!(
398            !result.is_empty(),
399            "Expected warnings for spaces in emphasis outside code block"
400        );
401    }
402
403    #[test]
404    fn test_emphasis_in_links_not_flagged() {
405        let rule = MD037NoSpaceInEmphasis;
406        let content = r#"Check this [* spaced asterisk *](https://example.com/*test*) link.
407
408This has * real spaced emphasis * that should be flagged."#;
409        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
410        let result = rule.check(&ctx).unwrap();
411
412        // Test passed - emphasis inside links are filtered out correctly
413
414        // Only the real emphasis outside links should be flagged
415        assert_eq!(
416            result.len(),
417            1,
418            "Expected exactly 1 warning, but got: {:?}",
419            result.len()
420        );
421        assert!(result[0].message.contains("Spaces inside emphasis markers"));
422        // Should flag "* real spaced emphasis *" but not emphasis patterns inside links
423        assert!(result[0].line == 3); // Line with "* real spaced emphasis *"
424    }
425
426    #[test]
427    fn test_emphasis_in_links_vs_outside_links() {
428        let rule = MD037NoSpaceInEmphasis;
429        let content = r#"Check [* spaced *](https://example.com/*test*) and inline * real spaced * text.
430
431[* link *]: https://example.com/*path*"#;
432        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
433        let result = rule.check(&ctx).unwrap();
434
435        // Only the actual emphasis outside links should be flagged
436        assert_eq!(result.len(), 1);
437        assert!(result[0].message.contains("Spaces inside emphasis markers"));
438        // Should be the "* real spaced *" text on line 1
439        assert!(result[0].line == 1);
440    }
441
442    #[test]
443    fn test_issue_49_asterisk_in_inline_code() {
444        // Test for issue #49 - Asterisk within backticks identified as for emphasis
445        let rule = MD037NoSpaceInEmphasis;
446
447        // Test case from issue #49
448        let content = "The `__mul__` method is needed for left-hand multiplication (`vector * 3`) and `__rmul__` is needed for right-hand multiplication (`3 * vector`).";
449        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
450        let result = rule.check(&ctx).unwrap();
451        assert!(
452            result.is_empty(),
453            "Should not flag asterisks inside inline code as emphasis (issue #49). Got: {result:?}"
454        );
455    }
456
457    #[test]
458    fn test_issue_28_inline_code_in_emphasis() {
459        // Test for issue #28 - MD037 should not flag inline code inside emphasis as spaces
460        let rule = MD037NoSpaceInEmphasis;
461
462        // Test case 1: inline code with single backticks inside bold emphasis
463        let content = "Though, we often call this an **inline `if`** because it looks sort of like an `if`-`else` statement all in *one line* of code.";
464        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
465        let result = rule.check(&ctx).unwrap();
466        assert!(
467            result.is_empty(),
468            "Should not flag inline code inside emphasis as spaces (issue #28). Got: {result:?}"
469        );
470
471        // Test case 2: multiple inline code snippets inside emphasis
472        let content2 = "The **`foo` and `bar`** methods are important.";
473        let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::Standard, None);
474        let result2 = rule.check(&ctx2).unwrap();
475        assert!(
476            result2.is_empty(),
477            "Should not flag multiple inline code snippets inside emphasis. Got: {result2:?}"
478        );
479
480        // Test case 3: inline code with underscores for emphasis
481        let content3 = "This is __inline `code`__ with underscores.";
482        let ctx3 = LintContext::new(content3, crate::config::MarkdownFlavor::Standard, None);
483        let result3 = rule.check(&ctx3).unwrap();
484        assert!(
485            result3.is_empty(),
486            "Should not flag inline code with underscore emphasis. Got: {result3:?}"
487        );
488
489        // Test case 4: single asterisk emphasis with inline code
490        let content4 = "This is *inline `test`* with single asterisks.";
491        let ctx4 = LintContext::new(content4, crate::config::MarkdownFlavor::Standard, None);
492        let result4 = rule.check(&ctx4).unwrap();
493        assert!(
494            result4.is_empty(),
495            "Should not flag inline code with single asterisk emphasis. Got: {result4:?}"
496        );
497
498        // Test case 5: actual spaces that should be flagged
499        let content5 = "This has * real spaces * that should be flagged.";
500        let ctx5 = LintContext::new(content5, crate::config::MarkdownFlavor::Standard, None);
501        let result5 = rule.check(&ctx5).unwrap();
502        assert!(!result5.is_empty(), "Should still flag actual spaces in emphasis");
503        assert!(result5[0].message.contains("Spaces inside emphasis markers"));
504    }
505
506    #[test]
507    fn test_multibyte_utf8_no_panic() {
508        // Regression test: ensure multi-byte UTF-8 characters don't cause panics
509        // in the truncate_for_display function when handling long emphasis spans.
510        // These test cases include various scripts that could trigger boundary issues.
511        let rule = MD037NoSpaceInEmphasis;
512
513        // Greek text with emphasis
514        let greek = "Αυτό είναι ένα * τεστ με ελληνικά * και πολύ μεγάλο κείμενο που θα πρέπει να περικοπεί σωστά.";
515        let ctx = LintContext::new(greek, crate::config::MarkdownFlavor::Standard, None);
516        let result = rule.check(&ctx);
517        assert!(result.is_ok(), "Greek text should not panic");
518
519        // Chinese text with emphasis
520        let chinese = "这是一个 * 测试文本 * 包含中文字符,需要正确处理多字节边界。";
521        let ctx = LintContext::new(chinese, crate::config::MarkdownFlavor::Standard, None);
522        let result = rule.check(&ctx);
523        assert!(result.is_ok(), "Chinese text should not panic");
524
525        // Cyrillic/Russian text with emphasis
526        let cyrillic = "Это * тест с кириллицей * и очень длинным текстом для проверки обрезки.";
527        let ctx = LintContext::new(cyrillic, crate::config::MarkdownFlavor::Standard, None);
528        let result = rule.check(&ctx);
529        assert!(result.is_ok(), "Cyrillic text should not panic");
530
531        // Mixed multi-byte characters in a long emphasis span that triggers truncation
532        let mixed =
533            "日本語と * 中文と한국어が混在する非常に長いテキストでtruncate_for_displayの境界処理をテスト * します。";
534        let ctx = LintContext::new(mixed, crate::config::MarkdownFlavor::Standard, None);
535        let result = rule.check(&ctx);
536        assert!(result.is_ok(), "Mixed CJK text should not panic");
537
538        // Arabic text (right-to-left) with emphasis
539        let arabic = "هذا * اختبار بالعربية * مع نص طويل جداً لاختبار معالجة حدود الأحرف.";
540        let ctx = LintContext::new(arabic, crate::config::MarkdownFlavor::Standard, None);
541        let result = rule.check(&ctx);
542        assert!(result.is_ok(), "Arabic text should not panic");
543
544        // Emoji with emphasis
545        let emoji = "This has * 🎉 party 🎊 celebration 🥳 emojis * that use multi-byte sequences.";
546        let ctx = LintContext::new(emoji, crate::config::MarkdownFlavor::Standard, None);
547        let result = rule.check(&ctx);
548        assert!(result.is_ok(), "Emoji text should not panic");
549    }
550
551    #[test]
552    fn test_template_shortcode_syntax_not_flagged() {
553        // Test for FastAPI/MkDocs style template syntax {* ... *}
554        // These should NOT be flagged as emphasis with spaces
555        let rule = MD037NoSpaceInEmphasis;
556
557        // FastAPI style code inclusion
558        let content = "{* ../../docs_src/cookie_param_models/tutorial001.py hl[9:12,16] *}";
559        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
560        let result = rule.check(&ctx).unwrap();
561        assert!(
562            result.is_empty(),
563            "Template shortcode syntax should not be flagged. Got: {result:?}"
564        );
565
566        // Another FastAPI example
567        let content = "{* ../../docs_src/conditional_openapi/tutorial001.py hl[6,11] *}";
568        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
569        let result = rule.check(&ctx).unwrap();
570        assert!(
571            result.is_empty(),
572            "Template shortcode syntax should not be flagged. Got: {result:?}"
573        );
574
575        // Multiple shortcodes on different lines
576        let content = "# Header\n\n{* file1.py *}\n\nSome text.\n\n{* file2.py hl[1-5] *}";
577        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
578        let result = rule.check(&ctx).unwrap();
579        assert!(
580            result.is_empty(),
581            "Multiple template shortcodes should not be flagged. Got: {result:?}"
582        );
583
584        // But actual emphasis with spaces should still be flagged
585        let content = "This has * real spaced emphasis * here.";
586        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
587        let result = rule.check(&ctx).unwrap();
588        assert!(!result.is_empty(), "Real spaced emphasis should still be flagged");
589    }
590
591    #[test]
592    fn test_multiline_code_span_not_flagged() {
593        // Test for multi-line code spans - asterisks inside should not be flagged
594        // This tests the case where a code span starts on one line and ends on another
595        let rule = MD037NoSpaceInEmphasis;
596
597        // Code span spanning multiple lines with asterisks inside
598        let content = "# Test\n\naffects the structure. `1 + 0 + 0` is parsed as `(1 + 0) +\n0` while `1 + 0 * 0` is parsed as `1 + (0 * 0)`. Since the pattern";
599        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
600        let result = rule.check(&ctx).unwrap();
601        assert!(
602            result.is_empty(),
603            "Should not flag asterisks inside multi-line code spans. Got: {result:?}"
604        );
605
606        // Another multi-line code span case
607        let content2 = "Text with `code that\nspans * multiple * lines` here.";
608        let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::Standard, None);
609        let result2 = rule.check(&ctx2).unwrap();
610        assert!(
611            result2.is_empty(),
612            "Should not flag asterisks inside multi-line code spans. Got: {result2:?}"
613        );
614    }
615
616    #[test]
617    fn test_html_block_asterisks_not_flagged() {
618        let rule = MD037NoSpaceInEmphasis;
619
620        // Asterisks used as multiplication inside HTML <code> tags within an HTML table
621        let content = r#"<table>
622<tr><td>Format</td><td>Size</td></tr>
623<tr><td>BC1</td><td><code>floor((width + 3) / 4) * floor((height + 3) / 4) * 8</code></td></tr>
624<tr><td>BC2</td><td><code>floor((width + 3) / 4) * floor((height + 3) / 4) * 16</code></td></tr>
625</table>"#;
626        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
627        let result = rule.check(&ctx).unwrap();
628        assert!(
629            result.is_empty(),
630            "Should not flag asterisks inside HTML blocks. Got: {result:?}"
631        );
632
633        // Standalone HTML block with emphasis-like patterns
634        let content2 = "<div>\n<p>Value is * something * here</p>\n</div>";
635        let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::Standard, None);
636        let result2 = rule.check(&ctx2).unwrap();
637        assert!(
638            result2.is_empty(),
639            "Should not flag emphasis-like patterns inside HTML div blocks. Got: {result2:?}"
640        );
641
642        // Regular markdown with spaced emphasis should still be flagged
643        let content3 = "Regular * spaced emphasis * text\n\n<div>* not emphasis *</div>";
644        let ctx3 = LintContext::new(content3, crate::config::MarkdownFlavor::Standard, None);
645        let result3 = rule.check(&ctx3).unwrap();
646        assert_eq!(
647            result3.len(),
648            1,
649            "Should flag spaced emphasis in regular markdown but not inside HTML blocks. Got: {result3:?}"
650        );
651        assert_eq!(result3[0].line, 1, "Warning should be on line 1 (regular markdown)");
652    }
653
654    #[test]
655    fn test_mkdocs_icon_shortcode_not_flagged() {
656        // Test that MkDocs icon shortcodes with asterisks inside are not flagged
657        let rule = MD037NoSpaceInEmphasis;
658
659        // Icon shortcode syntax like :material-star: should not trigger MD037
660        // because it's valid MkDocs Material syntax
661        let content = "Click :material-check: to confirm and :fontawesome-solid-star: for favorites.";
662        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
663        let result = rule.check(&ctx).unwrap();
664        assert!(
665            result.is_empty(),
666            "Should not flag MkDocs icon shortcodes. Got: {result:?}"
667        );
668
669        // Actual emphasis with spaces should still be flagged even in MkDocs mode
670        let content2 = "This has * real spaced emphasis * but also :material-check: icon.";
671        let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::MkDocs, None);
672        let result2 = rule.check(&ctx2).unwrap();
673        assert!(
674            !result2.is_empty(),
675            "Should still flag real spaced emphasis in MkDocs mode"
676        );
677    }
678
679    #[test]
680    fn test_mkdocs_pymdown_markup_not_flagged() {
681        // Test that PyMdown extension markup is not flagged as emphasis issues
682        let rule = MD037NoSpaceInEmphasis;
683
684        // Keys notation (++ctrl+alt+delete++)
685        let content = "Press ++ctrl+c++ to copy.";
686        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
687        let result = rule.check(&ctx).unwrap();
688        assert!(
689            result.is_empty(),
690            "Should not flag PyMdown Keys notation. Got: {result:?}"
691        );
692
693        // Mark notation (==highlighted==)
694        let content2 = "This is ==highlighted text== for emphasis.";
695        let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::MkDocs, None);
696        let result2 = rule.check(&ctx2).unwrap();
697        assert!(
698            result2.is_empty(),
699            "Should not flag PyMdown Mark notation. Got: {result2:?}"
700        );
701
702        // Insert notation (^^inserted^^)
703        let content3 = "This is ^^inserted text^^ here.";
704        let ctx3 = LintContext::new(content3, crate::config::MarkdownFlavor::MkDocs, None);
705        let result3 = rule.check(&ctx3).unwrap();
706        assert!(
707            result3.is_empty(),
708            "Should not flag PyMdown Insert notation. Got: {result3:?}"
709        );
710
711        // Mixed content with real emphasis issue and PyMdown markup
712        let content4 = "Press ++ctrl++ then * spaced emphasis * here.";
713        let ctx4 = LintContext::new(content4, crate::config::MarkdownFlavor::MkDocs, None);
714        let result4 = rule.check(&ctx4).unwrap();
715        assert!(
716            !result4.is_empty(),
717            "Should still flag real spaced emphasis alongside PyMdown markup"
718        );
719    }
720
721    // ==================== Obsidian highlight tests ====================
722
723    #[test]
724    fn test_obsidian_highlight_not_flagged() {
725        // Test that Obsidian highlight syntax (==text==) is not flagged as emphasis
726        let rule = MD037NoSpaceInEmphasis;
727
728        // Simple highlight
729        let content = "This is ==highlighted text== here.";
730        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
731        let result = rule.check(&ctx).unwrap();
732        assert!(
733            result.is_empty(),
734            "Should not flag Obsidian highlight syntax. Got: {result:?}"
735        );
736    }
737
738    #[test]
739    fn test_obsidian_highlight_multiple_on_line() {
740        // Multiple highlights on one line
741        let rule = MD037NoSpaceInEmphasis;
742
743        let content = "Both ==one== and ==two== are highlighted.";
744        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
745        let result = rule.check(&ctx).unwrap();
746        assert!(
747            result.is_empty(),
748            "Should not flag multiple Obsidian highlights. Got: {result:?}"
749        );
750    }
751
752    #[test]
753    fn test_obsidian_highlight_entire_paragraph() {
754        // Entire paragraph highlighted
755        let rule = MD037NoSpaceInEmphasis;
756
757        let content = "==Entire paragraph highlighted==";
758        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
759        let result = rule.check(&ctx).unwrap();
760        assert!(
761            result.is_empty(),
762            "Should not flag entire highlighted paragraph. Got: {result:?}"
763        );
764    }
765
766    #[test]
767    fn test_obsidian_highlight_with_emphasis() {
768        // Highlights nested with other emphasis
769        let rule = MD037NoSpaceInEmphasis;
770
771        // Bold highlight
772        let content = "**==bold highlight==**";
773        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
774        let result = rule.check(&ctx).unwrap();
775        assert!(
776            result.is_empty(),
777            "Should not flag bold highlight combination. Got: {result:?}"
778        );
779
780        // Italic highlight
781        let content2 = "*==italic highlight==*";
782        let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::Obsidian, None);
783        let result2 = rule.check(&ctx2).unwrap();
784        assert!(
785            result2.is_empty(),
786            "Should not flag italic highlight combination. Got: {result2:?}"
787        );
788    }
789
790    #[test]
791    fn test_obsidian_highlight_in_lists() {
792        // Highlights in list items
793        let rule = MD037NoSpaceInEmphasis;
794
795        let content = "- Item with ==highlight== text\n- Another ==highlighted== item";
796        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
797        let result = rule.check(&ctx).unwrap();
798        assert!(
799            result.is_empty(),
800            "Should not flag highlights in list items. Got: {result:?}"
801        );
802    }
803
804    #[test]
805    fn test_obsidian_highlight_in_blockquote() {
806        // Highlights in blockquotes
807        let rule = MD037NoSpaceInEmphasis;
808
809        let content = "> This quote has ==highlighted== text.";
810        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
811        let result = rule.check(&ctx).unwrap();
812        assert!(
813            result.is_empty(),
814            "Should not flag highlights in blockquotes. Got: {result:?}"
815        );
816    }
817
818    #[test]
819    fn test_obsidian_highlight_in_tables() {
820        // Highlights in tables
821        let rule = MD037NoSpaceInEmphasis;
822
823        let content = "| Header | Column |\n|--------|--------|\n| ==highlighted== | text |";
824        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
825        let result = rule.check(&ctx).unwrap();
826        assert!(
827            result.is_empty(),
828            "Should not flag highlights in tables. Got: {result:?}"
829        );
830    }
831
832    #[test]
833    fn test_obsidian_highlight_in_code_blocks_ignored() {
834        // Highlights inside code blocks should be ignored (they're in code)
835        let rule = MD037NoSpaceInEmphasis;
836
837        let content = "```\n==not highlight in code==\n```";
838        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
839        let result = rule.check(&ctx).unwrap();
840        assert!(
841            result.is_empty(),
842            "Should ignore highlights in code blocks. Got: {result:?}"
843        );
844    }
845
846    #[test]
847    fn test_obsidian_highlight_edge_case_three_equals() {
848        // Three equals signs (===) should not be treated as highlight
849        let rule = MD037NoSpaceInEmphasis;
850
851        // This is not valid highlight syntax
852        let content = "Test === something === here";
853        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
854        let result = rule.check(&ctx).unwrap();
855        // This may or may not generate warnings depending on if it looks like emphasis
856        // The key is it shouldn't crash and should be handled gracefully
857        let _ = result;
858    }
859
860    #[test]
861    fn test_obsidian_highlight_edge_case_four_equals() {
862        // Four equals signs (====) - empty highlight
863        let rule = MD037NoSpaceInEmphasis;
864
865        let content = "Test ==== here";
866        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
867        let result = rule.check(&ctx).unwrap();
868        // Empty highlights should not match as valid highlights
869        let _ = result;
870    }
871
872    #[test]
873    fn test_obsidian_highlight_adjacent() {
874        // Adjacent highlights
875        let rule = MD037NoSpaceInEmphasis;
876
877        let content = "==one====two==";
878        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
879        let result = rule.check(&ctx).unwrap();
880        // Should handle adjacent highlights gracefully
881        let _ = result;
882    }
883
884    #[test]
885    fn test_obsidian_highlight_with_special_chars() {
886        // Highlights with special characters inside
887        let rule = MD037NoSpaceInEmphasis;
888
889        // Highlight with backtick inside
890        let content = "Test ==code: `test`== here";
891        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
892        let result = rule.check(&ctx).unwrap();
893        // Should handle gracefully
894        let _ = result;
895    }
896
897    #[test]
898    fn test_obsidian_highlight_unclosed() {
899        // Unclosed highlight should not cause issues
900        let rule = MD037NoSpaceInEmphasis;
901
902        let content = "This ==starts but never ends";
903        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
904        let result = rule.check(&ctx).unwrap();
905        // Unclosed highlight should not match anything special
906        let _ = result;
907    }
908
909    #[test]
910    fn test_obsidian_highlight_still_flags_real_emphasis_issues() {
911        // Real emphasis issues should still be flagged in Obsidian mode
912        let rule = MD037NoSpaceInEmphasis;
913
914        let content = "This has * spaced emphasis * and ==valid highlight==";
915        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
916        let result = rule.check(&ctx).unwrap();
917        assert!(
918            !result.is_empty(),
919            "Should still flag real spaced emphasis in Obsidian mode"
920        );
921        assert!(
922            result.len() == 1,
923            "Should flag exactly one issue (the spaced emphasis). Got: {result:?}"
924        );
925    }
926
927    #[test]
928    fn test_standard_flavor_does_not_recognize_highlight() {
929        // Standard flavor should NOT recognize ==highlight== as special
930        // It may or may not flag it as emphasis depending on context
931        let rule = MD037NoSpaceInEmphasis;
932
933        let content = "This is ==highlighted text== here.";
934        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
935        let result = rule.check(&ctx).unwrap();
936        // In standard flavor, == is not recognized as highlight syntax
937        // It won't be flagged as "spaces in emphasis" because == is not * or _
938        // The key is that standard flavor doesn't give special treatment to ==
939        let _ = result; // Just ensure it runs without error
940    }
941
942    #[test]
943    fn test_obsidian_highlight_mixed_with_regular_emphasis() {
944        // Mix of highlights and regular emphasis
945        let rule = MD037NoSpaceInEmphasis;
946
947        let content = "==highlighted== and *italic* and **bold** text";
948        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
949        let result = rule.check(&ctx).unwrap();
950        assert!(
951            result.is_empty(),
952            "Should not flag valid highlight and emphasis. Got: {result:?}"
953        );
954    }
955
956    #[test]
957    fn test_obsidian_highlight_unicode() {
958        // Highlights with Unicode content
959        let rule = MD037NoSpaceInEmphasis;
960
961        let content = "Text ==日本語 highlighted== here";
962        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
963        let result = rule.check(&ctx).unwrap();
964        assert!(
965            result.is_empty(),
966            "Should handle Unicode in highlights. Got: {result:?}"
967        );
968    }
969
970    #[test]
971    fn test_obsidian_highlight_with_html() {
972        // Highlights inside HTML should be handled
973        let rule = MD037NoSpaceInEmphasis;
974
975        let content = "<!-- ==not highlight in comment== --> ==actual highlight==";
976        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
977        let result = rule.check(&ctx).unwrap();
978        // The highlight in HTML comment should be ignored, only the actual highlight is processed
979        let _ = result;
980    }
981
982    #[test]
983    fn test_obsidian_inline_comment_emphasis_ignored() {
984        // Emphasis inside Obsidian comments should be ignored
985        let rule = MD037NoSpaceInEmphasis;
986
987        let content = "Visible %%* spaced emphasis *%% still visible.";
988        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
989        let result = rule.check(&ctx).unwrap();
990
991        assert!(
992            result.is_empty(),
993            "Should ignore emphasis inside Obsidian comments. Got: {result:?}"
994        );
995    }
996
997    #[test]
998    fn test_inline_html_code_not_flagged() {
999        let rule = MD037NoSpaceInEmphasis;
1000
1001        // Asterisks used as multiplication inside inline <code> tags
1002        let content = "The formula is <code>a * b * c</code> in math.";
1003        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1004        let result = rule.check(&ctx).unwrap();
1005        assert!(
1006            result.is_empty(),
1007            "Should not flag asterisks inside inline <code> tags. Got: {result:?}"
1008        );
1009
1010        // Multiple inline code-like tags on the same line
1011        let content2 = "Use <kbd>Ctrl * A</kbd> and <samp>x * y</samp> here.";
1012        let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::Standard, None);
1013        let result2 = rule.check(&ctx2).unwrap();
1014        assert!(
1015            result2.is_empty(),
1016            "Should not flag asterisks inside inline <kbd> and <samp> tags. Got: {result2:?}"
1017        );
1018
1019        // Code tag with attributes
1020        let content3 = r#"Result: <code class="math">a * b</code> done."#;
1021        let ctx3 = LintContext::new(content3, crate::config::MarkdownFlavor::Standard, None);
1022        let result3 = rule.check(&ctx3).unwrap();
1023        assert!(
1024            result3.is_empty(),
1025            "Should not flag asterisks inside <code> with attributes. Got: {result3:?}"
1026        );
1027
1028        // Real emphasis on the same line as inline code should still be flagged
1029        let content4 = "Text * spaced * and <code>a * b</code>.";
1030        let ctx4 = LintContext::new(content4, crate::config::MarkdownFlavor::Standard, None);
1031        let result4 = rule.check(&ctx4).unwrap();
1032        assert_eq!(
1033            result4.len(),
1034            1,
1035            "Should flag real spaced emphasis but not code content. Got: {result4:?}"
1036        );
1037        assert_eq!(result4[0].column, 6);
1038    }
1039
1040    /// Emphasis with spaces inside Pandoc bracketed spans should be suppressed under Pandoc,
1041    /// but regular emphasis-with-spaces outside bracketed spans must still be flagged.
1042    #[test]
1043    fn test_pandoc_bracketed_span_guard() {
1044        use crate::config::MarkdownFlavor;
1045        let rule = MD037NoSpaceInEmphasis;
1046        // Emphasis-like pattern inside a bracketed span (Pandoc construct)
1047        let content = "See [* important *]{.highlight} for details.\n";
1048        let ctx = LintContext::new(content, MarkdownFlavor::Pandoc, None);
1049        let result = rule.check(&ctx).unwrap();
1050        assert!(
1051            result.is_empty(),
1052            "MD037 should not flag emphasis-like patterns inside Pandoc bracketed spans: {result:?}"
1053        );
1054
1055        // Outside Pandoc flavor, the same text should still be flagged
1056        let ctx_std = LintContext::new(content, MarkdownFlavor::Standard, None);
1057        let result_std = rule.check(&ctx_std).unwrap();
1058        assert!(
1059            !result_std.is_empty(),
1060            "MD037 should still flag spaces in emphasis under Standard flavor: {result_std:?}"
1061        );
1062    }
1063
1064    #[test]
1065    fn test_spaced_bold_metadata_pattern_detected() {
1066        let rule = MD037NoSpaceInEmphasis;
1067
1068        // Broken bold metadata — leading space after opening **
1069        let content = "# Test\n\n** Explicit Import**: Convert markdownlint configs to rumdl format:";
1070        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1071        let result = rule.check(&ctx).unwrap();
1072        assert_eq!(
1073            result.len(),
1074            1,
1075            "Should flag '** Explicit Import**' as spaced emphasis. Got: {result:?}"
1076        );
1077        assert_eq!(result[0].line, 3);
1078
1079        // Trailing space before closing **
1080        let content2 = "# Test\n\n**trailing only **: some text";
1081        let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::Standard, None);
1082        let result2 = rule.check(&ctx2).unwrap();
1083        assert_eq!(
1084            result2.len(),
1085            1,
1086            "Should flag '**trailing only **' as spaced emphasis. Got: {result2:?}"
1087        );
1088
1089        // Both leading and trailing spaces with colon
1090        let content3 = "# Test\n\n** both spaces **: some text";
1091        let ctx3 = LintContext::new(content3, crate::config::MarkdownFlavor::Standard, None);
1092        let result3 = rule.check(&ctx3).unwrap();
1093        assert_eq!(
1094            result3.len(),
1095            1,
1096            "Should flag '** both spaces **' as spaced emphasis. Got: {result3:?}"
1097        );
1098
1099        // Valid bold metadata — should NOT be flagged
1100        let content4 = "# Test\n\n**Key**: value";
1101        let ctx4 = LintContext::new(content4, crate::config::MarkdownFlavor::Standard, None);
1102        let result4 = rule.check(&ctx4).unwrap();
1103        assert!(
1104            result4.is_empty(),
1105            "Should not flag valid bold metadata '**Key**: value'. Got: {result4:?}"
1106        );
1107    }
1108}