Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
AugustinMauroy committed Feb 25, 2025
1 parent 7b7ef04 commit b9a1cb9
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
21 changes: 21 additions & 0 deletions crates/biome_markdown_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,31 @@ impl<'src> MarkdownLexer<'src> {
match dispatched {
WHS => self.consume_newline_or_whitespace(),
MUL | MIN | IDT => self.consume_thematic_break_literal(),
HSH => self.consume_header(),
_ => self.consume_textual(),
}
}

fn consume_header(&mut self) -> MarkdownSyntaxKind {
self.assert_at_char_boundary();

let mut level = 0;
while matches!(self.current_byte(), Some(b'#')) {
self.advance(1);
level += 1;
}

match level {
1 => MD_HEADER1,
2 => MD_HEADER2,
3 => MD_HEADER3,
4 => MD_HEADER4,
5 => MD_HEADER5,
6 => MD_HEADER6,
_ => ERROR_TOKEN,
}
}

fn text_position(&self) -> TextSize {
TextSize::try_from(self.position).expect("Input to be smaller than 4 GB")
}
Expand Down
30 changes: 30 additions & 0 deletions crates/biome_markdown_parser/src/lexer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,36 @@ fn whitespace() {
}
}

#[test]
fn heading_level_1() {
assert_lex! {
"# Heading 1",
MD_HEADER1:1,
WHITESPACE:1,
MD_TEXTUAL_LITERAL:9,
}
}

#[test]
fn heading_level_2() {
assert_lex! {
"## Heading 2",
MD_HEADER2:2,
WHITESPACE:1,
MD_TEXTUAL_LITERAL:9,
}
}

#[test]
fn heading_level_3() {
assert_lex! {
"### Heading 3",
MD_HEADER3:3,
WHITESPACE:1,
MD_TEXTUAL_LITERAL:9,
}
}

#[test]
fn thematic_break_literal() {
assert_lex! {
Expand Down
7 changes: 7 additions & 0 deletions xtask/codegen/markdown.ungram
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ AnyContainerBlock = MdQuote | MdBulletListItem | MdOrderListItem
// h1..h6
MdHeader = before:MdHashList MdParagraph? after:MdHashList

MdHeader1 = before:MdHashList MdParagraph? after:MdHashList
MdHeader2 = before:MdHashList MdParagraph? after:MdHashList
MdHeader3 = before:MdHashList MdParagraph? after:MdHashList
MdHeader4 = before:MdHashList MdParagraph? after:MdHashList
MdHeader5 = before:MdHashList MdParagraph? after:MdHashList
MdHeader6 = before:MdHashList MdParagraph? after:MdHashList

MdHashList = MdHash*

MdHash = '#'
Expand Down
7 changes: 6 additions & 1 deletion xtask/codegen/src/markdown_kinds_src.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ pub const MARKDOWN_KINDS_SRC: KindsSrc = KindsSrc {
"MD_BLOCK_LIST",
"MD_HASH_LIST",
"MD_HASH",
"MD_HEADER",
"MD_HEADER1",
"MD_HEADER2",
"MD_HEADER3",
"MD_HEADER4",
"MD_HEADER5",
"MD_HEADER6",
"MD_INDENT_CODE_BLOCK",
"MD_FENCED_CODE_BLOCK",
"MD_HTML_BLOCK",
Expand Down

0 comments on commit b9a1cb9

Please sign in to comment.