From 14dbeefc20087d2d53c75187a361cfcc52e02f1c Mon Sep 17 00:00:00 2001 From: Francisco Baio Dias Date: Sat, 12 Sep 2015 13:58:43 +0100 Subject: [PATCH 1/3] Added basic helper --- build.js | 3 ++- layouts/blog-index.hbs | 2 ++ scripts/helpers/summary.js | 5 +++++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 scripts/helpers/summary.js diff --git a/build.js b/build.js index ff9b3d108e50d..2ed1aabe5a84f 100755 --- a/build.js +++ b/build.js @@ -149,7 +149,8 @@ function buildlocale (locale) { startswith: require('./scripts/helpers/startswith.js'), i18n: require('./scripts/helpers/i18n.js'), changeloglink: require('./scripts/helpers/changeloglink.js'), - strftime: require('./scripts/helpers/strftime.js') + strftime: require('./scripts/helpers/strftime.js'), + summary: require('./scripts/helpers/summary.js') } })) .destination(path.join(__dirname, 'build', locale)); diff --git a/layouts/blog-index.hbs b/layouts/blog-index.hbs index 1d37a708e838b..29205d4899361 100644 --- a/layouts/blog-index.hbs +++ b/layouts/blog-index.hbs @@ -14,6 +14,8 @@
  • {{ title }} + + {{{ summary contents }}}
  • {{/if}} {{/each}} diff --git a/scripts/helpers/summary.js b/scripts/helpers/summary.js new file mode 100644 index 0000000000000..1e56ca654ddd8 --- /dev/null +++ b/scripts/helpers/summary.js @@ -0,0 +1,5 @@ +'use strict' + +module.exports = function (contents) { + return contents; +}; From b12da3555a5b1531bd934a1b9bd457fa75195ca9 Mon Sep 17 00:00:00 2001 From: Elin Date: Sat, 12 Sep 2015 14:34:26 +0100 Subject: [PATCH 2/3] broken summary of first para from blog --- package.json | 1 + scripts/helpers/summary.js | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index ba983254d4473..9d9a2df04b5ce 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ }, "dependencies": { "autoprefixer-stylus": "^0.7.1", + "cheerio": "^0.19.0", "chokidar": "^1.0.5", "gulp": "^3.9.0", "handlebars": "^3.0.0", diff --git a/scripts/helpers/summary.js b/scripts/helpers/summary.js index 1e56ca654ddd8..1b1a2e378c961 100644 --- a/scripts/helpers/summary.js +++ b/scripts/helpers/summary.js @@ -1,5 +1,16 @@ 'use strict' +const cheerio = require('cheerio'); + module.exports = function (contents) { - return contents; + var $ = cheerio.load('
    '+contents+'
    '); + + var summary = $(':nth-child(1)').html(); + console.log(contents); + + console.log("hello"); + + return summary; }; + +// module.exports('

    Hello world

    asdfasd

    '); From f3b40662ab623d1f17929b85dd446f9ed145fb96 Mon Sep 17 00:00:00 2001 From: Francisco Baio Dias Date: Sat, 12 Sep 2015 16:09:28 +0100 Subject: [PATCH 3/3] Looks like it's working --- layouts/blog-index.hbs | 4 +++- layouts/css/page-modules/_blog-index.styl | 3 +++ scripts/helpers/summary.js | 27 ++++++++++++++++------- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/layouts/blog-index.hbs b/layouts/blog-index.hbs index 29205d4899361..6b08ec18b033d 100644 --- a/layouts/blog-index.hbs +++ b/layouts/blog-index.hbs @@ -15,7 +15,9 @@ {{ title }} - {{{ summary contents }}} +
    + {{{ summary contents ../../site.locale path }}} +
    {{/if}} {{/each}} diff --git a/layouts/css/page-modules/_blog-index.styl b/layouts/css/page-modules/_blog-index.styl index b736bcabad260..f2df8e181f848 100644 --- a/layouts/css/page-modules/_blog-index.styl +++ b/layouts/css/page-modules/_blog-index.styl @@ -2,3 +2,6 @@ time margin-right 1em color $lightgray + + .summary + font-size: 75% diff --git a/scripts/helpers/summary.js b/scripts/helpers/summary.js index 1b1a2e378c961..db3326d9f5a0b 100644 --- a/scripts/helpers/summary.js +++ b/scripts/helpers/summary.js @@ -2,15 +2,26 @@ const cheerio = require('cheerio'); -module.exports = function (contents) { - var $ = cheerio.load('
    '+contents+'
    '); +const SUMMARY_LENGHT = 400; - var summary = $(':nth-child(1)').html(); - console.log(contents); +module.exports = function (contents, locale, path) { + let $ = cheerio.load(contents); - console.log("hello"); + let summary = ''; + let child = 1; - return summary; -}; + $('*').each((i, elem) => { + if (summary.length > SUMMARY_LENGHT) { + summary += `

    Read more...

    `; + return false; + } + + if (elem.parent) { + return; + } -// module.exports('

    Hello world

    asdfasd

    '); + summary += $.html(elem); + }) + + return `${summary}`; +};