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

vite-plugin-content

v0.1.4

Published

Similar to [contentlayer](https://contentlayer.dev/) but not as comprehensive, flexible and **only for md, mdx files currently**, still in WIP, and significant changes or errors may occur at any time, so please use it cautiously.

Downloads

321

Readme

Similar to contentlayer but not as comprehensive, flexible and only for md, mdx files currently, still in WIP, and significant changes or errors may occur at any time, so please use it cautiously.

Setup

1. Install

pnpm install vite-plugin-content

2. Define Schema

Add a file content.config.js in the project's root directory.

import { Config, z } from "vite-plugin-content";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import rehypeSlug from "rehype-slug";
import rehypeAutolinkHeadings from "rehype-autolink-headings";
import rehypeKatex from "rehype-katex";

const config: Config = {
  contentDirPath: "./data", // The directory for storing md and mdx files.
  outputDirPath: "./.content", // default: .content
  documents: [
    {
      name: "Blog",
      // sub directory in contentDirPath for Blog
      folder: "blog/",
      // zod schema definition for frontmatter
      fields: z.object({
        title: z.string(),
        date: z.coerce.date(),
        tags: z.array(z.string()).default([]),
        lastmod: z.coerce.date().default(new Date()),
        draft: z.boolean().default(false),
        summary: z.string().optional(),
      }),
    },
    {
      name: "Author",
      folder: "authors/",
      fields: z.object({
        tags: z.array(z.string()).default([]),
        lastmod: z.coerce.date().default(new Date()),
        draft: z.boolean().default(false),
        summary: z.string().optional(),
      }),
    },
  ],
  remarkPlugins: [remarkGfm, remarkMath],
  rehypePlugins: [rehypeSlug, rehypeAutolinkHeadings, rehypeKatex],
};

export default config;

3. Update TypeScript Configuration

The modified parts need to correspond to the outputDirPath in your content.config.js.

// tsconfig.json
{
  "compilerOptions": {
    "baseUrl": ".",
    //  ^^^^^^^^^^^
    "paths": {
      "#content/generated/*": ["./.content/generated/*"]
      // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    }
  },

  "include": [
    ".content/generated"
    // ^^^^^^^^^^^^^^^^^^^^^^
  ]
}

4. Update vite.config.ts

import { defineConfig } from "vite";
import vpc from "vite-plugin-content";

export default defineConfig({
  resolve: {
    alias: {
      "#content/generated": path.resolve(__dirname, "./.content/generated"),
    },
  },
  plugins: [vpc()],
});

5. Update build script in package.json

If the corresponding files have not been generated in advance by vite-plugin-content, errors may occur when building if there are code references to related content. Therefore, it is recommended to first execute the vite-plugin-content build command."

// package.json
{
  "scripts": {
    "build": "vite-plugin-content build && your-build-command"
  }
}

6. Ignore Build Output

The modified parts need to correspond to the outputDirPath in your content.config.js.

# .gitignore

.content

7. Add Code

Show all blog titles

import { allBlog } from "#content/generated/index.mjs";

function App() {
  return (
    <ul>
      {allBlog.map((blog) => (
        <li key={blog.data.slug}>{blog.frontmatter.title}</li>
      ))}
    </ul>
  );
}

Show blog page

import { Blog } from "#content/generated/index.mjs";
import { getMDXComponent } from "mdx-bundler/client";
import { useMemo } from "react";

function Blog({ blog }: { blog: Blog }) {
  const { frontmatter, code } = blog;
  const Component = useMemo(() => getMDXComponent(code), [code]);

  return (
    <>
      <h1>{frontmatter.title}</h1>
      <p>{frontmatter.date}</p>
      <article>
        <Component />
      </article>
    </>
  );
}

export default Blog;