Skip to content

Commit 1df05c8

Browse files
committed
Fix #4052
1 parent d6433ff commit 1df05c8

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/shared/components/Contentful/Article/Article.jsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import PT from 'prop-types';
88
import { fixStyle } from 'utils/contentful';
99
import { getService } from 'services/contentful';
1010
import MarkdownRenderer from 'components/MarkdownRenderer';
11+
import ReactDOMServer from 'react-dom/server';
12+
import markdown from 'utils/markdown';
1113
import ContentfulLoader from 'containers/ContentfulLoader';
1214
import LoadingIndicator from 'components/LoadingIndicator';
1315
import YouTubeVideo from 'components/YouTubeVideo';
@@ -21,6 +23,8 @@ import UserDefault from 'assets/images/ico-user-default.svg';
2123
import ReadMoreArrow from 'assets/images/read-more-arrow.svg';
2224
import qs from 'qs';
2325

26+
const htmlToText = require('html-to-text');
27+
2428
// character length for the content preview
2529
const CONTENT_PREVIEW_LENGTH = 110;
2630
// Votes local storage key
@@ -254,7 +258,17 @@ export default class Article extends React.Component {
254258
</h3>
255259
<div className={theme.recommendedCardContent}>
256260
{
257-
`${subData.entries.items[rec.sys.id].fields.content.substring(0, CONTENT_PREVIEW_LENGTH)}..`
261+
`${htmlToText.fromString(
262+
ReactDOMServer.renderToString(markdown(
263+
subData.entries.items[rec.sys.id].fields.content,
264+
)),
265+
{
266+
ignoreHref: true,
267+
ignoreImage: true,
268+
singleNewLineParagraphs: true,
269+
uppercaseHeadings: false,
270+
},
271+
).substring(0, CONTENT_PREVIEW_LENGTH)}...`
258272
}
259273
</div>
260274
{

src/shared/components/Contentful/ArticleCard/ArticleCard.jsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import React from 'react';
1212
import { themr } from 'react-css-super-themr';
1313
import { config } from 'topcoder-react-utils';
1414
import markdown from 'utils/markdown';
15+
import ReactDOMServer from 'react-dom/server';
1516
// SVG assets
1617
import ThumbUpIcon from 'assets/images/ico-thumb-up.svg';
1718
import CommentIcon from 'assets/images/ico-comment.svg';
@@ -21,17 +22,18 @@ import PlayIcon from 'assets/images/ico-play.svg';
2122
import ReadMoreArrow from 'assets/images/read-more-arrow.svg';
2223
import qs from 'qs';
2324

25+
const htmlToText = require('html-to-text');
26+
2427
// date/time format to use for the creation date
2528
const FORMAT = 'MMM DD, YYYY';
2629
// date/time format for 'Forum post' cards is different from others
2730
const FORUM_POST_FORMAT = 'MMM DD, YYYY [at] h:mm A';
2831
// max length for the title of the 'Article small' cards
2932
const ART_SMALL_TITLE_MAX_LENGTH = 29;
30-
// character length for the content preview
31-
const CONTENT_PREVIEW_LENGTH = 110;
3233
// Article large card 'breakpoint'
3334
const ARTICLE_LARGE_BREAKPOINT = 473;
34-
35+
// character length for the content preview
36+
const CONTENT_PREVIEW_LENGTH = 110;
3537

3638
class ArticleCard extends React.Component {
3739
constructor(props) {
@@ -132,10 +134,16 @@ class ArticleCard extends React.Component {
132134
: article.title;
133135

134136
// truncate content for 'Article large' cards
135-
const content = (
136-
(themeName === 'Article large' || themeName === 'Recommended')
137-
&& article.content.length > CONTENT_PREVIEW_LENGTH)
138-
? markdown(`${article.content.substring(0, CONTENT_PREVIEW_LENGTH)}...`)
137+
const content = themeName === 'Article large' || themeName === 'Recommended'
138+
? `${htmlToText.fromString(
139+
ReactDOMServer.renderToString(markdown(article.content)),
140+
{
141+
ignoreHref: true,
142+
ignoreImage: true,
143+
singleNewLineParagraphs: true,
144+
uppercaseHeadings: false,
145+
},
146+
).substring(0, CONTENT_PREVIEW_LENGTH)}...`
139147
: undefined;
140148

141149
// set the correct format to apply to the `article.creationDate`

0 commit comments

Comments
 (0)