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-vue-md

v1.4.0

Published

Vite plugin to import Markdown files as Vue components

Downloads

201

Readme

This Vite plugin adds support for importing a Markdown file as a Vue component. Works with Vue 2 & 3.

Vue.js Demo Blocks

Render your Vue.js code blocks inline by simply adding demo next to the language name.

For example, when this Markdown file is rendered with this plugin, you'll see a clickable button here:

<script setup>
const clickHandler = () => alert('Clicked!')
</script>

<template>
    <button @click="clickHandler">
        Click me
    </button>
</template>

Install

npm install -D vite-vue-md

Setup

In your vite.config.js file:

  1. Import vite-vue-md and add it to the plugins array.
  2. In your vue() plugin options, add a include option that includes .md files.

vite.config.js:

  import vue from '@vitejs/plugin-vue'
+ import vueMd from 'vite-vue-md'

  export default {
      plugins: [
          // ...

          vue({
+             include: [/\.vue$/, /\.md$/] // ← Treat MD files as Vue components
          }),

+         vueMd(/* Options */) // ← Compile MD files to Vue components
      ]
      // ...
  }

Demo Blocks

Demo Blocks are Vue.js code blocks that are rendered inline. They're useful for documentation docs to show off your components without compromising the readability of the Markdown file on GitHub.

To compile a Vue.js codeblock as a Demo Block, add demo next to the language name:

```vue demo
<script setup>
const clickHandler = () => alert('Clicked!');
</script>

<template>
    <button @click="clickHandler">
        Click me
    </button>
</template>
```

Multi-file demos

The entry point for demo blocks must be a Vue.js component. But you can import other code blocks in any language from the same Markdown file.

For non-entry files, set a file name via demo=<file name>. Then import it from the Vue.js demo block via the doc: protocol:

Entry file:
```vue demo
<script setup>
import { clickHandler } from 'doc:click-handler.js'
</script>

<template>
    <button @click="clicked">
        Click me
    </button>
</template>
```

Second file:
```js demo=click-handler.js
export const clickHandler = () => alert('Clicked!');
```

Demo + Code blocks

Since the code blocks are rendered inline, they're replaced by the actual Vue.js component. To show the code block, you can add a onDemo callback to the plugin options:

({
    onDemo(componentTag, code) {
        // Register the wrapper component
        this.registerComponent('DemoContainer', './DemoContainer.vue')

        // Return a custom HTML string
        return `
        <DemoContainer>
            <!-- Inline the component here -->
            ${componentTag}

            <!-- Pass in the code block here -->
            <template #code>
                <template v-pre>${this.escapeHtml(code)}</template>
            </template>
        </DemoContainer>
        `
    }
})

Options

include

Type: ReadonlyArray<string | RegExp> | string | RegExp

Files to include from being compiled as Vue files.

exclude

Type: ReadonlyArray<string | RegExp> | string | RegExp

Files to exclude from being compiled as Vue files.

markdownItOptions

Type: markdownIt.Options

MarkdownIt options. See MarkdownIt's documentation for more information.

markdownItSetup

Type: (md: markdownIt) => void;

Callback to add plugins to MarkdownIt.

wrapperClass

Type: string

Default: markdown-body

The class to add to the wrapper element that contains the Markdown page.

onDemo

Type:

(
    tag: string,
    code: string,
    demos: Map<string, string>
) => string

You can intercept each demo block and return a custom HTML string. This is useful for adding custom styling to demo blocks.

In addition, there are utils exposed in the this context:

  • escapeHtml: Escape HTML code to prevent it from being rendered as HTML.
  • registerComponent: Register a component to be used in the demo block. This is useful for registering components that are imported from other files.

See example above in the Demo Blocks section.

markdownCss

Type: string

File path to a stylesheet to use for the Markdown page. This will be added using <style scoped> so it will only apply to the markdown page. Useful for styling only the HTML generated by the MarkdownIt plugin.

useVOnce

Type: boolean

Whether to add v-once to the entire Markdown page. This will prevent the Markdown page from being re-rendered when the Vue component is updated.

Warning: This will disable demo blocks. Only use this if you have a large document and don't need demo blocks.

Related

unplugin-vue-markdown

Another Vite plugin for compiling Markdown files to Vue components.

This plugin has drawn inspiration from it but has a different feature set. This plugin only supports Vue.js code in code blocks.