vite-plugin-markdown
Version:
Import markdown files in vite
204 lines (145 loc) • 4.14 kB
Markdown
[](https://www.npmjs.com/package/vite-plugin-markdown) [](https://www.npmjs.com/package/vite-plugin-markdown/v/vite-1)
A plugin enables you to import a Markdown file as various formats on your [vite](https://github.com/vitejs/vite) project.
```
npx i -D vite-plugin-markdown
```
<details>
<summary>For vite v1</summary>
```
npx i -D vite-plugin-markdown@vite-1
```
</details>
```js
const mdPlugin = require('vite-plugin-markdown')
module.exports = {
plugins: [mdPlugin(options)]
}
```
```ts
mode?: ('html' | 'toc' | 'react' | 'vue')[]
markdown?: (body: string) => string
markdownIt?: MarkdownIt | MarkdownIt.Options
```
Enum for `mode` is provided as `Mode`
```ts
import { Mode } from 'vite-plugin-markdown'
console.log(Mode.HTML) //=> 'html'
console.log(Mode.TOC) //=> 'toc'
console.log(Mode.REACT) //=> 'react'
console.log(Mode.VUE) //=> 'vue'
```
```md
---
title: Awesome Title
description: Describe this awesome content
tags:
- "great"
- "awesome"
- "rad"
---
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.
```
```ts
import { attributes } from './contents/the-doc.md';
console.log(attributes) //=> { title: 'Awesome Title', description: 'Describe this awesome content', tags: ['great', 'awesome', 'rad'] }
```
```md
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.
```
```ts
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>"
```
```md
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.
```
```ts
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' },]
```
```jsx
import React from 'react'
import { ReactComponent } from './contents/the-doc.md'
function MyReactApp() {
return (
<div>
<ReactComponent />
</div>
}
```
<details>
<summary>Custom Element on a markdown file can be runnable as a React component as well</summary>
```md
Vite is <MyComponent type={'react'}>
```
```jsx
import React from 'react'
import { ReactComponent } from './contents/the-doc.md'
import { MyComponent } from './my-component'
function MyReactApp() {
return (
<div>
<ReactComponent my-component={MyComponent} />
</div>
}
```
`MyComponent` on markdown perform as a React component.
</details>
```vue
<template>
<article>
<markdown-content />
</article>
</template>
<script>
import { VueComponent } from './contents/the-doc.md'
export default {
components: {
MarkdownContent: VueComponent
}
};
</script>
```
<details>
<summary>Custom Element on a markdown file can be runnable as a Vue component as well</summary>
```md
Vite is <MyComponent :type="'vue'">
```
```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.
</details>
MIT