|
| 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> <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