Skip to content

Commit 3b50514

Browse files
author
Anna Wen
committed
fix(videoplayer): add markdown utility to remove html
1 parent 8c97bb9 commit 3b50514

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

packages/react/src/components/LightboxMediaViewer/LightboxMediaViewer.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
7+
import {
8+
settings as ddsSettings,
9+
markdownToHtml,
10+
} from '@carbon/ibmdotcom-utilities';
711
import React, { useEffect, useState } from 'react';
8-
import { settings as ddsSettings } from '@carbon/ibmdotcom-utilities';
912
import { ExpressiveModal } from '../ExpressiveModal';
1013
import { Image } from '../Image';
1114
import { ModalBody } from 'carbon-components-react';
@@ -81,7 +84,11 @@ const LightboxMediaViewer = ({ media, ...modalProps }) => {
8184
<div
8285
data-autoid={`${stablePrefix}--lightbox-media-viewer__content__desc`}
8386
className={`${prefix}--lightbox-media-viewer__content__desc`}>
84-
{videoData.description}
87+
{videoData.description &&
88+
markdownToHtml(videoData.description, {
89+
textOnly: true,
90+
cleanString: true,
91+
})}
8592
</div>
8693
)}
8794
</div>

packages/react/src/components/VideoPlayer/VideoPlayer.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {
9+
settings as ddsSettings,
10+
markdownToHtml,
11+
} from '@carbon/ibmdotcom-utilities';
812
import React, { useEffect, useState } from 'react';
913
import cx from 'classnames';
10-
import { settings as ddsSettings } from '@carbon/ibmdotcom-utilities';
1114
import PropTypes from 'prop-types';
1215
import { settings } from 'carbon-components';
1316
import { VideoPlayerAPI } from '@carbon/ibmdotcom-services';
@@ -47,13 +50,6 @@ const VideoPlayer = ({
4750
customClassName
4851
);
4952

50-
let videoDesc = '';
51-
if (videoData.description) {
52-
videoDesc = videoData.description
53-
.replace(/(&nbsp;)/gi, ' ')
54-
.replace(/(<([^>]+)>)/gi, '');
55-
}
56-
5753
return (
5854
<div
5955
aria-label={`${videoData.description} ${videoDuration}`}
@@ -67,7 +63,12 @@ const VideoPlayer = ({
6763
</div>
6864
{showDescription && (
6965
<div className={`${prefix}--video-player__video-description`}>
70-
{videoDesc} {videoDuration}
66+
{videoData.description &&
67+
markdownToHtml(videoData.description, {
68+
textOnly: true,
69+
cleanString: true,
70+
})}
71+
{` ${videoDuration}`}
7172
</div>
7273
)}
7374
</div>

packages/utilities/src/utilities/markdownToHtml/markdownToHtml.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { settings } from 'carbon-components';
22
const { prefix } = settings;
33

44
const _htmlTagRegex = /<.*?>/g;
5-
const _cleanStringRegex = /\n|\s{2,}/g;
5+
const _cleanStringRegex = /\n|\s{2,}|(&nbsp;)/g;
66
const _italicRegex = /[_*](.*?)[_*]/g;
77
const _boldRegex = /[_*]{2}(.*?)[_*]{2}/g;
88
const _paraRegex = /\n\n/g;
@@ -58,10 +58,13 @@ function markdownToHtml(
5858
bold = true,
5959
useCarbonClasses = true,
6060
allowHtml = false,
61+
cleanString = false,
62+
textOnly = false,
6163
} = {}
6264
) {
6365
let paraList = '';
6466
let converted = allowHtml ? str : _removeHtmlTags(str);
67+
converted = cleanString ? _cleanString(converted) : converted;
6568
const paras = converted.split(_paraRegex);
6669

6770
paras.map(para => {
@@ -87,7 +90,7 @@ function markdownToHtml(
8790
});
8891
}
8992

90-
paraList += `<p>${para}</p>`;
93+
paraList += textOnly ? para : `<p>${para}</p>`;
9194
});
9295

9396
return paraList;

0 commit comments

Comments
 (0)