Aller au contenu

Blog Data

Ce contenu n’est pas encore disponible dans votre langue.

Starlight Blog exposes information about the blog posts in your project. This guide explains how to use Starlight Blog’s data.

What is blog data?

Starlight Blog data is an object containing the information about all the blog posts in your project. It includes information like a list of all the blog posts, the title of each post, the date it was published, and the author.

Using blog data

The posts object contains an array of all the blog posts in your project.

You can access this data from the Astro.locals.starlightBlog global in Astro components:

example.astro
---
const { posts } = Astro.locals.starlightBlog
// ^ The list of all blog posts
---

This can be useful for example to create a widget that lists recent blog posts on your homepage:

RecentBlogPosts.astro
<ul>
{
Astro.locals.starlightBlog.posts.slice(0, 5).map((post) => (
<li>
<a href={post.href}>{post.title}</a>
</li>
))
}
</ul>

starlightBlog

The starlightBlog object has the following properties:

posts

An list of all the blog posts in your project ordered by descending publication date.

Each post has the following properties:

title

Type: string

The title of the blog post.

href

Type: string

The link to the blog post.

createdAt

Type: Date

The date of the blog post.

updatedAt

Type: Date | undefined

The last update date of the blog post. It is only defined if the lastUpdated frontmatter field is specified and different from the date frontmatter field.

draft

Type: boolean

Whether the blog post is a draft. Draft blog posts are only visible in development mode.

Type: boolean

Whether the blog post is featured.

authors

Type: { name: string; title?: string; url?: string; }[]

The list of authors of the blog post.

Each author has the following properties:

name

Type: string

The name of the author.

title

Type: string | undefined

The optional title of the author.

url

Type: string | undefined

The optional URL to link the author to.

tags

Type: { label: string; href: string; }[]

The list of tags of the blog post.

Each tag has the following properties:

label

Type: string

The label of the tag.

href

Type: string

The link to the tag page.