Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
[added] private Markup.markdownToUnwrappedHTML - If a simple single…
Browse files Browse the repository at this point in the history
… line string is passed to the Markdown parser it thinks that it is a paragraph (it sort of technically is) and unnecessarily wraps it into `<p></p>`, which most often is not the desired behavior. This function fixes that.
  • Loading branch information
thealjey committed Feb 11, 2016
1 parent ced1172 commit c398d90
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 24 deletions.
8 changes: 4 additions & 4 deletions docs/Markup.html
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ <h5>Parameters:</h5>
<ul class="dummy">
<li>
<a href="Markup.js.html">Markup.js</a>,
<a href="Markup.js.html#sunlight-1-line-149">line 149</a>
<a href="Markup.js.html#sunlight-1-line-171">line 171</a>
</li>
</ul>
</dd>
Expand Down Expand Up @@ -574,7 +574,7 @@ <h5>Parameters:</h5>
<ul class="dummy">
<li>
<a href="Markup.js.html">Markup.js</a>,
<a href="Markup.js.html#sunlight-1-line-189">line 189</a>
<a href="Markup.js.html#sunlight-1-line-211">line 211</a>
</li>
</ul>
</dd>
Expand Down Expand Up @@ -762,7 +762,7 @@ <h5>Parameters:</h5>
<ul class="dummy">
<li>
<a href="Markup.js.html">Markup.js</a>,
<a href="Markup.js.html#sunlight-1-line-205">line 205</a>
<a href="Markup.js.html#sunlight-1-line-227">line 227</a>
</li>
</ul>
</dd>
Expand Down Expand Up @@ -950,7 +950,7 @@ <h5>Parameters:</h5>
<ul class="dummy">
<li>
<a href="Markup.js.html">Markup.js</a>,
<a href="Markup.js.html#sunlight-1-line-221">line 221</a>
<a href="Markup.js.html#sunlight-1-line-243">line 243</a>
</li>
</ul>
</dd>
Expand Down
26 changes: 24 additions & 2 deletions docs/Markup.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,28 @@ <h1 class="page-title">Source: Markup.js</h1>
return map(reject(children, ['type', 'comment']), (c, i) => 'text' === c.type ? c.data : Markup.childToJSX(c, i));
}

/**
* If a simple single line string is passed to the Markdown parser it thinks that it's a paragraph (it sort of
* technically is) and unnecessarily wraps it into `&lt;p>&lt;/p>`, which most often is not the desired behavior.
*
* This function converts Markdown to HTML and then removes the wrapping paragraph if it is the only top level tag
* unwrapping its contents.
*
* @memberof Markup
* @static
* @private
* @method markdownToUnwrappedHTML
* @param {string} markdown - an arbitrary Markdown string
* @return {string} an HTML string
*/
static markdownToUnwrappedHTML(markdown: string): string {
const html = trim(marked(markdown)),
dom = load(html),
{children} = dom.root().toArray()[0];

return 1 === children.length &amp;&amp; 'tag' === children[0].type &amp;&amp; 'p' === children[0].name ? dom('p').html() : html;
}

/**
* Recursively flattens `args` and combines string values.
*
Expand Down Expand Up @@ -292,7 +314,7 @@ <h1 class="page-title">Source: Markup.js</h1>
*/
markdownToHTML(markdown: string = ''): string {
markdown = trim(markdown);
return markdown ? trim(this.transform(marked(markdown))) : '';
return markdown ? trim(this.transform(Markup.markdownToUnwrappedHTML(markdown))) : '';
}

/**
Expand All @@ -308,7 +330,7 @@ <h1 class="page-title">Source: Markup.js</h1>
*/
markdownToJSX(markdown: string = ''): Array&lt;any> {
markdown = trim(markdown);
return markdown ? this.htmlToJSX(marked(markdown)) : [];
return markdown ? this.htmlToJSX(Markup.markdownToUnwrappedHTML(markdown)) : [];
}

}
Expand Down
2 changes: 1 addition & 1 deletion docs/quicksearch.html

Large diffs are not rendered by default.

30 changes: 28 additions & 2 deletions lib/Markup.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ var Markup = exports.Markup = function () {
var markdown = arguments.length <= 0 || arguments[0] === undefined ? '' : arguments[0];

markdown = (0, _trim2.default)(markdown);
return markdown ? (0, _trim2.default)(this.transform((0, _marked2.default)(markdown))) : '';
return markdown ? (0, _trim2.default)(this.transform(Markup.markdownToUnwrappedHTML(markdown))) : '';
}

