• Stars
    star
    580
  • Rank 75,188 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Markdown with Vue for Vite

vite-plugin-md

Markdown for Vite

  • Use Markdown as Vue components
  • Use Vue components in Markdown
  • Extend functionality with Builder API

NPM version

From v0.13, we introduced a pipeline and builder engine (#54, #77) to provide full customizability. If you still prefer the simple Markdown-to-Vue transformation prior to v0.13, it has been moved to vite-plugin-vue-markdown.

Installing this Plugin

Installation can be done in a few simple steps. From the root of your repo do the following:

  1. NPM Install

    npm i vite-plugin-md -D # yarn add vite-plugin-md -D
  2. Vite Configuration

    Add the following to your vite.config.js / vite.config.ts file:

    import Vue from '@vitejs/plugin-vue'
    import Markdown from 'vite-plugin-md'
    
    export default {
      plugins: [
        Vue({
          include: [/\.vue$/, /\.md$/], // <--
        }),
        Markdown(),
      ],
    }

    This adds the VueJS along with this repo as "plugins" to Vite. With VueJS you'll also want to make sure to include both vue and md files.

  3. Typescript Config (optional)

    If you're using Typescript than you'll want take the additional step of adding a "shim file" to help Typescript to understand how to think of Vue SFC files and Markdown files structurally. For VueJS developers, you've probably already done this for your VueJS files but you can wrap this up with a single file -- shims.d.ts -- in the root of your repo:

    declare module '*.vue' {
      import type { ComponentOptions } from 'vue'
      const Component: ComponentOptions
      export default Component
    }
    
    declare module '*.md' {
      import type { ComponentOptions } from 'vue'
      const Component: ComponentOptions
      export default Component
    }
  4. Builder Installs (optional)

    Modern versions of this plugin provide a powerful pipeline system for extending the functionality of this plugin. You can use provided/recommended plugins but you can create these yourself. More on this below but for now be aware that the three builders which had been originally included as internal builders are now "external" to both demonstrate how you can do this and to keep this repo more focused on core pipelining.

    The three "built-in" builders were code(), link(), and meta(). Instead of importing them directly as symbols from this repo you can now just import them directly from their repos:

    • code - pnpm add -D @yankeeinlondon/code-builder

      npm install -D @yankeeinlondon/code-builder

      yarn add -D @yankeeinlondon/code-builder

    • meta - pnpm add --save-dev @yankeeinlondon/meta-builder

    • link - pnpm add --save-dev @yankeeinlondon/link-builder

    At this point the process is exactly the same as before, you simply add these builders into the configuration for this repo like so:

    import Markdown from 'vite-plugin-md'
    import code from '@yankeeinlondon/code-builder'
    
    export default {
      plugins: [
        Markdown({
          builders: [code()]
        })
      ]
    }

    Note: code, meta, and link can all be imported from md-powerpack -- npm install -D md-powerpack -- which is an aggregation repo for builder API's. Either approach is equally valid.

Using this Plugin

Refer to the example app in this repo for a working example but the really short answer is ... just write markdown files in the same places where you might have written VueJS SFC components and they will both be treated as Vue components in every way.

That means you can:

  1. Import Markdown as Vue components

    <template>
      <HelloWorld />
    </template>
    
    <script>
    import HelloWorld from './README.md'
    
    export default {
      components: {
        HelloWorld,
      },
    }
    </script>
  2. Use Vue Components inside your Markdown

    # My Page
    There I was, there I was ... in the jungle. Then I started hearing this ticking sound and I realized it was some sort of _counter_?
    
    <Counter :init='5'/>
    
    I looked a bit closer and realized I could **press** this counter and it would change! What is this magic?

    In this example we use a custom Vue component called Counter (actually found in the demo app) and intermix it with our Markdown content. In this example we leveraged the ability to automatically import components with the powerful Vite plugin unplugin-vue-components but if you prefer not to you can just manually import some components globally or you can also add the following block to your Markdown:

    <script setup>
    import { Counter } from './Counter.vue'
    </script>

    In most cases, however, use of the unplugin-vue-components just makes life simpler. :)

  3. Frontmatter

    Frontmatter is a meta-data standard used with most of the static content frameworks and allows you to put name/value pairs at the top of your Markdown files and then use this content within the page. For example:

    ---
    name: My Cool App
    ---
    
    # Hello World
    
    This is {{name}}

    Will be rendered as:

    <h1>Hello World</h1>
    <p>This is My Cool App</p>

    Leveraging Meta Properties

    It is often useful to have certain "meta properties" associated with your pages and you can do this easily in one of two ways:

    1. If you use @vueuse/head then you can enable the headEnabled configuration option
    2. If you want to go further you can add the meta() builder mentioned above

    With both options you can start to add frontmatter like this:

    meta:
      - name: My Cool App
        description: cool things happen to people who use cool apps

    This will then intelligently be incorporated into <meta> tags in the resulting output. For more information look at the corresponding docs:

  4. Import Frontmatter from Markdown

    Not only can you import Markdown files as VueJS components (using the default import) but you can also import a Markdown file's frontmatter via a named export:

    import { frontmatter } from 'my-app.md'

