Skip to content

Commit

Permalink
Extract blog post truncation logic and add tests (#640)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangshun committed May 6, 2018
1 parent 5b3f547 commit 8d676e6
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 30 deletions.
File renamed without changes.
34 changes: 17 additions & 17 deletions lib/core/BlogPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@
const MarkdownBlock = require('./MarkdownBlock.js');
const React = require('react');

const utils = require('./utils');

// inner blog component for the article itself, without sidebar/header/footer
class BlogPost extends React.Component {
renderContent() {
let content = this.props.content;
let hasSplit = false;
if (content.split('<!--truncate-->').length > 1) {
hasSplit = (
<div className="read-more">
<a
className="button"
href={this.props.config.baseUrl + 'blog/' + this.props.post.path}>
Read More
</a>
</div>
);
}
if (this.props.truncate) {
content = content.split('<!--truncate-->')[0];
return (
<article className="post-content">
<MarkdownBlock>{content}</MarkdownBlock>
{hasSplit}
<MarkdownBlock>
{utils.extractBlogPostBeforeTruncate(this.props.content)}
</MarkdownBlock>
{utils.blogPostHasTruncateMarker(this.props.content) && (
<div className="read-more">
<a
className="button"
href={
this.props.config.baseUrl + 'blog/' + this.props.post.path
}>
Read More
</a>
</div>
)}
</article>
);
}
return <MarkdownBlock>{content}</MarkdownBlock>;
return <MarkdownBlock>{this.props.content}</MarkdownBlock>;
}

renderAuthorPhoto() {
Expand Down
15 changes: 15 additions & 0 deletions lib/core/__tests__/__fixtures__/blog-post-with-truncate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
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.
13 changes: 13 additions & 0 deletions lib/core/__tests__/__fixtures__/blog-post-without-truncate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
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.
66 changes: 66 additions & 0 deletions lib/core/__tests__/__snapshots__/utils.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`utils extractBlogPostBeforeTruncate 1`] = `
"---
title: Truncation Example
---
All this will be part of the blog post summary.
Even this.
"
`;

exports[`utils extractBlogPostBeforeTruncate 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.
"
`;

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.
"
`;
File renamed without changes.
File renamed without changes.
File renamed without changes.
49 changes: 49 additions & 0 deletions lib/core/__tests__/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const path = require('path');
const utils = require('../utils');
const readFileSync = require('fs').readFileSync;

const blogPostWithTruncateContents = readFileSync(
path.join(__dirname, '__fixtures__', 'blog-post-with-truncate.md'),
'utf-8'
);

const blogPostWithoutTruncateContents = readFileSync(
path.join(__dirname, '__fixtures__', 'blog-post-without-truncate.md'),
'utf-8'
);

describe('utils', () => {
test('blogPostHasTruncateMarker', () => {
expect(utils.blogPostHasTruncateMarker(blogPostWithTruncateContents)).toBe(
true
);
expect(
utils.blogPostHasTruncateMarker(blogPostWithoutTruncateContents)
).toBe(false);
});

test('extractBlogPostBeforeTruncate', () => {
expect(
utils.extractBlogPostBeforeTruncate(blogPostWithTruncateContents)
).toMatchSnapshot();
expect(
utils.extractBlogPostBeforeTruncate(blogPostWithoutTruncateContents)
).toMatchSnapshot();
});

test('extractBlogPostSummary', () => {
expect(
utils.extractBlogPostSummary(blogPostWithTruncateContents)
).toMatchSnapshot();
expect(
utils.extractBlogPostSummary(blogPostWithoutTruncateContents)
).toMatchSnapshot();
});
});
27 changes: 27 additions & 0 deletions lib/core/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* 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) {
return TRUNCATE_MARKER.test(content);
}

function extractBlogPostBeforeTruncate(content) {
return content.split(TRUNCATE_MARKER)[0];
}

function extractBlogPostSummary(content) {
return content.substring(0, BLOG_POST_SUMMARY_LENGTH);
}

module.exports = {
blogPostHasTruncateMarker,
extractBlogPostBeforeTruncate,
extractBlogPostSummary,
};
18 changes: 5 additions & 13 deletions lib/server/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const blogFolder = path.resolve('../blog/');
const blogRootURL = siteConfig.url + siteConfig.baseUrl + 'blog';
const siteImageURL =
siteConfig.url + siteConfig.baseUrl + siteConfig.headerIcon;
const utils = require('../core/utils');

const renderMarkdown = require('../core/renderMarkdown.js');

Expand Down Expand Up @@ -56,18 +57,9 @@ module.exports = function(type) {

MetadataBlog.forEach(post => {
const url = blogRootURL + '/' + post.path;
let content = '';
if (post.content.indexOf('<!--truncate-->') == -1) {
content = post.content.trim().substring(0, 250);
} else {
let contentArr = post.content.split('<!--truncate-->');
if (contentArr.length > 0) {
content = contentArr[0];
}
}

content = renderMarkdown(content);

const content = utils.blogPostHasTruncateMarker(post.content)
? utils.extractBlogPostBeforeTruncate(post.content)
: utils.extractBlogPostSummary(post.content.trim());
feed.addItem({
title: post.title,
link: url,
Expand All @@ -78,7 +70,7 @@ module.exports = function(type) {
},
],
date: new Date(post.date),
description: content,
description: renderMarkdown(content),
});
});

Expand Down

0 comments on commit 8d676e6

Please sign in to comment.