@@ -61,7 +61,7 @@ VitePress 的 Markdown 文件有几个类型,通过 `frontmatter` 中的 `layo
61
61
- [ Registering Global Components] ( https://vitepress.dev/guide/extending-default-theme#registering-global-components )
62
62
- [ Layout Slots] ( https://vitepress.dev/guide/extending-default-theme#layout-slots )
63
63
64
- 当你用 Vue 组件自行实现了某个页面,可以根据 "Registering Global Components" 将其注册为全局组件。这样就可以在 Markdown 页面(区别于作为文章的 Markdown)中使用这个页面组件,避免在自定义 Layout 时混杂太多不同页面的实现。详情请看 :[ Octobug/blog/.vitepress/theme/pages] ( https://github.com/Octobug/blog/tree/main/.vitepress/theme/pages )
64
+ 当你用 Vue 组件自行实现了某个页面,可以根据 "Registering Global Components" 将其注册为全局组件。这样就可以在 Markdown 页面(区别于作为文章的 Markdown)中使用这个页面组件,避免在自定义 Layout 时混杂太多不同页面的实现。详情见 :[ Octobug/blog/.vitepress/theme/pages] ( https://github.com/Octobug/blog/tree/main/.vitepress/theme/pages ) 。
65
65
66
66
### SSR 兼容性
67
67
@@ -71,7 +71,7 @@ VitePress 的 Markdown 文件有几个类型,通过 `frontmatter` 中的 `layo
71
71
Hydration completed but contains mismatches.
72
72
:::
73
73
74
- 这种情况可以使用 ` <ClientOnly><NonSSRFriendlyComponent /></ClientOnly> ` 将动态部分包裹起来:[ SSR Compatibility - ` <ClientOnly> ` ] ( https://vitepress.dev/guide/ssr-compat#clientonly )
74
+ 这种情况可以使用 ` <ClientOnly><NonSSRFriendlyComponent /></ClientOnly> ` 将动态部分包裹起来:[ SSR Compatibility - ` <ClientOnly> ` ] ( https://vitepress.dev/guide/ssr-compat#clientonly ) 。
75
75
76
76
## 常见的博客功能
77
77
@@ -132,29 +132,43 @@ posts
132
132
133
133
在 VitePress 中,文章标题和正文是一个 ` <Content /> ` 整体,不可拆分。要在文章标题下方插入上面这行“三要素”有几个方案:
134
134
135
- 1 . ** 在每篇 Markdown 文章中插入全局注册的 Vue 组件** :这个方案对于后续写文章来说过于繁琐 ❌
136
- 2 . ** 使用 frontmatter title,而不使用 Markdown 一级标题** :VitePress 为文章建立索引时将段落和其前面最近的一个标题归入同个 section,如果不使用 Markdown 一级标题,会导致第一个标题前面的内容不被索引而搜索不到 ❌
135
+ 1 . ❌ ** 在每篇 Markdown 文章中插入全局注册的 Vue 组件** :这个方案对于后续写文章来说过于繁琐。
136
+ 2 . ❌ ** 使用 frontmatter title,而不使用 Markdown 一级标题** :VitePress 为文章建立索引时将段落和其前面最近的一个标题归入同个 section,如果不使用 Markdown 一级标题,会导致第一个标题前面的内容不被索引而搜索不到。
137
137
- [ vuejs/vitepress/src/node/plugins/localSearchPlugin.ts] ( https://github.com/vuejs/vitepress/blob/27f60e0b7784603c6fb300bd8dce64515eb98962/src/node/plugins/localSearchPlugin.ts#L226C35-L226C35 )
138
- - 这一点我不认为是 bug,因为规范的 Markdown 就是要有一个 ` # 一级标题 ` 。
139
- 3 . ** 通过 VitePress 的 markdown-it 接口写类插件代码** :[ markdown-it API] ( https://markdown-it.github.io/markdown-it/ ) 挺复杂的,而且需要想办法将 frontmatter 中的信息传递给插件代码 ❌
140
- 4 . ** 使用 JavaScript 操作 DOM 元素** :先将 Vue 组件放在 Layout 的 doc slots 里面,再用 JS 把渲染后的 DOM 元素搬运到标题之下。这个方案很丑陋,但似乎是这几个方案里面最方便可行的一个 🤷
138
+ - 这一点我不认为是 bug,因为规范的 Markdown 就是要有一个 ` # 一级标题 `
139
+ 3 . ❌ ** 使用 JavaScript 操作 DOM 元素** :先将 Vue 组件放在 Layout 的 doc slots 里面,再用 JS 把渲染后的 DOM 元素搬运到标题之下。这个方案过于 hack 过于丑陋。
140
+ 4 . ✅ ** 通过 VitePress 的 markdown-it 接口写类插件代码** :[ markdown-it API] ( https://markdown-it.github.io/markdown-it/ ) 很复杂,但它是最适合做这件事的:
141
+ 1 . 将需要插入 Markdown 文章中间的 Vue 组件注册为全局组件;
142
+ 2 . 改写 ` md.renderer.rules[token.type] ` 渲染函数,将 Vue 组件插入到 markdown-it 的渲染结果中;
143
+ 3 . VitePress 会进一步处理 Markdown 渲染后的 HTML,此时插入的 Vue 组件才会被编译:[ Using Vue in Markdown] ( https://vitepress.dev/guide/using-vue#using-vue-in-markdown ) 。
144
+
145
+ 例如:
146
+
147
+ ``` ts
148
+ md .renderer .rules .heading_close = (tokens , idx , options , _env , self ) => {
149
+ let result = self .renderToken (tokens , idx , options );
150
+ if (tokens [idx ].markup === " #" ) {
151
+ result += " \n\n <PostElements />\n\n " ;
152
+ }
153
+ return result ;
154
+ };
155
+ ```
156
+
157
+ 详情见:[ Octobug/blog/.vitepress/theme/mdit.ts] ( https://github.com/Octobug/blog/blob/main/.vitepress/theme/mdit.ts ) 。
141
158
142
159
#### 上一页/下一页
143
160
144
161
VitePress 本身有“上一页/下一页”的功能,但需要将文章列表数据喂给 [ Sidebar] ( https://vitepress.dev/reference/default-theme-sidebar#sidebar ) 才会触发这个页面组件。然而在 ` .vitepress/config.ts ` 中无法使用 [ Build-Time Data Loading - ` createContentLoader ` ] ( https://vitepress.dev/guide/data-loading#createcontentloader ) 接口加载文章列表:[ vuejs/vitepress/discussions - can I use createContentLoader in config.js?] ( https://github.com/vuejs/vitepress/discussions/2790#discussioncomment-6729116 )
145
162
146
- 也就是说需要自行读写文件把文章列表数据喂给 ` sidebar ` ,这就有点得不偿失。所以我选择自行实现“上一页/下一页”组件,为了保持样式一致,这个组件基本上是从 VitePress 源代码中 copy 的:[ Octobug/blog/.vitepress/theme/components/PrevNext.vue] ( https://github.com/Octobug/blog/blob/main/.vitepress/theme/components/PrevNext.vue )
163
+ 也就是说需要自行读写文件把文章列表数据喂给 ` sidebar ` ,这就有点得不偿失。所以我选择自行实现“上一页/下一页”组件,为了保持样式一致,这个组件基本上是从 VitePress 源代码中 copy 的:[ Octobug/blog/.vitepress/theme/components/PrevNext.vue] ( https://github.com/Octobug/blog/blob/main/.vitepress/theme/components/PrevNext.vue ) 。
147
164
148
165
#### markdown-it 插件
149
166
150
- [ markdown-it/markdown-it] ( https://github.com/markdown-it/markdown-it ) 非常强大,插件使用方便,且插件生态也足够丰富。但它的 API 有些复杂,文档对新手很不友好。
151
-
152
- 如何在 VitePress 中使用 markdown-it 插件:
167
+ [ markdown-it/markdown-it] ( https://github.com/markdown-it/markdown-it ) 非常强大,插件使用方便,且插件生态也足够丰富。但它的开发 API 有些复杂,文档对新手很不友好。
153
168
154
- - [ Markdown Extensions - Advanced Configuration] ( https://vitepress.dev/guide/markdown#advanced-configuration )
155
- - [ Octobug/blog/.vitepress/theme/mdit.ts] ( https://github.com/Octobug/blog/blob/main/.vitepress/theme/mdit.ts )
169
+ 如何在 VitePress 中使用 markdown-it 插件:[ Markdown Extensions - Advanced Configuration] ( https://vitepress.dev/guide/markdown#advanced-configuration ) 。
156
170
157
- 已使用的插件有 :
171
+ 已使用插件列表 :
158
172
159
173
- ** 图片注解** :[ arve0/markdown-it-implicit-figures] ( https://github.com/arve0/markdown-it-implicit-figures )
160
174
- 使用示例:[ vuejs/vitepress/issues - Image captions] ( https://github.com/vuejs/vitepress/issues/892#issuecomment-1172840466 )
@@ -164,7 +178,7 @@ VitePress 本身有“上一页/下一页”的功能,但需要将文章列表
164
178
165
179
#### 处理图片文件大小
166
180
167
- 对于部署在 GitHub Pages、Netlify 这些免费托管平台上的静态网站来说,图片文件如果太大十分影响用户体验。每次手动用 Photoshop 之类的软件处理图片大小都让我觉得很繁琐。macOS 系统用户可以用 ` sips ` 命令来处理,相较于使用 GUI 软件处理要快捷非常多。比如将一张图的长边分辨率转成 ` 1080 px ` ,且保留图片比例:
181
+ 对于部署在 GitHub Pages、Netlify 这些免费托管平台上的静态网站来说,图片文件如果太大十分影响用户体验。每次手动用 Photoshop 之类的软件处理图片大小都让我觉得很繁琐。macOS 系统用户可以用自带的 ` sips ` 命令来处理,相较于使用 GUI 软件处理要快捷非常多。比如将一张图的长边分辨率转成 ` 1080 px ` ,且保留图片比例:
168
182
169
183
``` sh
170
184
sips -Z 1080 origin.jpg -o resized.jpg
0 commit comments