-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
137 lines (121 loc) · 3.83 KB
/
vite.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { fileURLToPath } from 'url'
import { dirname, resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import Inspect from 'vite-plugin-inspect'
import Layouts from 'vite-plugin-vue-layouts'
import Markdown from 'unplugin-vue-markdown/vite'
import Pages from 'vite-plugin-pages'
import Unocss from 'unocss/vite'
import Vue from '@vitejs/plugin-vue'
import { defineConfig } from 'vite'
import PluginResolveMd from './plugins/vite-plugin-resolve-md'
import MarkdownItPrism from 'markdown-it-prism';
import MarkdownItAnchor from 'markdown-it-anchor';
import grayMatter from "gray-matter";
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import { toString as mdAstToString } from 'mdast-util-to-string';
const _dirname = typeof __dirname !== 'undefined'
? __dirname
: dirname(fileURLToPath(import.meta.url))
const config = defineConfig({
mode: 'spa',
resolve: {
alias: {
'~/': `${resolve(_dirname, 'src')}/`,
},
},
plugins: [
Vue({
include: [/\.vue$/, /\.md$/],
reactivityTransform: true,
}),
AutoImport({
imports: ['vue', 'vue-router', '@vueuse/head', '@vueuse/core'],
dts: 'src/auto-imports.d.ts',
}),
Components({
// allow auto load markdown components under `./src/components/`
extensions: ['vue', 'md'],
// allow auto import and register components used in markdown
include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
dts: 'src/components.d.ts',
}),
Unocss(),
Markdown({
markdownItOptions: {
html: true,
linkify: true,
typographer: true,
},
// A function providing the Markdown It instance gets the ability to apply custom settings/plugins
markdownItSetup(md) {
md.use(MarkdownItPrism)
md.use(MarkdownItAnchor)
},
// Class names for the wrapper div
wrapperClasses: 'markdown-body',
wrapperComponent: 'article-wrapper',
frontmatterPreprocess(fm, options, id, defaultHeadProcess) {
const mdContent = grayMatter.read(id);
const processor = unified()
.use(remarkParse).freeze();
const ast = processor.parse(mdContent.content);
const headings = ast.children.filter(x => x.type === 'heading').map(x => ({ depth: (x as any).depth, title: mdAstToString(x), children: [] as any[] }));
for (let i = 0; i < headings.length; i++) {
const heading = headings[i];
if (heading.depth <= 1) continue;
let j = i;
while (j-- > 0) {
const current = headings[j];
if (current.depth < heading.depth) {
current.children.push(heading);
headings.splice(i, 1);
i--;
break;
}
}
}
const frontmatter = {
title: 'default title',
description: 'default description',
...fm,
headings,
}
const meta: any[] = [
{ property: 'og:title', name: 'twitter:title', itemprop: 'title', content: frontmatter.title },
{
property: 'og:description',
name: 'twitter:description',
itemprop: 'description',
content: frontmatter.description,
},
]
return {
head: { ...frontmatter, meta },
frontmatter: { ...frontmatter, meta },
}
}
}),
// https://github.com/JohnCampionJr/vite-plugin-vue-layouts
Layouts(),
// https://github.com/hannoeru/vite-plugin-pages
Pages({
extensions: ['vue', 'md'],
dirs: [
{
baseRoute: '',
dir: 'src/pages'
},
{
baseRoute: 'articles',
dir: 'articles'
}
]
}),
Inspect(),
PluginResolveMd({}),
],
})
export default config