npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

github-discussions-blog-loader

v0.0.2

Published

GitHub Discussions Blog Loader for Astro

Downloads

10

Readme

GitHub Discussions Blog Loader

This package provides a GitHub Discussions loader for Astro, allowing you to use GitHub Discussions as a blog engine.

Requirements

This package requires Astro 5.0.0-beta or later.

Installation

npm install github-discussions-blog-loader

Usage

You can use the githubDiscussionsBlogLoader in your content configuration at src/content/config.ts:

import { defineCollection } from "astro:content";
import { githubDiscussionsBlogLoader } from "github-discussions-blog-loader";

const blogPosts = defineCollection({
  loader: githubDiscussionsBlogLoader({
      auth: GITHUB_ACCESS_TOKEN,
      repo: {
          name: GITHUB_REPO_NAME,
          owner: GITHUB_REPO_OWNER,
      },
  })
});

export const collections = { blogPosts };

You can then use these like any other content collection in Astro:

---
import type { GetStaticPaths } from "astro";
import { getCollection } from "astro:content";
import type { Post } from "github-discussions-blog-loader";
import Layout from "../../layouts/Layout.astro";

export const getStaticPaths: GetStaticPaths = async () => {
  const blogPosts = await getCollection("blogPosts");
  return blogPosts.map((blogPost) => ({
    params: {
      slug: blogPost.id,
    },
    props: { post: blogPost.data },
  }));
};

type Props = { post: Post };

const { post } = Astro.props;
---

<Layout title={post.title}>
  <h1>{post.title}</h1>
  <div set:html={post.body}></div>
</Layout>

Options

The githubDiscussionsBlogLoader function takes an options object with the following structure:

{
  auth: string,
  repo: {
    name: string,
    owner: string,
  },
  incremental?: boolean,
  mappings?: {
    blogPostCategory?: string,
    draftLabel?: string,
    tagLabelPrefix?: string,
    seriesLabelPrefix?: string,
  }
}

| Property | Description | | --- | --- | | auth | A GitHub personal access token with permissions to access discussions on the target repository. | | repo | Details of the GitHub repository to connect to. | | repo.name | The name of the repository. | | repo.owner | The owner of the repository. | | incremental | If true, the loader will only fetch new/updated discussions since the last build. Otherwise the loader will fetch all blog posts on every build. The default is false. | | mappings | Details of the how to map the GitHub Discussions data to the blog post data. | | mappings.blogPostCategory | A GitHub Discussions category that defines which discussion category is considered a blog post. The default is undefined and so will fetch all discussions. | | mappings.draftLabel | The GitHub Discussions label that defines a blog post as draft and so will be excluded from the loaders results. The default is "state/draft". | | mappings.tagLabelPrefix | A prefix that identifies a GitHub Discussions label as a tag. The default is "tag/". | | mappings.seriesLabelPrefix | A prefix that identifies a GitHub Discussions label as a series container. The default is "series/". |

The default mapping options are available via a DEFAULT_MAPPINGS export and so you can override just the properties you need to change using the spread operator:

import { defineCollection } from "astro:content";
import { githubDiscussionsBlogLoader, DEFAULT_MAPPINGS } from "github-discussions-blog-loader";

const blogPosts = defineCollection({
  loader: githubDiscussionsBlogLoader({
      auth: GITHUB_ACCESS_TOKEN,
      repo: {
          name: GITHUB_REPO_NAME,
          owner: GITHUB_REPO_OWNER,
      },
      mappings: {
          ...DEFAULT_MAPPINGS,
          blogPostCategory: "Article",
      }
  })
});

export const collections = { blogPosts };

Type Information

The loader will return an array of Post objects, each with the following data structure:

Post

{
  id: string,
  slug: string
  title: string
  description?: string
  body: string
  created: Date
  updated: Date
  published: Date
  readingTime: string
  category: Category
  tags: string[]
  series?: Series
  author: Actor
  githubUrl: string
  githubDiscussionId: string
  githubDiscussionNumber: number
}

Category

{
  id: string
  name: string
}

Series

{
  id: string
  name: string
}

Actor

{
  avatarUrl: string
  username: string
  url: string
}

TypeScript types are available for all of the above.

import type { Post, Category, Series, Actor } from 'github-discussions-blog-loader'