Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
feat: use summary as meta description
Browse files Browse the repository at this point in the history
  • Loading branch information
billyyyyy3320 committed Jan 16, 2020
1 parent d6746ee commit 9626ab8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ Since many features are powered by the plugin, we suggest you to read the [docum
- Type: `boolean`
- Default: `true`

Whether to extract summary from source markdowns. You can write summary manually by [front matter](./front-matter.md#summary).
Whether to automatically extract summary from source markdowns. You can write summary manually by [front matter](./front-matter.md#summary). Summary is not only for displaying but also page meta description.


## summaryLength
Expand Down
2 changes: 1 addition & 1 deletion example/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ module.exports = {
* Ref: https://vuepress-theme-blog.ulivz.com/#summary
*/

// summary:false,
summary:false,

/**
* Ref: https://vuepress-theme-blog.ulivz.com/#summarylength
Expand Down
33 changes: 18 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,28 @@ module.exports = themeConfig => {
alias: {
fonts: path.resolve(__dirname, 'fonts'),
},
}

/**
* Generate summary.
*/
if (themeConfig.summary) {
config.extendPageData = function(pageCtx) {
/**
* Generate summary.
*/
extendPageData(pageCtx) {
const strippedContent = pageCtx._strippedContent
if (!strippedContent) {
return
}
pageCtx.summary =
removeMd(
strippedContent
.trim()
.replace(/^#+\s+(.*)/, '')
.slice(0, themeConfig.summaryLength)
) + ' ...'
}
if (themeConfig.summary) {
pageCtx.summary =
removeMd(
strippedContent
.trim()
.replace(/^#+\s+(.*)/, '')
.slice(0, themeConfig.summaryLength)
) + ' ...'
pageCtx.frontmatter.description = pageCtx.summary
}
if (pageCtx.frontmatter.summary) {
pageCtx.frontmatter.description = pageCtx.frontmatter.summary
}
},
}

return config
Expand Down

0 comments on commit 9626ab8

Please sign in to comment.