Skip to content

Commit b80f43a

Browse files
committed
feat: builtin mdc and shiki
close #4 close #5
1 parent 0424687 commit b80f43a

File tree

8 files changed

+1425
-982
lines changed

8 files changed

+1425
-982
lines changed

README.md

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ Nuxt Module to compile markdown files into Vue SFC at **build time**. This enabl
99

1010
- 📚 Write pages & components in Markdown
1111
- 💚 Use Vue components in Markdown.
12-
- 👌 Support SEO & Page metas
12+
- 👌 Support SEO & Page Meta
13+
- 📦 Built-in support for [MDC (Markdown Components)](https://remark-mdc.nuxt.space/)
14+
- 🧑‍💻 Syntax highlighting with light/dark mode support
1315

1416
## Install
1517

@@ -46,7 +48,7 @@ pages/
4648
index.md
4749
```
4850

49-
## SEO & Page metas
51+
## SEO & Page Meta
5052

5153
Use the `seo` property in the frontmatter to leverage [`useSeoMeta()`](https://nuxt.com/docs/api/composables/use-seo-meta):
5254

@@ -64,7 +66,7 @@ seo:
6466

6567
Will be transformed to:
6668

67-
```vue
69+
```html
6870
<script setup>
6971
useSeoMeta({
7072
title: 'My title',
@@ -93,7 +95,7 @@ meta:
9395

9496
Will be transformed to:
9597

96-
```vue
98+
```html
9799
<script setup>
98100
definePageMeta({
99101
layout: 'dark',
@@ -137,6 +139,16 @@ I can use a Markdown component:
137139
<HelloWorld />
138140
```
139141

142+
[MDC (Markdown Components)](https://remark-mdc.nuxt.space/) support is also built-in, meaning you can also do:
143+
144+
```md
145+
Inline :my-component{name="World"} syntax.
146+
147+
::HelloWorld
148+
And also **block** syntax{.text-green}
149+
::
150+
```
151+
140152
If the Markdown is not inside the `components/` directory, you can import it and use it as a normal Vue component:
141153

142154
```vue
@@ -149,6 +161,36 @@ import Readme from '../README.md'
149161
</template>
150162
```
151163

164+
## Syntax Highlighting
165+
166+
We have [`shiki`](https://github.com/shikijs/shiki) built-in for syntax highlighting (actually uses [`shikiji`](https://github.com/antfu/shikiji), a ESM build of `shiki`). It's enabled by default and supports light/dark mode out of the box.
167+
168+
To apply the dark mode theme, you will need to add a bit of CSS:
169+
170+
###### Query-based Dark Mode
171+
172+
```css
173+
@media (prefers-color-scheme: dark) {
174+
.shiki,
175+
.shiki span {
176+
background-color: var(--shiki-dark-bg) !important;
177+
color: var(--shiki-dark) !important;
178+
}
179+
}
180+
```
181+
182+
###### Class-based Dark Mode
183+
184+
```css
185+
html.dark .shiki,
186+
html.dark .shiki span {
187+
background-color: var(--shiki-dark-bg) !important;
188+
color: var(--shiki-dark) !important;
189+
}
190+
```
191+
192+
You can learn more about the [Dual Themes suppport](https://github.com/antfu/shikiji#lightdark-dual-themes).
193+
152194
## `<script>` and `<style>`
153195

154196
Root-level `<script>` and `<style>` tags in Markdown files work just like they do in Vue SFCs, including `<script setup>`, `<style module>`, etc. The main difference here is that there is no `<template>` tag: all other root-level content is Markdown. Also note that all tags should be placed after the frontmatter:
@@ -175,7 +217,7 @@ The count is: {{ count }}
175217

176218
### How does this compare to [@nuxt/content](https://content.nuxtjs.org/)?
177219

178-
`nuxt-compile-markdown` works at built time and converts the Markdown files to Vue files for maximum performance. This module does not have the ability to query content like [Nuxt Content Query Builder](https://content.nuxtjs.org/guide/displaying/querying). Also `nuxt-compile-markdown` does not support the MDC syntax (for now).
220+
`nuxt-compile-markdown` works at built time and converts the Markdown files to Vue files for maximum performance. This module does not have the ability to query content like [Nuxt Content Query Builder](https://content.nuxtjs.org/guide/displaying/querying).
179221

180222
<!-- Badges -->
181223
[npm-version-src]: https://img.shields.io/npm/v/nuxt-compile-markdown/latest.svg?style=flat&colorA=18181B&colorB=28CF8D

package.json

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "nuxt-compile-markdown",
33
"type": "module",
44
"version": "0.0.1",
5-
"packageManager": "pnpm@8.6.12",
5+
"packageManager": "pnpm@8.7.0",
66
"description": "My new Nuxt module",
77
"license": "MIT",
88
"repository": "nuxt-modules/compile-markdown",
@@ -29,20 +29,23 @@
2929
"test:watch": "vitest watch"
3030
},
3131
"dependencies": {
32-
"@nuxt/kit": "^3.6.5",
33-
"unplugin-vue-markdown": "^0.24.2"
32+
"@nuxt/kit": "^3.7.0",
33+
"markdown-it": "^13.0.1",
34+
"markdown-it-mdc": "0.0.3",
35+
"markdown-it-shikiji": "^0.6.3",
36+
"shikiji": "^0.6.3",
37+
"unplugin-vue-markdown": "^0.24.3"
3438
},
3539
"devDependencies": {
36-
"@antfu/eslint-config": "^0.40.2",
40+
"@antfu/eslint-config": "^0.41.0",
3741
"@nuxt/devtools": "latest",
38-
"@nuxt/module-builder": "^0.4.0",
39-
"@nuxt/schema": "^3.6.5",
40-
"@nuxt/test-utils": "^3.6.5",
41-
"@types/node": "^20.5.0",
42-
"changelogen": "^0.5.4",
43-
"eslint": "^8.47.0",
44-
"nuxt": "^3.6.5",
45-
"shikiji": "^0.5.0",
46-
"vitest": "^0.34.2"
42+
"@nuxt/module-builder": "^0.5.0",
43+
"@nuxt/schema": "^3.7.0",
44+
"@nuxt/test-utils": "^3.7.0",
45+
"@types/node": "^20.5.7",
46+
"changelogen": "^0.5.5",
47+
"eslint": "^8.48.0",
48+
"nuxt": "^3.7.0",
49+
"vitest": "^0.34.3"
4750
}
48-
}
51+
}

playground/components/Docs.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
### Markdown as Component
22

33
This is a components written as markdown.
4+
5+
| Table |
6+
| ----- |
7+
| <slot /> |

playground/nuxt.config.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { bundledLanguages, getHighlighter } from 'shikiji'
2-
31
export default defineNuxtConfig({
42
// builder: 'webpack',
53
modules: ['../src'],
@@ -10,24 +8,6 @@ export default defineNuxtConfig({
108
},
119
},
1210
markdown: {
13-
async markdownItSetup(md) {
14-
const shiki = await getHighlighter({
15-
themes: ['vitesse-dark', 'vitesse-light'],
16-
langs: Object.keys(bundledLanguages) as any,
17-
})
18-
19-
md.use((markdown) => {
20-
markdown.options.highlight = (code, lang) => {
21-
return shiki.codeToHtml(code, {
22-
lang,
23-
themes: {
24-
light: 'vitesse-light',
25-
dark: 'vitesse-dark',
26-
},
27-
cssVariablePrefix: '--s-',
28-
})
29-
}
30-
})
31-
},
11+
mdc: true,
3212
},
3313
})

playground/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"generate": "nuxi generate"
99
},
1010
"devDependencies": {
11-
"@nuxt/webpack-builder": "^3.6.5",
11+
"@nuxt/webpack-builder": "^3.7.0",
1212
"nuxt": "latest"
1313
}
1414
}

playground/pages/index.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Home!
33
---
44

5-
# Hello
5+
# Hello{style="color:skyblue"}
66

77
Nuxt Page as Markdown
88

@@ -15,7 +15,9 @@ Nuxt Page as Markdown
1515

1616
<Counter />
1717

18-
<Docs />
18+
::docs
19+
Slot content
20+
::
1921

2022
---
2123

0 commit comments

Comments
 (0)