rumdl 0.1.51

A fast Markdown linter written in Rust (Ru(st) MarkDown Linter)
Documentation
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
use crate::config::MarkdownFlavor;
use crate::utils::table_utils::TableUtils;
use std::sync::LazyLock;

use super::types::*;

/// Detect headings and blockquotes (called after HTML block detection)
pub(super) fn detect_headings_and_blockquotes(
    content_lines: &[&str],
    lines: &mut [LineInfo],
    flavor: MarkdownFlavor,
    html_comment_ranges: &[crate::utils::skip_context::ByteRange],
    link_byte_ranges: &[(usize, usize)],
    front_matter_end: usize,
) {
    // Regex for heading detection
    static ATX_HEADING_REGEX: LazyLock<regex::Regex> =
        LazyLock::new(|| regex::Regex::new(r"^(\s*)(#{1,6})(\s*)(.*)$").unwrap());
    static SETEXT_UNDERLINE_REGEX: LazyLock<regex::Regex> =
        LazyLock::new(|| regex::Regex::new(r"^(\s*)(=+|-+)\s*$").unwrap());

    // Detect headings (including Setext which needs look-ahead) and blockquotes
    for i in 0..lines.len() {
        let line = content_lines[i];

        // Detect blockquotes FIRST, before any skip conditions.
        if !(front_matter_end > 0 && i < front_matter_end)
            && let Some(bq) = crate::utils::blockquote::parse_blockquote_prefix(line)
        {
            let nesting_level = bq.nesting_level;
            let marker_column = bq.indent.len();
            let content_leading_ws_len = bq.content.len() - bq.content.trim_start_matches([' ', '\t']).len();
            let full_prefix = format!("{}{}", bq.prefix, &bq.content[..content_leading_ws_len]);
            let normalized_content = &bq.content[content_leading_ws_len..];

            let has_no_space = bq.spaces_after_marker.is_empty() && !normalized_content.is_empty();
            let has_multiple_spaces = bq.spaces_after_marker.chars().filter(|&c| c == ' ').count() > 1;
            let needs_md028_fix = normalized_content.is_empty() && bq.spaces_after_marker.is_empty();

            lines[i].blockquote = Some(Box::new(BlockquoteInfo {
                nesting_level,
                indent: bq.indent.to_string(),
                marker_column,
                prefix: full_prefix,
                content: normalized_content.to_string(),
                has_no_space_after_marker: has_no_space,
                has_multiple_spaces_after_marker: has_multiple_spaces,
                needs_md028_fix,
            }));

            // Update is_horizontal_rule for blockquote content
            if !lines[i].in_code_block && is_horizontal_rule_content(normalized_content.trim()) {
                lines[i].is_horizontal_rule = true;
            }
        }

        // Now apply skip conditions for heading detection
        if lines[i].in_code_block {
            continue;
        }

        if front_matter_end > 0 && i < front_matter_end {
            continue;
        }

        if lines[i].in_html_block {
            continue;
        }

        if lines[i].is_blank {
            continue;
        }

        // Check for ATX headings (but skip MkDocs snippet lines)
        let is_snippet_line = if flavor == MarkdownFlavor::MkDocs {
            crate::utils::mkdocs_snippets::is_snippet_section_start(line)
                || crate::utils::mkdocs_snippets::is_snippet_section_end(line)
        } else {
            false
        };

        if !is_snippet_line && let Some(caps) = ATX_HEADING_REGEX.captures(line) {
            if crate::utils::skip_context::is_in_html_comment_ranges(html_comment_ranges, lines[i].byte_offset) {
                continue;
            }
            let line_offset = lines[i].byte_offset;
            if link_byte_ranges
                .iter()
                .any(|&(start, end)| line_offset > start && line_offset < end)
            {
                continue;
            }
            let leading_spaces = caps.get(1).map_or("", |m| m.as_str());
            let hashes = caps.get(2).map_or("", |m| m.as_str());
            let spaces_after = caps.get(3).map_or("", |m| m.as_str());
            let rest = caps.get(4).map_or("", |m| m.as_str());

            let level = hashes.len() as u8;
            let marker_column = leading_spaces.len();

            // Check for closing sequence, but handle custom IDs that might come after
            let (text, has_closing, closing_seq) = {
                let (rest_without_id, custom_id_part) = if let Some(id_start) = rest.rfind(" {#") {
                    if rest[id_start..].trim_end().ends_with('}') {
                        (&rest[..id_start], &rest[id_start..])
                    } else {
                        (rest, "")
                    }
                } else {
                    (rest, "")
                };

                let trimmed_rest = rest_without_id.trim_end();
                if let Some(last_hash_byte_pos) = trimmed_rest.rfind('#') {
                    let char_positions: Vec<(usize, char)> = trimmed_rest.char_indices().collect();

                    let last_hash_char_idx = char_positions
                        .iter()
                        .position(|(byte_pos, _)| *byte_pos == last_hash_byte_pos);

                    if let Some(mut char_idx) = last_hash_char_idx {
                        while char_idx > 0 && char_positions[char_idx - 1].1 == '#' {
                            char_idx -= 1;
                        }

                        let start_of_hashes = char_positions[char_idx].0;

                        let has_space_before = char_idx == 0 || char_positions[char_idx - 1].1.is_whitespace();

                        let potential_closing = &trimmed_rest[start_of_hashes..];
                        let is_all_hashes = potential_closing.chars().all(|c| c == '#');

                        if is_all_hashes && has_space_before {
                            let closing_hashes = potential_closing.to_string();
                            let text_part = if !custom_id_part.is_empty() {
                                format!("{}{}", trimmed_rest[..start_of_hashes].trim_end(), custom_id_part)
                            } else {
                                trimmed_rest[..start_of_hashes].trim_end().to_string()
                            };
                            (text_part, true, closing_hashes)
                        } else {
                            (rest.to_string(), false, String::new())
                        }
                    } else {
                        (rest.to_string(), false, String::new())
                    }
                } else {
                    (rest.to_string(), false, String::new())
                }
            };

            let content_column = marker_column + hashes.len() + spaces_after.len();

            let raw_text = text.trim().to_string();
            let (clean_text, mut custom_id) = crate::utils::header_id_utils::extract_header_id(&raw_text);

            if custom_id.is_none() && i + 1 < content_lines.len() && i + 1 < lines.len() {
                let next_line = content_lines[i + 1];
                if !lines[i + 1].in_code_block
                    && crate::utils::header_id_utils::is_standalone_attr_list(next_line)
                    && let Some(next_line_id) =
                        crate::utils::header_id_utils::extract_standalone_attr_list_id(next_line)
                {
                    custom_id = Some(next_line_id);
                }
            }

            let is_valid = !spaces_after.is_empty()
                || rest.is_empty()
                || level > 1
                || rest.trim().chars().next().is_some_and(|c| c.is_uppercase());

            lines[i].heading = Some(Box::new(HeadingInfo {
                level,
                style: HeadingStyle::ATX,
                marker: hashes.to_string(),
                marker_column,
                content_column,
                text: clean_text,
                custom_id,
                raw_text,
                has_closing_sequence: has_closing,
                closing_sequence: closing_seq,
                is_valid,
            }));
        }
        // Check for Setext headings (need to look at next line)
        else if i + 1 < content_lines.len() && i + 1 < lines.len() {
            let next_line = content_lines[i + 1];
            if !lines[i + 1].in_code_block && SETEXT_UNDERLINE_REGEX.is_match(next_line) {
                if front_matter_end > 0 && i < front_matter_end {
                    continue;
                }

                if crate::utils::skip_context::is_in_html_comment_ranges(html_comment_ranges, lines[i].byte_offset) {
                    continue;
                }

                let content_line = line.trim();

                if content_line.starts_with('-') || content_line.starts_with('*') || content_line.starts_with('+') {
                    continue;
                }

                if content_line.starts_with('_') {
                    let non_ws: String = content_line.chars().filter(|c| !c.is_whitespace()).collect();
                    if non_ws.len() >= 3 && non_ws.chars().all(|c| c == '_') {
                        continue;
                    }
                }

                if let Some(first_char) = content_line.chars().next()
                    && first_char.is_ascii_digit()
                {
                    let num_end = content_line.chars().take_while(|c| c.is_ascii_digit()).count();
                    if num_end < content_line.len() {
                        let next = content_line.chars().nth(num_end);
                        if next == Some('.') || next == Some(')') {
                            continue;
                        }
                    }
                }

                if ATX_HEADING_REGEX.is_match(line) {
                    continue;
                }

                if content_line.starts_with('>') {
                    continue;
                }

                let trimmed_start = line.trim_start();
                if trimmed_start.len() >= 3 {
                    let first_three: String = trimmed_start.chars().take(3).collect();
                    if first_three == "```" || first_three == "~~~" {
                        continue;
                    }
                }

                if content_line.starts_with('<') {
                    continue;
                }

                // Skip GFM table rows: a line that is part of a table cannot be
                // a Setext heading paragraph. A line is part of a table if:
                // - It starts with | and has a delimiter row above (body row), OR
                // - It IS a delimiter row with a pipe-containing header above (delimiter row)
                if content_line.starts_with('|') {
                    let mut is_in_table = false;

                    // Check if this line itself is a delimiter row with a header above
                    if TableUtils::is_delimiter_row(content_line)
                        && i > 0
                        && content_lines[i - 1].trim().contains('|')
                        && !lines[i - 1].in_code_block
                    {
                        is_in_table = true;
                    }

                    // Check if there's a delimiter row above (making this a body row)
                    if !is_in_table {
                        for j in (0..i).rev() {
                            let prev = content_lines[j].trim();
                            if prev.is_empty() || lines[j].in_code_block || lines[j].in_html_block {
                                break;
                            }
                            if TableUtils::is_delimiter_row(prev) {
                                is_in_table = true;
                                break;
                            }
                            if !prev.contains('|') {
                                break;
                            }
                        }
                    }

                    if is_in_table {
                        continue;
                    }
                }

                let underline = next_line.trim();

                let level = if underline.starts_with('=') { 1 } else { 2 };
                let style = if level == 1 {
                    HeadingStyle::Setext1
                } else {
                    HeadingStyle::Setext2
                };

                let raw_text = line.trim().to_string();
                let (clean_text, mut custom_id) = crate::utils::header_id_utils::extract_header_id(&raw_text);

                if custom_id.is_none() && i + 2 < content_lines.len() && i + 2 < lines.len() {
                    let attr_line = content_lines[i + 2];
                    if !lines[i + 2].in_code_block
                        && crate::utils::header_id_utils::is_standalone_attr_list(attr_line)
                        && let Some(attr_line_id) =
                            crate::utils::header_id_utils::extract_standalone_attr_list_id(attr_line)
                    {
                        custom_id = Some(attr_line_id);
                    }
                }

                lines[i].heading = Some(Box::new(HeadingInfo {
                    level,
                    style,
                    marker: underline.to_string(),
                    marker_column: next_line.len() - next_line.trim_start().len(),
                    content_column: lines[i].indent,
                    text: clean_text,
                    custom_id,
                    raw_text,
                    has_closing_sequence: false,
                    closing_sequence: String::new(),
                    is_valid: true,
                }));
            }
        }
    }
}

