-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract blog post truncation logic and add tests (#640)
- Loading branch information
Showing
13 changed files
with
192 additions
and
30 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
lib/core/__tests__/__fixtures__/blog-post-with-truncate.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
lib/core/__tests__/__fixtures__/blog-post-without-truncate.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters