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 🙏

© 2025 – Pkg Stats / Ryan Hefner

vitepress-plugin-demo

v0.8.1

Published

markdown plugin for building demos in vitepress.

Readme

vitepress-plugin-demo

npm version npm downloads bundle JSDocs License

Features

  • ✨ Use the <demo> or container in Markdown to reference a demo container.
  • ♾️ Automatically convert TS code and provide JS demo code.
  • 👽️ Support Vue, React, JS, TS and HTML demo block.
  • 📝 Use Markdown syntax by you demo block description.
  • 💡 Support twoslash syntax highlighting.
  • 📦️ Supports multiple presets, ready to use out of the box.
  • 🎨 Customize the demo container to suit your needs.

vitepress-plugin-demo is a markdown-it plugin specifically designed for Vitepress demos. It converts code blocks in Markdown into references to the <demo-container> component. It does not generate UI itself but serves as a plugin for creating demo containers.

With this plugin, you can use the <demo> tag in Markdown to reference a demo container. For example:

<demo src="../demo.vue" title="Demo block" description="use demo" />

You can use Markdown syntax in the desc field. For example:

<demo src="../demo.vue" title="Demo block" description="use `demo` ..." />

However, we recommend using the container mode to write the desc content with Markdown:

::: demo src="../demo.vue" title="Demo block"

This is a `description` that can be written using Markdown.

:::

This looks more aesthetically pleasing and adheres better to Markdown syntax.

In addition, you can pass the attrs parameter to props, so you can utilize the Line Highlighting in Code Blocks feature of VitePress:

<demo src="../demo.vue" attributes="{1,4,6-8}" />
<demo src="../demo.vue" attributes="{4}" />

Twoslash

vitepress-plugin-demo also supports vitepress/twoslash syntax highlighting. You can use the twoslash tag in Markdown to reference a demo container. For example:

<demo src="../demo.vue" title="Demo block" description="use demo" twoslash />

React

vitepress-plugin-demo also supports React demo blocks. You can use the react tag in Markdown to reference a demo container.

npm install react react-dom --save-dev

import your React component:

<demo type="react" src="../demo.tsx" title="Demo block" description="use demo" />

Install

npm install vitepress-plugin-demo --save-dev

Usage

import { demoMdPlugin } from 'vitepress-plugin-demo'
// .vitepress/config.js
export default defineConfig({
  markdown: {
    config(md) {
      md.use(demoMdPlugin)
    },
  },
})

Register your <demo-container> component in theme/index.ts|js:

// https://vitepress.dev/guide/custom-theme
import Theme from 'vitepress/theme'

// your demo component
import CustomDemoContainer from './components/CustomDemoContainer.vue'

export default {
  ...Theme,
  enhanceApp({ app, router, siteData }) {
    app.component('demo-container', CustomDemoContainer)
  },
}

Presets

vitepress-plugin-demo pre-set common component library themes, which you can directly use:

npm install vitepress-plugin-demo naive-ui --save-dev
import TwoslashFloating from '@shikijs/vitepress-twoslash/client'
import NaiveUIContainer from 'vitepress-plugin-demo/client/naive-ui'
import '@shikijs/vitepress-twoslash/style.css'

export default {
  ...Theme,
  async enhanceApp({ app, router, siteData }) {
    if (!import.meta.env.SSR) {
      const { default: NaiveUI } = await import('naive-ui')
      app.use(NaiveUI)
    }
    app.use(TwoslashFloating)
    app.use(NaiveUIContainer, {
      github: 'you github blob url',
      codeeditor: {
        editor: ['stackblitz', 'codesandbox'],
        globals: {
          package: {/* ... */},
          files: {/* ... */},
          opens: ['App.vue']
        },
        resolve(props) {
          const code = props.typescript || props.javascript
          return { /* cover global config */ }
        }
      },
    })
  },
}

Customs

The demo-container component will receive relevant information about the demo, and you need to implement the rendering of the demo:

<script lang="ts" setup>
import { computed } from 'vue'

const props = defineProps<{
  typescript: string
  // if using ts, javascript will transform the to js
  javascript: string
  title: string
  metadata: object
}>()

const code = computed(() => decodeURIComponent(props.typescript || props.javascript))
</script>

<template>
  <div>
    <div>{{ title }}</div>
    <!-- copy your demo source code -->
    <div @click="navigator.clipboard.writeText(code)">
      Copy Code
    </div>
    <!-- The description is rendered in the desc slot -->
    <slot name="md:description" />
    <!-- The demo is rendered in the default slot -->
    <slot />
    <!-- highlighted code for the demo -->
    <slot name="md:typescript" />
    <!-- or -->
    <slot name="md:javascript" />
  </div>
</template>

Other props will not be processed and will be directly passed to the <demo-container> component. For example, you can customize whether the code is expanded using the prop:

<demo src="../demo.vue" expand />

however, it is important to note that <demo> is not strictly a component and cannot handle excessively complex custom props, such as v-bind.

const props = defineProps<{
  // ...
  expand: boolean
}>()

Metadata

The demo-container component will receive relevant information about the demo. You can use the metadata to access and use this information within the demo:

<script lang="ts" setup>
const props = defineProps<{
  typescript: string
  javascript: string
  title: string
  // metadata returns information about the demo during build (absolutePath, relativePath, fileName)
  metadata: object
}>()
const githubBlobUrl = 'https://www.github.com/.../tree/main/'
const githubPath = githubBlobUrl + props.metadata.relativePath

function toEditGithubDemoFile() {
  window.open(githubPath)
}
</script>

CodeSandbox

You can define the parameters for CodeSandbox by using codesandbox/lib/api/define and create a sandbox environment by submitting them to the CodeSandbox API through a <form>:

<script lang="ts" setup>
import { getParameters } from 'codesandbox/lib/api/define'

const props = defineProps<{
  typescript: string
  javascript: string
  // ...
}>()

// Compute the parameters for CodeSandbox
const parameters = computed(() => {
  return getParameters({
    files: {
      'package.json': {
        // specify your dependencies
        content: { dependencies: { vue: 'latest' } },
      },
      'index.html': { content: `<div id="app"></div>` },
      'App.vue': { content: decodeURIComponent(props.javascript) },
      'src/main.js': { content: '...' },
    },
  })
})
</script>

<template>
  <!-- Form to submit the parameters to CodeSandbox -->
  <form action="https://codesandbox.io/api/v1/sandboxes/define" method="POST" target="_blank">
    <input type="hidden" name="parameters" :value="parameters">
    <button>Edit in CodeSandbox</button>
  </form>
</template>

Development

pnpm install

# Run development server
pnpm dev

# Have fun!
pnpm play

Unit tests are in progress, PRs welcome!

Acknowledgements

This project draws inspiration from the following projects:

License

MIT License © Hairyf