Skip to content

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

Merged
merged 8 commits into from
May 28, 2025

Conversation

cylewaitforit
Copy link
Contributor

Treats the layout in the mdx frontmatter in an Astro project as an import.

Related Issue: #1084

Copy link

pkg-pr-new bot commented May 25, 2025

Open in StackBlitz

npm i https://pkg.pr.new/knip@1102

commit: aeb4d1c

@cylewaitforit cylewaitforit marked this pull request as ready for review May 25, 2025 04:25
Copy link
Member

@webpro webpro left a 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;
Copy link
Member

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?

Copy link
Contributor Author

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.

let frontmatterImports = '';

// If this is an Astro project, also treat the layout path in the frontmatter as an import
if (mdxDependencies.includes('astro')) {
Copy link
Member

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).

Copy link
Contributor Author

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.


// 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']);
Copy link
Member

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()]

Copy link
Contributor Author

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.

@cylewaitforit cylewaitforit marked this pull request as draft May 26, 2025 17:32
@cylewaitforit cylewaitforit marked this pull request as ready for review May 26, 2025 19:48
@cylewaitforit
Copy link
Contributor Author

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.

@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).

@cylewaitforit cylewaitforit requested a review from webpro May 26, 2025 19:52
Copy link
Member

@webpro webpro left a 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.

@cylewaitforit cylewaitforit changed the title fix(astro): layout value from mdx frontmatter as import feat(astro-mdx): add compiler for @astrojs/mdx to treat layout value from mdx frontmatter as import May 28, 2025
@webpro webpro merged commit 4a8dd49 into webpro-nl:main May 28, 2025
29 checks passed
@webpro
Copy link
Member

webpro commented May 28, 2025

🚀 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.

@cylewaitforit cylewaitforit deleted the mdx-with-frontmatter branch July 2, 2025 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants