2017-05-23 17:15:32 +03:00
|
|
|
var markdownConverter = require('../../../utils/markdown-converter');
|
2017-03-14 21:07:33 +03:00
|
|
|
|
|
|
|
module.exports = {
|
2017-03-24 13:03:33 +03:00
|
|
|
name: 'card-markdown',
|
2017-03-14 21:07:33 +03:00
|
|
|
type: 'dom',
|
2017-05-23 17:15:32 +03:00
|
|
|
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;
|
|
|
|
|
|
|
|
// free up memory by closing the jsdom "window"
|
|
|
|
doc.defaultView.close();
|
|
|
|
|
2017-03-14 21:07:33 +03:00
|
|
|
parser = new SimpleDom.HTMLParser(tokenizer, opts.env.dom, SimpleDom.voidMap);
|
2017-05-23 17:15:32 +03:00
|
|
|
|
|
|
|
// generate a new SimpleDom object from the sanitzed HTML
|
replace custom showdown fork with markdown-it (#8451)
refs https://github.com/TryGhost/Ghost-Admin/pull/690, closes #1501, closes #2093, closes #4592, closes #4627, closes #4659, closes #5039, closes #5237, closes #5587, closes #5625, closes #5632, closes #5822, closes #5939, closes #6840, closes #7183, closes #7536
- replace custom showdown fork with markdown-it
- swaps showdown for markdown-it when rendering markdown
- match existing header ID behaviour
- allow headers without a space after the #s
- add duplicate header ID handling
- remove legacy markdown spec
- move markdown-it setup into markdown-converter util
- update mobiledoc specs to match markdown-it newline behaviour
- update data-generator HTML to match markdown-it newline behaviour
- fix Post "converts html to plaintext" test
- update rss spec to match markdown-it newline behaviour
- close almost all related showdown bugs
2017-05-15 19:48:14 +03:00
|
|
|
return parser.parse(''
|
|
|
|
+ '<div class="kg-card-markdown">'
|
2017-05-23 17:15:32 +03:00
|
|
|
+ sanitizedHTML
|
replace custom showdown fork with markdown-it (#8451)
refs https://github.com/TryGhost/Ghost-Admin/pull/690, closes #1501, closes #2093, closes #4592, closes #4627, closes #4659, closes #5039, closes #5237, closes #5587, closes #5625, closes #5632, closes #5822, closes #5939, closes #6840, closes #7183, closes #7536
- replace custom showdown fork with markdown-it
- swaps showdown for markdown-it when rendering markdown
- match existing header ID behaviour
- allow headers without a space after the #s
- add duplicate header ID handling
- remove legacy markdown spec
- move markdown-it setup into markdown-converter util
- update mobiledoc specs to match markdown-it newline behaviour
- update data-generator HTML to match markdown-it newline behaviour
- fix Post "converts html to plaintext" test
- update rss spec to match markdown-it newline behaviour
- close almost all related showdown bugs
2017-05-15 19:48:14 +03:00
|
|
|
+ '</div>'
|
|
|
|
);
|
2017-03-14 21:07:33 +03:00
|
|
|
}
|
|
|
|
};
|