starlight-blog

repository·main·Indexed 42 days ago

https://github.com/hideoo/starlight-blog

A 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.

Tokens
15.6K
Snippets
57
Records
72
Agent score
54%

What's inside starlight-blog

  1. Features of starlight-blog

    main

    The starlight-blog plugin 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.
  2. How metrics are calculated

    main

    Starlight Blog computes metrics based on the post content using these rules:

    Word Count

    • Calculated using the Intl.Segmenter API, 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.
  3. Handle fallback content for blog posts

    main

    To 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 create src/content/docs/fr/blog/hello.md (and for any other languages) to ensure the plugin can provide fallback behavior via Starlight's routing.

  4. Getting started with Starlight Blog

    main

    Starlight 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:

    1. Install the plugin: Follow the specific installation instructions provided in the getting started guide.
    2. Configure the plugin: Add the plugin to your astro.config.mjs file to enable its features.
    3. Write blog posts: Create blog posts using standard Markdown or MDX, treating them similarly to how you author pages in your documentation.
  5. Configure global authors in starlightBlog

    main

    To avoid repeating author details in every post, you can define them globally in your astro.config.mjs within the starlightBlog configuration 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',
        },
      },
    })
  6. Access blog posts and authors via Astro.locals

    main

    The 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.starlightBlog will not be available and will throw an error. In that case, you must use Astro.locals.starlightBlogs to 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>
  7. Enable the RSS feed

    main

    Starlight Blog automatically generates an RSS feed when the Astro site configuration option is set in your astro.config.mjs.

    When enabled, the following occurs:

    • An RSS feed is generated at /blog/rss.xml (the path can be customized using the prefix option).
    • 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',
        },
      }),
    })
  8. Define blog post excerpts

    main

    Excerpts 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:

    1. excerpt frontmatter property: Define a custom string in the post's YAML frontmatter. Supports basic Markdown formatting.
    2. Excerpt delimiters: Place a delimiter within the post content. Everything above the delimiter is used as the excerpt.
    3. Fallback: If neither of the above is provided, the entire blog post content is used as the excerpt.
  9. Use the `<Preview>` component to render blog posts

    main

    The <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.astro and pass a valid post object (of type StarlightBlogPostData) 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} />}