• Stars
    star
    204
  • Rank 192,063 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

A vite plugin to import a Markdown file in various formats like Front Matter, HTML, ToC, and React/Vue Component

vite-plugin-markdown

npm npm

A plugin enables you to import a Markdown file as various formats on your vite project.

Setup

npm i -D vite-plugin-markdown
For vite v1
npm i -D vite-plugin-markdown@vite-1

Config

const mdPlugin = require('vite-plugin-markdown')

module.exports = {
  plugins: [mdPlugin(options)]
}

Then you can import front matter attributes from .md file as default.

---
title: Awesome Title
description: Describe this awesome content
tags:
  - "great"
  - "awesome"
  - "rad"
---

# This is awesome

Vite is an opinionated web dev build tool that serves your code via native ES Module imports during dev and bundles it with Rollup for production.
import { attributes } from './contents/the-doc.md';

console.log(attributes) //=> { title: 'Awesome Title', description: 'Describe this awesome content', tags: ['great', 'awesome', 'rad'] }

Options

mode?: ('html' | 'markdown' | 'toc' | 'react' | 'vue')[]
markdown?: (body: string) => string
markdownIt?: MarkdownIt | MarkdownIt.Options

Enum for mode is provided as Mode

import { Mode } from 'vite-plugin-markdown'

console.log(Mode.HTML) //=> 'html'
console.log(Mode.MARKDOWN) //=> 'markdown'
console.log(Mode.TOC) //=> 'toc'
console.log(Mode.REACT) //=> 'react'
console.log(Mode.VUE) //=> 'vue'

"Mode" enables you to import markdown file in various formats (HTML, ToC, React/Vue Component)

Mode.HTML

Import compiled HTML
# This is awesome

Vite is an opinionated web dev build tool that serves your code via native ES Module imports during dev and bundles it with Rollup for production.
import { html } from './contents/the-doc.md';

console.log(html) //=> "<h1>This is awesome</h1><p>ite is an opinionated web dev build tool that serves your code via native ES Module imports during dev and bundles it with Rollup for production.</p>"

Mode.MARKDOWN

Import the raw Markdown content
import { markdown } from './contents/the-doc.md'

console.log(markdown) //=> "# This is awesome \n Vite is an opinionated web dev build tool that serves your code via native ES Module imports during dev and bundles it with Rollup for production."

Mode.TOC

Import ToC metadata
# vite

Vite is an opinionated web dev build tool that serves your code via native ES Module imports during dev and bundles it with Rollup for production.

## Status

## Getting Started

# Notes
import { toc } from './contents/the-doc.md'

console.log(toc) //=> [{ level: '1', content: 'vite' }, { level: '2', content: 'Status' }, { level: '2', content: 'Getting Started' }, { level: '1', content: 'Notes' },]

Mode.REACT

Import as a React component
import React from 'react'
import { ReactComponent } from './contents/the-doc.md'

function MyReactApp() {
  return (
    <div>
      <ReactComponent />
    </div>
}
Custom Element on a markdown file can be runnable as a React component as well
# This is awesome

Vite is <MyComponent type={'react'}>
import React from 'react'
import { ReactComponent } from './contents/the-doc.md'
import { MyComponent } from './my-component'

function MyReactApp() {
  return (
    <div>
      <ReactComponent MyComponent={MyComponent} />
    </div>
}

MyComponent on markdown perform as a React component.

Mode.VUE

Import as a Vue component
<template>
  <article>
    <markdown-content />
  </article>
</template>

<script>
import { VueComponent } from './contents/the-doc.md'

export default {
  components: {
    MarkdownContent: VueComponent
  }
};
</script>
Custom Element on a markdown file can be runnable as a Vue component as well
# This is awesome

Vite is <MyComponent :type="'vue'">
<template>
  <article>
    <markdown-content />
  </article>
</template>

<script>
import { VueComponentWith } from './contents/the-doc.md'
import MyComponent from './my-component.vue'

export default {
  components: {
    MarkdownContent: VueComponentWith({ MyComponent })
  }
};
</script>

MyComponent on markdown perform as a Vue component.

Type declarations

In TypeScript project, need to declare typedefs for .md file as you need.

declare module '*.md' {
  // "unknown" would be more detailed depends on how you structure frontmatter
  const attributes: Record<string, unknown>; 

  // When "Mode.TOC" is requested
  const toc: { level: string, content: string }[];

  // When "Mode.HTML" is requested
  const html: string;

  // When "Mode.RAW" is requested
  const raw: string

  // When "Mode.React" is requested. VFC could take a generic like React.VFC<{ MyComponent: TypeOfMyComponent }>
  import React from 'react'
  const ReactComponent: React.VFC;
  
  // When "Mode.Vue" is requested
  import { ComponentOptions, Component } from 'vue';
  const VueComponent: ComponentOptions;
  const VueComponentWith: (components: Record<string, Component>) => ComponentOptions;

  // Modify below per your usage
  export { attributes, toc, html, ReactComponent, VueComponent, VueComponentWith };
}

Save as vite.d.ts for instance.

License

MIT

More Repositories

1

frontmatter-markdown-loader

📝 Webpack Loader for: FrontMatter (.md) -> HTML + Attributes (+ React/Vue Component)
JavaScript
251
star
2

vite-plugin-elm

A plugin for Vite enables you to compile an Elm application/document/element
TypeScript
159
star
3

jest-matcher-vue-test-utils

✨ Cute jest matchers to test Vue components with vue-test-utils
TypeScript
32
star
4

nuxt-ts

⚠️ DEPRECATED: Enable your Nuxt project to use TypeScript easier and quicker
JavaScript
22
star
5

nuxt-typescript-template

Handy starter for Nuxt 2 + TypeScript
JavaScript
11
star
6

frontmatter-markdown-loader-vue-sample

A sample project for compiled Vue by frontmatter-markdown-loader
JavaScript
10
star
7

hmsk.me

🎮 A personal website of @hmsk with the UI like "Ring Command" from "Secret of Mana"
Elm
10
star
8

netlify-functions-in-go

Deployable template of multiple Go lambda functions for Netlify Functions
Go
8
star
9

hubot-esa

A Hubot script handling webhooks and retrieving info from esa: https://esa.io
CoffeeScript
8
star
10

ring-fit-adventure-icons

128x128 icons for Slack Team/Discord Guild with respecting Ring Fit Adventure by Nitendo.
7
star
11

frontmatter-markdown-loader-nuxt-sample

Nuxt app to demonstrate the usage of frontmatter-markdown-loader
Vue
7
star
12

symlink-webpack-plugin

🔗 Make symbolic links among files built by Webpack
JavaScript
5
star
13

harp-gh-pages-boilerplate

The boilerplate of harp project publishing compiled files to GitHub Pages
JavaScript
4
star
14

text.hmsk.me

A blog system with Elm, siteelm, and Netlify CMS
Elm
3
star
15

generator-harp-gh-pages

Yeoman generater for Harp project deploying with GitHub Pages, automating with Circle CI
JavaScript
3
star
16

webpay-sinatra-sample

Try WebPay API with Ruby, Sinatra
Ruby
3
star
17

persona-js

An unofficial JS client for Persona withpersona.com
TypeScript
3
star
18

hubot-sandbox

Shell
1
star
19

nord-atom-ui-vertical-tabs

Change tab direction of nord-atom-ui
CoffeeScript
1
star
20

elm-css-modern-normalize

An Elm port of modern-normalize for rtfeldman/elm-css.
Elm
1
star
21

lzfx

Ruby
1
star
22

elm-vite-plugin-helper

An Elm package to provide helpers for using vite-plugin-elm
Elm
1
star