1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
use crate::lint_context::CodeSpan;
use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
use crate::utils::mkdocs_extensions::is_inline_hilite_content;
/// Words that mark the text between two code spans as an illustration of nested backticks
const NESTING_WORDS: [&str; 2] = ["code", "backtick"];
/// The characters that end a line: a newline, a carriage return, or the pair.
///
/// A lone carriage return is a line ending of its own, so a guard that asks
/// whether whitespace crosses a line has to recognize both characters or it
/// speaks only for documents written on one of the two conventions.
const LINE_ENDINGS: [char; 2] = ['\n', '\r'];
/// State carried across the code spans of one document by the nested-backtick check
#[derive(Default)]
struct NestedBacktickState {
/// For each code span, the first and last index of the spans starting on its line
runs: Option<Vec<(usize, usize)>>,
/// The line most recently examined
line: Option<LineNesting>,
}
/// Where the nesting words sit on one line, relative to the spans that open and close it.
///
/// A span is compared against the farthest other span on each side, because the
/// text examined between two spans grows with the distance between them and a
/// shorter stretch is contained in a longer one. Both stretches therefore have
/// one bound fixed for the whole line, which is what these two offsets record.
struct LineNesting {
/// Line number (1-indexed) this data describes
line: usize,
/// Byte offset of each character, empty while every offset equals its index
char_offsets: Vec<usize>,
/// Length of the line in bytes
len: usize,
/// Smallest end offset among the nesting words that start after the span opening the line
word_end_after_first: Option<usize>,
/// Largest start offset among the nesting words that end before the span closing the line
word_start_before_last: Option<usize>,
}
impl LineNesting {
fn new(line_content: &str, line: usize, first: &CodeSpan, last: &CodeSpan) -> Self {
let char_offsets = if line_content.is_ascii() {
Vec::new()
} else {
line_content.char_indices().map(|(offset, _)| offset).collect()
};
let mut nesting = Self {
line,
char_offsets,
len: line_content.len(),
word_end_after_first: None,
word_start_before_last: None,
};
let after_first = nesting.char_offset(first.end_col);
let before_last = nesting.char_offset(last.start_col).unwrap_or(nesting.len);
for word in NESTING_WORDS {
for (start, matched) in line_content.match_indices(word) {
let end = start + matched.len();
if after_first.is_some_and(|bound| start >= bound) {
nesting.word_end_after_first = Some(nesting.word_end_after_first.map_or(end, |e| e.min(end)));
}
if end <= before_last {
nesting.word_start_before_last =
Some(nesting.word_start_before_last.map_or(start, |s| s.max(start)));
}
}
}
nesting
}
/// Byte offset of the character at `char_index`, or `None` past the end of the line
fn char_offset(&self, char_index: usize) -> Option<usize> {
if self.char_offsets.is_empty() {
(char_index < self.len).then_some(char_index)
} else {
self.char_offsets.get(char_index).copied()
}
}
/// Whether a nesting word sits between the span opening the line and this span
fn names_backticks_before(&self, span: &CodeSpan) -> bool {
let Some(word_end) = self.word_end_after_first else {
return false;
};
word_end <= self.char_offset(span.start_col).unwrap_or(self.len)
}
/// Whether a nesting word sits between this span and the span closing the line
fn names_backticks_after(&self, span: &CodeSpan, last: &CodeSpan) -> bool {
let Some(word_start) = self.word_start_before_last else {
return false;
};
let Some(span_end) = self.char_offset(span.end_col.min(last.end_col)) else {
return false;
};
word_start >= span_end
}
/// Whether a nesting word sits in the text between two code spans
fn names_backticks_between(&self, line_content: &str, current_span: &CodeSpan, other_span: &CodeSpan) -> bool {
let start_char = current_span.end_col.min(other_span.end_col);
let end_char = current_span.start_col.max(other_span.start_col);
if start_char >= end_char {
return false;
}
// Convert character positions to byte offsets for string slicing
let Some(start_byte) = self.char_offset(start_char) else {
return false;
};
let end_byte = self.char_offset(end_char).unwrap_or(self.len);
if start_byte >= end_byte {
return false;
}
let between = &line_content[start_byte..end_byte];
NESTING_WORDS.iter().any(|word| between.contains(word))
}
}
/// Rule MD038: No space inside code span markers
///
/// See [docs/md038.md](../../docs/md038.md) for full documentation, configuration, and examples.
///
/// MD038: Spaces inside code span elements
///
/// This rule is triggered when there are spaces inside code span elements.
///
/// For example:
///
/// ``` markdown
/// ` some text`
/// `some text `
/// ` some text `
/// ```
///
/// To fix this issue, remove the leading and trailing spaces within the code span markers:
///
/// ``` markdown
/// `some text`
/// ```
///
/// Note: Code spans containing backticks (e.g., `` `backticks` inside ``) are not flagged
/// to avoid breaking nested backtick structures used to display backticks in documentation.
#[derive(Debug, Clone, Default)]
pub struct MD038NoSpaceInCode {
pub enabled: bool,
}
impl MD038NoSpaceInCode {
pub fn new() -> Self {
Self { enabled: true }
}
/// Check if a code span is part of Hugo template syntax (e.g., {{raw `...`}})
///
/// Hugo static site generator uses backticks as part of template delimiters,
/// not markdown code spans. This function detects common Hugo shortcode patterns:
/// - {{raw `...`}} - Raw HTML shortcode
/// - {{< `...` >}} - Partial shortcode
/// - {{% `...` %}} - Shortcode with percent delimiters
/// - {{ `...` }} - Generic shortcode
///
/// The detection is conservative to avoid false positives:
/// - Requires opening {{ pattern before the backtick
/// - Requires closing }} after the code span
/// - Handles multi-line templates correctly
///
/// Returns true if the code span is part of Hugo template syntax and should be skipped.
fn is_hugo_template_syntax(&self, ctx: &crate::lint_context::LintContext, code_span: &CodeSpan) -> bool {
let start_line_idx = code_span.line.saturating_sub(1);
let Some(start_line) = ctx.lines.get(start_line_idx) else {
return false;
};
let start_line_content = start_line.content(ctx.content);
// Byte position of the opening backtick within its own line
let Some(span_start) = code_span
.byte_offset
.checked_sub(start_line.byte_offset)
.filter(|offset| *offset <= start_line_content.len())
else {
return false;
};
// Check if there's Hugo template syntax before the code span on the same line
// Pattern: {{raw ` or {{< ` or similar Hugo template patterns
// The code span starts at the backtick, so we need to check what's before it
// Every pattern below is at least the 3 bytes of "{{ " wide
if span_start >= 3 {
// Look backwards for Hugo template patterns
// Get the content up to (but not including) the backtick
let before_span = &start_line_content[..span_start];
// Check for Hugo template patterns: {{raw `, {{< `, {{% `, etc.
// The backtick is at span_start, so we check if the content before it
// ends with the Hugo pattern (without the backtick), and verify the next char is a backtick
let char_at_span_start = start_line_content[span_start..].chars().next().unwrap_or(' ');
// Match Hugo shortcode patterns:
// - {{raw ` - Raw HTML shortcode
// - {{< ` - Partial shortcode (may have parameters before backtick)
// - {{% ` - Shortcode with percent delimiters
// - {{ ` - Generic shortcode
// Also handle cases with parameters: {{< highlight go ` or {{< code ` etc.
// We check if the pattern starts with {{ and contains the shortcode type before the backtick
let is_hugo_start =
// Exact match: {{raw `
(before_span.ends_with("{{raw ") && char_at_span_start == '`')
// Partial shortcode: {{< ` or {{< name ` or {{< name param ` etc.
|| (before_span.starts_with("{{<") && before_span.ends_with(' ') && char_at_span_start == '`')
// Percent shortcode: {{% `
|| (before_span.ends_with("{{% ") && char_at_span_start == '`')
// Generic shortcode: {{ `
|| (before_span.ends_with("{{ ") && char_at_span_start == '`');
if is_hugo_start {
// Check if there's a closing }} after the code span
// First check the end line of the code span
let end_line_idx = code_span.end_line.saturating_sub(1);
if let Some(end_line) = ctx.lines.get(end_line_idx) {
let end_line_content = end_line.content(ctx.content);
let span_end = code_span
.byte_end
.checked_sub(end_line.byte_offset)
.unwrap_or(end_line_content.len())
.min(end_line_content.len());
// Check for closing }} on the same line as the end of the code span
if span_end < end_line_content.len() {
let after_span = &end_line_content[span_end..];
if after_span.trim_start().starts_with("}}") {
return true;
}
}
// Also check the next line for closing }}
let next_line_idx = code_span.end_line;
if next_line_idx < ctx.lines.len() {
let next_line = ctx.lines[next_line_idx].content(ctx.content);
if next_line.trim_start().starts_with("}}") {
return true;
}
}
}
}
}
false
}
/// Check if content is an Obsidian Dataview inline query
///
/// Dataview plugin uses two inline query syntaxes:
/// - Inline DQL: `= expression` - Starts with "= "
/// - Inline DataviewJS: `$= expression` - Starts with "$= "
///
/// Examples:
/// - `= this.file.name` - Get current file name
/// - `= date(today)` - Get today's date
/// - `= [[Page]].field` - Access field from another page
/// - `$= dv.current().file.mtime` - DataviewJS expression
/// - `$= dv.pages().length` - Count pages
///
/// These patterns legitimately start with a space after = or $=,
/// so they should not trigger MD038.
fn is_dataview_expression(content: &str) -> bool {
// Inline DQL: starts with "= " (equals followed by space)
// Inline DataviewJS: starts with "$= " (dollar-equals followed by space)
content.starts_with("= ") || content.starts_with("$= ")
}
/// Group code spans by the line they start on.
///
/// Entry `i` holds the first and last index of the run of spans starting on
/// the same line as span `i`. Spans arrive sorted by byte offset, so the
/// spans of one line are contiguous.
fn same_line_runs(code_spans: &[CodeSpan]) -> Vec<(usize, usize)> {
let mut runs = vec![(0, 0); code_spans.len()];
let mut run_start = 0;
for index in 1..=code_spans.len() {
if index == code_spans.len() || code_spans[index].line != code_spans[run_start].line {
runs[run_start..index].fill((run_start, index - 1));
run_start = index;
}
}
runs
}
/// Check if a code span is likely part of a nested backtick structure
fn is_likely_nested_backticks(
&self,
ctx: &crate::lint_context::LintContext,
code_spans: &[CodeSpan],
span_index: usize,
state: &mut NestedBacktickState,
) -> bool {
// If there are multiple code spans on the same line, and there's text
// between them that contains "code" or other indicators, it's likely nested
let current_span = &code_spans[span_index];
let (first, last) = {
let runs = state.runs.get_or_insert_with(|| Self::same_line_runs(code_spans));
runs[span_index]
};
// Look for other code spans on the same line
if first == last {
return false;
}
// Check if there's content between spans that might indicate nesting
// Get the line content
let line_idx = current_span.line - 1; // Convert to 0-based
if line_idx >= ctx.lines.len() {
return false;
}
let line_content = ctx.lines[line_idx].content(ctx.content);
let line = match &mut state.line {
Some(cached) if cached.line == current_span.line => cached,
slot => slot.insert(LineNesting::new(
line_content,
current_span.line,
&code_spans[first],
&code_spans[last],
)),
};
// A span continuing onto another line reports an end column belonging to
// that other line, which the bounds below assume stays on this one. Only
// the span closing a line can do that, so it is measured directly against
// the span opening the line, the farthest one from it.
if current_span.end_line != current_span.line {
return line.names_backticks_between(line_content, current_span, &code_spans[first]);
}
line.names_backticks_before(current_span) || line.names_backticks_after(current_span, &code_spans[last])
}
/// Check for a CommonMark parse shape produced by nested single backticks.
///
/// In text like `` `{ outer `inner` outer }` ``, CommonMark sees two adjacent
/// code spans rather than one nested span. Removing the apparent leading or
/// trailing space from those parsed spans moves prose across the inner
/// backticks and changes the rendered text.
fn has_attached_nested_backtick_boundary(
&self,
ctx: &crate::lint_context::LintContext,
code_span: &crate::lint_context::CodeSpan,
) -> bool {
let content = code_span.content.as_str();
let next_char = ctx.content[code_span.byte_end..].chars().next();
let prev_char = ctx.content[..code_span.byte_offset].chars().next_back();
// A Pandoc inline code attribute (`code`{.lang}) attached to the closing
// backtick is structural syntax, not a nested-backtick illustration.
// It must not silence inner-whitespace violations on the code span.
let trailing_neighbor_is_pandoc_attr =
ctx.flavor.is_pandoc_compatible() && ctx.is_in_inline_code_attr(code_span.byte_end);
(content.ends_with(char::is_whitespace)
&& next_char.is_some_and(|c| !c.is_whitespace())
&& !trailing_neighbor_is_pandoc_attr)
|| (content.starts_with(char::is_whitespace) && prev_char.is_some_and(|c| !c.is_whitespace()))
}
/// Whether everything between the start of `offset`'s line and `offset` is layout:
/// indentation and blockquote markers.
///
/// Whitespace that follows nothing but layout is itself the indentation of that
/// line. It positions the line within the document - inside a blockquote, inside
/// an indented block - rather than separating anything from a backtick, so it is
/// not this rule's to remove.
fn only_layout_before(content: &str, offset: usize) -> bool {
let line_start = content[..offset].rfind(LINE_ENDINGS).map_or(0, |i| i + 1);
content[line_start..offset]
.chars()
.all(|c| c.is_whitespace() || c == '>')
}
}
impl Rule for MD038NoSpaceInCode {
fn name(&self) -> &'static str {
"MD038"
}
fn description(&self) -> &'static str {
"Spaces inside code span elements"
}
fn category(&self) -> RuleCategory {
RuleCategory::Other
}
fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
if !self.enabled {
return Ok(vec![]);
}
let mut warnings = Vec::new();
// Use centralized code spans from LintContext
let code_spans = ctx.code_spans();
// Built on the first span that reaches the nested-backtick check, which most
// documents never do
let mut nesting = NestedBacktickState::default();
for (i, code_span) in code_spans.iter().enumerate() {
if let Some(line_info) = ctx.lines.get(code_span.line - 1) {
// Skip code spans that are inside fenced/indented code blocks, front-matter,
// math blocks, HTML blocks, HTML comments, mkdocstrings, or ESM blocks.
if line_info.in_code_block
|| line_info.in_front_matter
|| line_info.in_math_block
|| line_info.in_html_block
|| line_info.in_html_comment
|| line_info.in_mkdocstrings
|| line_info.in_esm_block
{
continue;
}
// Skip multi-line code spans inside MkDocs containers where pulldown-cmark
// misinterprets indented fenced code block markers as code spans.
// Covers admonitions, tabs, HTML markdown blocks, and PyMdown blocks.
if (line_info.in_mkdocs_container() || line_info.in_pymdown_block) && code_span.content.contains('\n') {
continue;
}
}
let code_content = &code_span.content;
// Skip empty code spans
if code_content.is_empty() {
continue;
}
// Early check: if no leading/trailing whitespace, skip
let has_leading_space = code_content.chars().next().is_some_and(char::is_whitespace);
let has_trailing_space = code_content.chars().last().is_some_and(char::is_whitespace);
if !has_leading_space && !has_trailing_space {
continue;
}
let trimmed = code_content.trim();
// CommonMark keeps a code span that consists entirely of whitespace
// verbatim: the single-space stripping rule only applies when the
// content is NOT all spaces. Flagging it would "fix" it to an empty
// code span (``), which reopens an unterminated span and changes the
// document's meaning. See https://spec.commonmark.org/0.31.2/#code-spans
if trimmed.is_empty() {
continue;
}
// Check if there are leading or trailing spaces
if code_content != trimmed {
// CommonMark behavior: if there is exactly ONE space at start AND ONE at end,
// and the content after trimming is non-empty, those spaces are stripped.
// We should NOT flag this case since the spaces are intentionally stripped.
// See: https://spec.commonmark.org/0.31.2/#code-spans
//
// Examples:
// ` text ` → "text" (spaces stripped, NOT flagged)
// ` text ` → " text" (extra leading space remains, FLAGGED)
// ` text ` → "text " (extra trailing space remains, FLAGGED)
// ` text` → " text" (no trailing space to balance, FLAGGED)
// `text ` → "text " (no leading space to balance, FLAGGED)
// (trimmed is guaranteed non-empty here: all-whitespace spans
// were already skipped above.)
if has_leading_space && has_trailing_space {
let leading_spaces = code_content.len() - code_content.trim_start().len();
let trailing_spaces = code_content.len() - code_content.trim_end().len();
// Exactly one space on each side - CommonMark strips them
if leading_spaces == 1 && trailing_spaces == 1 {
continue;
}
}
// This rule edits spacing, not structure, which puts two whitespace
// runs out of reach. Both only arise when a span crosses a line break:
//
// - a run holding the line ending itself. A line ending inside a code
// span is one of the spaces the rule removes, so the only way to
// remove it is to delete the line break and join two lines.
// - a run that is the indentation of the line the span ends on. It
// positions that line inside a blockquote or an indented block, so
// removing it moves the line's content out of its container.
//
// Where a parser reads indented block content as one long code span -
// a fenced block that cannot interrupt the paragraph above it, a
// Pandoc or Quarto div body - those two runs are exactly the source it
// would otherwise rewrite.
//
// Only the closing end needs the second test: a leading run that
// leaves its line necessarily swallows the line ending and is refused
// by the first, and an opening backtick always precedes its own run.
//
// Either one takes the whole span out of this rule's reach rather than
// just its own end, because the ends are not independent: CommonMark
// removes one space from each end only when BOTH ends have one, so
// trimming the reachable end alone moves the rendered space to the
// other end instead of removing it. `` ` a<line ending>` `` renders as
// " a" and would become `` `a<line ending>` ``, which renders as "a ",
// and the rule reads that as clean and never mentions it again. A span
// this rule cannot finish is one it should not start.
let leading = &code_content[..code_content.len() - code_content.trim_start().len()];
let trailing = &code_content[code_content.trim_end().len()..];
// Where that trailing run starts in the document. Taken from the source
// rather than from the span's length so it stays a character boundary.
let trailing_run_start = ctx.content[..code_span.byte_end - code_span.backtick_count]
.trim_end()
.len();
// The indentation question is asked only of a run that exists. With no
// trailing whitespace at all, a span ending on a line that is otherwise
// nothing but layout would answer yes and take the leading run - which
// may be ordinary spacing - out of reach with it.
let leading_is_structural = leading.contains(LINE_ENDINGS);
let trailing_is_structural = !trailing.is_empty()
&& (trailing.contains(LINE_ENDINGS) || Self::only_layout_before(ctx.content, trailing_run_start));
if leading_is_structural || trailing_is_structural {
continue;
}
// Check if the content itself contains backticks - if so, skip to avoid
// breaking nested backtick structures
if trimmed.contains('`') {
continue;
}
// Skip inline R code in Quarto/RMarkdown: `r expression`
// This is RMarkdown/Quarto-specific syntax for inline R evaluation.
// Pandoc itself has no concept of executing inline R expressions,
// so the exemption is intentionally Quarto-only.
if ctx.flavor == crate::config::MarkdownFlavor::Quarto
&& trimmed.starts_with('r')
&& trimmed.len() > 1
&& trimmed.chars().nth(1).is_some_and(char::is_whitespace)
{
continue;
}
// Skip InlineHilite syntax in MkDocs: `#!python code`
// The space after the language specifier is legitimate
if ctx.flavor == crate::config::MarkdownFlavor::MkDocs && is_inline_hilite_content(trimmed) {
continue;
}
// Skip Dataview inline queries in Obsidian: `= expression` or `$= expression`
// Dataview plugin uses these patterns for inline DQL and DataviewJS queries.
// The space after = or $= is part of the syntax, not a spacing error.
if ctx.flavor == crate::config::MarkdownFlavor::Obsidian && Self::is_dataview_expression(code_content) {
continue;
}
// Skip MyST role syntax: {role}`content` — the backtick content is part
// of the role's semantics, not a regular code span.
if ctx.flavor.supports_myst_roles() && ctx.is_in_myst_role(code_span.byte_offset) {
continue;
}
// Check if this is part of Hugo template syntax (e.g., {{raw `...`}})
// Hugo uses backticks as part of template delimiters, not markdown code spans
if self.is_hugo_template_syntax(ctx, code_span) {
continue;
}
// Anywhere inside a well-formed shortcode tag, whatever precedes the
// backtick. The check above only recognizes a backtick attached to
// the opening delimiter, so it misses `{{% note `code ` %}}`.
//
// Neither check subsumes the other, so both stay. A shortcode range
// ends at `%}}` or `>}}`, which leaves the bare-`}}` template forms
// (`{{raw `a `}}`, `{{% `a ` }}`) to the check above;
// `test_hugo_template_after_multibyte_text` is the test that fails
// if it is removed as redundant.
if ctx.is_in_shortcode(code_span.byte_offset) {
continue;
}
// Check if this might be part of a nested backtick structure
// by looking for other code spans nearby that might indicate nesting
if self.is_likely_nested_backticks(ctx, &code_spans, i, &mut nesting) {
continue;
}
if self.has_attached_nested_backtick_boundary(ctx, code_span) {
continue;
}
warnings.push(LintWarning {
rule_name: Some(self.name().to_string()),
line: code_span.line,
column: code_span.start_col + 1, // Convert to 1-indexed
// end_col is a column of the line the span ends on, which for a
// span crossing a line break is not the line it starts on.
end_line: code_span.end_line,
// end_col counts the characters before the span's end, so one past
// it is the 1-indexed column the diagnostic range excludes. Without
// the conversion the range stops before the closing backtick.
end_column: code_span.end_col + 1,
message: "Spaces inside code span elements".to_string(),
severity: Severity::Warning,
fix: Some(Fix::new(
code_span.byte_offset..code_span.byte_end,
format!(
"{}{}{}",
"`".repeat(code_span.backtick_count),
trimmed,
"`".repeat(code_span.backtick_count)
),
)),
});
}
}
Ok(warnings)
}
fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
let content = ctx.content;
if !self.enabled {
return Ok(content.to_string());
}
// Early return if no backticks in content
if !content.contains('`') {
return Ok(content.to_string());
}
// Get warnings to identify what needs to be fixed
let warnings = self.check(ctx)?;
let warnings =
crate::utils::fix_utils::filter_warnings_by_inline_config(warnings, ctx.inline_config(), self.name());
if warnings.is_empty() {
return Ok(content.to_string());
}
// Collect all fixes and sort by position (reverse order to avoid position shifts)
let mut fixes: Vec<(std::ops::Range<usize>, String)> = warnings
.into_iter()
.filter_map(|w| w.fix.map(|f| (f.range, f.replacement)))
.collect();
fixes.sort_by_key(|(range, _)| std::cmp::Reverse(range.start));
// Apply fixes - only allocate string when we have fixes to apply
let mut result = content.to_string();
for (range, replacement) in fixes {
result.replace_range(range, &replacement);
}
Ok(result)
}
/// Check if content is likely to have code spans
fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
!ctx.likely_has_code()
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn from_config(_config: &crate::config::Config) -> Box<dyn Rule>
where
Self: Sized,
{
Box::new(MD038NoSpaceInCode { enabled: true })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_md038_readme_false_positives() {
// These are the exact cases from README.md that are incorrectly flagged
let rule = MD038NoSpaceInCode::new();
let valid_cases = vec![
"3. `pyproject.toml` (must contain `[tool.rumdl]` section)",
"#### Effective Configuration (`rumdl config`)",
"- Blue: `.rumdl.toml`",
"### Defaults Only (`rumdl config --defaults`)",
];
for case in valid_cases {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Should not flag code spans without leading/trailing spaces: '{}'. Got {} warnings",
case,
result.len()
);
}
}
#[test]
fn test_md038_front_matter() {
let rule = MD038NoSpaceInCode::new();
let content = "---\ntitle: \"` code `\"\n---\n` code `";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
// Should only flag the one in the body (line 4), not the one in front-matter (line 2)
assert_eq!(result.len(), 1);
assert_eq!(result[0].line, 4);
}
#[test]
fn test_md038_math_block() {
let rule = MD038NoSpaceInCode::new();
let content = "$$\n` code `\n$$\n` code `";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
// Should only flag the one in the body (line 4), not the one in math block (line 2)
assert_eq!(result.len(), 1);
assert_eq!(result[0].line, 4);
}
#[test]
fn test_md038_html_comment() {
let rule = MD038NoSpaceInCode::new();
let content = "<!--\n` code `\n-->\n` code `";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
// Should only flag the one in the body (line 4), not the one in HTML comment (line 2)
assert_eq!(result.len(), 1);
assert_eq!(result[0].line, 4);
}
#[test]
fn test_md038_valid() {
let rule = MD038NoSpaceInCode::new();
let valid_cases = vec![
"This is `code` in a sentence.",
"This is a `longer code span` in a sentence.",
"This is `code with internal spaces` which is fine.",
"Code span at `end of line`",
"`Start of line` code span",
"Multiple `code spans` in `one line` are fine",
"Code span with `symbols: !@#$%^&*()`",
"Empty code span `` is technically valid",
];
for case in valid_cases {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(result.is_empty(), "Valid case should not have warnings: {case}");
}
}
#[test]
fn test_md038_invalid() {
let rule = MD038NoSpaceInCode::new();
// Flag cases that violate CommonMark:
// - Space only at start (no matching end space)
// - Space only at end (no matching start space)
// - Multiple spaces at start or end (extra space will remain after CommonMark stripping)
let invalid_cases = vec![
// Unbalanced: only leading space
"This is ` code` with leading space.",
// Unbalanced: only trailing space
"This is `code ` with trailing space.",
// Multiple leading spaces (one will remain after CommonMark strips one)
"This is ` code ` with double leading space.",
// Multiple trailing spaces (one will remain after CommonMark strips one)
"This is ` code ` with double trailing space.",
// Multiple spaces both sides
"This is ` code ` with double spaces both sides.",
];
for case in invalid_cases {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(!result.is_empty(), "Invalid case should have warnings: {case}");
}
}
#[test]
fn test_md038_valid_commonmark_stripping() {
let rule = MD038NoSpaceInCode::new();
// These cases have exactly ONE space at start AND ONE at end.
// CommonMark strips both, so these should NOT be flagged.
// See: https://spec.commonmark.org/0.31.2/#code-spans
let valid_cases = vec![
"Type ` y ` to confirm.",
"Use ` git commit -m \"message\" ` to commit.",
"The variable ` $HOME ` contains home path.",
"The pattern ` *.txt ` matches text files.",
"This is ` random word ` with unnecessary spaces.",
"Text with ` plain text ` is valid.",
"Code with ` just code ` here.",
"Multiple ` word ` spans with ` text ` in one line.",
"This is ` code ` with both leading and trailing single space.",
"Use ` - ` as separator.",
];
for case in valid_cases {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Single space on each side should not be flagged (CommonMark strips them): {case}"
);
}
}
#[test]
fn test_md038_whitespace_only_span_not_flagged() {
// CommonMark keeps a code span made up entirely of spaces verbatim: the
// single-space stripping rule only applies when the content is NOT all
// spaces (https://spec.commonmark.org/0.31.2/#code-spans). Flagging it
// would "fix" it to an empty code span (``), which reopens an
// unterminated span and changes the document's meaning.
let rule = MD038NoSpaceInCode::new();
let whitespace_only_cases = vec![
"A single-space span `\u{0020}` is intentional.",
"A two-space span `\u{0020}\u{0020}` is intentional.",
"A three-space span `\u{0020}\u{0020}\u{0020}` is intentional.",
"A tab span `\t` is intentional.",
"Just the span: ` `",
];
for case in whitespace_only_cases {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Whitespace-only code span should not be flagged (kept verbatim per CommonMark): {case}"
);
}
}
#[test]
fn test_md038_whitespace_only_span_fix_preserves_verbatim() {
// The fix must never collapse a whitespace-only span to `` (which is
// invalid Markdown). Each input is left untouched.
let rule = MD038NoSpaceInCode::new();
let unchanged_cases = vec![
"A single-space span `\u{0020}` is intentional.",
"A two-space span `\u{0020}\u{0020}` is intentional.",
"Just the span: ` `",
];
for case in unchanged_cases {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
let result = rule.fix(&ctx).unwrap();
assert_eq!(
result, case,
"Whitespace-only code span must be left verbatim by fix, not collapsed to ``"
);
}
}
#[test]
fn test_md038_fix() {
let rule = MD038NoSpaceInCode::new();
// Only cases that violate CommonMark should be fixed
let test_cases = vec![
// Unbalanced: only leading space - should be fixed
(
"This is ` code` with leading space.",
"This is `code` with leading space.",
),
// Unbalanced: only trailing space - should be fixed
(
"This is `code ` with trailing space.",
"This is `code` with trailing space.",
),
// Single space on both sides - NOT fixed (valid per CommonMark)
(
"This is ` code ` with both spaces.",
"This is ` code ` with both spaces.", // unchanged
),
// Double leading space - should be fixed
(
"This is ` code ` with double leading space.",
"This is `code` with double leading space.",
),
// Mixed: one valid (single space both), one invalid (trailing only)
(
"Multiple ` code ` and `spans ` to fix.",
"Multiple ` code ` and `spans` to fix.", // only spans is fixed
),
];
for (input, expected) in test_cases {
let ctx = crate::lint_context::LintContext::new(input, crate::config::MarkdownFlavor::Standard, None);
let result = rule.fix(&ctx).unwrap();
assert_eq!(result, expected, "Fix did not produce expected output for: {input}");
}
}
#[test]
fn test_check_invalid_leading_space() {
let rule = MD038NoSpaceInCode::new();
let input = "This has a ` leading space` in code";
let ctx = crate::lint_context::LintContext::new(input, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1);
assert_eq!(result[0].line, 1);
assert!(result[0].fix.is_some());
}
#[test]
fn test_code_span_parsing_nested_backticks() {
let content = "Code with ` nested `code` example ` should preserve backticks";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
println!("Content: {content}");
println!("Code spans found:");
let code_spans = ctx.code_spans();
for (i, span) in code_spans.iter().enumerate() {
println!(
" Span {}: line={}, col={}-{}, backticks={}, content='{}'",
i, span.line, span.start_col, span.end_col, span.backtick_count, span.content
);
}
// This test reveals the issue - we're getting multiple separate code spans instead of one
assert_eq!(code_spans.len(), 2, "Should parse as 2 code spans");
}
#[test]
fn test_nested_backtick_detection() {
let rule = MD038NoSpaceInCode::new();
// Test that code spans with backticks are skipped
let content = "Code with `` `backticks` inside `` should not be flagged";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(result.is_empty(), "Code spans with backticks should be skipped");
}
#[test]
fn test_quarto_inline_r_code() {
// Test that Quarto-specific R code exception works
let rule = MD038NoSpaceInCode::new();
// Test inline R code - should NOT trigger warning in Quarto flavor
// The key pattern is "r " followed by code
let content = r#"The result is `r nchar("test")` which equals 4."#;
// Quarto flavor should allow R code
let ctx_quarto = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Quarto, None);
let result_quarto = rule.check(&ctx_quarto).unwrap();
assert!(
result_quarto.is_empty(),
"Quarto inline R code should not trigger warnings. Got {} warnings",
result_quarto.len()
);
// Test that invalid code spans (not matching CommonMark stripping) still get flagged in Quarto
// Use only trailing space - this violates CommonMark (no balanced stripping)
let content_other = "This has `plain text ` with trailing space.";
let ctx_other =
crate::lint_context::LintContext::new(content_other, crate::config::MarkdownFlavor::Quarto, None);
let result_other = rule.check(&ctx_other).unwrap();
assert_eq!(
result_other.len(),
1,
"Quarto should still flag non-R code spans with improper spaces"
);
}
/// Comprehensive tests for Hugo template syntax detection
///
/// These tests ensure MD038 correctly handles Hugo template syntax patterns
/// without false positives, while maintaining correct detection of actual
/// code span spacing issues.
#[test]
fn test_hugo_template_syntax_comprehensive() {
let rule = MD038NoSpaceInCode::new();
// ===== VALID HUGO TEMPLATE SYNTAX (Should NOT trigger warnings) =====
// Basic Hugo shortcode patterns
let valid_hugo_cases = vec![
// Raw HTML shortcode
(
"{{raw `\n\tgo list -f '{{.DefaultGODEBUG}}' my/main/package\n`}}",
"Multi-line raw shortcode",
),
(
"Some text {{raw ` code `}} more text",
"Inline raw shortcode with spaces",
),
("{{raw `code`}}", "Raw shortcode without spaces"),
// Partial shortcode
("{{< ` code ` >}}", "Partial shortcode with spaces"),
("{{< `code` >}}", "Partial shortcode without spaces"),
// Shortcode with percent
("{{% ` code ` %}}", "Percent shortcode with spaces"),
("{{% `code` %}}", "Percent shortcode without spaces"),
// Generic shortcode
("{{ ` code ` }}", "Generic shortcode with spaces"),
("{{ `code` }}", "Generic shortcode without spaces"),
// Shortcodes with parameters (common Hugo pattern)
("{{< highlight go `code` >}}", "Shortcode with highlight parameter"),
("{{< code `go list` >}}", "Shortcode with code parameter"),
// Multi-line Hugo templates
("{{raw `\n\tcommand here\n\tmore code\n`}}", "Multi-line raw template"),
("{{< highlight `\ncode here\n` >}}", "Multi-line highlight template"),
// Hugo templates with nested Go template syntax
(
"{{raw `\n\t{{.Variable}}\n\t{{range .Items}}\n`}}",
"Nested Go template syntax",
),
// Edge case: Hugo template at start of line
("{{raw `code`}}", "Hugo template at line start"),
// Edge case: Hugo template at end of line
("Text {{raw `code`}}", "Hugo template at end of line"),
// Edge case: Multiple Hugo templates
("{{raw `code1`}} and {{raw `code2`}}", "Multiple Hugo templates"),
];
for (case, description) in valid_hugo_cases {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Hugo template syntax should not trigger MD038 warnings: {description} - {case}"
);
}
// ===== FALSE POSITIVE PREVENTION (Non-Hugo asymmetric spaces should be flagged) =====
// These have asymmetric spaces (leading-only or trailing-only) and should be flagged
// Per CommonMark spec: symmetric single-space pairs are stripped and NOT flagged
let should_be_flagged = vec![
("This is ` code` with leading space.", "Leading space only"),
("This is `code ` with trailing space.", "Trailing space only"),
("Text ` code ` here", "Extra leading space (asymmetric)"),
("Text ` code ` here", "Extra trailing space (asymmetric)"),
("Text ` code` here", "Double leading, no trailing"),
("Text `code ` here", "No leading, double trailing"),
];
for (case, description) in should_be_flagged {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
!result.is_empty(),
"Should flag asymmetric space code spans: {description} - {case}"
);
}
// ===== COMMONMARK SYMMETRIC SPACE BEHAVIOR (Should NOT be flagged) =====
// Per CommonMark 0.31.2: When a code span has exactly one space at start AND end,
// those spaces are stripped from the output. This is intentional, not an error.
// These cases should NOT trigger MD038.
let symmetric_single_space = vec![
("Text ` code ` here", "Symmetric single space - CommonMark strips"),
("{raw ` code `}", "Looks like Hugo but missing opening {{"),
("raw ` code `}}", "Missing opening {{ - but symmetric spaces"),
];
for (case, description) in symmetric_single_space {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"CommonMark symmetric spaces should NOT be flagged: {description} - {case}"
);
}
// ===== EDGE CASES: Unicode and Special Characters =====
let unicode_cases = vec![
("{{raw `\n\t你好世界\n`}}", "Unicode in Hugo template"),
("{{raw `\n\t🎉 emoji\n`}}", "Emoji in Hugo template"),
("{{raw `\n\tcode with \"quotes\"\n`}}", "Quotes in Hugo template"),
(
"{{raw `\n\tcode with 'single quotes'\n`}}",
"Single quotes in Hugo template",
),
];
for (case, description) in unicode_cases {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Hugo templates with special characters should not trigger warnings: {description} - {case}"
);
}
// ===== BOUNDARY CONDITIONS =====
// Minimum valid Hugo pattern
assert!(
rule.check(&crate::lint_context::LintContext::new(
"{{ ` ` }}",
crate::config::MarkdownFlavor::Standard,
None
))
.unwrap()
.is_empty(),
"Minimum Hugo pattern should be valid"
);
// Hugo template with only whitespace
assert!(
rule.check(&crate::lint_context::LintContext::new(
"{{raw `\n\t\n`}}",
crate::config::MarkdownFlavor::Standard,
None
))
.unwrap()
.is_empty(),
"Hugo template with only whitespace should be valid"
);
}
/// Hugo templates are located by byte offset, so a line whose character
/// positions differ from its byte positions must behave the same way
#[test]
fn test_hugo_template_after_multibyte_text() {
let rule = MD038NoSpaceInCode::new();
// Spans that would be flagged for their trailing space if the template
// around them were not recognized
let exempt = [
"日本語 {{raw `a ` }}",
"café {{% `a ` }}",
"{{< 日本語 `a ` }}",
"日本語 {{ `a `\n}}",
"日本語 {{raw `a\nb ` }}",
];
for case in exempt {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
assert!(
rule.check(&ctx).unwrap().is_empty(),
"Hugo template behind multibyte text should not trigger MD038: {case}"
);
}
// Control: the same lines without a recognized opener stay reported
let flagged = [
"日本語 {{raw`a ` }}",
"café {{ `a ` and",
"{{< 日本語`a ` }}",
"日本語 {{raw`a\nb ` }}",
];
for case in flagged {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
assert_eq!(
rule.check(&ctx).unwrap().len(),
1,
"Near miss behind multibyte text should still be reported: {case}"
);
}
}
/// Test interaction with other markdown elements
#[test]
fn test_hugo_template_with_other_markdown() {
let rule = MD038NoSpaceInCode::new();
// Hugo template inside a list
let content = r#"1. First item
2. Second item with {{raw `code`}} template
3. Third item"#;
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(result.is_empty(), "Hugo template in list should not trigger warnings");
// Hugo template in blockquote
let content = r#"> Quote with {{raw `code`}} template"#;
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Hugo template in blockquote should not trigger warnings"
);
// Hugo template near regular code span (should flag the regular one)
let content = r#"{{raw `code`}} and ` bad code` here"#;
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should flag regular code span but not Hugo template");
}
/// Performance test: Many Hugo templates
#[test]
fn test_hugo_template_performance() {
let rule = MD038NoSpaceInCode::new();
// Create content with many Hugo templates
let mut content = String::new();
for i in 0..100 {
content.push_str(&format!("{{{{raw `code{i}\n`}}}}\n"));
}
let ctx = crate::lint_context::LintContext::new(&content, crate::config::MarkdownFlavor::Standard, None);
let start = std::time::Instant::now();
let result = rule.check(&ctx).unwrap();
let duration = start.elapsed();
assert!(result.is_empty(), "Many Hugo templates should not trigger warnings");
assert!(
duration.as_millis() < 1000,
"Performance test: Should process 100 Hugo templates in <1s, took {duration:?}"
);
}
#[test]
fn test_mkdocs_inline_hilite_not_flagged() {
// InlineHilite syntax: `#!language code` should NOT be flagged
// The space after the language specifier is legitimate
let rule = MD038NoSpaceInCode::new();
let valid_cases = vec![
"`#!python print('hello')`",
"`#!js alert('hi')`",
"`#!c++ cout << x;`",
"Use `#!python import os` to import modules",
"`#!bash echo $HOME`",
];
for case in valid_cases {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::MkDocs, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"InlineHilite syntax should not be flagged in MkDocs: {case}"
);
}
// Test that InlineHilite IS flagged in Standard flavor (not MkDocs-aware)
let content = "`#!python print('hello')`";
let ctx_standard =
crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result_standard = rule.check(&ctx_standard).unwrap();
// In standard flavor, the content " print('hello')" has no special meaning
// But since "#!python print('hello')" doesn't have leading/trailing spaces, it's valid!
assert!(
result_standard.is_empty(),
"InlineHilite with no extra spaces should not be flagged even in Standard flavor"
);
}
#[test]
fn test_multibyte_utf8_no_panic() {
// Regression test: ensure multi-byte UTF-8 characters don't cause panics
// when checking for nested backticks between code spans.
// These are real examples from the-art-of-command-line translations.
let rule = MD038NoSpaceInCode::new();
// Greek text with code spans
let greek = "- Χρήσιμα εργαλεία της γραμμής εντολών είναι τα `ping`,` ipconfig`, `traceroute` και `netstat`.";
let ctx = crate::lint_context::LintContext::new(greek, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx);
assert!(result.is_ok(), "Greek text should not panic");
// Chinese text with code spans
let chinese = "- 當你需要對文字檔案做集合交、並、差運算時,`sort`/`uniq` 很有幫助。";
let ctx = crate::lint_context::LintContext::new(chinese, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx);
assert!(result.is_ok(), "Chinese text should not panic");
// Cyrillic/Ukrainian text with code spans
let cyrillic = "- Основи роботи з файлами: `ls` і `ls -l`, `less`, `head`,` tail` і `tail -f`.";
let ctx = crate::lint_context::LintContext::new(cyrillic, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx);
assert!(result.is_ok(), "Cyrillic text should not panic");
// Mixed multi-byte with multiple code spans on same line
let mixed = "使用 `git` 命令和 `npm` 工具来管理项目,可以用 `docker` 容器化。";
let ctx = crate::lint_context::LintContext::new(mixed, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx);
assert!(
result.is_ok(),
"Mixed Chinese text with multiple code spans should not panic"
);
}
// ==================== Obsidian Dataview Plugin Tests ====================
/// Test that Dataview inline DQL expressions are not flagged in Obsidian flavor
#[test]
fn test_obsidian_dataview_inline_dql_not_flagged() {
let rule = MD038NoSpaceInCode::new();
// Basic inline DQL expressions - should NOT be flagged in Obsidian
let valid_dql_cases = vec![
"`= this.file.name`",
"`= date(today)`",
"`= [[Page]].field`",
"`= choice(condition, \"yes\", \"no\")`",
"`= this.file.mtime`",
"`= this.file.ctime`",
"`= this.file.path`",
"`= this.file.folder`",
"`= this.file.size`",
"`= this.file.ext`",
"`= this.file.link`",
"`= this.file.outlinks`",
"`= this.file.inlinks`",
"`= this.file.tags`",
];
for case in valid_dql_cases {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Dataview DQL expression should not be flagged in Obsidian: {case}"
);
}
}
/// Test that Dataview inline DataviewJS expressions are not flagged in Obsidian flavor
#[test]
fn test_obsidian_dataview_inline_dvjs_not_flagged() {
let rule = MD038NoSpaceInCode::new();
// Inline DataviewJS expressions - should NOT be flagged in Obsidian
let valid_dvjs_cases = vec![
"`$= dv.current().file.mtime`",
"`$= dv.pages().length`",
"`$= dv.current()`",
"`$= dv.pages('#tag').length`",
"`$= dv.pages('\"folder\"').length`",
"`$= dv.current().file.name`",
"`$= dv.current().file.path`",
"`$= dv.current().file.folder`",
"`$= dv.current().file.link`",
];
for case in valid_dvjs_cases {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Dataview JS expression should not be flagged in Obsidian: {case}"
);
}
}
/// Test complex Dataview expressions with nested parentheses
#[test]
fn test_obsidian_dataview_complex_expressions() {
let rule = MD038NoSpaceInCode::new();
let complex_cases = vec![
// Nested function calls
"`= sum(filter(pages, (p) => p.done))`",
"`= length(filter(file.tags, (t) => startswith(t, \"project\")))`",
// choice() function
"`= choice(x > 5, \"big\", \"small\")`",
"`= choice(this.status = \"done\", \"✅\", \"⏳\")`",
// date functions
"`= date(today) - dur(7 days)`",
"`= dateformat(this.file.mtime, \"yyyy-MM-dd\")`",
// Math expressions
"`= sum(rows.amount)`",
"`= round(average(rows.score), 2)`",
"`= min(rows.priority)`",
"`= max(rows.priority)`",
// String operations
"`= join(this.file.tags, \", \")`",
"`= replace(this.title, \"-\", \" \")`",
"`= lower(this.file.name)`",
"`= upper(this.file.name)`",
// List operations
"`= length(this.file.outlinks)`",
"`= contains(this.file.tags, \"important\")`",
// Link references
"`= [[Page Name]].field`",
"`= [[Folder/Subfolder/Page]].nested.field`",
// Conditional expressions
"`= default(this.status, \"unknown\")`",
"`= coalesce(this.priority, this.importance, 0)`",
];
for case in complex_cases {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Complex Dataview expression should not be flagged in Obsidian: {case}"
);
}
}
/// Test that complex DataviewJS expressions with method chains are not flagged
#[test]
fn test_obsidian_dataviewjs_method_chains() {
let rule = MD038NoSpaceInCode::new();
let method_chain_cases = vec![
"`$= dv.pages().where(p => p.status).length`",
"`$= dv.pages('#project').where(p => !p.done).length`",
"`$= dv.pages().filter(p => p.file.day).sort(p => p.file.mtime, 'desc').limit(5)`",
"`$= dv.pages('\"folder\"').map(p => p.file.link).join(', ')`",
"`$= dv.current().file.tasks.where(t => !t.completed).length`",
"`$= dv.pages().flatMap(p => p.file.tags).distinct().sort()`",
"`$= dv.page('Index').children.map(p => p.title)`",
"`$= dv.pages().groupBy(p => p.status).map(g => [g.key, g.rows.length])`",
];
for case in method_chain_cases {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"DataviewJS method chain should not be flagged in Obsidian: {case}"
);
}
}
/// Test Dataview-like patterns in Standard flavor
///
/// Note: The actual content `= this.file.name` starts with `=`, not whitespace,
/// so it doesn't have a leading space issue. Dataview expressions only become
/// relevant when their content would otherwise be flagged.
///
/// To properly test the difference, we need patterns that have leading whitespace
/// issues that would be skipped in Obsidian but flagged in Standard.
#[test]
fn test_standard_flavor_vs_obsidian_dataview() {
let rule = MD038NoSpaceInCode::new();
// These Dataview expressions don't have leading whitespace (they start with "=")
// so they wouldn't be flagged in ANY flavor
let no_issue_cases = vec!["`= this.file.name`", "`$= dv.current()`"];
for case in no_issue_cases {
// Standard flavor - no issue because content doesn't start with whitespace
let ctx_std = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
let result_std = rule.check(&ctx_std).unwrap();
assert!(
result_std.is_empty(),
"Dataview expression without leading space shouldn't be flagged in Standard: {case}"
);
// Obsidian flavor - also no issue
let ctx_obs = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
let result_obs = rule.check(&ctx_obs).unwrap();
assert!(
result_obs.is_empty(),
"Dataview expression shouldn't be flagged in Obsidian: {case}"
);
}
// Test that regular code with leading/trailing spaces is still flagged in both flavors
// (when not matching Dataview pattern)
let space_issues = vec![
"` code`", // Leading space, no trailing
"`code `", // Trailing space, no leading
];
for case in space_issues {
// Standard flavor - should be flagged
let ctx_std = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Standard, None);
let result_std = rule.check(&ctx_std).unwrap();
assert!(
!result_std.is_empty(),
"Code with spacing issue should be flagged in Standard: {case}"
);
// Obsidian flavor - should also be flagged (not a Dataview pattern)
let ctx_obs = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
let result_obs = rule.check(&ctx_obs).unwrap();
assert!(
!result_obs.is_empty(),
"Code with spacing issue should be flagged in Obsidian (not Dataview): {case}"
);
}
}
/// Test that regular code spans with leading space are still flagged in Obsidian
#[test]
fn test_obsidian_still_flags_regular_code_spans_with_space() {
let rule = MD038NoSpaceInCode::new();
// These are NOT Dataview expressions, just regular code spans with leading space
// They should still be flagged even in Obsidian flavor
let invalid_cases = [
"` regular code`", // Space at start, not Dataview
"`code `", // Space at end
"` code `", // This is valid per CommonMark (symmetric single space)
"` code`", // Double space at start (not Dataview pattern)
];
// Only the asymmetric cases should be flagged
let expected_flags = [
true, // ` regular code` - leading space, no trailing
true, // `code ` - trailing space, no leading
false, // ` code ` - symmetric single space (CommonMark valid)
true, // ` code` - double leading space
];
for (case, should_flag) in invalid_cases.iter().zip(expected_flags.iter()) {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
if *should_flag {
assert!(
!result.is_empty(),
"Non-Dataview code span with spacing issue should be flagged in Obsidian: {case}"
);
} else {
assert!(
result.is_empty(),
"CommonMark-valid symmetric spacing should not be flagged: {case}"
);
}
}
}
/// Test edge cases for Dataview pattern detection
#[test]
fn test_obsidian_dataview_edge_cases() {
let rule = MD038NoSpaceInCode::new();
// Valid Dataview patterns
let valid_cases = vec![
("`= x`", true), // Minimal DQL
("`$= x`", true), // Minimal DVJS
("`= `", true), // Just equals-space (empty expression)
("`$= `", true), // Just dollar-equals-space (empty expression)
("`=x`", false), // No space after = (not Dataview, and no leading whitespace issue)
("`$=x`", false), // No space after $= (not Dataview, and no leading whitespace issue)
("`= [[Link]]`", true), // Link in expression
("`= this`", true), // Simple this reference
("`$= dv`", true), // Just dv object reference
("`= 1 + 2`", true), // Math expression
("`$= 1 + 2`", true), // Math in DVJS
("`= \"string\"`", true), // String literal
("`$= 'string'`", true), // Single-quoted string
("`= this.field ?? \"default\"`", true), // Null coalescing
("`$= dv?.pages()`", true), // Optional chaining
];
for (case, should_be_valid) in valid_cases {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
if should_be_valid {
assert!(
result.is_empty(),
"Valid Dataview expression should not be flagged: {case}"
);
} else {
// These might or might not be flagged depending on other MD038 rules
// We just verify they don't crash
let _ = result;
}
}
}
/// Test Dataview expressions in context (mixed with regular markdown)
#[test]
fn test_obsidian_dataview_in_context() {
let rule = MD038NoSpaceInCode::new();
// Document with mixed Dataview and regular code spans
let content = r#"# My Note
The file name is `= this.file.name` and it was created on `= this.file.ctime`.
Regular code: `println!("hello")` and `let x = 5;`
DataviewJS count: `$= dv.pages('#project').length` projects found.
More regular code with issue: ` bad code` should be flagged.
"#;
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
// Should only flag ` bad code` (line 9)
assert_eq!(
result.len(),
1,
"Should only flag the regular code span with leading space, not Dataview expressions"
);
assert_eq!(result[0].line, 9, "Warning should be on line 9");
}
/// Test that Dataview expressions in code blocks are properly handled
#[test]
fn test_obsidian_dataview_in_code_blocks() {
let rule = MD038NoSpaceInCode::new();
// Dataview expressions inside fenced code blocks should be ignored
// (because they're inside code blocks, not because of Dataview logic)
let content = r#"# Example
```
`= this.file.name`
`$= dv.current()`
```
Regular paragraph with `= this.file.name` Dataview.
"#;
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
// Should not flag anything - code blocks are skipped, and inline Dataview is valid
assert!(
result.is_empty(),
"Dataview in code blocks should be ignored, inline Dataview should be valid"
);
}
/// Test Dataview with Unicode content
#[test]
fn test_obsidian_dataview_unicode() {
let rule = MD038NoSpaceInCode::new();
let unicode_cases = vec![
"`= this.日本語`", // Japanese field name
"`= this.中文字段`", // Chinese field name
"`= \"Привет мир\"`", // Russian string
"`$= dv.pages('#日本語タグ')`", // Japanese tag
"`= choice(true, \"✅\", \"❌\")`", // Emoji in strings
"`= this.file.name + \" 📝\"`", // Emoji concatenation
];
for case in unicode_cases {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Unicode Dataview expression should not be flagged: {case}"
);
}
}
/// Test that Dataview detection doesn't break regular equals patterns
#[test]
fn test_obsidian_regular_equals_still_works() {
let rule = MD038NoSpaceInCode::new();
// Regular code with equals signs should still work normally
let valid_regular_cases = vec![
"`x = 5`", // Assignment (no leading space)
"`a == b`", // Equality check
"`x >= 10`", // Comparison
"`let x = 10`", // Variable declaration
"`const y = 5`", // Const declaration
];
for case in valid_regular_cases {
let ctx = crate::lint_context::LintContext::new(case, crate::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Regular code with equals should not be flagged: {case}"
);
}
}
/// Test fix behavior doesn't break Dataview expressions
#[test]
fn test_obsidian_dataview_fix_preserves_expressions() {
let rule = MD038NoSpaceInCode::new();
// Content with Dataview expressions and one fixable issue
let content = "Dataview: `= this.file.name` and bad: ` fixme`";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
let fixed = rule.fix(&ctx).unwrap();
// Should fix ` fixme` but preserve `= this.file.name`
assert!(
fixed.contains("`= this.file.name`"),
"Dataview expression should be preserved after fix"
);
assert!(
fixed.contains("`fixme`"),
"Regular code span should be fixed (space removed)"
);
assert!(!fixed.contains("` fixme`"), "Bad code span should have been fixed");
}
/// Test multiple Dataview expressions on same line
#[test]
fn test_obsidian_multiple_dataview_same_line() {
let rule = MD038NoSpaceInCode::new();
let content = "Created: `= this.file.ctime` | Modified: `= this.file.mtime` | Count: `$= dv.pages().length`";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Multiple Dataview expressions on same line should all be valid"
);
}
/// Performance test: Many Dataview expressions
#[test]
fn test_obsidian_dataview_performance() {
let rule = MD038NoSpaceInCode::new();
// Create content with many Dataview expressions
let mut content = String::new();
for i in 0..100 {
content.push_str(&format!("Field {i}: `= this.field{i}` | JS: `$= dv.current().f{i}`\n"));
}
let ctx = crate::lint_context::LintContext::new(&content, crate::config::MarkdownFlavor::Obsidian, None);
let start = std::time::Instant::now();
let result = rule.check(&ctx).unwrap();
let duration = start.elapsed();
assert!(result.is_empty(), "All Dataview expressions should be valid");
assert!(
duration.as_millis() < 1000,
"Performance test: Should process 200 Dataview expressions in <1s, took {duration:?}"
);
}
/// Test is_dataview_expression helper function directly
#[test]
fn test_is_dataview_expression_helper() {
// Valid Dataview patterns
assert!(MD038NoSpaceInCode::is_dataview_expression("= this.file.name"));
assert!(MD038NoSpaceInCode::is_dataview_expression("= "));
assert!(MD038NoSpaceInCode::is_dataview_expression("$= dv.current()"));
assert!(MD038NoSpaceInCode::is_dataview_expression("$= "));
assert!(MD038NoSpaceInCode::is_dataview_expression("= x"));
assert!(MD038NoSpaceInCode::is_dataview_expression("$= x"));
// Invalid Dataview patterns
assert!(!MD038NoSpaceInCode::is_dataview_expression("=")); // No space after =
assert!(!MD038NoSpaceInCode::is_dataview_expression("$=")); // No space after $=
assert!(!MD038NoSpaceInCode::is_dataview_expression("=x")); // No space
assert!(!MD038NoSpaceInCode::is_dataview_expression("$=x")); // No space
assert!(!MD038NoSpaceInCode::is_dataview_expression(" = x")); // Leading space before =
assert!(!MD038NoSpaceInCode::is_dataview_expression("x = 5")); // Assignment, not Dataview
assert!(!MD038NoSpaceInCode::is_dataview_expression("== x")); // Double equals
assert!(!MD038NoSpaceInCode::is_dataview_expression("")); // Empty
assert!(!MD038NoSpaceInCode::is_dataview_expression("regular")); // Regular text
}
/// Test Dataview expressions work alongside other Obsidian features (tags)
#[test]
fn test_obsidian_dataview_with_tags() {
let rule = MD038NoSpaceInCode::new();
// Document using both Dataview and Obsidian tags
let content = r#"# Project Status
Tags: #project #active
Status: `= this.status`
Count: `$= dv.pages('#project').length`
Regular code: `function test() {}`
"#;
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
// Nothing should be flagged
assert!(
result.is_empty(),
"Dataview expressions and regular code should work together"
);
}
#[test]
fn test_unicode_between_code_spans_no_panic() {
// Verify that multi-byte characters between code spans do not cause panics
// or incorrect slicing in the nested-backtick detection logic.
let rule = MD038NoSpaceInCode::new();
// Multi-byte character (U-umlaut = 2 bytes) between two code spans
let content = "Use `one` \u{00DC}nited `two` for backtick examples.";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx);
// Should not panic; any warnings or lack thereof are acceptable
assert!(result.is_ok(), "Should not panic with Unicode between code spans");
// CJK characters (3 bytes each) between code spans
let content_cjk = "Use `one` \u{4E16}\u{754C} `two` for examples.";
let ctx_cjk = crate::lint_context::LintContext::new(content_cjk, crate::config::MarkdownFlavor::Standard, None);
let result_cjk = rule.check(&ctx_cjk);
assert!(result_cjk.is_ok(), "Should not panic with CJK between code spans");
}
#[test]
fn test_pandoc_inline_r_code_not_exempt() {
// The `r expression` pattern is RMarkdown/Quarto-specific inline R evaluation syntax.
// A code span like `r foo ` (trailing space, starts with `r `) triggers the Quarto
// guard when in Quarto flavor — the trailing space violation is suppressed because the
// content looks like inline R code. Under Pandoc flavor the guard must NOT fire:
// `r ` is not special Pandoc syntax, so the trailing space is a genuine MD038 violation.
let rule = MD038NoSpaceInCode::new();
// Trailing space only (no leading space) — CommonMark does not strip this, so it's a
// real MD038 violation. The `r ` prefix makes it match the Quarto `r expression` guard.
let content = "See `r foo ` for details.\n";
// Under Quarto flavor, the `r expression` guard fires and suppresses the warning.
let ctx_quarto = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Quarto, None);
let result_quarto = rule.check(&ctx_quarto).unwrap();
assert!(
result_quarto.is_empty(),
"MD038 should suppress trailing-space warning for `r expression` under Quarto: {result_quarto:?}"
);
// Under Pandoc flavor, the guard does NOT fire — trailing space is flagged.
let ctx_pandoc = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Pandoc, None);
let result_pandoc = rule.check(&ctx_pandoc).unwrap();
assert!(
!result_pandoc.is_empty(),
"MD038 should flag trailing space in `r expression` under Pandoc flavor (not Quarto/RMarkdown syntax): {result_pandoc:?}"
);
}
/// Pandoc inline code attribute syntax (`` `code`{.lang} ``) does not exempt
/// the code span from MD038's inner-whitespace check: the attribute block lives
/// outside the closing backtick, so a leading space inside the backticks is a
/// real spacing violation regardless of any attached attribute.
#[test]
fn test_pandoc_inline_code_attr_does_not_suppress_leading_space() {
let rule = MD038NoSpaceInCode::new();
let content = "Use ` print()`{.python} for output.\n";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Pandoc, None);
let result = rule.check(&ctx).unwrap();
assert!(
!result.is_empty(),
"MD038 must flag leading space inside `code`{{.lang}} under Pandoc — the attribute is outside the span: {result:?}"
);
}
/// Trailing space inside an attributed code span is also a real violation
/// under Pandoc — the `{.lang}` attribute does not absorb whitespace from
/// inside the backticks.
#[test]
fn test_pandoc_inline_code_attr_does_not_suppress_trailing_space() {
let rule = MD038NoSpaceInCode::new();
let content = "Use `print() `{.python} for output.\n";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Pandoc, None);
let result = rule.check(&ctx).unwrap();
assert!(
!result.is_empty(),
"MD038 must flag trailing space inside `code`{{.lang}} under Pandoc — the attribute is outside the span: {result:?}"
);
}
/// Cross-flavor parity: Standard flavor still flags the same content.
#[test]
fn test_standard_still_flags_leading_space_with_attr_syntax() {
let rule = MD038NoSpaceInCode::new();
let content = "Use ` print()`{.python} for output.\n";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
!result.is_empty(),
"MD038 should flag leading space in code span under Standard flavor: {result:?}"
);
}
/// Clean attributed code spans (no inner whitespace) must still pass under
/// Pandoc — the no-whitespace fast path handles them, no special guard needed.
#[test]
fn test_pandoc_inline_code_attr_clean_span_not_flagged() {
let rule = MD038NoSpaceInCode::new();
let content = "Use `print()`{.python} for output.\n";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Pandoc, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"MD038 must not flag a clean attributed code span under Pandoc: {result:?}"
);
}
/// The whitespace before a closing backtick that sits on its own line is a
/// line ending. Removing it deletes a line from the document, which is a
/// structural rewrite, not a spacing fix.
#[test]
fn test_trailing_line_ending_is_not_removed() {
let rule = MD038NoSpaceInCode::new();
let content = "Text `a\n` tail\n";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"MD038 must not flag whitespace it can only remove by deleting a line: {result:?}"
);
assert_eq!(rule.fix(&ctx).unwrap(), content);
}
/// The same in the other direction: an opening backtick at the end of a line.
#[test]
fn test_leading_line_ending_is_not_removed() {
let rule = MD038NoSpaceInCode::new();
let content = "Text ` \na` tail\n";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"MD038 must not flag a leading whitespace run holding a line ending: {result:?}"
);
assert_eq!(rule.fix(&ctx).unwrap(), content);
}
/// The shape that made this destructive. An indented fenced block cannot
/// interrupt the paragraph the div marker opens, so CommonMark reads the whole
/// chunk as one code span, while Pandoc and Quarto restart block parsing inside
/// the div body and render it as code. Rewriting it joins two lines of the
/// user's source.
#[test]
fn test_quarto_callout_with_indented_chunk_is_left_alone() {
let rule = MD038NoSpaceInCode::new();
let content = "::: callout-note\n ```{r}\n x <- 1\n ```\n:::\n";
for flavor in [
crate::config::MarkdownFlavor::Quarto,
crate::config::MarkdownFlavor::Pandoc,
crate::config::MarkdownFlavor::Standard,
] {
let ctx = crate::lint_context::LintContext::new(content, flavor, None);
assert_eq!(
rule.fix(&ctx).unwrap(),
content,
"MD038 rewrote an indented code chunk inside a div under {flavor:?}"
);
}
}
/// The same shape one container deeper. Trimming this span pulled the closing
/// fence out of the quote entirely: 0.2.52 rewrites the first case as
/// `> text\n> ```> x\n>```\n`, a deleted line and a lost `>` prefix.
#[test]
fn test_blockquoted_indented_fence_keeps_its_indentation() {
let rule = MD038NoSpaceInCode::new();
for content in [
"> text\n> ```\n> x\n> ```\n",
">> text\n>> ```\n>> x\n>> ```\n",
"text\n ```\n x\n ```\n",
] {
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
assert!(
rule.check(&ctx).unwrap().is_empty(),
"MD038 flagged a line's own indentation in {content:?}"
);
assert_eq!(rule.fix(&ctx).unwrap(), content);
}
}
/// The shape only the second half of the guard refuses. A `>` is the one
/// non-whitespace character that can sit between a line ending and a line's
/// indentation, so it is the only way a closing whitespace run can be a line's
/// own indentation without holding the line ending too. Removing it here would
/// pull the closing backtick up against the quote marker, and would not even
/// remove the space it flagged: the code span still renders with a trailing
/// space, and the rule then reads the result as clean.
#[test]
fn test_quoted_line_indentation_is_not_removed() {
let rule = MD038NoSpaceInCode::new();
for content in [
"> Text `a\n> ` tail\n",
"> Text `a\n> ` tail\n",
">> Text `a\n>> ` tail\n",
"> Text `a\n> b\n> ` tail\n",
// The search back to the start of that line has to recognize a
// carriage return, or a classic-Mac document reports no line at all.
"> Text `a\r> ` tail\r",
] {
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
assert!(
rule.check(&ctx).unwrap().is_empty(),
"MD038 flagged a quoted line's own indentation in {content:?}"
);
assert_eq!(rule.fix(&ctx).unwrap(), content);
}
}
/// The question about a line's indentation is only asked of a run this rule
/// would otherwise remove. Here there is no trailing run at all, and the span
/// ends on a line whose every earlier character is layout, so asking anyway
/// answers yes and refuses a span whose LEADING space is ordinary spacing. Four
/// spaces of indent are load-bearing: fewer would open a blockquote and end the
/// paragraph, so the span would not cross the line break in the first place.
#[test]
fn test_a_span_with_no_trailing_run_is_still_trimmed_at_the_front() {
let rule = MD038NoSpaceInCode::new();
let content = "Text ` a\n >` tail\n";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
assert_eq!(rule.check(&ctx).unwrap().len(), 1, "no warning for {content:?}");
assert_eq!(rule.fix(&ctx).unwrap(), "Text `a\n >` tail\n");
}
/// A container prefix does not by itself put a span out of reach: this span's
/// trailing space really is spacing, and is still removed. Without this control
/// a blanket refusal of multi-line spans in containers would look correct.
#[test]
fn test_container_multiline_span_still_trims_a_trailing_space() {
let rule = MD038NoSpaceInCode::new();
for (content, expected) in [
("> text `a\n> b ` tail\n", "> text `a\n> b` tail\n"),
("- text `a\n b ` tail\n", "- text `a\n b` tail\n"),
] {
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
assert_eq!(rule.check(&ctx).unwrap().len(), 1, "no warning for {content:?}");
assert_eq!(rule.fix(&ctx).unwrap(), expected);
}
}
/// Positive control for all of the above: a code span really does cross a line
/// break here, and the trailing space before the closing backtick is not a line
/// ending, so it is still removed, with the line break kept.
#[test]
fn test_multiline_span_still_trims_a_trailing_space() {
let rule = MD038NoSpaceInCode::new();
let content = "Text `a\nb ` tail\n";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
assert_eq!(rule.check(&ctx).unwrap().len(), 1);
assert_eq!(rule.fix(&ctx).unwrap(), "Text `a\nb` tail\n");
}
/// A span can have one end this rule may edit and one it may not, and then it
/// edits neither. CommonMark removes a space from each end only when BOTH ends
/// have one, so trimming the reachable end alone moves the rendered space to the
/// other end instead of removing it. The first case here renders as "a " and a
/// partial fix would render it as " a"; the second is the mirror. Worse, the
/// rule then reads the result as clean, so the space it moved is never
/// mentioned again.
#[test]
fn test_a_span_with_one_untrimmable_end_is_left_alone() {
let rule = MD038NoSpaceInCode::new();
for content in ["Text `\na ` tail\n", "Text ` a\n` tail\n"] {
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
assert!(
rule.check(&ctx).unwrap().is_empty(),
"MD038 offered a partial fix that only moves the rendered space: {content:?}"
);
assert_eq!(rule.fix(&ctx).unwrap(), content);
}
}
/// A warning's end position is a column of the line the span ends on, so a span
/// crossing a line break must report that line. Reporting the start line gives
/// an editor a range running backwards.
#[test]
fn test_multiline_span_reports_the_line_it_ends_on() {
let rule = MD038NoSpaceInCode::new();
let content = "Text `a\nb ` tail\n";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1);
assert_eq!(result[0].line, 1);
assert_eq!(result[0].end_line, 2, "warning: {:?}", result[0]);
}
/// The invariant behind every case above, stated once: MD038 edits spacing, so
/// whatever it does, the document keeps its lines. Every entry is one this rule
/// used to rewrite, and the last is the positive control: it must still be
/// rewritten, and still keep its line count.
#[test]
fn test_fix_never_changes_the_line_count() {
let rule = MD038NoSpaceInCode::new();
let cases = [
"Text `a\n` tail\n",
"Text `a\n ` tail\n",
"Text ` \na` tail\n",
"::: callout-note\n ```{r}\n x <- 1\n ```\n:::\n",
"Some text.\n ```{r}\n x <- 1\n ```\n",
"> text\n> ```\n> x\n> ```\n",
"Text `a\r\n` tail\r\n",
"Text `a\r` tail\r",
"> text\r> ```\r> x\r> ```\r",
"Text `a\nb ` tail\n",
"Text `a\rb ` tail\r",
];
// A line ending is \n, \r\n, or a lone \r, so counting one character
// would report a classic-Mac document as a single line however it is
// rewritten.
let line_endings = |s: &str| s.matches('\n').count() + s.matches('\r').count() - s.matches("\r\n").count();
let mut rewritten = 0;
for content in cases {
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let fixed = rule.fix(&ctx).unwrap();
assert_eq!(
line_endings(&fixed),
line_endings(content),
"MD038 changed the line count of {content:?} -> {fixed:?}"
);
if fixed != content {
rewritten += 1;
}
}
assert_eq!(rewritten, 2, "both positive controls must still be rewritten");
}
/// A lone carriage return ends a line as much as a newline does, so every case
/// above has a classic-Mac twin. Before the guard recognized one, `--enable
/// MD038` still produced the original corruption verbatim on these documents.
#[test]
fn test_carriage_return_is_a_line_ending() {
let rule = MD038NoSpaceInCode::new();
for content in [
"Text `a\r` tail\r",
"Text ` \ra` tail\r",
"text\r ```\r x\r ```\r",
"> text\r> ```\r> x\r> ```\r",
] {
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
assert!(
rule.check(&ctx).unwrap().is_empty(),
"MD038 flagged a carriage return it can only remove by joining lines: {content:?}"
);
assert_eq!(rule.fix(&ctx).unwrap(), content);
}
// Positive control: spacing that really is spacing is still removed, and
// the carriage return separating the two lines survives.
let content = "Text `a\rb ` tail\r";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
assert_eq!(rule.check(&ctx).unwrap().len(), 1);
assert_eq!(rule.fix(&ctx).unwrap(), "Text `a\rb` tail\r");
}
}