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

Blog Support 1/3: Data fetching #62

Merged
merged 8 commits into from
Apr 6, 2021
Merged

Blog Support 1/3: Data fetching #62

merged 8 commits into from
Apr 6, 2021

Conversation

drwpow
Copy link
Member

@drwpow drwpow commented Apr 6, 2021

Changes

Edit: based on feedback, and discussions, we‘ll go with import.meta.collection() for now. So this will be handled in 3 steps:

  1. import.meta.collection() support (this PR). Load markdown files, expose to page.
  2. Pagination/filtering/sorting/etc. Expanding on Add watch mode #1, basically. This makes pagination possible, but annoying to use.
  3. Dynamic routes: needed to make pagination elegant (but not just pagination—any kind of filtering, such as tags, posts by year, searching, etc. etc.)

Testing

  • Tests are passing
  • Tests updated where necessary

Docs

  • Docs / READMEs updated
  • Code comments added where helpful

@drwpow drwpow force-pushed the blog-pagination branch 3 times, most recently from 2eb90d4 to e58ce8b Compare April 6, 2021 00:26
// expose imported data to Astro script
dataStatement += `const ${name} = [${data.map((_, j) => `${name}_${j}`).join(',')}];\n`;
})
);
Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Member Author

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];

collectionItem = { name: 'Fragment', jsx: 'h(Fragment, null' };
items.push(collectionItem);
}
collectionItem.jsx += `,(${code.trim().replace(/\;$/, '')})`;
Copy link
Member Author

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';
Copy link
Member Author

@drwpow drwpow Apr 6, 2021

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} />)}
Copy link
Member Author

Choose a reason for hiding this comment

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

OpenGraph tags, baybeeee

Screen Shot 2021-04-05 at 19 13 47

@natemoo-re
Copy link
Member

I really love how clean this feels, but it does fall into the overloading native import behavior category. I'd be more comfortable if we aligned with Snowpack's import.meta.glob implementation. With top-level await it should still feel ergonomic, but it's more clear that this is something custom. Thoughts?

@drwpow drwpow changed the title Add example blog Blog Support 1/2: Data fetching Apr 6, 2021
@drwpow drwpow changed the title Blog Support 1/2: Data fetching Blog Support 1/3: Data fetching Apr 6, 2021
@drwpow drwpow force-pushed the blog-pagination branch 2 times, most recently from 80c6534 to c03496f Compare April 6, 2021 20:12
@drwpow drwpow merged commit 2b346d7 into main Apr 6, 2021
@drwpow drwpow deleted the blog-pagination branch April 6, 2021 21:54
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.

None yet

2 participants