/// Detect HTML blocks in the content
pub(super) fn detect_html_blocks(content: &str, lines: &mut [LineInfo]) {
    const BLOCK_ELEMENTS: &[&str] = &[
        "address",
        "article",
        "aside",
        "audio",
        "blockquote",
        "canvas",
        "details",
        "dialog",
        "dd",
        "div",
        "dl",
        "dt",
        "embed",
        "fieldset",
        "figcaption",
        "figure",
        "footer",
        "form",
        "h1",
        "h2",
        "h3",
        "h4",
        "h5",
        "h6",
        "header",
        "hr",
        "iframe",
        "li",
        "main",
        "menu",
        "nav",
        "noscript",
        "object",
        "ol",
        "p",
        "picture",
        "pre",
        "script",
        "search",
        "section",
        "source",
        "style",
        "summary",
        "svg",
        "table",
        "tbody",
        "td",
        "template",
        "textarea",
        "tfoot",
        "th",
        "thead",
        "tr",
        "track",
        "ul",
        "video",
    ];

    let mut i = 0;
    while i < lines.len() {
        if lines[i].in_code_block || lines[i].in_front_matter {
            i += 1;
            continue;
        }

        let trimmed = lines[i].content(content).trim_start();

        if trimmed.starts_with('<') && trimmed.len() > 1 {
            let after_bracket = &trimmed[1..];
            let is_closing = after_bracket.starts_with('/');
            let tag_start = if is_closing { &after_bracket[1..] } else { after_bracket };

            let tag_name = tag_start
                .chars()
                .take_while(|c| c.is_ascii_alphabetic() || *c == '-' || c.is_ascii_digit())
                .collect::<String>()
                .to_lowercase();

            if !tag_name.is_empty() && BLOCK_ELEMENTS.contains(&tag_name.as_str()) {
                lines[i].in_html_block = true;

                if !is_closing {
                    let closing_tag = format!("</{tag_name}>");

                    let same_line_close = lines[i].content(content).contains(&closing_tag);

                    if !same_line_close {
                        let allow_blank_lines = tag_name == "style" || tag_name == "script";
                        let mut j = i + 1;
                        let mut found_closing_tag = false;
                        while j < lines.len() && j < i + 100 {
                            if !allow_blank_lines && lines[j].is_blank {
                                break;
                            }

                            lines[j].in_html_block = true;

                            if lines[j].content(content).contains(&closing_tag) {
                                found_closing_tag = true;
                            }

                            if found_closing_tag {
                                j += 1;
                                while j < lines.len() && j < i + 100 {
                                    if lines[j].is_blank {
                                        break;
                                    }
                                    lines[j].in_html_block = true;
                                    j += 1;
                                }
                                break;
                            }
                            j += 1;
                        }
                    }
                }
            }
        }

        i += 1;
    }
}