Skip to content

Commit f74d225

Browse files
authored
docs: update README.md
1 parent f791806 commit f74d225

File tree

1 file changed

+220
-15
lines changed

1 file changed

+220
-15
lines changed

README.md

Lines changed: 220 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,134 @@ export default defineNuxtConfig({
4444
})
4545
```
4646

47-
## Use
47+
That's it! You can start writing and rendering markdown files ✨
4848

49-
Parse MDC content in any environment:
49+
## Parsing Markdown
5050

51-
```ts [parse-mdc.ts]
51+
Nuxt MDC exposes a handy helper to parse MDC files. You can import the `parseMarkdown` function from `@nuxtjs/mdc/runtime` and use it to parse markdown files written with MDC syntax.
52+
53+
### Node.js
54+
55+
```ts [/server/api/parse-mdc.ts]
5256
import { parseMarkdown } from '@nuxtjs/mdc/runtime'
5357

54-
async function main(mdc: string) {
55-
const ast = await parseMarkdown(mdc)
58+
export default eventHandler(async () => {
59+
const mdc = [
60+
'# Hello MDC',
61+
'',
62+
'::alert',
63+
'This is an Alert',
64+
'::'
65+
].join('\n')
5666

57-
// Do your magic with parsed AST tree
67+
const ast = await parseMarkdown(mdc)
5868

5969
return ast
60-
}
70+
})
71+
```
72+
73+
### Browser
74+
75+
The `parseMarkdown` function is a universal helper, and you can also use it in the browser, for example inside a Vue component.
76+
77+
```vue [mdc-test.vue]
78+
<template>
79+
<div>This is a test</div>
80+
</template>
81+
82+
<script setup lang="ts">
83+
import { parseMarkdown } from '@nuxtjs/mdc/runtime'
84+
85+
const props = defineProps({
86+
md: {
87+
type: String,
88+
default: () => '::alert\nMissing markdown input\n::'
89+
}
90+
})
91+
92+
const ast = await parseMarkdown(props.md)
93+
</script>
94+
```
95+
96+
### Options
97+
98+
The `parseMarkdown` helper also accepts options as the second argument to control the parser's behavior. (Checkout [`MDCParseOptions` interface↗︎](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/types/parser.ts)).
99+
100+
| Name | Default | Description |
101+
| -- | -- | -- |
102+
| `remark.plugins` | `{}` | Register / Configure parser's remark plugins. |
103+
| `rehype.options` | `{}` | Configure `remark-rehype` options. |
104+
| `rehype.plugins` | `{}` | Register / Configure parser's rehype plugins. |
105+
| `highlight` | `false` | Control whether code blocks should highlight or not. You can also provide a custom highlighter. |
106+
| `toc.depth` | `2` | Maximum heading depth to include in the table of contents. |
107+
| `toc.searchDepth` | `2` | Maximum depth of nested tags to search for heading. |
108+
109+
Checkout [`MDCParseOptions` types↗︎](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/types/parser.ts).
110+
111+
## Rendering (Vue)
112+
113+
`@nuxtjs/mdc` exposes three components to render markdown files.
114+
115+
### `<MDCRenderer>`
116+
117+
This component will take the result of [`parseMarkdown`](#parsing-markdown) function and render the contents. For example, this is an extended version of the sample code in the [Browser section](#browser) which uses `MDCRenderer` to render the parsed markdown.
118+
119+
```html [mdc-test.vue]
120+
<template>
121+
<MDCRenderer :body="ast.body" :data="ast.data" />
122+
</template>
123+
124+
<script setup lang="ts">
125+
import { parseMarkdown } from '@nuxtjs/mdc/runtime'
126+
127+
const props = defineProps({
128+
md: {
129+
type: String,
130+
default: () => '::alert\nMissing markdown input\n::'
131+
}
132+
})
133+
134+
const ast = await parseMarkdown(props.md)
135+
</script>
136+
```
137+
138+
### `<MDCSlot>`
139+
140+
This component is a replacement for Vue's `<slot/>` component, specifically designed for MDC. Using this component, you can render a component's children while removing one or multiple wrapping elements. In the below example, the Alert component receives text and its default slot (children). But if the component renders this slot using the normal `<slot/>`, it will render a `<p>` element around the text.
141+
142+
```md [markdown.md]
143+
::alert
144+
This is an Alert
145+
::
61146
```
62147

63-
Render MDC content with `<MDC>` component:
148+
```html [Alert.vue]
149+
<template>
150+
<div class="alert">
151+
<!-- Slot will render <p> tag around the text -->
152+
<slot />
153+
</div>
154+
</template>
155+
```
156+
157+
It is the default behavior of markdown to wrap every text inside a paragraph. MDC didn't come to break markdown behavior; instead, the goal of MDC is to make markdown powerful. In this example and all similar situations, you can use `<MDCSlot />` to remove unwanted wrappers.
158+
159+
```html [Alert.vue]
160+
<template>
161+
<div class="alert">
162+
<!-- MDCSlot will only render the actual text without the wrapping <p> -->
163+
<MDCSlot unwrap="p" />
164+
</div>
165+
</template>
166+
```
167+
168+
### `<MDC>`
169+
170+
Using `<MDC>`, you can parse and render markdown contents right inside your components/pages. This component takes raw markdown, parses it using the `parseMarkdown` function, and then renders it with `<MDCRenderer>`.
64171

65172
```html
66173
<template>
67-
<MDC :value="md" tag="article" />
174+
<MDC :value="md" tag="article" />
68175
</template>
69176

70177
<script setup lang="ts">
@@ -76,16 +183,113 @@ Hello MDC
76183
</script>
77184
```
78185

79-
## 💻 Development
186+
### Prose Components
187+
188+
Prose components are a list of components that will be rendered instead of regular HTML tags. For example, instead of rendering a `<p>` tag, `@nuxtjs/mdc` renders a `<ProseP>` component. This is useful when you want to add extra features to your markdown files. For example, you can add a `copy` button to your code blocks.
189+
190+
You can disable prose components by setting the `prose` option to `false` in `nuxt.config.ts`. Or extend the map of prose components to add your own components.
191+
192+
```ts [nuxt.config.ts]
193+
export default defineNuxtConfig({
194+
modules: ['@nuxtjs/mdc'],
195+
mdc: {
196+
components: {
197+
prose: false, // Disable predefined prose components
198+
map: {
199+
p: 'MyCustomPComponent'
200+
}
201+
}
202+
}
203+
})
204+
```
205+
206+
Here is the list of available prose components:
207+
208+
| Tag | Component | Source | Description |
209+
| -- | -- | -- | -- |
210+
| `p` | `<ProseP>` | [ProseP.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseP.vue) | Paragraph |
211+
| `h1` | `<ProseH1>` | [ProseH1.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseH1.vue) | Heading 1 |
212+
| `h2` | `<ProseH2>` | [ProseH2.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseH2.vue) | Heading 2 |
213+
| `h3` | `<ProseH3>` | [ProseH3.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseH3.vue) | Heading 3 |
214+
| `h4` | `<ProseH4>` | [ProseH4.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseH4.vue) | Heading 4 |
215+
| `h5` | `<ProseH5>` | [ProseH5.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseH5.vue) | Heading 5 |
216+
| `h6` | `<ProseH6>` | [ProseH6.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseH6.vue) | Heading 6 |
217+
| `ul` | `<ProseUl>` | [ProseUl.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseUl.vue) | Unordered List |
218+
| `ol` | `<ProseOl>` | [ProseOl.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseOl.vue) | Ordered List |
219+
| `li` | `<ProseLi>` | [ProseLi.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseLi.vue) | List Item |
220+
| `blockquote` | `<ProseBlockquote>` | [ProseBlockquote.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseBlockquote.vue) | Blockquote |
221+
| `hr` | `<ProseHr>` | [ProseHr.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseHr.vue) | Horizontal Rule |
222+
| `pre` | `<ProsePre>` | [ProsePre.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProsePre.vue) | Preformatted Text |
223+
| `code` | `<ProseCode>` | [ProseCode.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseCode.vue) | Code Block |
224+
| `table` | `<ProseTable>` | [ProseTable.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseTable.vue) | Table |
225+
| `thead` | `<ProseThead>` | [ProseThead.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseThead.vue) | Table Head |
226+
| `tbody` | `<ProseTbody>` | [ProseTbody.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseTbody.vue) | Table Body |
227+
| `tr` | `<ProseTr>` | [ProseTr.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseTr.vue) | Table Row |
228+
| `th` | `<ProseTh>` | [ProseTh.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseTh.vue) | Table Header |
229+
| `td` | `<ProseTd>` | [ProseTd.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseTd.vue) | Table Data |
230+
| `a` | `<ProseA>` | [ProseA.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseA.vue) | Anchor Link |
231+
| `img` | `<ProseImg>` | [ProseImg.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseImg.vue) | Image |
232+
| `em` | `<ProseEm>` | [ProseEm.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseEm.vue) | Emphasis |
233+
| `strong` | `<ProseStrong>` | [ProseStrong.vue](https://github.com/nuxt-modules/mdc/blob/main/src/runtime/components/prose/ProseStrong.vue) | Strong |
80234

81-
- Clone repository
82-
- Install dependencies using `pnpm install`
83-
- Prepare using `pnpm dev:prepare`
84-
- Try playground using `pnpm dev`
235+
## Configurations
236+
237+
You can configure the module by providing the `mdc` property in your `nuxt.config.js`; here are the default options:
238+
239+
```ts [nuxt.config.js]
240+
import { defineNuxtConfig } from 'nuxt/config'
241+
242+
export default defineNuxtConfig({
243+
modules: ['@nuxtjs/mdc'],
244+
mdc: {
245+
remarkPlugins: {
246+
plugins: {
247+
// Register/Configure remark plugin to extend the parser
248+
}
249+
},
250+
rehypePlugins: {
251+
options: {
252+
// Configure rehype options to extend the parser
253+
},
254+
plugins: {
255+
// Register/Configure rehype plugin to extend the parser
256+
}
257+
},
258+
headings: {
259+
anchorLinks: {
260+
// Enable/Disable heading anchor links. { h1: true, h2: false }
261+
}
262+
},
263+
highlight: false, // Control syntax highlighting
264+
components: {
265+
prose: false, // Add predefined map to render Prose Components instead of HTML tags, like p, ul, code
266+
map: {
267+
// This map will be used in `<MDCRenderer>` to control rendered components
268+
}
269+
}
270+
}
271+
})
272+
```
273+
274+
Checkout [`ModuleOptions` types↗︎](https://github.com/nuxt-modules/mdc/blob/main/src/types.ts).
275+
276+
## Contributing
277+
278+
You can contribute to this module online with CodeSandbox:
279+
280+
[![Edit @nuxtjs/mdc](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/nuxt-modules/mdc/tree/main/?fontsize=14&hidenavigation=1&theme=dark)
281+
282+
Or locally:
283+
284+
1. Clone this repository
285+
2. Install dependencies using `pnpm install`
286+
3. Start the development server using `pnpm dev`
85287

86288
## License
87289

88-
[MIT](./LICENSE) - Made with 💚
290+
[MIT License](https://github.com/nuxt-modules/mdc/blob/main/LICENSE)
291+
292+
Copyright (c) NuxtLabs
89293

90294
<!-- Badges -->
91295
[npm-version-src]: https://img.shields.io/npm/v/@nuxtjs/mdc/latest.svg?style=flat&colorA=18181B&colorB=28CF8D
@@ -97,5 +301,6 @@ Hello MDC
97301
[license-src]: https://img.shields.io/github/license/nuxt-modules/mdc.svg?style=flat&colorA=18181B&colorB=28CF8D
98302
[license-href]: https://github.com/nuxt-modules/mdc/blob/main/LICENSE
99303

304+
100305
[nuxt-src]: https://img.shields.io/badge/Nuxt-18181B?logo=nuxt.js
101306
[nuxt-href]: https://nuxt.com

0 commit comments

Comments
 (0)