-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Changes from 11 commits
7d2575f
48a75cb
ab59fcf
6e2a770
915abcc
34515c4
32e4659
bacf5c0
7a0a853
9ca8234
98fd50d
df1f3ee
f77ac34
210496f
8159fe9
25b51ff
440f604
ebca862
b01d281
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/markdown-remark': patch | ||
--- | ||
|
||
Improved type checking |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Improved markdown config type checking |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import type { AstroConfig, AstroUserConfig, CLIFlags } from '../@types/astro'; | |
import type { Arguments as Flags } from 'yargs-parser'; | ||
import type * as Postcss from 'postcss'; | ||
|
||
import { astroMarkdownOptions } from '@astrojs/markdown-remark'; | ||
import * as colors from 'kleur/colors'; | ||
import path from 'path'; | ||
import { pathToFileURL, fileURLToPath } from 'url'; | ||
|
@@ -13,6 +14,12 @@ import postcssrc from 'postcss-load-config'; | |
import { arraify, isObject } from './util.js'; | ||
import { appendForwardSlash, trimSlashes } from './path.js'; | ||
|
||
// These two imports make astroMarkdownOptions work | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wahhh??? Would love an explanation why here, for future readers There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
// If you remove them, TypeScript throws an error similar to this: | ||
// `The inferred type of 'AstroConfigSchema' cannot be named without a reference to '../../../markdown/remark/node_modules/@types/unist'. This is likely not portable. A type annotation is necessary` | ||
import type {} from 'shiki'; | ||
import type {} from 'unist'; | ||
|
||
load.use([loadTypeScript]); | ||
|
||
interface PostCSSConfigResult { | ||
|
@@ -140,27 +147,7 @@ export const AstroConfigSchema = z.object({ | |
}) | ||
.optional() | ||
.default({}), | ||
markdown: z | ||
.object({ | ||
drafts: z.boolean().optional().default(false), | ||
mode: z | ||
.union([z.literal('md'), z.literal('mdx')]) | ||
.optional() | ||
// NOTE: "mdx" allows us to parse/compile Astro components in markdown. | ||
// TODO: This should probably be updated to something more like "md" | "astro" | ||
.default('mdx'), | ||
syntaxHighlight: z | ||
.union([z.literal('shiki'), z.literal('prism'), z.literal(false)]) | ||
.optional() | ||
.default('shiki'), | ||
// TODO: add better type checking | ||
shikiConfig: z.any().optional().default({}), | ||
remarkPlugins: z.array(z.any()).optional().default([]), | ||
rehypePlugins: z.array(z.any()).optional().default([]), | ||
}) | ||
.passthrough() | ||
.optional() | ||
.default({}), | ||
markdown: astroMarkdownOptions.default({}), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 I'll integrate this configuration in a single place |
||
vite: z.any().optional().default({}), | ||
experimental: z | ||
.object({ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does add
rehypeJsx
andrehypeExpressions
. I could remove the MDX mode altogether if these plugins do nothingThere was a problem hiding this comment.
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!There was a problem hiding this comment.
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 withmd
. Expressions works in both modes, tho.