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 if !self.is_in_link(ctx, byte_pos)
138 && !is_in_html_comment(content, byte_pos)
139 && !is_in_math_context(ctx, byte_pos)
140 && !is_in_table_cell(ctx, line_num, warning.column)
141 && !ctx.is_in_code_span(line_num, warning.column)
142 && !is_in_inline_html_code(line, line_pos)
143 && !is_in_jsx_expression(ctx, byte_pos)
144 && !is_in_mdx_comment(ctx, byte_pos)
145 && !is_in_mkdocs_markup(line, line_pos, ctx.flavor)
146 && !ctx.is_position_in_obsidian_comment(line_num, warning.column)
147 {
148 let mut adjusted_warning = warning.clone();
149 if let Some(fix) = &mut adjusted_warning.fix {
150 let abs_start = line_start_pos + fix.range.start;
152 let abs_end = line_start_pos + fix.range.end;
153 fix.range = abs_start..abs_end;
154 }
155 filtered_warnings.push(adjusted_warning);
156 }
157 }
158 }
159 }
160
161 Ok(filtered_warnings)
162 }
163
164 fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
165 let content = ctx.content;
166 let _timer = crate::profiling::ScopedTimer::new("MD037_fix");
167
168 if !content.contains('*') && !content.contains('_') {
170 return Ok(content.to_string());
171 }
172
173 let warnings = self.check(ctx)?;
175 let warnings =
176 crate::utils::fix_utils::filter_warnings_by_inline_config(warnings, ctx.inline_config(), self.name());
177
178 if warnings.is_empty() {
180 return Ok(content.to_string());
181 }
182
183 let mut result = content.to_string();
185 let mut offset: isize = 0;
186
187 let mut sorted_warnings: Vec<_> = warnings.iter().filter(|w| w.fix.is_some()).collect();
189 sorted_warnings.sort_by_key(|w| (w.line, w.column));
190
191 for warning in sorted_warnings {
192 if let Some(fix) = &warning.fix {
193 let actual_start = (fix.range.start as isize + offset) as usize;
195 let actual_end = (fix.range.end as isize + offset) as usize;
196
197 if actual_start < result.len() && actual_end <= result.len() {
199 result.replace_range(actual_start..actual_end, &fix.replacement);
201 offset += fix.replacement.len() as isize - (fix.range.end - fix.range.start) as isize;
203 }
204 }
205 }
206
207 Ok(result)
208 }
209
210 fn category(&self) -> RuleCategory {
212 RuleCategory::Emphasis
213 }
214
215 fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
217 ctx.content.is_empty() || !ctx.likely_has_emphasis()
218 }
219
220 fn as_any(&self) -> &dyn std::any::Any {
221 self
222 }
223
224 fn from_config(_config: &crate::config::Config) -> Box<dyn Rule>
225 where
226 Self: Sized,
227 {
228 Box::new(MD037NoSpaceInEmphasis)
229 }
230}
231
232impl MD037NoSpaceInEmphasis {
233 #[inline]
235 fn check_line_for_emphasis_issues_fast(&self, line: &str, line_num: usize, warnings: &mut Vec<LintWarning>) {
236 if has_doc_patterns(line) {
238 return;
239 }
240
241 if (line.starts_with(' ') || line.starts_with('*') || line.starts_with('+') || line.starts_with('-'))
246 && UNORDERED_LIST_MARKER_REGEX.is_match(line)
247 {
248 if let Some(caps) = UNORDERED_LIST_MARKER_REGEX.captures(line)
249 && let Some(full_match) = caps.get(0)
250 {
251 let list_marker_end = full_match.end();
252 if list_marker_end < line.len() {
253 let remaining_content = &line[list_marker_end..];
254
255 self.check_line_content_for_emphasis_fast(remaining_content, line_num, list_marker_end, warnings);
258 }
259 }
260 return;
261 }
262
263 self.check_line_content_for_emphasis_fast(line, line_num, 0, warnings);
265 }
266
267 fn check_line_content_for_emphasis_fast(
269 &self,
270 content: &str,
271 line_num: usize,
272 offset: usize,
273 warnings: &mut Vec<LintWarning>,
274 ) {
275 let processed_content = replace_inline_code(content);
278 let processed_content = replace_inline_math(&processed_content);
279
280 let markers = find_emphasis_markers(&processed_content);
282 if markers.is_empty() {
283 return;
284 }
285
286 let spans = find_emphasis_spans(&processed_content, markers);
288
289 for span in spans {
291 if has_spacing_issues(&span) {
292 let full_start = span.opening.start_pos;
294 let full_end = span.closing.end_pos();
295 let full_text = &content[full_start..full_end];
296
297 if full_end < content.len() {
300 let remaining = &content[full_end..];
301 if remaining.starts_with('{') && has_span_ial(remaining.split_whitespace().next().unwrap_or("")) {
303 continue;
304 }
305 }
306
307 let marker_char = span.opening.as_char();
309 let marker_str = if span.opening.count == 1 {
310 marker_char.to_string()
311 } else {
312 format!("{marker_char}{marker_char}")
313 };
314
315 let trimmed_content = span.content.trim();
317 let fixed_text = format!("{marker_str}{trimmed_content}{marker_str}");
318
319 let display_text = truncate_for_display(full_text, 60);
321
322 let warning = LintWarning {
323 rule_name: Some(self.name().to_string()),
324 message: format!("Spaces inside emphasis markers: {display_text:?}"),
325 line: line_num,
326 column: offset + full_start + 1, end_line: line_num,
328 end_column: offset + full_end + 1,
329 severity: Severity::Warning,
330 fix: Some(Fix {
331 range: (offset + full_start)..(offset + full_end),
332 replacement: fixed_text,
333 }),
334 };
335
336 warnings.push(warning);
337 }
338 }
339 }
340}
341
342#[cfg(test)]
343mod tests {
344 use super::*;
345 use crate::lint_context::LintContext;
346
347 #[test]
348 fn test_emphasis_marker_parsing() {
349 let markers = find_emphasis_markers("This has *single* and **double** emphasis");
350 assert_eq!(markers.len(), 4); let markers = find_emphasis_markers("*start* and *end*");
353 assert_eq!(markers.len(), 4); }
355
356 #[test]
357 fn test_emphasis_span_detection() {
358 let markers = find_emphasis_markers("This has *valid* emphasis");
359 let spans = find_emphasis_spans("This has *valid* emphasis", markers);
360 assert_eq!(spans.len(), 1);
361 assert_eq!(spans[0].content, "valid");
362 assert!(!spans[0].has_leading_space);
363 assert!(!spans[0].has_trailing_space);
364
365 let markers = find_emphasis_markers("This has * invalid * emphasis");
366 let spans = find_emphasis_spans("This has * invalid * emphasis", markers);
367 assert_eq!(spans.len(), 1);
368 assert_eq!(spans[0].content, " invalid ");
369 assert!(spans[0].has_leading_space);
370 assert!(spans[0].has_trailing_space);
371 }
372
373 #[test]
374 fn test_with_document_structure() {
375 let rule = MD037NoSpaceInEmphasis;
376
377 let content = "This is *correct* emphasis and **strong emphasis**";
379 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
380 let result = rule.check(&ctx).unwrap();
381 assert!(result.is_empty(), "No warnings expected for correct emphasis");
382
383 let content = "This is * text with spaces * and more content";
385 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
386 let result = rule.check(&ctx).unwrap();
387 assert!(!result.is_empty(), "Expected warnings for spaces in emphasis");
388
389 let content = "This is *correct* emphasis\n```\n* incorrect * in code block\n```\nOutside block with * spaces in emphasis *";
391 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
392 let result = rule.check(&ctx).unwrap();
393 assert!(
394 !result.is_empty(),
395 "Expected warnings for spaces in emphasis outside code block"
396 );
397 }
398
399 #[test]
400 fn test_emphasis_in_links_not_flagged() {
401 let rule = MD037NoSpaceInEmphasis;
402 let content = r#"Check this [* spaced asterisk *](https://example.com/*test*) link.
403
404This has * real spaced emphasis * that should be flagged."#;
405 let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
406 let result = rule.check(&ctx).unwrap();
407
408 assert_eq!(
412 result.len(),
413 1,
414 "Expected exactly 1 warning, but got: {:?}",
415 result.len()
416 );
417 assert!(result[0].message.contains("Spaces inside emphasis markers"));
418 assert!(result[0].line == 3); }
421
422 #[test]
423 fn test_emphasis_in_links_vs_outside_links() {
424 let rule = MD037NoSpaceInEmphasis;
425 let content = r#"Check [* spaced *](https://example.com/*test*) and inline * real spaced * text.
426
427[* link *]: https://example.com/*path*"#;
428 let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
429 let result = rule.check(&ctx).unwrap();
430
431 assert_eq!(result.len(), 1);
433 assert!(result[0].message.contains("Spaces inside emphasis markers"));
434 assert!(result[0].line == 1);
436 }
437
438 #[test]
439 fn test_issue_49_asterisk_in_inline_code() {
440 let rule = MD037NoSpaceInEmphasis;
442
443 let content = "The `__mul__` method is needed for left-hand multiplication (`vector * 3`) and `__rmul__` is needed for right-hand multiplication (`3 * vector`).";
445 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
446 let result = rule.check(&ctx).unwrap();
447 assert!(
448 result.is_empty(),
449 "Should not flag asterisks inside inline code as emphasis (issue #49). Got: {result:?}"
450 );
451 }
452
453 #[test]
454 fn test_issue_28_inline_code_in_emphasis() {
455 let rule = MD037NoSpaceInEmphasis;
457
458 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.";
460 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
461 let result = rule.check(&ctx).unwrap();
462 assert!(
463 result.is_empty(),
464 "Should not flag inline code inside emphasis as spaces (issue #28). Got: {result:?}"
465 );
466
467 let content2 = "The **`foo` and `bar`** methods are important.";
469 let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::Standard, None);
470 let result2 = rule.check(&ctx2).unwrap();
471 assert!(
472 result2.is_empty(),
473 "Should not flag multiple inline code snippets inside emphasis. Got: {result2:?}"
474 );
475
476 let content3 = "This is __inline `code`__ with underscores.";
478 let ctx3 = LintContext::new(content3, crate::config::MarkdownFlavor::Standard, None);
479 let result3 = rule.check(&ctx3).unwrap();
480 assert!(
481 result3.is_empty(),
482 "Should not flag inline code with underscore emphasis. Got: {result3:?}"
483 );
484
485 let content4 = "This is *inline `test`* with single asterisks.";
487 let ctx4 = LintContext::new(content4, crate::config::MarkdownFlavor::Standard, None);
488 let result4 = rule.check(&ctx4).unwrap();
489 assert!(
490 result4.is_empty(),
491 "Should not flag inline code with single asterisk emphasis. Got: {result4:?}"
492 );
493
494 let content5 = "This has * real spaces * that should be flagged.";
496 let ctx5 = LintContext::new(content5, crate::config::MarkdownFlavor::Standard, None);
497 let result5 = rule.check(&ctx5).unwrap();
498 assert!(!result5.is_empty(), "Should still flag actual spaces in emphasis");
499 assert!(result5[0].message.contains("Spaces inside emphasis markers"));
500 }
501
502 #[test]
503 fn test_multibyte_utf8_no_panic() {
504 let rule = MD037NoSpaceInEmphasis;
508
509 let greek = "Αυτό είναι ένα * τεστ με ελληνικά * και πολύ μεγάλο κείμενο που θα πρέπει να περικοπεί σωστά.";
511 let ctx = LintContext::new(greek, crate::config::MarkdownFlavor::Standard, None);
512 let result = rule.check(&ctx);
513 assert!(result.is_ok(), "Greek text should not panic");
514
515 let chinese = "这是一个 * 测试文本 * 包含中文字符,需要正确处理多字节边界。";
517 let ctx = LintContext::new(chinese, crate::config::MarkdownFlavor::Standard, None);
518 let result = rule.check(&ctx);
519 assert!(result.is_ok(), "Chinese text should not panic");
520
521 let cyrillic = "Это * тест с кириллицей * и очень длинным текстом для проверки обрезки.";
523 let ctx = LintContext::new(cyrillic, crate::config::MarkdownFlavor::Standard, None);
524 let result = rule.check(&ctx);
525 assert!(result.is_ok(), "Cyrillic text should not panic");
526
527 let mixed =
529 "日本語と * 中文と한국어が混在する非常に長いテキストでtruncate_for_displayの境界処理をテスト * します。";
530 let ctx = LintContext::new(mixed, crate::config::MarkdownFlavor::Standard, None);
531 let result = rule.check(&ctx);
532 assert!(result.is_ok(), "Mixed CJK text should not panic");
533
534 let arabic = "هذا * اختبار بالعربية * مع نص طويل جداً لاختبار معالجة حدود الأحرف.";
536 let ctx = LintContext::new(arabic, crate::config::MarkdownFlavor::Standard, None);
537 let result = rule.check(&ctx);
538 assert!(result.is_ok(), "Arabic text should not panic");
539
540 let emoji = "This has * 🎉 party 🎊 celebration 🥳 emojis * that use multi-byte sequences.";
542 let ctx = LintContext::new(emoji, crate::config::MarkdownFlavor::Standard, None);
543 let result = rule.check(&ctx);
544 assert!(result.is_ok(), "Emoji text should not panic");
545 }
546
547 #[test]
548 fn test_template_shortcode_syntax_not_flagged() {
549 let rule = MD037NoSpaceInEmphasis;
552
553 let content = "{* ../../docs_src/cookie_param_models/tutorial001.py hl[9:12,16] *}";
555 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
556 let result = rule.check(&ctx).unwrap();
557 assert!(
558 result.is_empty(),
559 "Template shortcode syntax should not be flagged. Got: {result:?}"
560 );
561
562 let content = "{* ../../docs_src/conditional_openapi/tutorial001.py hl[6,11] *}";
564 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
565 let result = rule.check(&ctx).unwrap();
566 assert!(
567 result.is_empty(),
568 "Template shortcode syntax should not be flagged. Got: {result:?}"
569 );
570
571 let content = "# Header\n\n{* file1.py *}\n\nSome text.\n\n{* file2.py hl[1-5] *}";
573 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
574 let result = rule.check(&ctx).unwrap();
575 assert!(
576 result.is_empty(),
577 "Multiple template shortcodes should not be flagged. Got: {result:?}"
578 );
579
580 let content = "This has * real spaced emphasis * here.";
582 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
583 let result = rule.check(&ctx).unwrap();
584 assert!(!result.is_empty(), "Real spaced emphasis should still be flagged");
585 }
586
587 #[test]
588 fn test_multiline_code_span_not_flagged() {
589 let rule = MD037NoSpaceInEmphasis;
592
593 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";
595 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
596 let result = rule.check(&ctx).unwrap();
597 assert!(
598 result.is_empty(),
599 "Should not flag asterisks inside multi-line code spans. Got: {result:?}"
600 );
601
602 let content2 = "Text with `code that\nspans * multiple * lines` here.";
604 let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::Standard, None);
605 let result2 = rule.check(&ctx2).unwrap();
606 assert!(
607 result2.is_empty(),
608 "Should not flag asterisks inside multi-line code spans. Got: {result2:?}"
609 );
610 }
611
612 #[test]
613 fn test_html_block_asterisks_not_flagged() {
614 let rule = MD037NoSpaceInEmphasis;
615
616 let content = r#"<table>
618<tr><td>Format</td><td>Size</td></tr>
619<tr><td>BC1</td><td><code>floor((width + 3) / 4) * floor((height + 3) / 4) * 8</code></td></tr>
620<tr><td>BC2</td><td><code>floor((width + 3) / 4) * floor((height + 3) / 4) * 16</code></td></tr>
621</table>"#;
622 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
623 let result = rule.check(&ctx).unwrap();
624 assert!(
625 result.is_empty(),
626 "Should not flag asterisks inside HTML blocks. Got: {result:?}"
627 );
628
629 let content2 = "<div>\n<p>Value is * something * here</p>\n</div>";
631 let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::Standard, None);
632 let result2 = rule.check(&ctx2).unwrap();
633 assert!(
634 result2.is_empty(),
635 "Should not flag emphasis-like patterns inside HTML div blocks. Got: {result2:?}"
636 );
637
638 let content3 = "Regular * spaced emphasis * text\n\n<div>* not emphasis *</div>";
640 let ctx3 = LintContext::new(content3, crate::config::MarkdownFlavor::Standard, None);
641 let result3 = rule.check(&ctx3).unwrap();
642 assert_eq!(
643 result3.len(),
644 1,
645 "Should flag spaced emphasis in regular markdown but not inside HTML blocks. Got: {result3:?}"
646 );
647 assert_eq!(result3[0].line, 1, "Warning should be on line 1 (regular markdown)");
648 }
649
650 #[test]
651 fn test_mkdocs_icon_shortcode_not_flagged() {
652 let rule = MD037NoSpaceInEmphasis;
654
655 let content = "Click :material-check: to confirm and :fontawesome-solid-star: for favorites.";
658 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
659 let result = rule.check(&ctx).unwrap();
660 assert!(
661 result.is_empty(),
662 "Should not flag MkDocs icon shortcodes. Got: {result:?}"
663 );
664
665 let content2 = "This has * real spaced emphasis * but also :material-check: icon.";
667 let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::MkDocs, None);
668 let result2 = rule.check(&ctx2).unwrap();
669 assert!(
670 !result2.is_empty(),
671 "Should still flag real spaced emphasis in MkDocs mode"
672 );
673 }
674
675 #[test]
676 fn test_mkdocs_pymdown_markup_not_flagged() {
677 let rule = MD037NoSpaceInEmphasis;
679
680 let content = "Press ++ctrl+c++ to copy.";
682 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
683 let result = rule.check(&ctx).unwrap();
684 assert!(
685 result.is_empty(),
686 "Should not flag PyMdown Keys notation. Got: {result:?}"
687 );
688
689 let content2 = "This is ==highlighted text== for emphasis.";
691 let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::MkDocs, None);
692 let result2 = rule.check(&ctx2).unwrap();
693 assert!(
694 result2.is_empty(),
695 "Should not flag PyMdown Mark notation. Got: {result2:?}"
696 );
697
698 let content3 = "This is ^^inserted text^^ here.";
700 let ctx3 = LintContext::new(content3, crate::config::MarkdownFlavor::MkDocs, None);
701 let result3 = rule.check(&ctx3).unwrap();
702 assert!(
703 result3.is_empty(),
704 "Should not flag PyMdown Insert notation. Got: {result3:?}"
705 );
706
707 let content4 = "Press ++ctrl++ then * spaced emphasis * here.";
709 let ctx4 = LintContext::new(content4, crate::config::MarkdownFlavor::MkDocs, None);
710 let result4 = rule.check(&ctx4).unwrap();
711 assert!(
712 !result4.is_empty(),
713 "Should still flag real spaced emphasis alongside PyMdown markup"
714 );
715 }
716
717 #[test]
720 fn test_obsidian_highlight_not_flagged() {
721 let rule = MD037NoSpaceInEmphasis;
723
724 let content = "This is ==highlighted text== here.";
726 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
727 let result = rule.check(&ctx).unwrap();
728 assert!(
729 result.is_empty(),
730 "Should not flag Obsidian highlight syntax. Got: {result:?}"
731 );
732 }
733
734 #[test]
735 fn test_obsidian_highlight_multiple_on_line() {
736 let rule = MD037NoSpaceInEmphasis;
738
739 let content = "Both ==one== and ==two== are highlighted.";
740 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
741 let result = rule.check(&ctx).unwrap();
742 assert!(
743 result.is_empty(),
744 "Should not flag multiple Obsidian highlights. Got: {result:?}"
745 );
746 }
747
748 #[test]
749 fn test_obsidian_highlight_entire_paragraph() {
750 let rule = MD037NoSpaceInEmphasis;
752
753 let content = "==Entire paragraph highlighted==";
754 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
755 let result = rule.check(&ctx).unwrap();
756 assert!(
757 result.is_empty(),
758 "Should not flag entire highlighted paragraph. Got: {result:?}"
759 );
760 }
761
762 #[test]
763 fn test_obsidian_highlight_with_emphasis() {
764 let rule = MD037NoSpaceInEmphasis;
766
767 let content = "**==bold highlight==**";
769 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
770 let result = rule.check(&ctx).unwrap();
771 assert!(
772 result.is_empty(),
773 "Should not flag bold highlight combination. Got: {result:?}"
774 );
775
776 let content2 = "*==italic highlight==*";
778 let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::Obsidian, None);
779 let result2 = rule.check(&ctx2).unwrap();
780 assert!(
781 result2.is_empty(),
782 "Should not flag italic highlight combination. Got: {result2:?}"
783 );
784 }
785
786 #[test]
787 fn test_obsidian_highlight_in_lists() {
788 let rule = MD037NoSpaceInEmphasis;
790
791 let content = "- Item with ==highlight== text\n- Another ==highlighted== item";
792 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
793 let result = rule.check(&ctx).unwrap();
794 assert!(
795 result.is_empty(),
796 "Should not flag highlights in list items. Got: {result:?}"
797 );
798 }
799
800 #[test]
801 fn test_obsidian_highlight_in_blockquote() {
802 let rule = MD037NoSpaceInEmphasis;
804
805 let content = "> This quote has ==highlighted== text.";
806 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
807 let result = rule.check(&ctx).unwrap();
808 assert!(
809 result.is_empty(),
810 "Should not flag highlights in blockquotes. Got: {result:?}"
811 );
812 }
813
814 #[test]
815 fn test_obsidian_highlight_in_tables() {
816 let rule = MD037NoSpaceInEmphasis;
818
819 let content = "| Header | Column |\n|--------|--------|\n| ==highlighted== | text |";
820 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
821 let result = rule.check(&ctx).unwrap();
822 assert!(
823 result.is_empty(),
824 "Should not flag highlights in tables. Got: {result:?}"
825 );
826 }
827
828 #[test]
829 fn test_obsidian_highlight_in_code_blocks_ignored() {
830 let rule = MD037NoSpaceInEmphasis;
832
833 let content = "```\n==not highlight in code==\n```";
834 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
835 let result = rule.check(&ctx).unwrap();
836 assert!(
837 result.is_empty(),
838 "Should ignore highlights in code blocks. Got: {result:?}"
839 );
840 }
841
842 #[test]
843 fn test_obsidian_highlight_edge_case_three_equals() {
844 let rule = MD037NoSpaceInEmphasis;
846
847 let content = "Test === something === here";
849 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
850 let result = rule.check(&ctx).unwrap();
851 let _ = result;
854 }
855
856 #[test]
857 fn test_obsidian_highlight_edge_case_four_equals() {
858 let rule = MD037NoSpaceInEmphasis;
860
861 let content = "Test ==== here";
862 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
863 let result = rule.check(&ctx).unwrap();
864 let _ = result;
866 }
867
868 #[test]
869 fn test_obsidian_highlight_adjacent() {
870 let rule = MD037NoSpaceInEmphasis;
872
873 let content = "==one====two==";
874 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
875 let result = rule.check(&ctx).unwrap();
876 let _ = result;
878 }
879
880 #[test]
881 fn test_obsidian_highlight_with_special_chars() {
882 let rule = MD037NoSpaceInEmphasis;
884
885 let content = "Test ==code: `test`== here";
887 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
888 let result = rule.check(&ctx).unwrap();
889 let _ = result;
891 }
892
893 #[test]
894 fn test_obsidian_highlight_unclosed() {
895 let rule = MD037NoSpaceInEmphasis;
897
898 let content = "This ==starts but never ends";
899 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
900 let result = rule.check(&ctx).unwrap();
901 let _ = result;
903 }
904
905 #[test]
906 fn test_obsidian_highlight_still_flags_real_emphasis_issues() {
907 let rule = MD037NoSpaceInEmphasis;
909
910 let content = "This has * spaced emphasis * and ==valid highlight==";
911 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
912 let result = rule.check(&ctx).unwrap();
913 assert!(
914 !result.is_empty(),
915 "Should still flag real spaced emphasis in Obsidian mode"
916 );
917 assert!(
918 result.len() == 1,
919 "Should flag exactly one issue (the spaced emphasis). Got: {result:?}"
920 );
921 }
922
923 #[test]
924 fn test_standard_flavor_does_not_recognize_highlight() {
925 let rule = MD037NoSpaceInEmphasis;
928
929 let content = "This is ==highlighted text== here.";
930 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
931 let result = rule.check(&ctx).unwrap();
932 let _ = result; }
937
938 #[test]
939 fn test_obsidian_highlight_mixed_with_regular_emphasis() {
940 let rule = MD037NoSpaceInEmphasis;
942
943 let content = "==highlighted== and *italic* and **bold** text";
944 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
945 let result = rule.check(&ctx).unwrap();
946 assert!(
947 result.is_empty(),
948 "Should not flag valid highlight and emphasis. Got: {result:?}"
949 );
950 }
951
952 #[test]
953 fn test_obsidian_highlight_unicode() {
954 let rule = MD037NoSpaceInEmphasis;
956
957 let content = "Text ==日本語 highlighted== here";
958 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
959 let result = rule.check(&ctx).unwrap();
960 assert!(
961 result.is_empty(),
962 "Should handle Unicode in highlights. Got: {result:?}"
963 );
964 }
965
966 #[test]
967 fn test_obsidian_highlight_with_html() {
968 let rule = MD037NoSpaceInEmphasis;
970
971 let content = "<!-- ==not highlight in comment== --> ==actual highlight==";
972 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
973 let result = rule.check(&ctx).unwrap();
974 let _ = result;
976 }
977
978 #[test]
979 fn test_obsidian_inline_comment_emphasis_ignored() {
980 let rule = MD037NoSpaceInEmphasis;
982
983 let content = "Visible %%* spaced emphasis *%% still visible.";
984 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
985 let result = rule.check(&ctx).unwrap();
986
987 assert!(
988 result.is_empty(),
989 "Should ignore emphasis inside Obsidian comments. Got: {result:?}"
990 );
991 }
992
993 #[test]
994 fn test_inline_html_code_not_flagged() {
995 let rule = MD037NoSpaceInEmphasis;
996
997 let content = "The formula is <code>a * b * c</code> in math.";
999 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1000 let result = rule.check(&ctx).unwrap();
1001 assert!(
1002 result.is_empty(),
1003 "Should not flag asterisks inside inline <code> tags. Got: {result:?}"
1004 );
1005
1006 let content2 = "Use <kbd>Ctrl * A</kbd> and <samp>x * y</samp> here.";
1008 let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::Standard, None);
1009 let result2 = rule.check(&ctx2).unwrap();
1010 assert!(
1011 result2.is_empty(),
1012 "Should not flag asterisks inside inline <kbd> and <samp> tags. Got: {result2:?}"
1013 );
1014
1015 let content3 = r#"Result: <code class="math">a * b</code> done."#;
1017 let ctx3 = LintContext::new(content3, crate::config::MarkdownFlavor::Standard, None);
1018 let result3 = rule.check(&ctx3).unwrap();
1019 assert!(
1020 result3.is_empty(),
1021 "Should not flag asterisks inside <code> with attributes. Got: {result3:?}"
1022 );
1023
1024 let content4 = "Text * spaced * and <code>a * b</code>.";
1026 let ctx4 = LintContext::new(content4, crate::config::MarkdownFlavor::Standard, None);
1027 let result4 = rule.check(&ctx4).unwrap();
1028 assert_eq!(
1029 result4.len(),
1030 1,
1031 "Should flag real spaced emphasis but not code content. Got: {result4:?}"
1032 );
1033 assert_eq!(result4[0].column, 6);
1034 }
1035}