-
-
Notifications
You must be signed in to change notification settings - Fork 265
feat(astro-mdx): add compiler for @astrojs/mdx to treat layout value from mdx frontmatter as import #1102
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
Conversation
commit: |
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.
Thanks for the PR. Seeing this, I'm a bit on the fence when it comes to the matter of adding more complexity to the already "hacky" compilers. This is a "cheap" solution without dependencies, but what's next? Also, can layout
be some other user-defined key in the frontmatter without issues?
Just wondering, wouldn't something like https://knip.dev/features/compilers#mdx but with @astrojs/mdx
be feasible? If we can support this layout
somewhat nicely & safely without users needing to config anything would still be nice, though.
@@ -28,3 +28,16 @@ export const scriptBodies: SyncCompilerFn = (text: string) => { | |||
} | |||
return scripts.join(';\n'); | |||
}; | |||
|
|||
// Extract paths as imports from frontmatter for given keys (e.g., 'layout') | |||
const frontmatterMatcher = /---[\s\S]*?---/g; |
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.
This is a greedy match, while there can only be a single frontmatter I think?
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 believe there would only be one. Updated the frontmatter matcher to not be a greedy match.
packages/knip/src/compilers/mdx.ts
Outdated
let frontmatterImports = ''; | ||
|
||
// If this is an Astro project, also treat the layout path in the frontmatter as an import | ||
if (mdxDependencies.includes('astro')) { |
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.
The generic mdxDependencies
array contains enablers for the mdx compiler (it's not specific to the project).
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.
That makes sense. Seems like one option would be to treat layout as a frontmatter for all mdx but that seemed wasteful. Instead I moved AstroMDX as its own compiler and swapped that in the getIncludedCompilers if there was a dependency match.
packages/knip/src/compilers/mdx.ts
Outdated
|
||
// If this is an Astro project, also treat the layout path in the frontmatter as an import | ||
if (mdxDependencies.includes('astro')) { | ||
frontmatterImports = importsWithinFrontmatter(text, ['layout']); |
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.
Feels a bit odd with array/string concatenation. How about working with an array of strings only, just until the end? Empty strings aren't an issue then either.
Or even something like return [...text.replace(), ...importsWithinFrontmatter()]
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.
Agree, I think working with array of strings until the end is easier to follow.
@webpro I iterated more on this based on the feedback. I think it still feels mostly "hacky". I will feel no real loss if this doesn't feel like the right direction to solve this. At least if it's wrong it gives a better picture of what right might end up looking like( or not looking like). |
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.
Thanks for working on this! I like that less users have to install a custom compiler, without affecting deps/perf negatively. If it unexpectedly gives trouble we can always fix/revert.
Co-authored-by: Lars Kappert <[email protected]>
🚀 This pull request is included in v5.59.0. See Release 5.59.0 for release notes. Using Knip in a commercial project? Please consider becoming a sponsor. |
Treats the layout in the mdx frontmatter in an Astro project as an import.
Related Issue: #1084