Skip to content

Commit d5a0f56

Browse files
authoredJan 28, 2025··
Fix expressions indent
Closes GH-150. Closes GH-152. Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 5c9eba1 commit d5a0f56

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed
 

Diff for: ‎src/construct/partial_mdx_expression.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,16 @@
5656
//! [mdx_expression_text]: crate::construct::mdx_expression_text
5757
//! [interleaving]: https://mdxjs.com/docs/what-is-mdx/#interleaving
5858
59-
use crate::construct::partial_space_or_tab::space_or_tab_min_max;
6059
use crate::event::Name;
6160
use crate::message;
6261
use crate::state::{Name as StateName, State};
6362
use crate::tokenizer::Tokenizer;
64-
use crate::util::{constant::TAB_SIZE, mdx_collect::collect};
63+
use crate::util::mdx_collect::collect;
6564
use crate::{MdxExpressionKind, MdxExpressionParse, MdxSignal};
6665
use alloc::boxed::Box;
6766

67+
pub const INDENT_SIZE: usize = 4;
68+
6869
/// Start of an MDX expression.
6970
///
7071
/// ```markdown
@@ -180,7 +181,6 @@ pub fn eol_after(tokenizer: &mut Tokenizer) -> State {
180181
}
181182
)
182183
} else if matches!(tokenizer.current, Some(b'\t' | b' ')) {
183-
tokenizer.attempt(State::Next(StateName::MdxExpressionBefore), State::Nok);
184184
// Idea: investigate if we’d need to use more complex stripping.
185185
// Take this example:
186186
//
@@ -200,12 +200,27 @@ pub fn eol_after(tokenizer: &mut Tokenizer) -> State {
200200
// the start of the expression and move past whitespace.
201201
// For future lines, we’d move at most to
202202
// `line_start_shifted.column + 4`.
203-
State::Retry(space_or_tab_min_max(tokenizer, 0, TAB_SIZE))
203+
tokenizer.enter(Name::LinePrefix);
204+
State::Retry(StateName::MdxExpressionPrefix)
204205
} else {
205206
State::Retry(StateName::MdxExpressionBefore)
206207
}
207208
}
208209

210+
pub fn prefix(tokenizer: &mut Tokenizer) -> State {
211+
tokenizer.tokenize_state.size_c += 1;
212+
if matches!(tokenizer.current, Some(b'\t' | b' '))
213+
&& tokenizer.tokenize_state.size_c < INDENT_SIZE - 1
214+
{
215+
tokenizer.consume();
216+
return State::Next(StateName::MdxExpressionPrefix);
217+
}
218+
219+
tokenizer.exit(Name::LinePrefix);
220+
tokenizer.tokenize_state.size_c = 0;
221+
State::Retry(StateName::MdxExpressionBefore)
222+
}
223+
209224
/// Parse an expression with a given function.
210225
fn parse_expression(tokenizer: &mut Tokenizer, parse: &MdxExpressionParse) -> State {
211226
// Collect the body of the expression and positional info for each run of it.

Diff for: ‎src/event.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3376,6 +3376,8 @@ pub enum Name {
33763376
/// ^ ^ ^
33773377
/// ```
33783378
ThematicBreakSequence,
3379+
3380+
LinePrefix,
33793381
}
33803382

33813383
/// List of void events, used to make sure everything is working well.

Diff for: ‎src/state.rs

+2
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ pub enum Name {
360360

361361
MdxExpressionStart,
362362
MdxExpressionBefore,
363+
MdxExpressionPrefix,
363364
MdxExpressionInside,
364365
MdxExpressionEolAfter,
365366

@@ -835,6 +836,7 @@ pub fn call(tokenizer: &mut Tokenizer, name: Name) -> State {
835836
Name::MdxEsmAtEnd => construct::mdx_esm::at_end,
836837

837838
Name::MdxExpressionStart => construct::partial_mdx_expression::start,
839+
Name::MdxExpressionPrefix => construct::partial_mdx_expression::prefix,
838840
Name::MdxExpressionBefore => construct::partial_mdx_expression::before,
839841
Name::MdxExpressionInside => construct::partial_mdx_expression::inside,
840842
Name::MdxExpressionEolAfter => construct::partial_mdx_expression::eol_after,

Diff for: ‎tests/mdx_expression_flow.rs

+13
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,19 @@ fn mdx_expression_flow_agnostic() -> Result<(), message::Message> {
144144
"should support mdx expressions (flow) as `MdxFlowExpression`s in mdast"
145145
);
146146

147+
assert_eq!(
148+
to_mdast(" {`\n a\n `}", &mdx.parse)?,
149+
Node::Root(Root {
150+
children: vec![Node::MdxFlowExpression(MdxFlowExpression {
151+
value: "`\n a\n`".into(),
152+
position: Some(Position::new(1, 3, 2, 3, 5, 15)),
153+
stops: vec![(0, 3), (1, 4), (2, 7), (5, 10), (6, 13)]
154+
})],
155+
position: Some(Position::new(1, 1, 0, 3, 5, 15))
156+
}),
157+
"should support indent in `MdxFlowExpression` in mdast"
158+
);
159+
147160
Ok(())
148161
}
149162

0 commit comments

Comments
 (0)
Please sign in to comment.