Configuration / Options

  1. Options Hash

    The configuration for this plugin is a fully typed dictionary of options and therefore is largely self-documenting.

    See the ts-doc for more advanced options

  2. Markdown-It plugins (and options)

    Under the hood this plugin leverages markdown-it for converting Markdown content to HTML. This parser is very mature and has a rich set of plugins that you use quite easily. If you don't find what you want you can also build your own plugin relatively easily [ docs ].

    Whether you're using or building a plugin, you will incorporate it into this plugin using the markdownItSetup property. Alternatively you can also set configuration options of markdown-it with markdownItOptions:

    // vite.config.js
    import Markdown from 'vite-plugin-md'
    
    export default {
      plugins: [
        Markdown({
          markdownItOptions: {
            html: true,
            linkify: true,
            typographer: true,
          },
          markdownItSetup(md) {
            // add anchor links to your H[x] tags
            md.use(require('markdown-it-anchor'))
            // add code syntax highlighting with Prism
            md.use(require('markdown-it-prism'))
          },
        }),
      ],
    }
  3. Builder APIs

    Builder API's are mini-configurators for a particular feature area. The idea behind them is to allow extending functionality quickly with sensible defaults but also providing their own configurations to allow users to grow into and configure that feature area. The Builder API and Builder pipeline are the preferred way of extending the functionality of this plugin where possible but due to the vast array of MarkdownIt plugins you may still need to rely on that ecosystem in some cases.

    To empower developers the docs and a createBuilder utility can be found here:

    and examples of builders can be found here:

    If you wanted to use any of these builders in their default configuration, you would simply add the following to your options config for this plugin:

    import Markdown from 'vite-plugin-md'
    // note: all of these plugins are available as part of an aggregation
    // repo for Builder APIs (but you can import directly if you prefer)
    import { code, link } from 'md-powerpack'
    export default {
      plugins: [
        Markdown({
          builders: [link(), code()],
        }),
      ],
    }

Example Usage

See the /example app in this repo.

Or the pre-configured starter template Vitesse.

Integrations

This plugin has good integrations with several other plugins, including:

License

MIT License © 2020-PRESENT Anthony Fu and Ken Snyder

More Repositories

1

vitesse

🏕 Opinionated Vite + Vue Starter Template
TypeScript
8,522
star
2

ni

💡 Use the right package manager
TypeScript
4,527
star
3

icones

⚡️ Icon Explorer with Instant searching, powered by Iconify
Vue
4,525
star
4

unplugin-vue-components

📲 On-demand components auto importing for Vue
TypeScript
3,011
star
5

unocss

The instant on-demand atomic CSS engine.
TypeScript
2,990
star
6

unplugin-icons

🤹 Access thousands of icons as components on-demand universally.
TypeScript
2,793
star
7

vitesse-webext

⚡️ WebExtension Vite Starter Template
TypeScript
2,581
star
8

unplugin-auto-import

Auto import APIs on-demand for Vite, Webpack and Rollup
TypeScript
2,307
star
9

vscode-file-nesting-config

Config of File Nesting for VS Code
JavaScript
2,132
star
10

vue-starport

🛰 Shared component across routes with animations
TypeScript
1,744
star
11

