Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix expressions indent #152

Merged
merged 6 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/construct/partial_mdx_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@
//! [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};
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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub const INDENT_SIZE: usize = 4;
pub const INDENT_SIZE: usize = 2;

There were some inconsistencies in the JS APIs, value is 2 now: https://github.com/micromark/micromark-extension-mdx-expression/blob/7c305ffb7dbce7a452c54d4f4a5fbd2e19652be0/packages/micromark-factory-mdx-expression/dev/index.js#L37

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@bnchi bnchi Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In markdown-rs the indent size is 4

There's a comment which notes the above statement in here :

micromark/micromark-extension-mdx-expression@103af9a#diff-c2e30ee41c9d137472ba9d4238ac9f7397b19a6294ad47e259f58906b60f4fdfR257

so I think there might be some subtlety here which i don't quite understand

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one more thing: changing the indent size in here will break other tests

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, there are different places with different values. I managed to straighten that out in the JS world!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ll try my hand at this!


/// Start of an MDX expression.
///
/// ```markdown
Expand Down Expand Up @@ -180,7 +181,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:
//
Expand All @@ -200,12 +200,27 @@ 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);
State::Retry(StateName::MdxExpressionPrefix)
} 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.
Expand Down
2 changes: 2 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3376,6 +3376,8 @@ pub enum Name {
/// ^ ^ ^
/// ```
ThematicBreakSequence,

LinePrefix,
}

/// List of void events, used to make sure everything is working well.
Expand Down
2 changes: 2 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ pub enum Name {

MdxExpressionStart,
MdxExpressionBefore,
MdxExpressionPrefix,
MdxExpressionInside,
MdxExpressionEolAfter,

Expand Down Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions tests/mdx_expression_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 indent in `MdxFlowExpression` in mdast"
);

Ok(())
}

Expand Down
Loading