-
-
Notifications
You must be signed in to change notification settings - Fork 10k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-instate mobiledoc dom rendering with bypass of SimpleDOM parsing (#…
…8937) closes #8757 - update the markdown card render method to use SimpleDOM's `createRawHtmlSection`. This avoids SimpleDOM parsing and tokenization of broken or unsupported free-form HTML that markdown allows - replace markdown extraction/render with mobiledoc's renderer in the `Post` model - removes `jsdom` as it's no longer necessary
- Loading branch information
1 parent
8606ea2
commit 47322e4
Showing
5 changed files
with
17 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,19 @@ | ||
var markdownConverter = require('../../../utils/markdown-converter'); | ||
|
||
module.exports = { | ||
name: 'card-markdown', | ||
type: 'dom', | ||
render: function (opts) { | ||
var SimpleDom = require('simple-dom'), | ||
tokenizer = require('simple-html-tokenizer').tokenize, | ||
jsdom = require('jsdom').jsdom, | ||
html, doc, parser, sanitizedHTML; | ||
|
||
// markdown can be autosaved at any point by the client, even when | ||
// writing HTML so you can end up with unbalanced HTML elements | ||
// | ||
// mobiledoc uses simple-dom to build a DOM object. simple-dom is | ||
// purposefully very basic and only designed to handle valid HTML, | ||
// if it's fed unbalanced or invalid HTML it will throw an error. | ||
// | ||
// to work around the possibility of having invalid HTML we first | ||
// pass the HTML through jsdom which seeks to fully emulate the | ||
// WHATWG DOM/HTML standards including the ability to handle | ||
// unbalanced HTML in the same way a browser does | ||
html = markdownConverter.render(opts.payload.markdown || ''); | ||
doc = jsdom(html, { | ||
features: { | ||
FetchExternalResources: false, | ||
ProcessExternalResources: false | ||
} | ||
}); | ||
|
||
// grab the rendered + sanitized body HTML | ||
sanitizedHTML = doc.body.innerHTML; | ||
var markdownConverter = require('../../../utils/markdown-converter'), | ||
html, element; | ||
|
||
// free up memory by closing the jsdom "window" | ||
doc.defaultView.close(); | ||
// convert markdown to HTML ready for insertion into dom | ||
html = '<div class="kg-card-markdown">' | ||
+ markdownConverter.render(opts.payload.markdown || '') | ||
+ '</div>'; | ||
|
||
parser = new SimpleDom.HTMLParser(tokenizer, opts.env.dom, SimpleDom.voidMap); | ||
// use the SimpleDOM document to create a raw HTML section. | ||
// avoids parsing/rendering of potentially broken or unsupported HTML | ||
element = opts.env.dom.createRawHTMLSection(html); | ||
|
||
// generate a new SimpleDom object from the sanitzed HTML | ||
return parser.parse('' | ||
+ '<div class="kg-card-markdown">' | ||
+ sanitizedHTML | ||
+ '</div>' | ||
); | ||
return element; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters