Skip to content

Commit 9dea0f5

Browse files
feat(markdown-converter): js docs and defaults corrections
1 parent 388f863 commit 9dea0f5

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

package.json

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

packages/utilities/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"babel-plugin-transform-inline-environment-variables": "^0.4.0",
5757
"babel-polyfill": "^6.26.0",
5858
"browserslist-config-carbon": "10.3.0",
59+
"carbon-components": "^10.7.4",
5960
"chalk": "^2.3.0",
6061
"cli-table": "^0.3.0",
6162
"core-js": "^3.1.3",
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
1+
import { settings } from 'carbon-components';
12
import { markdownToHtml } from '../';
23

4+
const { prefix } = settings;
5+
36
describe('Markdown converter utility', () => {
47
const str =
5-
'This <p>is</p> <input value="something" /> _italic_ and *italic*. <span>This</span> is **bold** and __bold__.';
8+
'This <p>is</p> <input value="something" /> _italic_ and **bold**.';
69

710
it('return the converted string with italic and bold', () => {
811
const output = markdownToHtml(str);
9-
const expected =
10-
'This is <em>italic</em> and <em>italic</em>. This is <strong>bold</strong> and <strong>bold</strong>.';
12+
const expected = `This is <em class="${prefix}--type-light">italic</em> and <strong class="${prefix}--type-semibold">bold</strong>.`;
1113
expect(output).toBe(expected);
1214
});
1315

1416
it('return the converted string with italic', () => {
1517
const output = markdownToHtml(str, { italic: true });
16-
const expected =
17-
'This is <em>italic</em> and <em>italic</em>. This is **bold** and __bold__.';
18+
const expected = `This is <em class="${prefix}--type-light">italic</em> and **bold**.`;
1819
expect(output).toBe(expected);
1920
});
2021

2122
it('return the converted string with bold', () => {
2223
const output = markdownToHtml(str, { bold: true });
23-
const expected =
24-
'This is _italic_ and *italic*. This is <strong>bold</strong> and <strong>bold</strong>.';
24+
const expected = `This is _italic_ and <strong class="${prefix}--type-semibold">bold</strong>.`;
25+
expect(output).toBe(expected);
26+
});
27+
28+
it('return the converted string without carbon classes', () => {
29+
const output = markdownToHtml(str, { useCarbonClasses: false });
30+
const expected = 'This is <em>italic</em> and <strong>bold</strong>.';
2531
expect(output).toBe(expected);
2632
});
2733
});

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

+19-11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const _boldRegex = /[_*]{2}(.*?)[_*]{2}/g;
1111
*
1212
* @param {string} str String to be checked for html tags
1313
* @returns {string} String with html tags stripped out
14+
* @private
1415
*/
1516
const _removeHtmlTags = str => str.replace(_htmlTagRegex, '');
1617

@@ -19,31 +20,38 @@ const _removeHtmlTags = str => str.replace(_htmlTagRegex, '');
1920
*
2021
* @param {string} str String to be checked for double spaces
2122
* @returns {string} String with html double spaces fixed
23+
* @private
2224
*/
2325
const _fixDoubleSpaces = str => str.replace(_doubleSpaceRegex, ' ');
2426

2527
/**
2628
* 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*
29+
* <p>It's not a full markdown-to-html converter</p>
30+
* <p>It currently supports two syntaxes: <strong>Bold</strong> and <em>Italic</em></p>
31+
* <ul>
32+
* <li>Bold: Double asterisk (**) or double underscore (__).</li>
33+
* <li>Bold examples: **Lorem ipsum** __dolor__</li>
34+
* <li>Italic: Single asterisk (*) or single underscore (_)</li>
35+
* <li>Italic examples: _Lorem ipsum_ *dolor*</li>
36+
* </ul>
37+
*
3338
*
3439
* @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 typography classes
40+
* @param {object} [options={}] Object with options for the conversion
41+
* @param {boolean} [options.italic=false] Defines if should convert italic
42+
* @param {boolean} [options.bold=false] Defines if should convert bold
43+
* @param {boolean} [options.useCarbonClasses=true] Defines if should use carbon typography classes
3944
* @returns {string} String converted to html
4045
* @example
4146
* import { markdownToHtml } from '@carbon/ibmdotcom-utilities';
4247
*
4348
* markdownToHtml('Lorem _ipsum_ __dolor__ *sit* **amet**.')
4449
* // 'Lorem <em>ipsum</em> <strong>dolor</strong> <em>sit</em> <strong>amet</strong>.'
4550
*/
46-
function markdownToHtml(str, { italic, bold, useCarbonClasses } = {}) {
51+
function markdownToHtml(
52+
str,
53+
{ italic = false, bold = false, useCarbonClasses = true } = {}
54+
) {
4755
const isAllStyles = !italic && !bold;
4856
let converted = _removeHtmlTags(str);
4957

0 commit comments

Comments
 (0)