Skip to content

Commit

Permalink
feat: frontmatter, close antfu#2
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 21, 2020
1 parent d7d87db commit 2168f79
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 17 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,31 @@ app.mount()

Use [`vite-plugin-components`](#work-with-vite-plugin-components) for auto components registration.

## Frontmatter

Frontmatter will be parsed and inject into Vue's instance data `frontmatter` field.

For example:

```md
---
title: My Cool App
---

# Hello World

This is {{frontmatter.title}}
```

Will be rendered as

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

It will also be passed to the wrapper component's props if you have set `wrapperComponent` option.

## Options

`vite-plugin-md` uses [`markdown-it`](https://github.com/markdown-it/markdown-it) under the hood, see [`markdown-it`'s docs](https://markdown-it.github.io/markdown-it/) for more details
Expand Down
14 changes: 5 additions & 9 deletions example/App.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<template>
<HelloWorld style="padding: 20px 40px" />
<ComponentA style="padding: 20px 40px" />
<ComponentB style="padding: 20px 40px" />
</template>

<script>
import HelloWorld from './README.md'
export default {
components: {
HelloWorld,
},
}
<script setup>
import ComponentA from './README.md'
import ComponentB from './matter.md'
</script>
8 changes: 8 additions & 0 deletions example/matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Hello
slug: home
---

<h1>Hello world!</h1>

Front matter: {{ frontmatter }}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"vite": "^1.0.0-rc.13"
},
"dependencies": {
"gray-matter": "^4.0.2",
"markdown-it": "^12.0.2"
}
}
54 changes: 50 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Plugin } from 'vite'
import MarkdownIt from 'markdown-it'
import { compileTemplate } from '@vue/compiler-sfc'
import matter from 'gray-matter'
import { Options, ResolvedOptions } from './types'

function toArray<T>(n: T | T[]): T[] {
Expand Down Expand Up @@ -44,16 +45,17 @@ function VitePluginMarkdown(options: Options = {}): Plugin {
},
transform(ctx) {
const { isBuild, path } = ctx
let md = ctx.code
let raw = ctx.code

if (resolved.transforms.before)
md = resolved.transforms.before({ ...ctx, code: md })
raw = resolved.transforms.before({ ...ctx, code: raw })

const { content: md, data: frontmatter } = matter(raw)
let sfc = markdown.render(md, {})
if (resolved.wrapperClasses)
sfc = `<div class="${wrapperClasses}">${sfc}</div>`
if (resolved.wrapperComponent)
sfc = `<${resolved.wrapperComponent}>${sfc}</${resolved.wrapperComponent}>`
sfc = `<${resolved.wrapperComponent} :frontmatter="frontmatter">${sfc}</${resolved.wrapperComponent}>`

if (resolved.transforms.after)
sfc = resolved.transforms.after({ ...ctx, code: sfc })
Expand All @@ -66,7 +68,9 @@ function VitePluginMarkdown(options: Options = {}): Plugin {
})

result = result.replace('export function render', 'function render')
result += '\nconst __script = { render };'
result += `\nconst __matter = ${JSON.stringify(frontmatter)};`
result += '\nconst data = () => ({ frontmatter: __matter });'
result += '\nconst __script = { render, data };'

if (!isBuild)
result += `\n__script.__hmrId = ${JSON.stringify(path)};`
Expand Down

0 comments on commit 2168f79

Please sign in to comment.