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
//! Setext headings
//!
//! Paragraph underlined with `===` or `---`.
//!
//! <https://spec.commonmark.org/0.30/#setext-headings>
use crate::parser::block::{BlockRule, BlockState};
use crate::parser::inline::InlineRoot;
use crate::plugins::cmark::block::paragraph::ParagraphScanner;
use crate::{MarkdownIt, Node, NodeValue, Renderer};

#[derive(Debug)]
pub struct SetextHeader {
    pub level: u8,
    pub marker: char,
}

impl NodeValue for SetextHeader {
    fn render(&self, node: &Node, fmt: &mut dyn Renderer) {
        static TAG : [&str; 2] = [ "h1", "h2" ];
        debug_assert!(self.level >= 1 && self.level <= 2);

        fmt.cr();
        fmt.open(TAG[self.level as usize - 1], &node.attrs);
        fmt.contents(&node.children);
        fmt.close(TAG[self.level as usize - 1]);
        fmt.cr();
    }
}

pub fn add(md: &mut MarkdownIt) {
    md.block.add_rule::<LHeadingScanner>()
        .before::<ParagraphScanner>()
        .after_all();
}

#[doc(hidden)]
pub struct LHeadingScanner;
impl BlockRule for LHeadingScanner {
    fn check(_: &mut BlockState) -> Option<()> {
        None // can't interrupt any tags
    }

    fn run(state: &mut BlockState) -> Option<(Node, usize)> {
        // if it's indented more than 3 spaces, it should be a code block
        if state.line_indent(state.line) >= 4 { return None; }

        let start_line = state.line;
        let mut next_line = start_line;
        let mut level = 0;

        'outer: loop {
            next_line += 1;

            if next_line >= state.line_max || state.is_empty(next_line) { break; }

            // this would be a code block normally, but after paragraph
            // it's considered a lazy continuation regardless of what's there
            if state.line_indent(next_line) >= 4 { continue; }

            //
            // Check for underline in setext header
            //
            if state.line_indent(next_line) >= 0 {
                let mut chars = state.get_line(next_line).chars().peekable();
                if let Some(marker @ ('-' | '=')) = chars.next() {
                    while Some(&marker) == chars.peek() { chars.next(); }
                    while let Some(' ' | '\t') = chars.peek() { chars.next(); }
                    if chars.next().is_none() {
                        level = if marker == '=' { 1 } else { 2 };
                        break 'outer;
                    }
                }
            }

            // quirk for blockquotes, this line should already be checked by that rule
            if state.line_offsets[next_line].indent_nonspace < 0 { continue; }

            // Some tags can terminate paragraph without empty line.
            let old_state_line = state.line;
            state.line = next_line;
            if state.test_rules_at_line() {
                state.line = old_state_line;
                break 'outer;
            }
            state.line = old_state_line;
        }


        if level == 0 {
            // Didn't find valid underline
            return None;
        }

        let (content, mapping) = state.get_lines(start_line, next_line, state.blk_indent, false);

        let mut node = Node::new(SetextHeader {
            level,
            marker: if level == 2 { '-' } else { '=' }
        });
        node.children.push(Node::new(InlineRoot::new(content, mapping)));

        Some((node, next_line + 1 - start_line))
    }
}