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

@ulu/vite-plugin-markdown-in-vue

v0.0.7

Published

This Vite plugin lets you write Markdown directly within Vue components or other files. It pre-processes the Markdown into HTML during the build process, removing the need for browser-side rendering

Downloads

496

Readme

@ulu/vite-plugin-markdown-in-vue

This Vite plugin lets you write Markdown directly within Vue components or other files. It pre-processes the Markdown into HTML during the build process, removing the need for browser-side rendering.

In addition to Vue files it also supports ".md" files (ie. if using something like unplugin-vue-markdown), see Advanced Setup Example. Technically this could be used by any source file that you bundle with vite, you would just need to add the extensions to options.include. Currently only ".vue" and ".md" files have been tested.

While primarily designed for .vue and .md files, this plugin can potentially work with any file type bundled by Vite. Simply add the desired extensions to the options.include configuration.

Table of Contents

Usage

This plugin provides two placeholder components "MarkdownBlock" and "MarkdownInline". Markdown placed within the component start/end tags will be replaced with HTML.

<template>
  <div>
    <h1>Tests</h1>
    <h2>Markdown block below</h2>
    <MarkdownBlock>
      ## Hello

      - This is working
      - This is a bullet

      This is a paragraph with a [link in it](https://www.google.com) and some text
      after it too. Maybe another sentence.

      <TestComponent/>
    </MarkdownBlock>
    <h2>
      <MarkdownInline>
        This title has *Wow*
      </MarkdownInline>
    </h2>
    <SomeComponent>
      <template #slotName>
        <MarkdownBlock>
          ## Hello

          - This is working
          - This is a bullet
        </MarkdownBlock>
      </template>
    </SomeComponent>
  </div>
</template>

The components "MarkdownBlock" and "MarkdownInline" are not actual Vue components, they are replaced with HTML by this plugin before Vue transforms the single file component (ie .vue file).

Note, indentation will be trimmed off all markdown lines before passing to markdown render (unless customParser). This should only be an issue if you are relying on whitespace (ie. pre) or something, you can implement your own parser to adjust how this works. Custom parser is passed the original string with indentation.

Vite Setup

Note it is important that this plugin is loaded before Vue Vite plugin.

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";

import markdownInVue from "@ulu/vite-plugin-markdown-in-vue";

export default defineConfig({
  plugins: [
    markdownInVue(),
    vue(),
  ],
})

Advanced Setup Example

Example with changing the element names to "MdBlock" and "MdInline" and using unplugin-vue-markdown. Note this plugin would only be used for transforming markdown used within custom components or html within the markdown files, the normal markdown (not nested in components, etc) will be transformed after by the unplugin-vue-markdown when it transforms it into the final vue component.

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";

import markdownInVue from "@ulu/vite-plugin-markdown-in-vue";
import markdown from "unplugin-vue-markdown/vite";

export default defineConfig({
  plugins: [
    markdownInVue({
      elementNameBlock: "MdBlock",
      elementNameInline: "MdInline"
    }),
    // This has to be included after markdownInVue! 
    markdown(),
    vue({
      include: [/\.vue$/, /\.md$/]
    }),
  ],
})

Options

Code below show's defaults until they're documented, setting these in the options object passed to the plugin will override them

const defaults = {
  /**
   * Define custom element name ie <MdBlock>
   */
  elementNameBlock: "MarkdownBlock",
  /**
   * Define custom element name ie <MdInline>
   */
  elementNameInline: "MarkdownInline",
  /**
   * What file types to include (regex)
   */
  include: /\.(vue|md)$/,
  /**
   * What file types to exclude (regex)
   */
  exclude: null,
  /**
   * Options to pass to markdown-it
   */
  markdownItOptions: {
    html: true
  },
  /**
   * Alter markdown-it instance (add plugins, etc)
   */
  markdownItSetup(md) {
    // md.use(something)
  },
  /**
   * Wrap content in <div>, only if default parser
   */
  wrapBlock: false,
  /**
   * Wrap content in <span>, only if default parser
   */
  wrapInline: false,
  /**
   * Class to add to block wrapper
   */
  wrapBlockClasses: "markdown-block",
  /**
   * Class to add to inline wrapper
   */
  wrapInlineClasses: "markdown-inline",
  /**
   * Provide custom markdown parser (gets string, return string)
   * @example 
   *   const customParser = (content, ctx) => someMarkdownLibrary(content)
   */
  customParser: null,
  /**
   * Provide custom markdown parser for inline (gets string and ctx, return string)
   */
  customParserInline: null,
  /**
   * Vue files will pull <MarkdownBlock>'s only from within <template>
   * for all other extensions it will parse the whole file and replace the blocks
   * - This is matched against the file/id in vite
   */
  isVueSfc: /\.vue$/,
}

Bugs, Issues and Changelog

If you encounter bugs or have a feature request, feel free to open an issue on github

Change Log