Skip to content

Commit

Permalink
fix: truncate blogpost properly with truncate-html (#880)
Browse files Browse the repository at this point in the history
* fix: truncate blogpost properly with truncate-html

* revert test file change

* chore: truncate blogpost for feed changelog
  • Loading branch information
endiliey authored Aug 2, 2018
1 parent 609e1b4 commit 18158d6
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 60 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 0 additions & 36 deletions lib/core/__tests__/__snapshots__/utils.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<!--truncate-->
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.
"
`;
9 changes: 0 additions & 9 deletions lib/core/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 0 additions & 6 deletions lib/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

const BLOG_POST_SUMMARY_LENGTH = 250;
const TRUNCATE_MARKER = /<!--\s*truncate\s*-->/;

function blogPostHasTruncateMarker(content) {
Expand All @@ -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(/\.[^/.]+$/, '');
}
Expand All @@ -36,7 +31,6 @@ function getPath(path, cleanUrl = false) {
module.exports = {
blogPostHasTruncateMarker,
extractBlogPostBeforeTruncate,
extractBlogPostSummary,
getPath,
removeExtension,
};
12 changes: 8 additions & 4 deletions lib/server/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down Expand Up @@ -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,
Expand All @@ -53,7 +57,7 @@ module.exports = function(type) {
},
],
date: new Date(post.date),
description: renderMarkdown(content),
description,
});
});

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,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",
Expand Down
116 changes: 112 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -2025,28 +2055,41 @@ 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"

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"
dependencies:
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"
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 18158d6

Please sign in to comment.