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

feat: markdown config typechecking #2970

Merged
merged 19 commits into from
Apr 11, 2022
Merged

feat: markdown config typechecking #2970

merged 19 commits into from
Apr 11, 2022

Conversation

JuanM04
Copy link
Contributor

@JuanM04 JuanM04 commented Apr 2, 2022

Changes

Following the comment // TODO: add better type checking, I've added type checking for the markdown configuration

Testing

Docs

@changeset-bot
Copy link

changeset-bot bot commented Apr 2, 2022

🦋 Changeset detected

Latest commit: b01d281

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

This PR includes changesets to release 2 packages
Name Type
@astrojs/markdown-remark Patch
astro Patch

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

@JuanM04 JuanM04 requested a review from natemoo-re April 2, 2022 19:50
@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) feat: markdown Related to Markdown (scope) labels Apr 2, 2022
@@ -12,6 +13,9 @@ import loadTypeScript from '@proload/plugin-tsm';
import postcssrc from 'postcss-load-config';
import { arraify, isObject } from './util.js';
import { appendForwardSlash, trimSlashes } from './path.js';
// These two imports make astroMarkdownOptions work
Copy link
Member

Choose a reason for hiding this comment

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

wahhh??? Would love an explanation why here, for future readers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check out the new commit, I added a longer (not sure if better) explanation

Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

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

I'd remove any public references to the mode option for now, since it doesn't do anything.

Other than that, looks great!

Comment on lines +352 to +361
/**
* @docs
* @name markdown.mode
* @type {'md' | 'mdx'}
* @default `mdx`
* @description
* Control wheater to allow components inside markdown files ('mdx') or not ('md').
*/
mode?: 'md' | 'mdx';

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this actually does anything right now, which is why we didn't include it!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does add rehypeJsx and rehypeExpressions. I could remove the MDX mode altogether if these plugins do nothing

Copy link
Member

Choose a reason for hiding this comment

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

Ah no you're right! I also saw that #2971 added escaping for expressions when mode === 'md'. So I think this is fine!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After doing some testing, the only difference I've found is that components are being rendered with mdx but completely ignored with md. Expressions works in both modes, tho.

@FredKSchott FredKSchott self-requested a review April 4, 2022 06:22
Copy link
Member

@FredKSchott FredKSchott left a comment

Choose a reason for hiding this comment

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

Marking as red just to make sure we hold off on this until post release day. I have some comments that I'd like to make but just won't have the time until Monday/Tuesday to make them. Appreciate the patience!

.passthrough()
.optional()
.default({}),
markdown: astroMarkdownOptions.default({}),
Copy link
Member

Choose a reason for hiding this comment

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

Thanks again for your patience on this review @JuanM04!

My main comment is that we really benefit from having all of our config parsing logic here in one file and in one single parser object instantiation.

Reading this PR, I can see the value you've put into validation reuse, which is great. But, I'm also bouncing around to follow where the different parsers / validation logic lives, defined across multiple files, and even multiple packages, with multiple zod parser instances referencing each other.

If you'd like to improve the existing markdown: z.object(... validation logic defined here, I'm all for it. I could even see the value in a new Plugin validator that both remarkPlugins and rehypePlugins, which breaks up the single-object-validator that I love but for a valid, valuable reason.

But at the end of the day, Astro owns the "Astro Config" type and therefore its validation, so I'd like to keep it as simple as possible and only inside the Astro package, for our sake as maintainers.

@JuanM04 lmk what you think, and how you'd like to move forward!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're totally right. The idea behind the division was about separation of concerns, since @astrojs/markdown-remark is its own package, but I see how that can be a problem.

I'll integrate this configuration in a single place

@JuanM04
Copy link
Contributor Author

JuanM04 commented Apr 11, 2022

@FredKSchott I've moved the validation inside config.ts. Also, I've removed all the internal references to the old markdownRender way of importing markdown, so it's cleaner and doesn't do unnecessary work

@JuanM04
Copy link
Contributor Author

JuanM04 commented Apr 11, 2022

Note: the error thrown by Deno it's not caused by this PR directly. If you have an Astro page with the <Markdown /> component and build it with Deno, it will throw "No Markdown parser found"

Copy link
Member

@FredKSchott FredKSchott left a comment

Choose a reason for hiding this comment

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

Nice! This is looking great, thanks for making those changes. LGTM!

@FredKSchott
Copy link
Member

Note: the error thrown by Deno it's not caused by this PR directly. If you have an Astro page with the <Markdown /> component and build it with Deno, it will throw "No Markdown parser found"

Can't merge with failing tests! What's the best way to resolve?

@JuanM04
Copy link
Contributor Author

JuanM04 commented Apr 11, 2022

Can't merge with failing tests! What's the best way to resolve?

I'm looking into it. Either a fix is found or the test is ignored

@FredKSchott
Copy link
Member

As long as you can confirm that this isn't a regression caused by this PR

@JuanM04
Copy link
Contributor Author

JuanM04 commented Apr 11, 2022

Ok, this way it should work. The markdown plugin now is dynamically imported only when using <Markdown /> and Node.js. When using Deno, it throws an error because it isn't compatible

Sidenote: I've just found out that this error was encountered in another PR: #3036 (comment)

Copy link
Member

@FredKSchott FredKSchott left a comment

Choose a reason for hiding this comment

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

LGTM!

@FredKSchott FredKSchott merged commit b835e28 into main Apr 11, 2022
@FredKSchott FredKSchott deleted the feat/new-config-md branch April 11, 2022 23:01
@github-actions github-actions bot mentioned this pull request Apr 11, 2022
SiriousHunter pushed a commit to SiriousHunter/astro that referenced this pull request Feb 3, 2023
* Added schemas to markdown plugin

* Added new schemas to main package

* Changesets

* typeraw

* Explaination about the weird type hack

* Added markdown.mode to config

* Added comment

* Formatted

* Moved validation to `astro` and added RemarkPlugin ad RehypePlugin

* Removed the ability to have a custom markdown renderer internally

* Fixed plugin type

* Removed unused renderMarkdownWithFrontmatter

* Added missing dependency

* Dynamically import astro markdown

* Cache import
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: markdown Related to Markdown (scope) pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants