Skip to content

Commit fa404dc

Browse files
committed
feat: support excerpt extraction with <!-- more --> (close #174)
1 parent cd9b788 commit fa404dc

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

docs/guide/custom-themes.md

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ If the user provided `themeConfig` in `.vuepress/config.js`, it will also be ava
5454

5555
Finally, don't forget that `this.$route` and `this.$router` are also available as part of Vue Router's API.
5656

57+
## Content Excerpt
58+
59+
If a markdown file contains a `<!-- more -->` comment, any content above the comment will be extracted and exposed as `$page.excerpt`. If you are building custom theme for blogging, this data can be used to render a post list with excerpts.
60+
5761
## Content Outlet
5862

5963
The compiled content of the current `.md` file being rendered will be available as a special `<Content/>` global component. You will need to render it somewhere in your layout in order to display the content of the page. The simplest theme can be just a single `Layout.vue` component with the following content:

docs/zh/guide/custom-themes.md

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ VuePress 使用单文件组件来构建自定义主题。想要开发一个自
5454

5555
最后,别忘了,作为 Vue Router API 的一部分,`this.$route``this.$router` 同样可以使用。
5656

57+
## 内容摘抄
58+
59+
如果一个 markdown 文件中有一个 `<!-- more -->` 注释,则该注释之前的内容会被抓取并暴露在 `$page.excerpt` 属性中。如果你在开发一个博客主题,你可以用这个属性来渲染一个带摘抄的文章列表。
60+
5761
## 获取渲染内容
5862

5963
当前的 `.md` 文件渲染的内容,可以作为一个独特的全局组件 `<Content/>` 来使用,你可能想要它显示在页面中的某个地方。一个最简单的主题,可以是一个唯一的 `Layout.vue` 组件,并包含以下内容:

lib/prepare.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,12 @@ async function resolveOptions (sourceDir) {
187187
if (headers.length) {
188188
data.headers = headers
189189
}
190-
delete frontmatter.content
191190
if (Object.keys(frontmatter.data).length) {
192191
data.frontmatter = frontmatter.data
193192
}
193+
if (frontmatter.excerpt) {
194+
data.excerpt = frontmatter.excerpt
195+
}
194196
return data
195197
}))
196198

lib/util/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ exports.parseFrontmatter = content => {
4848
const toml = require('toml')
4949

5050
return matter(content, {
51+
excerpt_separator: '<!-- more -->',
5152
engines: {
5253
toml: toml.parse.bind(toml),
5354
excerpt: false

0 commit comments

Comments
 (0)