From 5868c5d89b0085b89f69e4c481efcf6264910de3 Mon Sep 17 00:00:00 2001 From: Bnchi Date: Thu, 17 Oct 2024 17:29:35 +0300 Subject: [PATCH 1/5] Fix expressions indent --- src/construct/partial_mdx_expression.rs | 24 +++++++++++++++++++++--- src/event.rs | 2 ++ src/state.rs | 2 ++ tests/mdx_expression_flow.rs | 15 ++++++++++++++- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/construct/partial_mdx_expression.rs b/src/construct/partial_mdx_expression.rs index 22d0eb5b..e14ac7e8 100644 --- a/src/construct/partial_mdx_expression.rs +++ b/src/construct/partial_mdx_expression.rs @@ -61,10 +61,12 @@ use crate::event::Name; use crate::message; use crate::state::{Name as StateName, State}; use crate::tokenizer::Tokenizer; -use crate::util::{constant::TAB_SIZE, mdx_collect::collect}; +use crate::util::mdx_collect::collect; use crate::{MdxExpressionKind, MdxExpressionParse, MdxSignal}; use alloc::boxed::Box; +pub const INDENT_SIZE: usize = 4; + /// Start of an MDX expression. /// /// ```markdown @@ -180,7 +182,6 @@ pub fn eol_after(tokenizer: &mut Tokenizer) -> State { } ) } else if matches!(tokenizer.current, Some(b'\t' | b' ')) { - tokenizer.attempt(State::Next(StateName::MdxExpressionBefore), State::Nok); // Idea: investigate if we’d need to use more complex stripping. // Take this example: // @@ -200,12 +201,29 @@ pub fn eol_after(tokenizer: &mut Tokenizer) -> State { // the start of the expression and move past whitespace. // For future lines, we’d move at most to // `line_start_shifted.column + 4`. - State::Retry(space_or_tab_min_max(tokenizer, 0, TAB_SIZE)) + //State::Retry(space_or_tab_min_max(tokenizer, 0, TAB_SIZE)) + tokenizer.enter(Name::LinePrefix); + prefix(tokenizer) } else { State::Retry(StateName::MdxExpressionBefore) } } +pub fn prefix(tokenizer: &mut Tokenizer) -> State { + tokenizer.tokenize_state.size_c += 1; + if matches!(tokenizer.current, Some(b'\t' | b' ')) + && tokenizer.tokenize_state.size_c < INDENT_SIZE - 1 + { + tokenizer.consume(); + return State::Next(StateName::MdxExpressionPrefix); + } + + tokenizer.exit(Name::LinePrefix); + tokenizer.tokenize_state.size_c = 0; + + State::Retry(StateName::MdxExpressionBefore) +} + /// Parse an expression with a given function. fn parse_expression(tokenizer: &mut Tokenizer, parse: &MdxExpressionParse) -> State { // Collect the body of the expression and positional info for each run of it. diff --git a/src/event.rs b/src/event.rs index 3e8d5139..1ccff847 100644 --- a/src/event.rs +++ b/src/event.rs @@ -3376,6 +3376,8 @@ pub enum Name { /// ^ ^ ^ /// ``` ThematicBreakSequence, + + LinePrefix, } /// List of void events, used to make sure everything is working well. diff --git a/src/state.rs b/src/state.rs index cb32ea8a..5e765c79 100644 --- a/src/state.rs +++ b/src/state.rs @@ -360,6 +360,7 @@ pub enum Name { MdxExpressionStart, MdxExpressionBefore, + MdxExpressionPrefix, MdxExpressionInside, MdxExpressionEolAfter, @@ -835,6 +836,7 @@ pub fn call(tokenizer: &mut Tokenizer, name: Name) -> State { Name::MdxEsmAtEnd => construct::mdx_esm::at_end, Name::MdxExpressionStart => construct::partial_mdx_expression::start, + Name::MdxExpressionPrefix => construct::partial_mdx_expression::prefix, Name::MdxExpressionBefore => construct::partial_mdx_expression::before, Name::MdxExpressionInside => construct::partial_mdx_expression::inside, Name::MdxExpressionEolAfter => construct::partial_mdx_expression::eol_after, diff --git a/tests/mdx_expression_flow.rs b/tests/mdx_expression_flow.rs index 49c89b97..d1a822ba 100644 --- a/tests/mdx_expression_flow.rs +++ b/tests/mdx_expression_flow.rs @@ -1,6 +1,6 @@ mod test_utils; use markdown::{ - mdast::{MdxFlowExpression, Node, Root}, + mdast::{MdxFlowExpression, MdxTextExpression, Node, Paragraph, Root, Text}, message, to_html_with_options, to_mdast, unist::Position, Constructs, Options, ParseOptions, @@ -144,6 +144,19 @@ fn mdx_expression_flow_agnostic() -> Result<(), message::Message> { "should support mdx expressions (flow) as `MdxFlowExpression`s in mdast" ); + assert_eq!( + to_mdast(" {`\n a\n `}", &mdx.parse)?, + Node::Root(Root { + children: vec![Node::MdxFlowExpression(MdxFlowExpression { + value: "`\n a\n`".into(), + position: Some(Position::new(1, 3, 2, 3, 5, 15)), + stops: vec![(0, 3), (1, 4), (2, 7), (5, 10), (6, 13)] + })], + position: Some(Position::new(1, 1, 0, 3, 5, 15)) + }), + "should support mdx expressions (flow) as `MdxFlowExpression`s in mdast" + ); + Ok(()) } From 1a6a0460185cec7c4bab1f7b7b1649e8b901ba58 Mon Sep 17 00:00:00 2001 From: Bnchi Date: Thu, 17 Oct 2024 17:33:25 +0300 Subject: [PATCH 2/5] Remove comments --- src/construct/partial_mdx_expression.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/construct/partial_mdx_expression.rs b/src/construct/partial_mdx_expression.rs index e14ac7e8..e9d03105 100644 --- a/src/construct/partial_mdx_expression.rs +++ b/src/construct/partial_mdx_expression.rs @@ -201,7 +201,6 @@ pub fn eol_after(tokenizer: &mut Tokenizer) -> State { // the start of the expression and move past whitespace. // For future lines, we’d move at most to // `line_start_shifted.column + 4`. - //State::Retry(space_or_tab_min_max(tokenizer, 0, TAB_SIZE)) tokenizer.enter(Name::LinePrefix); prefix(tokenizer) } else { @@ -220,7 +219,6 @@ pub fn prefix(tokenizer: &mut Tokenizer) -> State { tokenizer.exit(Name::LinePrefix); tokenizer.tokenize_state.size_c = 0; - State::Retry(StateName::MdxExpressionBefore) } From 3d20731007799d0e19e27dc5c38e4d00c23cc2fd Mon Sep 17 00:00:00 2001 From: Bnchi Date: Thu, 17 Oct 2024 17:41:43 +0300 Subject: [PATCH 3/5] Update test assertion message --- tests/mdx_expression_flow.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/mdx_expression_flow.rs b/tests/mdx_expression_flow.rs index d1a822ba..af4df0fd 100644 --- a/tests/mdx_expression_flow.rs +++ b/tests/mdx_expression_flow.rs @@ -154,7 +154,7 @@ fn mdx_expression_flow_agnostic() -> Result<(), message::Message> { })], position: Some(Position::new(1, 1, 0, 3, 5, 15)) }), - "should support mdx expressions (flow) as `MdxFlowExpression`s in mdast" + "should support indent in `MdxFlowExpression` in mdast" ); Ok(()) From 9bce5d19312e51dd4f1e2a65eba8a765650d04b5 Mon Sep 17 00:00:00 2001 From: Bnchi Date: Fri, 18 Oct 2024 16:58:55 +0300 Subject: [PATCH 4/5] Don't call prefix straight --- src/construct/partial_mdx_expression.rs | 3 +-- tests/mdx_expression_flow.rs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/construct/partial_mdx_expression.rs b/src/construct/partial_mdx_expression.rs index e9d03105..aeac26ee 100644 --- a/src/construct/partial_mdx_expression.rs +++ b/src/construct/partial_mdx_expression.rs @@ -56,7 +56,6 @@ //! [mdx_expression_text]: crate::construct::mdx_expression_text //! [interleaving]: https://mdxjs.com/docs/what-is-mdx/#interleaving -use crate::construct::partial_space_or_tab::space_or_tab_min_max; use crate::event::Name; use crate::message; use crate::state::{Name as StateName, State}; @@ -202,7 +201,7 @@ pub fn eol_after(tokenizer: &mut Tokenizer) -> State { // For future lines, we’d move at most to // `line_start_shifted.column + 4`. tokenizer.enter(Name::LinePrefix); - prefix(tokenizer) + return State::Retry(StateName::MdxExpressionPrefix); } else { State::Retry(StateName::MdxExpressionBefore) } diff --git a/tests/mdx_expression_flow.rs b/tests/mdx_expression_flow.rs index af4df0fd..e0b6a5a4 100644 --- a/tests/mdx_expression_flow.rs +++ b/tests/mdx_expression_flow.rs @@ -1,6 +1,6 @@ mod test_utils; use markdown::{ - mdast::{MdxFlowExpression, MdxTextExpression, Node, Paragraph, Root, Text}, + mdast::{MdxFlowExpression, Node, Root}, message, to_html_with_options, to_mdast, unist::Position, Constructs, Options, ParseOptions, From c2a4dbc01ec9b2a8910c8029fc4b48a40b72fab8 Mon Sep 17 00:00:00 2001 From: Bnchi Date: Fri, 18 Oct 2024 17:03:26 +0300 Subject: [PATCH 5/5] Try to fix clippy --- src/construct/partial_mdx_expression.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/construct/partial_mdx_expression.rs b/src/construct/partial_mdx_expression.rs index aeac26ee..fd480248 100644 --- a/src/construct/partial_mdx_expression.rs +++ b/src/construct/partial_mdx_expression.rs @@ -201,7 +201,7 @@ pub fn eol_after(tokenizer: &mut Tokenizer) -> State { // For future lines, we’d move at most to // `line_start_shifted.column + 4`. tokenizer.enter(Name::LinePrefix); - return State::Retry(StateName::MdxExpressionPrefix); + State::Retry(StateName::MdxExpressionPrefix) } else { State::Retry(StateName::MdxExpressionBefore) }