vscode-settings

My VS Code settings and extensions
1,739
star
12

eslint-config

Anthony's ESLint config presets
JavaScript
1,605
star
13

vitesse-nuxt3

Vitesse for Nuxt 3 🏔💚⚡️
TypeScript
1,510
star
14

shikiji

A syntax highlighter based on TextMate grammars. ESM rewrite of shiki, with more features and capabilities.
TypeScript
1,483
star
15

reactivue

🙊 Use Vue Composition API in React components
TypeScript
1,352
star
16

taze

🥦 A modern cli tool that keeps your deps fresh
TypeScript
1,233
star
17

vite-ssg

Static site generation for Vue 3 on Vite
TypeScript
1,198
star
18

handle

A Chinese Hanzi variation of Wordle - 汉字 Wordle
TypeScript
1,194
star
19

case-police

🚨 Make the case correct, PLEASE!
TypeScript
1,098
star
20

vite-plugin-inspect

Inspect the intermediate state of Vite plugins
Vue
1,009
star
21

qrcode-toolkit

Anthony's QR Code Toolkit for AI generated QR Codes
Vue
1,008
star
22

use

Things I am using
946
star
23

vitesse-lite

⛺️ Lightweight version of Vitesse
TypeScript
901
star
24

drauu

Headless SVG-based drawboard in browser.
TypeScript
817
star
25

broz

A simple, frameless browser for screenshots
JavaScript
745
star
26

iroiro

Beautiful Colors Lookup in CLI
TypeScript
735
star
27

retypewriter

Replay the steps of your changes at ease.
TypeScript
715
star
28

live-draw

A tool allows you to draw on screen real-time.
C#
640
star
29

changelogithub

Generate changelog for GitHub
TypeScript
613
star
30

sd-webui-qrcode-toolkit

Anthony's QR Toolkit for Stable Diffusion WebUI
JavaScript
604
star
31

vscode-smart-clicks

Smart selection with double clicks for VS Code.
TypeScript
601
star
32

unplugin-vue2-script-setup

💡 Bring `<script setup>` to Vue 2.
TypeScript
567
star
33

eslint-flat-config-viewer

A visual tool to help you view and understand your ESLint Flat config.
Vue
557
star
34

sponsorkit

💖 Toolkit for generating sponsors images 😄
TypeScript
550
star
35

unconfig

A universal solution for loading configurations.
TypeScript
524
star
36

utils

Collection of common JavaScript / TypeScript utils
TypeScript
493
star
37

100

My 100-day project of exploring design, compform, and new things.
Vue
491
star
38

v-lazy-show

Compile-time directive to lazy initialize v-show for Vue
TypeScript
480
star
39

antfu.me

My personal website
Markdown
463
star
40

vue-reuse-template

Define and reuse Vue template inside the component scope.
TypeScript
440
star
41

raycast-multi-translate

A Raycast extension that translates text to multiple languages at once
TypeScript
431
star
42

vite-node

Vite as Node.js runtime
JavaScript
428
star
43

vscode-browse-lite

🚀 An embedded browser in VS Code
TypeScript
425
star
44

vscode-vite

One step faster for Vite in VS Code ⚡️
TypeScript
404
star
45

vscode-theme-vitesse

🏕 Vitesse theme for VS Code
TypeScript
395
star
46

starter-ts

Starter template for TypeScript library
TypeScript
386
star
47

vue-template-promise

Template as Promise in Vue
TypeScript
376
star
48

vue-global-api

Use Vue Composition API globally
TypeScript
331
star
49

vue-router-better-scroller

Enhanced scroll behavior for Vue Router
TypeScript
321
star
50

starter-vscode

Starter template for VS Code Extension
TypeScript
299
star
51

vscode-iconify

🙂 Iconify IntelliSense for VS Code
TypeScript
294
star
52

1990-script

Make your GitHub history back to 1990
Shell
293
star
53

p

Toolkit for managing multiple promises
TypeScript
284
star
54

qr-scanner-wechat

QR Code scanner in JS with Open CV and WeChat's Algorithm
JavaScript
281
star
55

wenyan-lang-vscode

文言 Wenyan Lang for VS Code
TypeScript
277
star
56

fsxx

