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

Add MDX optimize option #7151

Merged
merged 8 commits into from
May 25, 2023
Merged

Add MDX optimize option #7151

merged 8 commits into from
May 25, 2023

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented May 20, 2023

Changes

Add optimize option for @astrojs/mdx to optimize the MDX output for faster builds and rendering.

Most of the rehype code is copied from https://github.com/withastro/docs/blob/6b6f629e2241dce12dd4763124d00eeb223aa1f4/plugins/rehype-optimize-static.ts, but:

  • Removed special heading handling as this rehype plugin will always run last
  • Add custom components handling (export const components and from a config option)

Testing

Added tests to @astrojs/mdx. Also manually tested with Astro docs.

Docs

Added docs for the new option in the @astrojs/mdx README.

/cc @withastro/maintainers-docs for feedback!

@bluwy bluwy requested a review from a team as a code owner May 20, 2023 17:13
@changeset-bot
Copy link

changeset-bot bot commented May 20, 2023

🦋 Changeset detected

Latest commit: c67e5e1

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: integration Related to any renderer integration (scope) label May 20, 2023
Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

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

Thanks @bluwy ! I left some suggestions for your consideration below because I think we can try to make the explanations less implementation-y and more explanatory to the reader when and why to use this config option! See what you think, and let me know where I've gotten things wrong or missed something! 😅

@@ -183,6 +183,42 @@ These are plugins that modify the output [estree](https://github.com/estree/estr

We suggest [using AST Explorer](https://astexplorer.net/) to play with estree outputs, and trying [`estree-util-visit`](https://unifiedjs.com/explore/package/estree-util-visit/) for searching across JavaScript nodes.

### `optimize`
Copy link
Member

Choose a reason for hiding this comment

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

@bluwy Can you also add somthing around line 87 linking to this section?

Also, I'm wondering about the order of these 4 subsections. Optimizing sounds like a pretty important thing, so I might be tempted to put it first if you think it's going to be a no-brainer that most people would enable this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch! I'm thinking of putting this last as a special feature we can point people to turn on if they're having issues. Ideally in the next breaking release when we get good feedback, we can make it the default so the option won't stay here long.

packages/integrations/mdx/README.md Outdated Show resolved Hide resolved
packages/integrations/mdx/README.md Outdated Show resolved Hide resolved
packages/integrations/mdx/README.md Outdated Show resolved Hide resolved
@bluwy
Copy link
Member Author

bluwy commented May 23, 2023

Thanks for the review @sarah11918. I've updated the docs with hopefully friendlier explanations now 😄

packages/integrations/mdx/src/plugins.ts Show resolved Hide resolved
// stack for this node, things will still work but we won't repeatedly
// run the above for other nodes anymore. If this is confusing, you can
// comment out the code below when reading.
elementStack.length = 0;
Copy link
Member

Choose a reason for hiding this comment

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

nit: What confuses me is that we drain the elementStack inside an if block before another if block. Logically, we can enter both if, which means we can drain the stack, push nodes, and then do it again.

The comment fails to address that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah the idea is that we can and want to enter both if block, because if this node is non-optimizable, that doesn't mean we can't optimize the children into this node.

For example:

<Foo>
  <Bar>
    # this is markdown
  </Bar>
</Foo>

Can be turned into:

<Foo>
  <Bar set:html="<h1>this is markdown</h1>"></Bar>
</Foo>

So Bar here is something we want to track because in the leave hook later, we can add it to allPossibleElements.


I don't think it's easy to explain this as a comment because this optimization only comes clear when you read it last after understanding the (unfortunately complex 😅) algorithm.

Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't mind having a README.md next to this file that explains a bit about what we do :) it's not always easy to explain what's on our mind in a few comments! I completely understand

Copy link
Member Author

Choose a reason for hiding this comment

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

I think that's a good idea. I'll note a followup PR to document this so the docs can be reviewed separately.

packages/integrations/mdx/src/rehype-optimize-static.ts Outdated Show resolved Hide resolved
@ematipico
Copy link
Member

Great PR! 🚀

Copy link
Member

@yanthomasdev yanthomasdev left a comment

Choose a reason for hiding this comment

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

Docs LGTM!

Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

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

Looks great, @bluwy! Thanks for revisiting this! Just left a couple of thoughts! 🙌

packages/integrations/mdx/README.md Outdated Show resolved Hide resolved
packages/integrations/mdx/README.md Outdated Show resolved Hide resolved
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
@bluwy
Copy link
Member Author

bluwy commented May 24, 2023

Thanks @sarah11918. The couple suggestions look great to me. I made some small whitespace adjustments before merging your suggestions.

@bluwy bluwy merged commit ea16570 into main May 25, 2023
14 checks passed
@bluwy bluwy deleted the mdx-optimize branch May 25, 2023 11:43
@astrobot-houston astrobot-houston mentioned this pull request May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: integration Related to any renderer integration (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants