Skip to content

Commit a8e4d31

Browse files
feat(markdown-converter): add carbon classes option + fixes
1 parent f36dc46 commit a8e4d31

File tree

6 files changed

+78
-59
lines changed

6 files changed

+78
-59
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"axios": "^0.19.0",
4444
"babel-eslint": "^10.0.2",
4545
"browserslist-config-carbon": "^10.3.0",
46+
"carbon-components": "^10.7.4",
4647
"cross-env": "^5.2.0",
4748
"doctoc": "^1.4.0",
4849
"enzyme-to-json": "^3.3.5",

packages/utilities/src/utilities/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ export * from './escaperegexp';
1010
export * from './featureflag';
1111
export * from './geolocation';
1212
export * from './ipcinfoCookie';
13+
export * from './markdownToHtml';
1314
export * from './serialize';
14-
export * from './markdownconverter';
1515
export * from './settings';

packages/utilities/src/utilities/markdownconverter/__tests__/markdownconverter.test.js packages/utilities/src/utilities/markdownToHtml/__tests__/markdownToHtml.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
import { markdownConverter } from '../';
1+
import { markdownToHtml } from '../';
22

33
describe('Markdown converter utility', () => {
44
const str =
55
'This <p>is</p> <input value="something" /> _italic_ and *italic*. <span>This</span> is **bold** and __bold__.';
66

77
it('return the converted string with italic and bold', () => {
8-
const output = markdownConverter(str);
8+
const output = markdownToHtml(str);
99
const expected =
1010
'This is <em>italic</em> and <em>italic</em>. This is <strong>bold</strong> and <strong>bold</strong>.';
1111
expect(output).toBe(expected);
1212
});
1313

1414
it('return the converted string with italic', () => {
15-
const output = markdownConverter(str, { italic: true });
15+
const output = markdownToHtml(str, { italic: true });
1616
const expected =
1717
'This is <em>italic</em> and <em>italic</em>. This is **bold** and __bold__.';
1818
expect(output).toBe(expected);
1919
});
2020

2121
it('return the converted string with bold', () => {
22-
const output = markdownConverter(str, { bold: true });
22+
const output = markdownToHtml(str, { bold: true });
2323
const expected =
2424
'This is _italic_ and *italic*. This is <strong>bold</strong> and <strong>bold</strong>.';
2525
expect(output).toBe(expected);

packages/utilities/src/utilities/markdownconverter/index.js packages/utilities/src/utilities/markdownToHtml/index.js

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

8-
export { default as markdownConverter } from './markdownconverter';
8+
export { default as markdownToHtml } from './markdownToHtml';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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;

packages/utilities/src/utilities/markdownconverter/markdownconverter.js

-53
This file was deleted.

0 commit comments

Comments
 (0)