File system in zx style
JavaScript
262
star
57

birpc

Message-based two-way remote procedure call.
TypeScript
245
star
58

pnpm-patch-i

A better and interactive pnpm patch
TypeScript
243
star
59

vue-minesweeper

A tiny minesweeper clone in Vue 3
TypeScript
242
star
60

contribute

Contribution guide for my projects
231
star
61

purge-icons

🎐 Bundles icons on demand
TypeScript
229
star
62

vscode-open-in-github-button

Add a button to go to the GitHub on the status bar.
TypeScript
223
star
63

github-doorcat

😼 Supercharges GitHub navbar for fast navigation [WIP]
TypeScript
214
star
64

vscode-goto-alias

Go to Definition following alias redirections.
TypeScript
211
star
65

nuxt-server-fn

Server functions in client for Nuxt 3
TypeScript
194
star
66

refined-github-notifications

UserScript that enhances the GitHub Notifications
JavaScript
194
star
67

prism-theme-vars

A customizable Prism.js theme using CSS variables
CSS
178
star
68

what-time

What time works for you?
Vue
177
star
69

esbuild-node-loader

Transpile TypeScript to ESM with Node.js loader.
JavaScript
176
star
70

vitesse-nuxt-bridge

🏕 Vitesse experience for Nuxt 2 and Vue 2
Vue
174
star
71

talks

Slides & code for my talks
Vue
170
star
72

userscript-clean-twitter

Bring back the peace on Twitter
JavaScript
170
star
73

fs-spy

Monitoring fs accessing for Node process
TypeScript
166
star
74

vscode-icons-carbon

Carbon Visual Studio Code product icon theme
TypeScript
166
star
75

magic-string-stack

magic-string with the capability of committing changes.
TypeScript
165
star
76

vite-plugin-glob

The design experiment for import.meta.glob from Vite.
TypeScript
164
star
77

eslint-plugin-command

Comment-as-command for one-off codemod with ESLint.
TypeScript
163
star
78

vite-plugin-optimize-persist

Persist dynamically analyzed dependencies optimization
TypeScript
155
star
79

diff-match-patch-es

ESM and TypeScript rewrite of Google's diff-match-patch for JavaScript
TypeScript
150
star
80

v-dollar

jQuery-like Vue Reactivity API
TypeScript
146
star
81

eslint-ts-patch

Support loading eslint.config.mjs and eslint.config.ts as flat config files for ESLint.
JavaScript
146
star
82

eslint-typegen

Generate types from ESLint rule schemas, with auto-completion and type-checking for rule options.
TypeScript
144
star
83

.github

The default community health files for all my repos on GitHub
143
star
84

p5i

p5.js, but with more friendly instance mode APIs
TypeScript
139
star
85

markdown-it-github-alerts

Support GitHub-style alerts for markdown-it
TypeScript
132
star
86

magic-string-extra

Extended magic-string with extra utilities
TypeScript
131
star
87

export-size

Analysis bundle cost for each export of a package
TypeScript
130
star
88

local-pkg

Get information on local packages.
TypeScript
126
star
89

vite-plugin-restart

Custom files/globs to restart Vite server
TypeScript
124
star
90

pkg-exports

Get exports of an local npm package.
TypeScript
116
star
91

install-pkg

Install package programmatically.
TypeScript
114
star
92

deploy-check

WIP, Prevent runtime errors earlier in CI
TypeScript
113
star
93

qr-verify

A CLI to verify scannable QR Code in batch
TypeScript
106
star
94

issue-up

Mirror issues to the upstream repos
TypeScript
104
star
95

unplugin-starter

Starter template for unplugin
TypeScript
100
star
96

windicss-runtime-dom

🪄 Enables Windi CSS for any site with one-line code without any build tools
TypeScript
100
star
97

rex

📑 Transform texts with RegExp like a Pro.
Vue
98
star
98

vscode-auto-npx

Auto resolving local Node.js binaries in VS Code terminal.
TypeScript
96
star
99

vite-plugin-remote-assets

Bundles your assets from remote urls with your app
TypeScript
94
star
100

markdown-it-mdc

MDC (Markdown Components) syntax for markdown-it.
TypeScript
92
star