Skip to main content

oxc_markdown_parser/block/
lexical.rs

1//! Lexical predicates: grammar facts for formatters.
2//!
3//! A markdown formatter constantly synthesizes new line starts
4//! (joining lines for prose wrap, stripping indent, removing escapes).
5//! Whether the synthesized line would open a block is a grammar fact,
6//! and approximating it downstream is the classic printer failure mode
7//! (Prettier's `/^>|^(?:[*+-]|#{1,6}|\d+[).])$/` misses `:::`, fences, `<`, and `|`.
8//! Each a documented meaning-breaking bug).
9//! This module exposes the parser's own classification instead:
10//! [`line_start`] runs the exact probe table the block engine uses, so the two cannot drift.
11//!
12//! Policy-free: these functions state what the grammar would do with a line,
13//! never what a formatter should do about it.
14
15use crate::options::Constructs;
16use crate::syntax::run_len;
17
18use super::probe::{Start, probe};
19use super::scan;
20
21/// What a line would open at flow position.
22/// One variant per block construct;
23/// the payload-free mirror of the engine's probe result.
24#[derive(Clone, Copy, Debug, PartialEq, Eq)]
25#[non_exhaustive]
26pub enum LineStart {
27    Blockquote,
28    /// Only reported with `in_paragraph` (an underline needs a paragraph).
29    SetextUnderline,
30    ThematicBreak,
31    AtxHeading,
32    CodeFence,
33    HtmlBlock,
34    MathFence,
35    Liquid,
36    /// A directive opening fence (`:::name…`): opens a container, interrupts a paragraph.
37    DirectiveOpener,
38    /// A bare `:::` run: closes an open directive, inert otherwise.
39    DirectiveCloser,
40    FootnoteDefinition,
41    ListItem,
42    /// Only reported with `in_paragraph`
43    /// (a delimiter row activates by stealing the paragraph's last line as the header).
44    TableDelimiterRow,
45    /// A `|`-initiated line: a row if a table is open or forming.
46    TableRow,
47}
48
49/// The number of an ordered list marker (`123.` / `123)`), for a printer that renumbers.
50/// CommonMark caps markers at 9 digits, so the value always fits; `None` for a bullet.
51pub fn ordered_number(marker: &str) -> Option<u32> {
52    marker.trim_end_matches(['.', ')']).parse().ok()
53}
54
55/// Classifies what `line` (one line, no line ending) would start as a block,
56/// or `None` for paragraph text.
57/// `in_paragraph` enables the constructs that only exist relative to an open paragraph.
58///
59/// Leading indent is handled here: 4+ columns of indent is indented code (never a block start),
60/// per the default `code_indented` construct.
61pub fn line_start(constructs: &Constructs, line: &str, in_paragraph: bool) -> Option<LineStart> {
62    let indent = run_len(line.as_bytes(), b' ');
63    // 4+ spaces, or a tab anywhere in the first ≤3 columns
64    // (a tab there always advances past column 4): indented code, not a block start.
65    if indent >= 4 || line.as_bytes().get(indent) == Some(&b'\t') {
66        return None;
67    }
68    let tail = &line[indent..];
69    if let Some(start) = probe(constructs, tail, in_paragraph) {
70        return Some(match start {
71            Start::Quote => LineStart::Blockquote,
72            Start::Setext(_) => LineStart::SetextUnderline,
73            Start::ThematicBreak => LineStart::ThematicBreak,
74            Start::Atx { .. } => LineStart::AtxHeading,
75            Start::Fence { .. } => LineStart::CodeFence,
76            Start::Html { .. } => LineStart::HtmlBlock,
77            Start::MathFence { .. } => LineStart::MathFence,
78            Start::Liquid => LineStart::Liquid,
79            Start::Directive { .. } => LineStart::DirectiveOpener,
80            Start::FootnoteDef { .. } => LineStart::FootnoteDefinition,
81            Start::ListItem { .. } => LineStart::ListItem,
82            Start::TableDelimiter(_) => LineStart::TableDelimiterRow,
83        });
84    }
85    // Below the probe table:
86    // line shapes that are inert on their own but significant next to an open construct.
87    // Exactly what a formatter must not create by joining lines.
88    if constructs.container_directive && scan::fence_close(tail, b':', 3) {
89        return Some(LineStart::DirectiveCloser);
90    }
91    if constructs.gfm_table && tail.starts_with('|') {
92        return Some(LineStart::TableRow);
93    }
94    None
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    fn md(line: &str, in_paragraph: bool) -> Option<LineStart> {
102        line_start(&Constructs::markdown(), line, in_paragraph)
103    }
104
105    #[test]
106    fn probe_table_mirrors_engine() {
107        assert_eq!(md("> q", false), Some(LineStart::Blockquote));
108        assert_eq!(md("---", true), Some(LineStart::SetextUnderline));
109        assert_eq!(md("---", false), Some(LineStart::ThematicBreak));
110        assert_eq!(md("# h", false), Some(LineStart::AtxHeading));
111        assert_eq!(md("```rs", false), Some(LineStart::CodeFence));
112        assert_eq!(md("<div>", false), Some(LineStart::HtmlBlock));
113        assert_eq!(md("$$", false), Some(LineStart::MathFence));
114        assert_eq!(md("{% t %}", false), Some(LineStart::Liquid));
115        assert_eq!(md("[^1]: n", false), Some(LineStart::FootnoteDefinition));
116        assert_eq!(md("- item", false), Some(LineStart::ListItem));
117        assert_eq!(md("plain text", true), None);
118    }
119
120    #[test]
121    fn ordered_numbers() {
122        assert_eq!(ordered_number("007."), Some(7));
123        assert_eq!(ordered_number("999999999)"), Some(999_999_999));
124        assert_eq!(ordered_number("-"), None);
125    }
126
127    #[test]
128    fn covers_prettiers_documented_misses() {
129        // The regex holes behind Blume's directive corruption and #19847.
130        assert_eq!(md(":::note", false), Some(LineStart::DirectiveOpener));
131        assert_eq!(md(":::", false), Some(LineStart::DirectiveCloser));
132        assert_eq!(md(":::)", false), None, "an emoticon is paragraph text");
133        assert_eq!(md("| - | - |", true), Some(LineStart::TableDelimiterRow));
134        assert_eq!(md("| a | b |", false), Some(LineStart::TableRow));
135    }
136
137    #[test]
138    fn indent_is_indented_code() {
139        assert_eq!(md("    # not a heading", false), None);
140        assert_eq!(md("\t- not a list", false), None);
141        assert_eq!(md("  \t- not a list", false), None);
142        assert_eq!(md("   - list", false), Some(LineStart::ListItem));
143    }
144
145    /// The rows below the probe table carry their own construct gates
146    /// (the probe rows are pinned in `probe`'s tests).
147    #[test]
148    fn construct_gating() {
149        let mut c = Constructs::markdown();
150        c.container_directive = false;
151        c.gfm_table = false;
152        assert_eq!(line_start(&c, ":::", false), None);
153        assert_eq!(line_start(&c, "| a |", false), None);
154    }
155}