starlight-blog
repository·main·Indexed 42 days ago
https://github.com/hideoo/starlight-blogA plugin for the Starlight documentation framework that integrates a fully-featured blog into documentation sites. Version 0.15.0 features include post management with pagination, global and per-post authorship, tag support, cover images, RSS feeds, and structured data for SEO. It provides a custom sidebar for recent or featured posts and allows for multiple blog instances within a single site.
What's inside starlight-blog
- starlight-blog is a plugin designed for Starlight that allows you to add blog functionality to your documentation site.
Features of starlight-blog
mainThe
starlight-blogplugin provides several features for integrating a blog into a Starlight site:- Navigation: Automatically links to the blog in the header.
- Post Management: Includes a post list with pagination support.
- Authorship: Supports both global and per-post authors.
- Organization: Includes tag support and cover images for posts.
- UI Components: Provides a custom sidebar that can display recent/featured posts and tags.
- Syndication & SEO: Supports RSS feeds and structured data.
- Scalability: Allows for multiple blog instances within a single site.
How metrics are calculated
mainStarlight Blog computes metrics based on the post content using these rules:
Word Count
- Calculated using the
Intl.SegmenterAPI, which respects the language of the blog post. - Code blocks are ignored.
- All markup is stripped from the content before calculation.
Reading Time
- Based on an average reading speed of 213 words per minute.
- Image rules (following Medium's logic):
- 1st image: +12 seconds
- 2nd image: +11 seconds
- Subsequent images: Each adds one less second than the previous (until the 10th image).
- 10th+ images: Each adds 3 seconds.
- Calculated using the
Handle fallback content for blog posts
mainTo enable automatic fallback content (where a user sees a version of a post in a different language if the current language version is missing), you must create equivalent blog posts in all supported languages.
For example, if you have
src/content/docs/en/blog/hello.md, you should also createsrc/content/docs/fr/blog/hello.md(and for any other languages) to ensure the plugin can provide fallback behavior via Starlight's routing.Getting started with Starlight Blog
mainStarlight Blog is a plugin designed to add blog functionality to an existing Starlight documentation site. To implement a blog, you must follow these core steps:
- Install the plugin: Follow the specific installation instructions provided in the getting started guide.
- Configure the plugin: Add the plugin to your
astro.config.mjsfile to enable its features. - Write blog posts: Create blog posts using standard Markdown or MDX, treating them similarly to how you author pages in your documentation.
Configure global authors in starlightBlog
mainTo avoid repeating author details in every post, you can define them globally in your
astro.config.mjswithin thestarlightBlogconfiguration object. Each global author is assigned a unique key. If a blog post's frontmatter does not define an author, the plugin will use these global authors. You can also reference these global authors in a post's frontmatter by using their configuration key.// astro.config.mjs starlightBlog({ authors: { hideoo: { name: 'HiDeoo', title: 'Starlight Aficionado', picture: '/hideoo.png', // Images in the `public` directory are supported. url: 'https://hideoo.dev', }, }, })Access blog posts and authors via Astro.locals
mainThe Starlight Blog plugin exposes blog-wide data through
Astro.locals. You can use this data in Astro components, MDX pages, or custom pages rendered via the<StarlightPage>component to build custom widgets like recent post lists or author summaries.Important: If you have configured multiple blog instances,
Astro.locals.starlightBlogwill not be available and will throw an error. In that case, you must useAstro.locals.starlightBlogsto access data for a specific instance.--- const { posts, authors } = Astro.locals.starlightBlog --- <ul> {posts.slice(0, 5).map((post) => ( <li> <a href={post.href}>{post.title}</a> <em>({post.metrics.words.total} words)</em> </li> ))} </ul>Enable the RSS feed
mainStarlight Blog automatically generates an RSS feed when the Astro
siteconfiguration option is set in yourastro.config.mjs.When enabled, the following occurs:
- An RSS feed is generated at
/blog/rss.xml(the path can be customized using theprefixoption). - A sidebar link to the RSS feed is added to the blog.
- A social link to the RSS feed is added to the Starlight header (if exactly one RSS feed is generated).
- RSS feed auto-discovery is automatically configured.
If you are using multiple blog instances, each instance with an enabled RSS feed will receive its own unique feed.
import starlight from '@astrojs/starlight' import { defineConfig } from 'astro/config' import starlightBlog from 'starlight-blog' export default defineConfig({ site: 'https://www.example.com', starlight({ social: { plugins: [starlightBlog()], title: 'My Docs', }, }), })- An RSS feed is generated at
Translate the blog UI
mainThe Starlight Blog plugin supports translating its default UI strings. You can localize the interface by providing translation files to the plugin's internal translation mechanism. (Note: The specific implementation details for applying these translations depend on how you are extending the plugin components).Get started with starlight-blog
mainTo add a blog to your Starlight documentation site, you can follow the official getting started guide or view the demo to see the plugin's features in action.Define blog post excerpts
mainExcerpts are short summaries or previews of blog posts displayed on list pages (blog post list, authors, and tags pages). You can define excerpts using three methods:
excerptfrontmatter property: Define a custom string in the post's YAML frontmatter. Supports basic Markdown formatting.- Excerpt delimiters: Place a delimiter within the post content. Everything above the delimiter is used as the excerpt.
- Fallback: If neither of the above is provided, the entire blog post content is used as the excerpt.
Use the `<Preview>` component to render blog posts
mainThe
<Preview>component allows you to render a blog post using the same preview layout found on blog listing pages. This is useful for showcasing specific posts within other pages of your site.To use it, import the component from
starlight-blog/ui/Preview.astroand pass a validpostobject (of typeStarlightBlogPostData) to it.--- import Preview from 'starlight-blog/ui/Preview.astro' // Retrieve a post from the local starlightBlog data const post = Astro.locals.starlightBlog.posts.at(0) --- {post && <Preview {post} />}