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

markdown-it-vitepress-demo

v0.2.2

Published

[![NPM version](https://img.shields.io/npm/v/markdown-it-vitepress-demo?color=a1b858&label=)](https://www.npmjs.com/package/markdown-it-vitepress-demo)

Downloads

165

Readme

markdown-it-vitepress-demo

NPM version

markdown-it-vitepress-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. This means that you need to implement and register the <demo-container> component yourself, and markdown-it-vitepress-demo makes this process easier.

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" desc="use demo" />

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

<demo src="../demo.vue" title="Demo block" desc="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" attrs="{1,4,6-8}" />
<demo src="../demo.vue" attrs="{4}" />

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.

Install

npm install markdown-it-vitepress-demo --save-dev

Usage

// .vitepress/config.js
export default defineConfig({
  markdown: {
    config(md) {
      md.use(require('markdown-it-vitepress-demo'))
    },
  },
})

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)
  },
}

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<{
  sfcTsCode: string
  // if using ts, sfcJsCode will transform the to js
  sfcJsCode: string
  sfcTsHtml: string
  sfcJsHtml: string
  title: string
  metadata: object
}>()

const sfcCode = computed(() => decodeURIComponent(props.sfcTsCode || props.sfcJsCode))
const highlightedHtml = computed(() => decodeURIComponent(props.sfcTsHtml || props.sfcJsHtml))
</script>

<template>
  <div>
    <div>{{ title }}</div>
    <!-- copy your demo source code -->
    <div @click="navigator.clipboard.writeText(sfcCode)"> Copy Code </div>
    <!-- The description is rendered in the desc slot -->
    <slot name="desc" />
    <!-- The demo is rendered in the default slot -->
    <slot />
    <!-- highlighted code for the demo -->
    <div class="language-vue" v-html="highlightedHtml"></div>
  </div>
</template>

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<{
  sfcTsCode: string
  sfcJsCode: string
  sfcTsHtml: string
  sfcJsHtml: 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<{
  sfcTsCode: string
  sfcJsCode: 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.sfcJsCode) },
      '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 © 2022 Hairyf