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

@__mazerty__/rollup-plugin-pages

v1.0.0

Published

Rollup/Vite plugin that can be used to extract metadata from a directory of markdown files with frontmatter (such as blog pages)

Downloads

19

Readme

rollup-plugin-pages

Rollup/Vite plugin that can be used to extract metadata from a directory of markdown files with frontmatter (such as blog pages).
It can then be imported like any other module in your existing application to populate a router or a summary page.

Demo

rollup-plugin-pages.mazerty.fr is a React application that has been built around this plugin and features a table of contents and a basic router.
The source code is available on GitLab and can be used to bootstrap your own application.

Installation

You can add @__mazerty__/rollup-plugin-pages in your devDependencies yourself, or you can let your favorite package manager do it for you.
For example:

npm install @__mazerty__/rollup-plugin-pages --save-dev

Usage

Let's say you have a project with a file structure that looks something like this:

src/
  pages/
    1970-01-01_lorem-ipsum.md
    1970-01-02_dolor-sit-amet.md
tests/
package.json
rollup.config.js or vite.config.js

And let's say the frontmatter of your markdown pages looks something like this:

---
title: Lorem Ipsum
description: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
---

Just add the plugin in your Rollup/Vite configuration file:

import {defineConfig} from "vite"
import pages from "@__mazerty__/rollup-plugin-pages"

export default defineConfig({
    plugins: [
        pages(__dirname + "/src/pages")
    ]
})

And Rollup/Vite will build a virtual module that you can import like any other module:

import pages from "virtual:pages"

The code of the virtual module would look something like this:

export default [
    {
        "import": () => import("src/pages/1970-01-01_lorem-ipsum.md"),
        "date": "1970-01-01",
        "key": "lorem-ipsum",
        "title": "Lorem Ipsum",
        "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
    },
    {
        "import": () => import("src/pages/1970-01-02_dolor-sit-amet.md"),
        "date": "1970-01-02",
        "key": "dolor-sit-amet",
        "title": "Dolor Sit Amet",
        "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
    }
]
  • The date and key attributes are extracted from the filename (this is fully customizable through the filenamePattern parameter, see below).
  • The title and description attributes come from the frontmatter, which can be any valid yaml mapping.
  • The import attribute is a function that allows you to dynamically import the page into your application (see also lazy loading and code splitting).

You'll still need a library such as MDX or unplugin-vue-markdown to turn your markdown files into components, but this is outside the scope of this plugin :)
Check out the demo to see how it can be done with MDX.

Parameters

There are actually 3 parameters that allow you to customize the behavior of the plugin:

  • absolutePath (string): absolute path to the pages' directory.
  • virtualModuleId (string, optional): name of the virtual module, defaults to virtual:pages.
    Has to be unique, allows for multiple use of the plugin in the same project (i.e. for different directories).
  • filenamePattern (RegExp, optional): regular expression for the pages' filenames inside the directory, defaults to /(?<date>.*)_(?<key>.*)\./ (regexr).
    Only named capturing groups are considered valid, regular capturing groups will be ignored.

Known issues