From b788fe8ed673274b4419fcddf4ddec47d3a407ef Mon Sep 17 00:00:00 2001 From: endiliey Date: Wed, 1 Aug 2018 21:07:21 +0800 Subject: [PATCH 1/3] fix: truncate blogpost properly with truncate-html --- .../__snapshots__/utils.test.js.snap | 36 ------ lib/core/__tests__/utils.test.js | 9 -- lib/core/utils.js | 6 - lib/server/feed.js | 12 +- package.json | 3 +- .../blog/2017-12-14-introducing-docusaurus.md | 2 - yarn.lock | 116 +++++++++++++++++- 7 files changed, 122 insertions(+), 62 deletions(-) diff --git a/lib/core/__tests__/__snapshots__/utils.test.js.snap b/lib/core/__tests__/__snapshots__/utils.test.js.snap index a7eb240a4fcb..d29e3b1b762e 100644 --- a/lib/core/__tests__/__snapshots__/utils.test.js.snap +++ b/lib/core/__tests__/__snapshots__/utils.test.js.snap @@ -28,39 +28,3 @@ And this. And this too. " `; - -exports[`utils extractBlogPostSummary 1`] = ` -"--- -title: Truncation Example ---- - -All this will be part of the blog post summary. - -Even this. - - - -But anything from here on down will not be. - -Not this. - -Or this. -" -`; - -exports[`utils extractBlogPostSummary 2`] = ` -"--- -title: Non-truncation Example ---- - -All this will be part of the blog post summary. - -Even this. - -And anything from here on down will still be. - -And this. - -And this too. -" -`; diff --git a/lib/core/__tests__/utils.test.js b/lib/core/__tests__/utils.test.js index 04ccef614e20..5c1e9ec53d4d 100644 --- a/lib/core/__tests__/utils.test.js +++ b/lib/core/__tests__/utils.test.js @@ -38,15 +38,6 @@ describe('utils', () => { ).toMatchSnapshot(); }); - test('extractBlogPostSummary', () => { - expect( - utils.extractBlogPostSummary(blogPostWithTruncateContents) - ).toMatchSnapshot(); - expect( - utils.extractBlogPostSummary(blogPostWithoutTruncateContents) - ).toMatchSnapshot(); - }); - test('getPath', () => { expect(utils.getPath('/docs/en/versioning.html', true)).toBe( '/docs/en/versioning' diff --git a/lib/core/utils.js b/lib/core/utils.js index 855f38906a14..5e4b66e9fed6 100644 --- a/lib/core/utils.js +++ b/lib/core/utils.js @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -const BLOG_POST_SUMMARY_LENGTH = 250; const TRUNCATE_MARKER = //; function blogPostHasTruncateMarker(content) { @@ -16,10 +15,6 @@ function extractBlogPostBeforeTruncate(content) { return content.split(TRUNCATE_MARKER)[0]; } -function extractBlogPostSummary(content) { - return content.substring(0, BLOG_POST_SUMMARY_LENGTH); -} - function removeExtension(path) { return path.replace(/\.[^/.]+$/, ''); } @@ -36,7 +31,6 @@ function getPath(path, cleanUrl = false) { module.exports = { blogPostHasTruncateMarker, extractBlogPostBeforeTruncate, - extractBlogPostSummary, getPath, removeExtension, }; diff --git a/lib/server/feed.js b/lib/server/feed.js index 1adb533067af..54acf919292e 100644 --- a/lib/server/feed.js +++ b/lib/server/feed.js @@ -6,6 +6,9 @@ */ const Feed = require('feed'); +const truncateHtml = require('truncate-html'); + +const BLOG_POST_SUMMARY_LENGTH = 250; const CWD = process.cwd(); const siteConfig = require(`${CWD}/siteConfig.js`); @@ -40,9 +43,10 @@ module.exports = function(type) { MetadataBlog.forEach(post => { const url = `${blogRootURL}/${post.path}`; - const content = utils.blogPostHasTruncateMarker(post.content) - ? utils.extractBlogPostBeforeTruncate(post.content) - : utils.extractBlogPostSummary(post.content.trim()); + const description = utils.blogPostHasTruncateMarker(post.content) + ? renderMarkdown(utils.extractBlogPostBeforeTruncate(post.content)) + : truncateHtml(renderMarkdown(post.content), BLOG_POST_SUMMARY_LENGTH); + feed.addItem({ title: post.title, link: url, @@ -53,7 +57,7 @@ module.exports = function(type) { }, ], date: new Date(post.date), - description: renderMarkdown(content), + description, }); }); diff --git a/package.json b/package.json index eb683b5cdec9..5050597236e5 100644 --- a/package.json +++ b/package.json @@ -93,7 +93,8 @@ "sitemap": "^1.13.0", "tcp-port-used": "^0.1.2", "tiny-lr": "^1.1.1", - "tree-node-cli": "^1.2.5" + "tree-node-cli": "^1.2.5", + "truncate-html": "^1.0.0" }, "devDependencies": { "babel-eslint": "^8.2.5", diff --git a/website/blog/2017-12-14-introducing-docusaurus.md b/website/blog/2017-12-14-introducing-docusaurus.md index a1c156ec3c7f..5a2937296138 100644 --- a/website/blog/2017-12-14-introducing-docusaurus.md +++ b/website/blog/2017-12-14-introducing-docusaurus.md @@ -17,8 +17,6 @@ We created [Docusaurus](https://docusaurus.io) for the following reasons: 1. To make it easy to push updates, new features, and bug fixes to everyone all at once. 1. And, finally, to provide a consistent look and feel across all of our open source projects. - - Docusaurus is a tool designed to make it easy for teams to publish documentation websites without having to worry about the infrastructure and design details. At its core, all a user has to provide are documentation files written in markdown, customization of a provided home page written in React, and a few configuration modifications. Docusaurus handles the rest by providing default styles, site formatting, and simple document navigation. Getting started is easy, as users can [install](https://docusaurus.io/docs/en/installation.html) it using `npm` or `yarn` via a simple initialization script that [creates a working example website out of the box](https://docusaurus.io/docs/en/site-preparation.html). Docusaurus also provides core website and documentation features out-of-the-box including [blog support](https://docusaurus.io/docs/en/blog.html), [internationalization](https://docusaurus.io/docs/en/translation.html), [search](https://docusaurus.io/docs/en/search.html), and [versioning](https://docusaurus.io/docs/en/versioning.html). While some projects may not require any of these features, enabling them is generally a matter of updating configuration options instead of having to add the infrastructure from the ground up. As more features get added to Docusaurus, users just can easily update to the latest version. This can be done by simply running npm or yarn update and updating configuration options. Users or teams will no longer need to manually rework their entire website infrastructure each time a new feature gets added. diff --git a/yarn.lock b/yarn.lock index a94efdef47c2..d4254653dff5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1382,6 +1382,27 @@ chardet@^0.4.0: version "0.4.2" resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.4.2.tgz#b5473b33dc97c424e5d98dc87d55d4d8a29c8bf2" +cheerio@0.22.0: + version "0.22.0" + resolved "https://registry.yarnpkg.com/cheerio/-/cheerio-0.22.0.tgz#a9baa860a3f9b595a6b81b1a86873121ed3a269e" + dependencies: + css-select "~1.2.0" + dom-serializer "~0.1.0" + entities "~1.1.1" + htmlparser2 "^3.9.1" + lodash.assignin "^4.0.9" + lodash.bind "^4.1.4" + lodash.defaults "^4.0.1" + lodash.filter "^4.4.0" + lodash.flatten "^4.2.0" + lodash.foreach "^4.3.0" + lodash.map "^4.4.0" + lodash.merge "^4.4.0" + lodash.pick "^4.2.1" + lodash.reduce "^4.4.0" + lodash.reject "^4.4.0" + lodash.some "^4.4.0" + chownr@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/chownr/-/chownr-1.0.1.tgz#e2a75042a9551908bebd25b8523d5f9769d79181" @@ -1700,6 +1721,15 @@ css-select-base-adapter@~0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/css-select-base-adapter/-/css-select-base-adapter-0.1.0.tgz#0102b3d14630df86c3eb9fa9f5456270106cf990" +css-select@~1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/css-select/-/css-select-1.2.0.tgz#2b3a110539c5355f1cd8d314623e870b121ec858" + dependencies: + boolbase "~1.0.0" + css-what "2.1" + domutils "1.5.1" + nth-check "~1.0.1" + css-select@~1.3.0-rc0: version "1.3.0-rc0" resolved "https://registry.yarnpkg.com/css-select/-/css-select-1.3.0-rc0.tgz#6f93196aaae737666ea1036a8cb14a8fcb7a9231" @@ -2025,14 +2055,14 @@ doctrine@^2.1.0: dependencies: esutils "^2.0.2" -dom-serializer@0: +dom-serializer@0, dom-serializer@~0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/dom-serializer/-/dom-serializer-0.1.0.tgz#073c697546ce0780ce23be4a28e293e40bc30c82" dependencies: domelementtype "~1.1.1" entities "~1.1.1" -domelementtype@1: +domelementtype@1, domelementtype@^1.3.0: version "1.3.0" resolved "https://registry.yarnpkg.com/domelementtype/-/domelementtype-1.3.0.tgz#b17aed82e8ab59e52dd9c19b1756e0fc187204c2" @@ -2040,6 +2070,12 @@ domelementtype@~1.1.1: version "1.1.3" resolved "https://registry.yarnpkg.com/domelementtype/-/domelementtype-1.1.3.tgz#bd28773e2642881aec51544924299c5cd822185b" +domhandler@^2.3.0: + version "2.4.2" + resolved "https://registry.yarnpkg.com/domhandler/-/domhandler-2.4.2.tgz#8805097e933d65e85546f726d60f5eb88b44f803" + dependencies: + domelementtype "1" + domutils@1.5.1: version "1.5.1" resolved "https://registry.yarnpkg.com/domutils/-/domutils-1.5.1.tgz#dcd8488a26f563d61079e48c9f7b7e32373682cf" @@ -2047,6 +2083,13 @@ domutils@1.5.1: dom-serializer "0" domelementtype "1" +domutils@^1.5.1: + version "1.7.0" + resolved "https://registry.yarnpkg.com/domutils/-/domutils-1.7.0.tgz#56ea341e834e06e6748af7a1cb25da67ea9f8c2a" + dependencies: + dom-serializer "0" + domelementtype "1" + download@^4.0.0, download@^4.1.2: version "4.4.3" resolved "https://registry.yarnpkg.com/download/-/download-4.4.3.tgz#aa55fdad392d95d4b68e8c2be03e0c2aa21ba9ac" @@ -2141,7 +2184,7 @@ end-of-stream@^1.0.0: dependencies: once "^1.4.0" -entities@~1.1.1: +entities@^1.1.1, entities@~1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/entities/-/entities-1.1.1.tgz#6e5c2d0a5621b5dadaecef80b90edfb5cd7772f0" @@ -3248,6 +3291,17 @@ html-encoding-sniffer@^1.0.1: dependencies: whatwg-encoding "^1.0.1" +htmlparser2@^3.9.1: + version "3.9.2" + resolved "https://registry.yarnpkg.com/htmlparser2/-/htmlparser2-3.9.2.tgz#1bdf87acca0f3f9e53fa4fcceb0f4b4cbb00b338" + dependencies: + domelementtype "^1.3.0" + domhandler "^2.3.0" + domutils "^1.5.1" + entities "^1.1.1" + inherits "^2.0.1" + readable-stream "^2.0.2" + http-errors@1.6.2: version "1.6.2" resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.6.2.tgz#0a002cc85707192a7e7946ceedc11155f60ec736" @@ -4429,12 +4483,36 @@ lodash._root@^3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692" +lodash.assignin@^4.0.9: + version "4.2.0" + resolved "https://registry.yarnpkg.com/lodash.assignin/-/lodash.assignin-4.2.0.tgz#ba8df5fb841eb0a3e8044232b0e263a8dc6a28a2" + +lodash.bind@^4.1.4: + version "4.2.1" + resolved "https://registry.yarnpkg.com/lodash.bind/-/lodash.bind-4.2.1.tgz#7ae3017e939622ac31b7d7d7dcb1b34db1690d35" + +lodash.defaults@^4.0.1: + version "4.2.0" + resolved "https://registry.yarnpkg.com/lodash.defaults/-/lodash.defaults-4.2.0.tgz#d09178716ffea4dde9e5fb7b37f6f0802274580c" + lodash.escape@^3.0.0: version "3.2.0" resolved "https://registry.yarnpkg.com/lodash.escape/-/lodash.escape-3.2.0.tgz#995ee0dc18c1b48cc92effae71a10aab5b487698" dependencies: lodash._root "^3.0.0" +lodash.filter@^4.4.0: + version "4.6.0" + resolved "https://registry.yarnpkg.com/lodash.filter/-/lodash.filter-4.6.0.tgz#668b1d4981603ae1cc5a6fa760143e480b4c4ace" + +lodash.flatten@^4.2.0: + version "4.4.0" + resolved "https://registry.yarnpkg.com/lodash.flatten/-/lodash.flatten-4.4.0.tgz#f31c22225a9632d2bbf8e4addbef240aa765a61f" + +lodash.foreach@^4.3.0: + version "4.5.0" + resolved "https://registry.yarnpkg.com/lodash.foreach/-/lodash.foreach-4.5.0.tgz#1a6a35eace401280c7f06dddec35165ab27e3e53" + lodash.isarguments@^3.0.0: version "3.1.0" resolved "https://registry.yarnpkg.com/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz#2f573d85c6a24289ff00663b491c1d338ff3458a" @@ -4455,14 +4533,38 @@ lodash.keys@^3.0.0: lodash.isarguments "^3.0.0" lodash.isarray "^3.0.0" +lodash.map@^4.4.0: + version "4.6.0" + resolved "https://registry.yarnpkg.com/lodash.map/-/lodash.map-4.6.0.tgz#771ec7839e3473d9c4cde28b19394c3562f4f6d3" + lodash.memoize@^4.1.2: version "4.1.2" resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe" +lodash.merge@^4.4.0: + version "4.6.1" + resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.1.tgz#adc25d9cb99b9391c59624f379fbba60d7111d54" + +lodash.pick@^4.2.1: + version "4.4.0" + resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3" + +lodash.reduce@^4.4.0: + version "4.6.0" + resolved "https://registry.yarnpkg.com/lodash.reduce/-/lodash.reduce-4.6.0.tgz#f1ab6b839299ad48f784abbf476596f03b914d3b" + +lodash.reject@^4.4.0: + version "4.6.0" + resolved "https://registry.yarnpkg.com/lodash.reject/-/lodash.reject-4.6.0.tgz#80d6492dc1470864bbf583533b651f42a9f52415" + lodash.restparam@^3.0.0: version "3.6.1" resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805" +lodash.some@^4.4.0: + version "4.6.0" + resolved "https://registry.yarnpkg.com/lodash.some/-/lodash.some-4.6.0.tgz#1bb9f314ef6b8baded13b549169b2a945eb68e4d" + lodash.template@^3.0.0: version "3.6.2" resolved "https://registry.yarnpkg.com/lodash.template/-/lodash.template-3.6.2.tgz#f8cdecc6169a255be9098ae8b0c53d378931d14f" @@ -4935,7 +5037,7 @@ npmlog@^4.0.2: gauge "~2.7.3" set-blocking "~2.0.0" -nth-check@^1.0.1: +nth-check@^1.0.1, nth-check@~1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/nth-check/-/nth-check-1.0.1.tgz#9929acdf628fc2c41098deab82ac580cf149aae4" dependencies: @@ -6805,6 +6907,12 @@ trim-right@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003" +truncate-html@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/truncate-html/-/truncate-html-1.0.0.tgz#5a0e03c3dbc4bb739f8023629958acf956b94641" + dependencies: + cheerio "0.22.0" + tslib@^1.9.0: version "1.9.3" resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.3.tgz#d7e4dd79245d85428c4d7e4822a79917954ca286" From 4b759ba7c06078e8a8e23441f2e1158aea870ea4 Mon Sep 17 00:00:00 2001 From: endiliey Date: Wed, 1 Aug 2018 21:16:44 +0800 Subject: [PATCH 2/3] revert test change --- website/blog/2017-12-14-introducing-docusaurus.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/blog/2017-12-14-introducing-docusaurus.md b/website/blog/2017-12-14-introducing-docusaurus.md index 5a2937296138..a1c156ec3c7f 100644 --- a/website/blog/2017-12-14-introducing-docusaurus.md +++ b/website/blog/2017-12-14-introducing-docusaurus.md @@ -17,6 +17,8 @@ We created [Docusaurus](https://docusaurus.io) for the following reasons: 1. To make it easy to push updates, new features, and bug fixes to everyone all at once. 1. And, finally, to provide a consistent look and feel across all of our open source projects. + + Docusaurus is a tool designed to make it easy for teams to publish documentation websites without having to worry about the infrastructure and design details. At its core, all a user has to provide are documentation files written in markdown, customization of a provided home page written in React, and a few configuration modifications. Docusaurus handles the rest by providing default styles, site formatting, and simple document navigation. Getting started is easy, as users can [install](https://docusaurus.io/docs/en/installation.html) it using `npm` or `yarn` via a simple initialization script that [creates a working example website out of the box](https://docusaurus.io/docs/en/site-preparation.html). Docusaurus also provides core website and documentation features out-of-the-box including [blog support](https://docusaurus.io/docs/en/blog.html), [internationalization](https://docusaurus.io/docs/en/translation.html), [search](https://docusaurus.io/docs/en/search.html), and [versioning](https://docusaurus.io/docs/en/versioning.html). While some projects may not require any of these features, enabling them is generally a matter of updating configuration options instead of having to add the infrastructure from the ground up. As more features get added to Docusaurus, users just can easily update to the latest version. This can be done by simply running npm or yarn update and updating configuration options. Users or teams will no longer need to manually rework their entire website infrastructure each time a new feature gets added. From 6bc6d95b1b5ceea48ff24373311432539a9e2dd4 Mon Sep 17 00:00:00 2001 From: endiliey Date: Thu, 2 Aug 2018 15:12:38 +0800 Subject: [PATCH 3/3] chore: truncate blogpost for feed changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63b3659c3887..16aa1e4241e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ Thank you to the following contributors who helped with this release: - Improve baseUrl documentation with an example [\#863](https://github.com/facebook/Docusaurus/pull/863) - Docusaurus own search will now search the docs in the correct language & version [\#859](https://github.com/facebook/Docusaurus/pull/859) - Fix phrase emphasis not italicized [\#850](https://github.com/facebook/Docusaurus/pull/850) +- Blogpost summary for blog feed is now properly truncated [\#880](https://github.com/facebook/Docusaurus/pull/880) **Chores** - Remove unused files [\#881](https://github.com/facebook/Docusaurus/pull/881)