Skip to content

Commit 9bab97f

Browse files
annawen1asudoh
andauthored
fix(utilities): add new util to remove html tags and entities
* fix(utilities): add new util to remove html tags and entities Co-authored-by: Akira Sudoh <asudoh@gmail.com> Co-authored-by: Anna Wen
1 parent b1a6e4f commit 9bab97f

File tree

6 files changed

+87
-7
lines changed

6 files changed

+87
-7
lines changed

packages/react/src/__tests__/__snapshots__/storyshots.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -4898,7 +4898,7 @@ exports[`Storyshots Components|LightboxMediaViewer Default 1`] = `
48984898
className="bx--lightbox-media-viewer__content__desc"
48994899
data-autoid="dds--lightbox-media-viewer__content__desc"
49004900
>
4901-
&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean et ultricies est.Mauris iaculis eget dolor nec hendrerit. Phasellus at elit sollicitudin, sodales nulla quis, consequat libero. Here are some common categories:&lt;/p&gt;
4901+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean et ultricies est.Mauris iaculis eget dolor nec hendrerit. Phasellus at elit sollicitudin, sodales nulla quis, consequat libero. Here are some common categories:
49024902
</div>
49034903
</div>
49044904
</div>

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
import {
88
settings as ddsSettings,
9-
markdownToHtml,
9+
removeHtmlTagEntities,
1010
} from '@carbon/ibmdotcom-utilities';
1111
import React, { useEffect, useState } from 'react';
1212
import { ExpressiveModal } from '../ExpressiveModal';
@@ -55,10 +55,7 @@ const LightboxMediaViewer = ({ media, ...modalProps }) => {
5555
};
5656
}, [media]);
5757

58-
const videoDesc = markdownToHtml(videoData.description, {
59-
createParagraphs: false,
60-
cleanString: true,
61-
});
58+
const videoDesc = removeHtmlTagEntities(videoData.description);
6259

6360
return (
6461
<section

packages/utilities/src/utilities/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright IBM Corp. 2016, 2018
2+
* Copyright IBM Corp. 2016, 2020
33
*
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.
@@ -12,6 +12,7 @@ export * from './featureflag';
1212
export * from './geolocation';
1313
export * from './ipcinfoCookie';
1414
export * from './markdownToHtml';
15+
export * from './removeHtmlTagEntities';
1516
export * from './sameHeight';
1617
export * from './serialize';
1718
export * from './settings';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright IBM Corp. 2020
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import { removeHtmlTagEntities } from '..';
9+
10+
describe('Html Tag and Entities remover utility', () => {
11+
const str =
12+
'<div><div><p>Lorem ipsum dolor sit amet,&nbsp;consectetur adipiscing elit.</p></div></div>';
13+
14+
it('returns the converted string with no html tags', () => {
15+
const output = removeHtmlTagEntities(str, { removeEntities: false });
16+
const expected =
17+
'Lorem ipsum dolor sit amet,&nbsp;consectetur adipiscing elit.';
18+
expect(output.trim()).toBe(expected);
19+
});
20+
21+
it('returns the converted string with no html tags or entities', () => {
22+
const output = removeHtmlTagEntities(str);
23+
const expected = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
24+
expect(output.trim()).toBe(expected);
25+
});
26+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright IBM Corp. 2016, 2020
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export { default as removeHtmlTagEntities } from './removeHtmlTagEntities';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright IBM Corp. 2020
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
const _htmlTagRegex = /<.*?>/g;
8+
const _cleanStringRegex = /\n|\s{2,}|&([a-zA-Z]+);/g;
9+
10+
/**
11+
* Removes any html tags from a string and keeps inner text if any
12+
*
13+
* @param {string} str String to be checked for html tags
14+
* @returns {string} String with html tags stripped out
15+
* @private
16+
*/
17+
const _removeHtmlTags = str => str.replace(_htmlTagRegex, '');
18+
19+
/**
20+
* Cleans string by replacing multiple spaces with a single space
21+
* and removing single new lines.
22+
*
23+
* @param {string} str String to be checked
24+
* @returns {string} String with multiple spaces and single new lines removed
25+
* @private
26+
*/
27+
const _cleanString = str => str.replace(_cleanStringRegex, ' ');
28+
29+
/**
30+
*
31+
* @param {string} str html string passed in to remove html tags and entities
32+
* @param {object} [options={}] Object with options for the conversion
33+
* @param {boolean} [options.removeEntities=true] Defines if should remove html entities
34+
* @returns {string} String removed of html tags
35+
* @example
36+
* import { removeHtmlTagEntities } from '@carbon/ibmdotcom-utilities';
37+
*
38+
* markdownToHtml('<p>example string</p>&nbsp;<p>here</>')
39+
* // 'example string here'
40+
*/
41+
function removeHtmlTagEntities(str, { removeEntities = true } = {}) {
42+
let removedTags = _removeHtmlTags(str);
43+
removedTags = removeEntities ? _cleanString(removedTags) : removedTags;
44+
45+
return removedTags;
46+
}
47+
48+
export default removeHtmlTagEntities;

0 commit comments

Comments
 (0)