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

vitepress-pages

v2.0.1

Published

![npm badge](https://img.shields.io/npm/v/vitepress-pages?color=%23eee&style=for-the-badge)

Downloads

299

Readme

File system based routing for digital gardening with Vitepress

npm badge

A Vitepress extension for automatic navigation generation for any markdown data collection.

This is vitepress-pages v.2.0 documentation, which is suitable for Vitepress v.1.0-beta.1 and higher. For v.1.x docs go https://www.npmjs.com/package/vitepress-pages/v/1.0.5

What does it do?

It helps you generate navigation data: page hierarchy, parents, siblings and children for any given page using Vitepress built-in functionality.

In components

Import the usePages, useChildren, useParents or useSiblings from vitepress-pages and get reactive computed values to build your own navigation interface.

In data-loaders

With Vitepress 1.0 we got the build-time data loading that give a list of all markdown pages with a glob pattern with the createContentLoader within special *.data.js files.

You can use the vitepress-pages/media default export function to go through the raw routes to optimize all the used mediaTypes images and copy them to the publicFolder/mediaFolder path. The script will take an image path relative to a given the .md file, optimize it with sharp.js and change the frontmatter media URL to match the new static image location. This is really helpful to generate have illustrated blogs and much more.

In config

There's a configurable opinionated transformHead() function generator to generate per-page head tags. You provide a config object and get a function that will return a bunch of SEO-friendly head tags. The script adds og: and twitter: meta-tags, as well as title, description and some other to all the pages correspondingly.

Installation

Add vitepress-pages as a dependency to your project.

pnpm i vitepress-pages

How to use

Put a *.data.js to the root folder of your digital garden.

pages.data.js

// We use the new Vitepress loaders feature https://vitepress.dev/guide/data-loading
import { createContentLoader } from 'vitepress'

// import the main transformer factory
import VPMedia from 'vitepress-pages/media'

// export the content data-loader for your markdown files folder
export default createContentLoader('./**/*/*.md', {
  
  //transform pages: optimize images
  transform: VPMedia({
    
     // Mandatory field
  root: new URL('../', import.meta.url),
  
  // Where are static files stored?
  publicFolder: "public",
  
  // Where to put the optimized images?
  mediaFolder: 'media_files',

  // What fields in the frontmatter contain pictures to optimize. The most useful are 'cover', 'icon', 'avatar', 'logo'. 
  mediaTypes: { cover: { size: 1200, height: 1000, fit: "inside", webp: false } }
  })
})

Usage

You can use the usePages() function in your Vitepress extended default theme layout to have all the navigation primitives you need: parents, siblings and children.

.vitepress/theme/MyLayout.vue

<script setup>

// Default theme layout to extend
import DefaultTheme from 'vitepress/theme'
const { Layout } = DefaultTheme

// Composables to use
import { usePages, cleanLink } from 'vitepress-pages';

// The way to react to route changes
import { useRoute } from 'vitepress'

// Build-time data-loader
import { data } from '../../pages.data.js'

// Component to display pages in a list
import NavCard from './NavCard.vue';

// Composable to process route and data and return reactive computed lists of pages
const { children, parents, siblings } = usePages(useRoute(), data)
</script>

<template>
  <Layout>
    
    <!-- Extending the default layout - put parents list right into the nav bar -->
    <template #nav-bar-title-after>
      <nav id="parents" class="grid">
        <a v-for="parent in parents.slice(0, -1)" :key="parent.url" class="parent" :href="cleanLink(parent.url)">
          {{ parent.frontmatter?.title }}
        </a>
      </nav>
    </template>

    <!-- This block goes right after the page text -->
    <template #doc-after>

      <!-- Children list -->
      <nav id="children" class="grid">
        <NavCard v-for="child in children" :key="child.url" :page="child" class="child">
        </NavCard>
      </nav>

      <!-- Siblings pair -->
      <nav id="siblings" class="grid">
        <template v-for="sb in ['prev', 'next']" :key="sb">
          <NavCard v-if="siblings?.[sb]" :page="siblings?.[sb]" class="sibling">
          </NavCard>
        </template>
      </nav>

    </template>
  </Layout>
</template>

Display

Your card for displaying pages can be any level of complexity. Here's a basic one for you to build upon.

<script setup>
const props = defineProps({
  page: { type: Object, default: () => ({}) }
})
</script>

<template>
  <a v-if="page" :href="page?.url" :style="{ background: `url(${page?.frontmatter?.cover})` }">
    <div>
      <slot></slot>
      <h3>{{ page?.frontmatter?.title }}</h3>
      <h4>{{ page?.frontmatter?.description }}</h4>
    </div>
  </a>
</template>

Generate head meta-tags

.vitepress/config.js

//import the generator 
import generateMeta from 'vitepress-pages/head'

//define options
const metaData = {
  title: "My site",
  description: "On the web",
  site: "my-site.com",
  url: "https://my-site.com/", //the end slash here is mandatory
  repo: "https://github.com/my/site",
  locale: "en",
  icon: "path/to/icon.png",
  logo: "path/to/logo.png",
  image: "path/to/image.jpg", // used for og:image, should be 1.91x1 ratio
  color: '#cccccc',
  mediaFolder: 'media_files', //where are the media files generated by `vitepress-pages/media` located
  author: "me-myself", //your twitter handle
  tags: "list, of, tags",
  // add it if you use [umami](https://umami.is/) for stats
  umamiId: "xxxxxxx-xxx-xxxxx-xxxxxxxx",
  umamiScript: "https://stats.my-site.com/umami.js"
};

// set your Vitepress config
export default defineConfig({
  title: meta.title,
  description: meta.description,
  lang: meta.locale,
  outDir: "dist",
  themeConfig: {
    repo: meta.repo,
    logo: meta.logo,
    color: meta.color
  },
  // Add this line to get head tags generated
  transformHead: generateMeta(meta)
});

Enjoy your digital garden being published online!