|
| 1 | +import { settings } from 'carbon-components'; |
| 2 | +const { prefix } = settings; |
| 3 | + |
| 4 | +const _htmlTagRegex = /<.*?>/g; |
| 5 | +const _doubleSpaceRegex = /\s{2,}/g; |
| 6 | +const _italicRegex = /[_*](.*?)[_*]/g; |
| 7 | +const _boldRegex = /[_*]{2}(.*?)[_*]{2}/g; |
| 8 | + |
| 9 | +/** |
| 10 | + * Removes any html tags from a string and keeps inner text if any |
| 11 | + * |
| 12 | + * @param {string} str String to be checked for html tags |
| 13 | + * @returns {string} String with html tags stripped out |
| 14 | + */ |
| 15 | +const _removeHtmlTags = str => str.replace(_htmlTagRegex, ''); |
| 16 | + |
| 17 | +/** |
| 18 | + * Checks for double spaces between words in a string and replaces for single spaces |
| 19 | + * |
| 20 | + * @param {string} str String to be checked for double spaces |
| 21 | + * @returns {string} String with html double spaces fixed |
| 22 | + */ |
| 23 | +const _fixDoubleSpaces = str => str.replace(_doubleSpaceRegex, ' '); |
| 24 | + |
| 25 | +/** |
| 26 | + * Converts some markdown syntaxes into html |
| 27 | + * It's not a full markdown-to-html converter |
| 28 | + * It currently supports two syntaxes: Bold and Italic |
| 29 | + * Bold: Double asterisk (**) or double underscore (__). |
| 30 | + * Bold examples: **Lorem ipsum** __dolor__ |
| 31 | + * Italic: Single asterisk (*) or single underscore (_) |
| 32 | + * Italic examples: _Lorem ipsum_ *dolor* |
| 33 | + * |
| 34 | + * @param {string} str String to convert to html |
| 35 | + * @param {object} options Object with options for the conversion |
| 36 | + * @param {boolean} options.italic Defines if should convert italic |
| 37 | + * @param {boolean} options.bold Defines if should convert bold |
| 38 | + * @param {boolean} options.useCarbonClasses If true uses carbon type classes otherwise uses sematic html tags. |
| 39 | + * @returns {string} String converted to html |
| 40 | + * @example |
| 41 | + * import { markdownToHtml } from '@carbon/ibmdotcom-utilities'; |
| 42 | + * |
| 43 | + * markdownToHtml('Lorem _ipsum_ __dolor__ *sit* **amet**.') |
| 44 | + * // 'Lorem <em>ipsum</em> <strong>dolor</strong> <em>sit</em> <strong>amet</strong>.' |
| 45 | + */ |
| 46 | +function markdownToHtml(str, { italic, bold, useCarbonClasses } = {}) { |
| 47 | + const isAllStyles = !italic && !bold; |
| 48 | + let converted = _removeHtmlTags(str); |
| 49 | + |
| 50 | + if (italic || isAllStyles) { |
| 51 | + converted = converted.replace(_italicRegex, (match, p1) => { |
| 52 | + if (!p1.length) { |
| 53 | + return match; |
| 54 | + } |
| 55 | + return useCarbonClasses |
| 56 | + ? `<span class="${prefix}--type-light">${p1}</span>` |
| 57 | + : `<em>${p1}</em>`; |
| 58 | + }); |
| 59 | + } |
| 60 | + if (bold || isAllStyles) { |
| 61 | + converted = converted.replace(_boldRegex, (match, p1) => { |
| 62 | + return useCarbonClasses |
| 63 | + ? `<span class="${prefix}--type-semibold">${p1}</span>` |
| 64 | + : `<strong>${p1}</strong>`; |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + return _fixDoubleSpaces(converted); |
| 69 | +} |
| 70 | + |
| 71 | +export default markdownToHtml; |
0 commit comments