discourse/app/assets/javascripts/pretty-text/engines/discourse-markdown/html.js.es6
Sam 234694b50f Feature: CommonMark support
This adds the markdown.it engine to Discourse.
https://github.com/markdown-it/markdown-it

As the migration is going to take a while the new engine is default
disabled. To enable it you must change the hidden site setting:
enable_experimental_markdown_it.

This commit is a squash of many other commits, it also includes some
improvements to autospec (ability to run plugins), and a dev dependency
on the og gem for html normalization.
2017-06-23 12:01:33 -04:00

53 lines
1.8 KiB
JavaScript

const BLOCK_TAGS = ['address', 'article', 'aside', 'audio', 'blockquote', 'canvas', 'dd', 'details',
'div', 'dl', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3',
'h4', 'h5', 'h6', 'header', 'hgroup', 'hr', 'iframe', 'noscript', 'ol', 'output',
'p', 'pre', 'section', 'table', 'tfoot', 'ul', 'video', 'summary'];
function splitAtLast(tag, block, next, first) {
const endTag = `</${tag}>`;
let endTagIndex = first ? block.indexOf(endTag) : block.lastIndexOf(endTag);
if (endTagIndex !== -1) {
endTagIndex += endTag.length;
const trailing = block.substr(endTagIndex).replace(/^\s+/, '');
if (trailing.length) {
next.unshift(trailing);
}
return [ block.substr(0, endTagIndex) ];
}
};
export function setup(helper) {
if (helper.markdownIt) { return; }
// If a row begins with HTML tags, don't parse it.
helper.registerBlock('html', function(block, next) {
let split, pos;
// Fix manual blockquote paragraphing even though it's not strictly correct
// PERF NOTE: /\S+<blockquote/ is a perf hog for search, try on huge string
if (pos = block.search(/<blockquote/) >= 0) {
if(block.substring(0, pos).search(/\s/) === -1) {
split = splitAtLast('blockquote', block, next, true);
if (split) { return this.processInline(split[0]); }
}
}
const m = /^\s*<\/?([^>]+)\>/.exec(block);
if (m && m[1]) {
const tag = m[1].split(/\s/);
if (tag && tag[0] && BLOCK_TAGS.indexOf(tag[0]) !== -1) {
split = splitAtLast(tag[0], block, next);
if (split) {
if (split.length === 1 && split[0] === block) { return; }
return split;
}
return [ block.toString() ];
}
}
});
}