Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Commit

Permalink
fix(html): enable (dangerous) HTML in Markdown and pass it through
Browse files Browse the repository at this point in the history
This change enables HTML in Markdown. HTML in Markdown is passed through unchanged and can be potentially dangerous

Fixes #154
  • Loading branch information
trieloff committed Apr 24, 2019
1 parent bdad96c commit 93efaf7
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/utils/mdast-to-vdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ class VDOMTransformer {
* @returns {string} the corresponding HTML
*/
static toHTML(mdast, handlers) {
return hast2html(mdast2hast(mdast, { handlers }));
return hast2html(mdast2hast(mdast, {
handlers,
allowDangerousHTML: true,
}), { allowDangerousHTML: true });
}

/**
Expand Down
31 changes: 31 additions & 0 deletions test/fixtures/forms.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"type": "root",
"children": [
{
"type": "heading",
"depth": 1,
"children": [
{
"type": "text",
"value": "Hello "
},
{
"type": "html",
"value": "<em>"
},
{
"type": "text",
"value": "World"
},
{
"type": "html",
"value": "</em>"
}
]
},
{
"type": "html",
"value": "<form action=\"/cgi-bin/contact\">\n <input type=\"text\"><label>Message</label>\n</form>"
}
]
}
5 changes: 5 additions & 0 deletions test/fixtures/forms.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Hello <em>World</em>

<form action="/cgi-bin/contact">
<input type="text"><label>Message</label>
</form>
24 changes: 24 additions & 0 deletions test/testHTMLFromMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,30 @@ describe('Testing Markdown conversion', () => {
`);
});

it('HTML Block elements', async () => {
await assertMd(`
# Foo
<form>
<input type="text" name="name"><label for="name">Name</label>
</form>
`, `
<h1 id="foo">Foo</h1>
<form><input type="text" name="name"><label for="name">Name</label></form>
`);
});

it('HTML inline elements', async () => {
await assertMd(`
# Foo <em>Bar</em>
Hello World [link](λ)
`, `
<h1 id="foo">Foo</h1>
<p>Hello World <a href="%CE%BB">link</a></p>
`);
});

it('GFM', async () => {
await assertMd(`
Hello World.
Expand Down
4 changes: 4 additions & 0 deletions test/testParseMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ describe('Test Markdown Parsing', () => {
assertMatch('headings', callback);
});

it('Parses HTML in Markdown', () => {
assertMatch('forms', callback);
});

it('Does not get confused by thematic breaks', () => {
assertMatch('confusing', callback);
});
Expand Down

0 comments on commit 93efaf7

Please sign in to comment.