AstroのAPIについて

2024-08-10

ブログサイトの作成を通じて、AstroのAPIを学ぶ

Written by: 茂田ゆうすけ

tags

The Astro logo on a dark background with a pink glow.

ブログ投稿アーカイブ

// allPostsのログ
[
  {
    frontmatter: [Getter],
    file: [Getter],
    url: [Getter],
    rawContent: [Getter],
    compiledContent: [Getter],
    getHeadings: [Getter],
    Content: [Getter],
    default: [Function (anonymous)] {
      isAstroComponentFactory: true,
      moduleId: undefined,
      propagation: undefined
    },
    [Symbol(Symbol.toStringTag)]: 'Module'
  }
]

// frontmatterのログ
[
  {
    layout: '../../layouts/MarkdownPostLayout.astro',
    title: 'AstroのAPI',
    pubDate: '2024-08-10T00:00:00.000Z',
    description: 'ブログサイトの作成を通じて、AstroのAPIを学ぶ',
    author: '茂田ゆうすけ',
    image: {
      url: 'https://docs.astro.build/assets/rose.webp',
      alt: 'The Astro logo on a dark background with a pink glow.'
    },
    tags: [ 'astro' ]
  }
]
---
const allPosts = await Astro.glob("./posts/*.md");
---
<ul>
  {allPosts.map((post) => <li><a href={post.url}>{post.frontmatter.title}</a></li>)}
</ul>

ブログのリストを、コンポーネント化したい

---
const { title, url, description, pubDate, imageUrl, imageAlt } = Astro.props;
---
---
import BlogPost from "../components/ListBlog.astro";

const allPosts = await Astro.glob("./posts/*.md");
const allFrontmatters = allPosts.map((post) => post.frontmatter);
---
<ul>
<!-- url, title, description, pubDate, imageUrl, imageAlt -->
  {allPosts.map((post) => <BlogPost url={post.url} title={post.frontmatter.title} />)}
</ul>
<p>{frontmatter.pubDate.toString().slice(0, 10)}</p>