-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Blog Support 1/3: Data fetching #62
Conversation
2eb90d4
to
e58ce8b
Compare
src/compiler/codegen.ts
Outdated
// expose imported data to Astro script | ||
dataStatement += `const ${name} = [${data.map((_, j) => `${name}_${j}`).join(',')}];\n`; | ||
}) | ||
); |
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 adds import glob support, but fortunately in Astro it’s much, much easier. This is basically how it‘s transformed:
- import postData from './posts/*.md';
+ import { __content as postData_0 } from './posts/muppet-babies.md';
+ import { __content as postData_1 } from './posts/muppet-treasure-island.md';
+ // …
+ const postData = [postData_0, postData_1, … postData_8];
Once the import statement is passed off to Astro, Astro does the rest on its own.
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.
Also this part is a bit hacky, but it works for .md
files:
importPath.replace(/\.md$/, '')
Basically, if /pages/post/[file].md
was resolved, assign it a URL of the same location but strip the extension.
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 updated syntax is the same:
- const postData = import.meta.collection('./posts/*.md');
+ import { __content as postData_0 } from './posts/muppet-babies.md';
+ import { __content as postData_1 } from './posts/muppet-treasure-island.md';
+ // …
+ const postData = [postData_0, postData_1, … postData_8];
src/compiler/codegen.ts
Outdated
collectionItem = { name: 'Fragment', jsx: 'h(Fragment, null' }; | ||
items.push(collectionItem); | ||
} | ||
collectionItem.jsx += `,(${code.trim().replace(/\;$/, '')})`; |
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 the fix to the issue in Slack: if you have a top-level mustache statement, the compiler would throw before. Now we are simply wrapping it in a Fragment
.
import Pagination from '../components/Pagination.astro'; | ||
|
||
// posts | ||
import postData from './post/*.md'; |
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 the big decision to evaluate in this PR. This is basically saying that we don’t want you to define a top-level schema; we just want you to load things as you need to. That way you can fold in your own data like we do with authors.json
here.
<meta name="twitter:site" content="@astro" /> | ||
<meta name="twitter:title" content={title} /> | ||
<meta name="twitter:description" content={description} /> | ||
{image && (<meta name="twitter:image" content={image} />)} |
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 really love how clean this feels, but it does fall into the overloading native |
80c6534
to
c03496f
Compare
Changes
Edit: based on feedback, and discussions, we‘ll go with
import.meta.collection()
for now. So this will be handled in 3 steps:import.meta.collection()
support (this PR). Load markdown files, expose to page.Testing
Docs