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

@ldlework/astro-loader-gather

v0.2.0

Published

A loader for [Astro](https://astro.build/)'s [Content Layer](https://astro.build/blog/astro-4140/#experimental-content-layer-api) ([RFC](https://github.com/withastro/roadmap/blob/content-layer/proposals/0047-content-layer.md#loaders)) similar to the built

Downloads

3

Readme

astro-loader-gather

A loader for Astro's Content Layer (RFC) similar to the built-in glob loader with some extensions for extracting metadata from filesystem paths.

Most of the credit goes to the contributors to the built-in glob loader, this is a relatively small change ontop.

const posts = defineCollection({
  loader: gather({
    base: "src/data/",
    patterns: [
      // standalone posts - fixed type and group
      ["blog/{{id}}.(md|mdx)", { type: 'normal', group: 'none'}],
      // articles in a series - group comes from folder name!
      ["articles/{{group}}/{{id}}.(md|mdx)", { type: 'series'}],
      // news posts for projects - group comes from folder name!
      ["projects/{{group}}/news/{{id}}.(md|mdx)", { type: 'projects'}],
    ],
  }),
  schema: z.object({
    title: z.string(),
    type: z.enum(['projects', 'series', 'normal']),
    group: z.string(),
    // etc...
  }),
})

In this example, we have a single content type "posts" -- but three different kinds of posts:

  • standalone blog posts
  • articles in a series
  • project-specific news

With astro-loader-gather, you can keep all your content more logically co-located while associating the metadata you'll need later for filtering and showing the right data in the right context.

We may want to show all posts regardless of type at /blog but only a project's news at /project/foo/news.

Installation

npm i @ldlework/astro-loader-gather

Configuration

The gather() function takes an object with two properties:

  • base: the root path of your content
  • patterns: an array of patterns relative to the root
  • transform: optional callback for modifying entries

Core Behavior

For details on how content is rendered, validated, cached, etc, see the documentation for the built-in glob loader.

Gather Patterns

The most simple pattern is simply a glob, like those supported by fast-glob:

gather({
    base: "src/data",
    patterns: [
        `*.(md|mdx)`,
        `posts/**/*.md`,
        // etc
    ]
})

Capture Syntax

You can capture elements of the filesystem path into the metadata of content entries by surrounding a path element in double {{}} curly-braces:

gather({
    base: "src/data",
    patterns: [
        `projects/{{projectName}}/news/*.md`
    ]
})

In this example, {{projectName}} behaves like a glob wildcard *. Any matched directory will be assigned to the entry.data.projectName metadata attribute. Neat!

Static Metadata

You can associate static metadata with each pattern by specifying that pattern as a [string, object] tuple instead. The object will be merged into the metadata of any entries matched by this pattern.

This makes it easy to know later on, which specific pattern was used to source an entry:

gather({
  base: "src/data"
  patterns: [
    ["posts/*.md", { type: 'normal' }],
    ["projects/{{projectName}}/news/*.md", { type: 'news' }]
  ]
})

const posts = await getCollection('posts')
const news = posts.filter(p => p.type === 'news')

Metadata Transformations

If you need to pre-process your entry metadata before it gets validated then you can use the transform callback:

gather({
  base: "src/data",
  patterns: [ "somedata.json" ],
  transform: e => ({ 
    ...e, 
    data: { 
      ...e.data, 
      slug: slugify(e.data.name) 
    }
  })
})

Type-safe Transforms

To make your transform functions type-safe, trade defineCollection with gatherCollection:

const someCollection = gatherCollection({
  options: {
    base: "src/data",
    patterns: [ "somedata.json" ],
    transform: e => ({ 
      ...e, 
      data: { 
        ...e.data, 
        slug: slugify(e.data.name) 
      }
    })
  },
  schema: z.object({
    name: z.string(),
    slug: z.string(),
  })
})