1use 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#[inline]
19fn has_spacing_issues(span: &EmphasisSpan) -> bool {
20 span.has_leading_space || span.has_trailing_space
21}
22
23#[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; let suffix_len = max_len / 2 - 2;
33
34 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#[derive(Clone)]
43pub struct MD037NoSpaceInEmphasis;
44
45impl Default for MD037NoSpaceInEmphasis {
46 fn default() -> Self {
47 Self
48 }
49}
50
51impl MD037NoSpaceInEmphasis {
52 fn is_in_link(&self, ctx: &crate::lint_context::LintContext, byte_pos: usize) -> bool {
54 for link in &ctx.links {
56 if link.byte_offset <= byte_pos && byte_pos < link.byte_end {
57 return true;
58 }
59 }
60
61 for image in &ctx.images {
63 if image.byte_offset <= byte_pos && byte_pos < image.byte_end {
64 return true;
65 }
66 }
67
68 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 if !content.contains('*') && !content.contains('_') {
88 return Ok(vec![]);
89 }
90
91 let line_index = &ctx.line_index;
93
94 let mut warnings = Vec::new();
95
96 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 if !line.content.contains('*') && !line.content.contains('_') {
112 continue;
113 }
114
115 self.check_line_for_emphasis_issues_fast(line.content, line.line_num, &mut warnings);
117 }
118
119 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 for warning in &warnings {
129 if warning.line == line_num {
130 let byte_pos = line_start_pos + (warning.column - 1);
132 let line_pos = warning.column - 1;
134
135 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 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 if !content.contains('*') && !content.contains('_') {
177 return Ok(content.to_string());
178 }
179
180 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 warnings.is_empty() {
187 return Ok(content.to_string());
188 }
189
190 let mut result = content.to_string();
192 let mut offset: isize = 0;
193
194 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 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 if actual_start < result.len() && actual_end <= result.len() {
206 result.replace_range(actual_start..actual_end, &fix.replacement);
208 offset += fix.replacement.len() as isize - (fix.range.end - fix.range.start) as isize;
210 }
211 }
212 }
213
214 Ok(result)
215 }
216
217 fn category(&self) -> RuleCategory {
219 RuleCategory::Emphasis
220 }
221
222 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 #[inline]
242 fn check_line_for_emphasis_issues_fast(&self, line: &str, line_num: usize, warnings: &mut Vec<LintWarning>) {
243 if has_doc_patterns(line) {
245 return;
246 }
247
248 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 self.check_line_content_for_emphasis_fast(remaining_content, line_num, list_marker_end, warnings);
265 }
266 }
267 return;
268 }
269
270 self.check_line_content_for_emphasis_fast(line, line_num, 0, warnings);
272 }
273
274 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 let processed_content = replace_inline_code(content);
285 let processed_content = replace_inline_math(&processed_content);
286
287 let markers = find_emphasis_markers(&processed_content);
289 if markers.is_empty() {
290 return;
291 }
292
293 let spans = find_emphasis_spans(&processed_content, &markers);
295
296 for span in spans {
298 if has_spacing_issues(&span) {
299 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 if full_end < content.len() {
307 let remaining = &content[full_end..];
308 if remaining.starts_with('{') && has_span_ial(remaining.split_whitespace().next().unwrap_or("")) {
310 continue;
311 }
312 }
313
314 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 let trimmed_content = span.content.trim();
324 let fixed_text = format!("{marker_str}{trimmed_content}{marker_str}");
325
326 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, 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); let markers = find_emphasis_markers("*start* and *end*");
357 assert_eq!(markers.len(), 4); }
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 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 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 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 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 assert!(result[0].line == 3); }
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 assert_eq!(result.len(), 1);
437 assert!(result[0].message.contains("Spaces inside emphasis markers"));
438 assert!(result[0].line == 1);
440 }
441
442 #[test]
443 fn test_issue_49_asterisk_in_inline_code() {
444 let rule = MD037NoSpaceInEmphasis;
446
447 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 let rule = MD037NoSpaceInEmphasis;
461
462 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 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 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 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 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 let rule = MD037NoSpaceInEmphasis;
512
513 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 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 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 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 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 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 let rule = MD037NoSpaceInEmphasis;
556
557 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 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 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 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 let rule = MD037NoSpaceInEmphasis;
596
597 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 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 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 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 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 let rule = MD037NoSpaceInEmphasis;
658
659 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 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 let rule = MD037NoSpaceInEmphasis;
683
684 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 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 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 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 #[test]
724 fn test_obsidian_highlight_not_flagged() {
725 let rule = MD037NoSpaceInEmphasis;
727
728 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 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 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 let rule = MD037NoSpaceInEmphasis;
770
771 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 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 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 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 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 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 let rule = MD037NoSpaceInEmphasis;
850
851 let content = "Test === something === here";
853 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
854 let result = rule.check(&ctx).unwrap();
855 let _ = result;
858 }
859
860 #[test]
861 fn test_obsidian_highlight_edge_case_four_equals() {
862 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 let _ = result;
870 }
871
872 #[test]
873 fn test_obsidian_highlight_adjacent() {
874 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 let _ = result;
882 }
883
884 #[test]
885 fn test_obsidian_highlight_with_special_chars() {
886 let rule = MD037NoSpaceInEmphasis;
888
889 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 let _ = result;
895 }
896
897 #[test]
898 fn test_obsidian_highlight_unclosed() {
899 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 let _ = result;
907 }
908
909 #[test]
910 fn test_obsidian_highlight_still_flags_real_emphasis_issues() {
911 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 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 let _ = result; }
941
942 #[test]
943 fn test_obsidian_highlight_mixed_with_regular_emphasis() {
944 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 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 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 let _ = result;
980 }
981
982 #[test]
983 fn test_obsidian_inline_comment_emphasis_ignored() {
984 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 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 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 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 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 #[test]
1043 fn test_pandoc_bracketed_span_guard() {
1044 use crate::config::MarkdownFlavor;
1045 let rule = MD037NoSpaceInEmphasis;
1046 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 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 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 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 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 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}