From 9626ab8dda63b3ec49a1ce6a8d4dcb995f132051 Mon Sep 17 00:00:00 2001 From: Billyyyyy3320 Date: Fri, 17 Jan 2020 00:05:41 +0800 Subject: [PATCH] feat: use summary as meta description --- docs/config/README.md | 2 +- example/.vuepress/config.js | 2 +- index.js | 33 ++++++++++++++++++--------------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 0e83e6e..b7c9f32 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -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 diff --git a/example/.vuepress/config.js b/example/.vuepress/config.js index 7a3e44b..6d3d412 100755 --- a/example/.vuepress/config.js +++ b/example/.vuepress/config.js @@ -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 diff --git a/index.js b/index.js index 5a3891b..2c31d7d 100644 --- a/index.js +++ b/index.js @@ -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