/**
Expand All @@ -189,7 +189,7 @@ var Markup = exports.Markup = function () {
var markdown = arguments.length <= 0 || arguments[0] === undefined ? '' : arguments[0];

markdown = (0, _trim2.default)(markdown);
return markdown ? this.htmlToJSX((0, _marked2.default)(markdown)) : [];
return markdown ? this.htmlToJSX(Markup.markdownToUnwrappedHTML(markdown)) : [];
}
}], [{
key: 'toJSXKey',
Expand Down Expand Up @@ -305,6 +305,32 @@ var Markup = exports.Markup = function () {
});
}

/**
* If a simple single line string is passed to the Markdown parser it thinks that it's a paragraph (it sort of
* technically is) and unnecessarily wraps it into `<p></p>`, which most often is not the desired behavior.
*
* This function converts Markdown to HTML and then removes the wrapping paragraph if it is the only top level tag
* unwrapping its contents.
*
* @memberof Markup
* @static
* @private
* @method markdownToUnwrappedHTML
* @param {string} markdown - an arbitrary Markdown string
* @return {string} an HTML string
*/

}, {
key: 'markdownToUnwrappedHTML',
value: function markdownToUnwrappedHTML(markdown) {
var html = (0, _trim2.default)((0, _marked2.default)(markdown));
var dom = (0, _cheerio.load)(html);
var children = dom.root().toArray()[0].children;


return 1 === children.length && 'tag' === children[0].type && 'p' === children[0].name ? dom('p').html() : html;
}

/**
* Recursively flattens `args` and combines string values.
*
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"eslint-plugin-react": "^3.16.1",
"fb-watchman": "^1.9.0",
"ink-docstrap": "^1.1.1",
"js-yaml": "^3.5.2",
"js-yaml": "^3.5.3",
"jsdoc": "^3.4.0",
"json-loader": "^0.5.4",
"lodash": "^4.3.0",
Expand All @@ -85,7 +85,7 @@
"mkdirp": "^0.5.1",
"node-sass": "^3.4.2",
"node-sass-import-once": "^1.2.0",
"postcss": "^5.0.14",
"postcss": "^5.0.15",
"react": "^0.14.7",
"react-hot-loader": "^1.3.0",
"tiny-lr": "^0.2.1",
Expand Down
26 changes: 24 additions & 2 deletions src/Markup.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ export class Markup {
return map(reject(children, ['type', 'comment']), (c, i) => 'text' === c.type ? c.data : Markup.childToJSX(c, i));
}

/**
* If a simple single line string is passed to the Markdown parser it thinks that it's a paragraph (it sort of
* technically is) and unnecessarily wraps it into `<p></p>`, which most often is not the desired behavior.
*
* This function converts Markdown to HTML and then removes the wrapping paragraph if it is the only top level tag
* unwrapping its contents.
*
* @memberof Markup
* @static
* @private
* @method markdownToUnwrappedHTML
* @param {string} markdown - an arbitrary Markdown string
* @return {string} an HTML string
*/
static markdownToUnwrappedHTML(markdown: string): string {
const html = trim(marked(markdown)),
dom = load(html),
{children} = dom.root().toArray()[0];

return 1 === children.length && 'tag' === children[0].type && 'p' === children[0].name ? dom('p').html() : html;
}

/**
* Recursively flattens `args` and combines string values.
*
Expand Down Expand Up @@ -215,7 +237,7 @@ export class Markup {
*/
markdownToHTML(markdown: string = ''): string {
markdown = trim(markdown);
return markdown ? trim(this.transform(marked(markdown))) : '';
return markdown ? trim(this.transform(Markup.markdownToUnwrappedHTML(markdown))) : '';
}

/**
Expand All @@ -231,7 +253,7 @@ export class Markup {
*/
markdownToJSX(markdown: string = ''): Array<any> {
markdown = trim(markdown);
return markdown ? this.htmlToJSX(marked(markdown)) : [];
return markdown ? this.htmlToJSX(Markup.markdownToUnwrappedHTML(markdown)) : [];
}

}
50 changes: 39 additions & 11 deletions test/Markup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let marked, Markup, object, cmp, args, attribs;
describe('Markup', () => {

beforeEach(() => {
marked = stub().returns('<h1>Hello world!</h1>');
marked = stub().returnsArg(0);
Markup = proxyquire('../src/Markup', {marked}).Markup;
});

Expand Down Expand Up @@ -208,6 +208,42 @@ describe('Markup', () => {

});

describe('markdownToUnwrappedHTML', () => {

beforeEach(() => {
spy(Markup, 'markdownToUnwrappedHTML');
});

afterEach(() => {
Markup.markdownToUnwrappedHTML.restore();
});

describe('unwrap', () => {

beforeEach(() => {
Markup.markdownToUnwrappedHTML(' <p>something</p> ');
});

it('returns result', () => {
expect(Markup.markdownToUnwrappedHTML).returned('something');
});

});

describe('do not unwrap', () => {

beforeEach(() => {
Markup.markdownToUnwrappedHTML('<h1>something</h1>');
});

it('returns result', () => {
expect(Markup.markdownToUnwrappedHTML).returned('<h1>something</h1>');
});

});

});

describe('flatten', () => {

beforeEach(() => {
Expand Down Expand Up @@ -292,11 +328,7 @@ describe('Markup', () => {
describe('arg', () => {

beforeEach(() => {
cmp.markdownToHTML('# Hello world!');
});

it('calls marked', () => {
expect(marked).calledWith('# Hello world!');
cmp.markdownToHTML('<h1>Hello world!</h1>');
});

it('calls transform', () => {
Expand Down Expand Up @@ -338,11 +370,7 @@ describe('Markup', () => {
describe('arg', () => {

beforeEach(() => {
cmp.markdownToJSX('# Hello world!');
});

it('calls marked', () => {
expect(marked).calledWith('# Hello world!');
cmp.markdownToJSX('<h1>Hello world!</h1>');
});

it('calls htmlToJSX', () => {
Expand Down

0 comments on commit c398d90

Please sign in to comment.