Skip to content

Commit

Permalink
feat: support excerpt extraction with <!-- more --> (close #174)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 23, 2018
1 parent cd9b788 commit fa404dc
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/guide/custom-themes.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ If the user provided `themeConfig` in `.vuepress/config.js`, it will also be ava

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

## Content Excerpt

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.

## Content Outlet

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:
Expand Down
4 changes: 4 additions & 0 deletions docs/zh/guide/custom-themes.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ VuePress 使用单文件组件来构建自定义主题。想要开发一个自

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

## 内容摘抄

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

## 获取渲染内容

当前的 `.md` 文件渲染的内容,可以作为一个独特的全局组件 `<Content/>` 来使用,你可能想要它显示在页面中的某个地方。一个最简单的主题,可以是一个唯一的 `Layout.vue` 组件,并包含以下内容:
Expand Down
4 changes: 3 additions & 1 deletion lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,12 @@ async function resolveOptions (sourceDir) {
if (headers.length) {
data.headers = headers
}
delete frontmatter.content
if (Object.keys(frontmatter.data).length) {
data.frontmatter = frontmatter.data
}
if (frontmatter.excerpt) {
data.excerpt = frontmatter.excerpt
}
return data
}))

Expand Down
1 change: 1 addition & 0 deletions lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ exports.parseFrontmatter = content => {
const toml = require('toml')

return matter(content, {
excerpt_separator: '<!-- more -->',
engines: {
toml: toml.parse.bind(toml),
excerpt: false
Expand Down

0 comments on commit fa404dc

Please sign in to comment.