diff --git a/build.js b/build.js index 7d0512aac9172..50fb9f4f13df5 100755 --- a/build.js +++ b/build.js @@ -11,6 +11,7 @@ const markdown = require('metalsmith-markdown') const prism = require('metalsmith-prism') const stylus = require('metalsmith-stylus') const permalinks = require('metalsmith-permalinks') +const paginate = require('metalsmith-paginate') const marked = require('marked') const path = require('path') const fs = require('fs') @@ -112,6 +113,10 @@ function buildlocale (source, locale) { refer: false } })) + .use(paginate({ + perPage: 15, + path: 'blog/page' + })) .use(markdown(markedOptions)) .use(prism()) .use(filterStylusPartials()) @@ -159,7 +164,8 @@ function buildlocale (source, locale) { changeloglink: require('./scripts/helpers/changeloglink.js'), strftime: require('./scripts/helpers/strftime.js'), apidocslink: require('./scripts/helpers/apidocslink.js'), - summary: require('./scripts/helpers/summary.js') + summary: require('./scripts/helpers/summary.js'), + limit: require('./scripts/helpers/limit.js') } })) .destination(path.join(__dirname, 'build', locale)) diff --git a/layouts/blog-index.hbs b/layouts/blog-index.hbs index 7077a1d25c161..8f4f90448cb63 100644 --- a/layouts/blog-index.hbs +++ b/layouts/blog-index.hbs @@ -7,21 +7,31 @@
- + {{#if this.pagination}} + + {{/if}}
diff --git a/locale/en/blog/index.md b/locale/en/blog/index.md index b1a0378e971ad..24248de1867d0 100644 --- a/locale/en/blog/index.md +++ b/locale/en/blog/index.md @@ -1,3 +1,4 @@ --- layout: blog-index.hbs +paginate: blog --- diff --git a/package.json b/package.json index 3d20e8f4a8f9b..39b677ef3479b 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "metalsmith-layouts": "1.4.2", "metalsmith-markdown": "0.2.1", "metalsmith-metadata": "0.0.2", + "metalsmith-paginate": "0.3.0", "metalsmith-permalinks": "0.4.0", "metalsmith-prism": "2.1.1", "metalsmith-stylus": "1.0.0", diff --git a/scripts/helpers/limit.js b/scripts/helpers/limit.js new file mode 100644 index 0000000000000..9f48c5bd63512 --- /dev/null +++ b/scripts/helpers/limit.js @@ -0,0 +1,5 @@ +'use strict' + +module.exports = (collection, limit, start) => { + return collection.slice(start, limit + 1) +} diff --git a/scripts/helpers/summary.js b/scripts/helpers/summary.js index 80822e3bdbedb..c1222e72107d8 100644 --- a/scripts/helpers/summary.js +++ b/scripts/helpers/summary.js @@ -2,25 +2,46 @@ const cheerio = require('cheerio') -const SUMMARY_LENGHT = 400 +const SUMMARY_MAX_LENGTH = 300 +const IGNORE_SELECTORS = ['.blogpost-header', '.anchor', 'h1', 'h2', 'h3', 'blockquote'] + +/** + * Due to the nature of metalsmith and + * how the metalsmith-paginate plugin operates, + * this helper has to handle two different types of + * HTML contents: + * - clean blog posts converted from markdown to HTML, + * seen on the first page of blog posts + * - the remaining paginated pages has gone + * through the handlebars process and therefore has the + * entire page layout (w/, and etc) + * wrapped around the actual blog contents :( + */ module.exports = function (contents, locale, path) { - let $ = cheerio.load(contents) + const $ = cheerio.load(contents) + const $body = $('body') + const hasBody = $body.length > 0 + const $elements = hasBody ? $body.find('article > *') : $('*') let summary = '' - $('*').each((i, elem) => { - if (summary.length > SUMMARY_LENGHT) { - summary += `

Read more...

` - return false - } - - if (elem.parent) { - return - } - - summary += $.html(elem) - }) + $elements + .not((i, elem) => IGNORE_SELECTORS.some((selector) => $(elem).is(selector))) + .each((i, elem) => { + if (summary.length > SUMMARY_MAX_LENGTH) { + summary += `

Read more...

` + return false + } + + // dont re-add nested elements when extracting summary + // from blog posts not contained in a complete HTML document + if (!hasBody && elem.parent) { + return + } + + summary += $.html(elem) + }) return summary }