medium-editor-markdown
Advanced tools
Comparing version 3.0.0 to 3.0.1
@@ -0,1 +1,3 @@ | ||
"use strict"; | ||
(function (root) { | ||
@@ -6,79 +8,95 @@ if (typeof MediumEditor !== "function") { | ||
var MeMarkdown = function (options, callback) { | ||
var MeMarkdown = function MeMarkdown(options, callback) { | ||
if (typeof options === "function") { | ||
callback = options; | ||
options = {}; | ||
} | ||
if (typeof options === "function") { | ||
callback = options; | ||
options = {}; | ||
} | ||
// Defaults | ||
options = Object(options); | ||
options.events = options.events || ["input", "change"]; | ||
callback = callback || options.callback || function () {}; | ||
// Defaults | ||
options = Object(options); | ||
options.events = options.events || ["input", "change"]; | ||
callback = callback || options.callback || function () {}; | ||
var toTurnDownOptions = options.toTurnDownOptions = Object(options.toTurnDownOptions); | ||
toTurnDownOptions.converters = toTurnDownOptions.converters || []; | ||
var toTurndownOptions = options.toTurndownOptions = Object(options.toTurndownOptions); | ||
toTurndownOptions.converters = toTurndownOptions.converters || []; | ||
toTurndownOptions.customRules = toTurndownOptions.customRules || []; | ||
if (!options.ignoreBuiltInConverters) { | ||
toTurnDownOptions.converters.push({ | ||
filter: function (node) { | ||
return node.nodeName === "DIV" && !node.attributes.length; | ||
} | ||
, replacement: function (content) { | ||
return content; | ||
} | ||
}); | ||
} | ||
if (!options.ignoreBuiltinConverters) { | ||
toTurndownOptions.converters.push({ | ||
filter: function filter(node) { | ||
return node.nodeName === "DIV" && !node.attributes.length; | ||
}, | ||
replacement: function replacement(content) { | ||
return content; | ||
} | ||
}); | ||
} | ||
function normalizeList ($elm) { | ||
var $children = $elm.children; | ||
for (var i = 0; i < $children.length; ++i) { | ||
var $cChild = $children[i]; | ||
var $br = $cChild.querySelector("br"); | ||
$br && $br.remove(); | ||
!$cChild.innerHTML.trim() && $cChild.remove(); | ||
var $prevChild = $children[i - 1]; | ||
if (/^UL|OL$/.test($cChild.tagName)) { | ||
try { | ||
$prevChild.appendChild($cChild); | ||
} catch (e) { console.warn(e); } | ||
normalizeList($cChild); | ||
function normalizeList($elm) { | ||
var $children = $elm.children; | ||
for (var i = 0; i < $children.length; ++i) { | ||
var $cChild = $children[i]; | ||
var $br = $cChild.querySelector("br"); | ||
$br && $br.remove(); | ||
!$cChild.innerHTML.trim() && $cChild.remove(); | ||
var $prevChild = $children[i - 1]; | ||
if (/^UL|OL$/.test($cChild.tagName)) { | ||
try { | ||
$prevChild.appendChild($cChild); | ||
} catch (e) { | ||
console.warn(e); | ||
} | ||
normalizeList($cChild); | ||
} | ||
} | ||
} | ||
} | ||
// Called by medium-editor during init | ||
this.init = function () { | ||
// Called by medium-editor during init | ||
this.init = function () { | ||
// If this instance of medium-editor doesn't have any elements, there's nothing for us to do | ||
if (!this.base.elements || !this.base.elements.length) { | ||
return; | ||
} | ||
// If this instance of medium-editor doesn't have any elements, there's nothing for us to do | ||
if (!this.base.elements || !this.base.elements.length) { | ||
return; | ||
} | ||
// Element(s) that this instance of medium-editor is attached to is/are stored in .elements | ||
this.element = this.base.elements[0]; | ||
// Element(s) that this instance of medium-editor is attached to is/are stored in .elements | ||
this.element = this.base.elements[0]; | ||
// String.prototype.trimRight is non-standard, this should have the same effect | ||
var rightWhitespace = /[\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF]*$/; | ||
// String.prototype.trimRight is non-standard, this should have the same effect | ||
var rightWhitespace = /[\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF]*$/; | ||
var handler = function () { | ||
var $clone = this.element.cloneNode(true); | ||
var $lists = $clone.querySelectorAll("ul, ol"); | ||
for (var i = 0; i < $lists.length; ++i) { | ||
normalizeList($lists[i]); | ||
} | ||
var handler = function () { | ||
var $clone = this.element.cloneNode(true); | ||
var $lists = $clone.querySelectorAll("ul, ol"); | ||
for (var i = 0; i < $lists.length; ++i) { | ||
normalizeList($lists[i]); | ||
} | ||
callback( new TurndownService(options.toTurnDownOptions).turndown($clone.innerHTML).split("\n").map(function (c) { | ||
return c.replace(rightWhitespace, ''); | ||
}).join("\n").replace(rightWhitespace, '')); | ||
}.bind(this); | ||
var turndownService = new TurndownService(options.toTurndownOptions); | ||
options.events.forEach(function (c) { | ||
this.element.addEventListener(c, handler); | ||
}.bind(this)); | ||
toTurndownOptions.customRules.forEach(function (customRule) { | ||
turndownService.addRule(customRule.key, { | ||
filter: customRule.filter, | ||
replacement: customRule.replacement | ||
}); | ||
}); | ||
handler(); | ||
callback(turndownService.turndown($clone.innerHTML).split("\n").map(function (c) { | ||
return c.replace(rightWhitespace, ''); | ||
}).join("\n").replace(rightWhitespace, '')); | ||
}.bind(this); | ||
if (options.subscribeToMeEditableInput) { | ||
this.base.subscribe('editableInput', handler); | ||
} else { | ||
options.events.forEach(function (c) { | ||
this.element.addEventListener(c, handler); | ||
}.bind(this)); | ||
} | ||
handler(); | ||
}; | ||
}; | ||
}; | ||
root.MeMarkdown = MeMarkdown; | ||
})(this); | ||
})(undefined); |
@@ -1,1 +0,37 @@ | ||
!function(e){if("function"!=typeof MediumEditor)throw new Error("Medium Editor is not loaded on the page.");var n=function(e,n){function t(e){for(var n=e.children,r=0;r<n.length;++r){var o=n[r],i=o.querySelector("br");i&&i.remove(),!o.innerHTML.trim()&&o.remove();var u=n[r-1];if(/^UL|OL$/.test(o.tagName)){try{u.appendChild(o)}catch(e){console.warn(e)}t(o)}}}"function"==typeof e&&(n=e,e={}),e=Object(e),e.events=e.events||["input","change"],n=n||e.callback||function(){};var r=e.toTurnDownOptions=Object(e.toTurnDownOptions);r.converters=r.converters||[],e.ignoreBuiltInConverters||r.converters.push({filter:function(e){return"DIV"===e.nodeName&&!e.attributes.length},replacement:function(e){return e}}),this.init=function(){if(this.base.elements&&this.base.elements.length){this.element=this.base.elements[0];var r=/[\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF]*$/,o=function(){for(var o=this.element.cloneNode(!0),i=o.querySelectorAll("ul, ol"),u=0;u<i.length;++u)t(i[u]);n(new TurndownService(e.toTurnDownOptions).turndown(o.innerHTML).split("\n").map(function(e){return e.replace(r,"")}).join("\n").replace(r,""))}.bind(this);e.events.forEach(function(e){this.element.addEventListener(e,o)}.bind(this)),o()}}};e.MeMarkdown=n}(this); | ||
"use strict"; | ||
!function (e) { | ||
if ("function" != typeof MediumEditor) throw new Error("Medium Editor is not loaded on the page.");var n = function n(e, _n) { | ||
function t(e) { | ||
for (var n = e.children, r = 0; r < n.length; ++r) { | ||
var i = n[r], | ||
u = i.querySelector("br");u && u.remove(), !i.innerHTML.trim() && i.remove();var o = n[r - 1];if (/^UL|OL$/.test(i.tagName)) { | ||
try { | ||
o.appendChild(i); | ||
} catch (e) { | ||
console.warn(e); | ||
}t(i); | ||
} | ||
} | ||
}"function" == typeof e && (_n = e, e = {}), e = Object(e), e.events = e.events || ["input", "change"], _n = _n || e.callback || function () {};var r = e.toTurndownOptions = Object(e.toTurndownOptions);r.converters = r.converters || [], r.customRules = r.customRules || [], e.ignoreBuiltinConverters || r.converters.push({ filter: function filter(e) { | ||
return "DIV" === e.nodeName && !e.attributes.length; | ||
}, replacement: function replacement(e) { | ||
return e; | ||
} }), this.init = function () { | ||
if (this.base.elements && this.base.elements.length) { | ||
this.element = this.base.elements[0];var i = /[\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF]*$/, | ||
u = function () { | ||
for (var u = this.element.cloneNode(!0), o = u.querySelectorAll("ul, ol"), s = 0; s < o.length; ++s) { | ||
t(o[s]); | ||
}var c = new TurndownService(e.toTurndownOptions);r.customRules.forEach(function (e) { | ||
c.addRule(e.key, { filter: e.filter, replacement: e.replacement }); | ||
}), _n(c.turndown(u.innerHTML).split("\n").map(function (e) { | ||
return e.replace(i, ""); | ||
}).join("\n").replace(i, "")); | ||
}.bind(this);e.subscribeToMeEditableInput ? this.base.subscribe("editableInput", u) : e.events.forEach(function (e) { | ||
this.element.addEventListener(e, u); | ||
}.bind(this)), u(); | ||
} | ||
}; | ||
};e.MeMarkdown = n; | ||
}(undefined); |
@@ -0,782 +1,742 @@ | ||
"use strict"; | ||
(function (root) { | ||
if (typeof MediumEditor !== "function") { | ||
throw new Error("Medium Editor is not loaded on the page."); | ||
if (typeof MediumEditor !== "function") { | ||
throw new Error("Medium Editor is not loaded on the page."); | ||
} | ||
var TurndownService = function () { | ||
'use strict'; | ||
function extend(destination) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (source.hasOwnProperty(key)) destination[key] = source[key]; | ||
} | ||
} | ||
return destination; | ||
} | ||
var TurndownService = (function () { | ||
'use strict'; | ||
function repeat(character, count) { | ||
return Array(count + 1).join(character); | ||
} | ||
function extend (destination) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (source.hasOwnProperty(key)) destination[key] = source[key]; | ||
var blockElements = ['address', 'article', 'aside', 'audio', 'blockquote', 'body', 'canvas', 'center', 'dd', 'dir', 'div', 'dl', 'dt', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'hr', 'html', 'isindex', 'li', 'main', 'menu', 'nav', 'noframes', 'noscript', 'ol', 'output', 'p', 'pre', 'section', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'ul']; | ||
function isBlock(node) { | ||
return blockElements.indexOf(node.nodeName.toLowerCase()) !== -1; | ||
} | ||
} | ||
return destination | ||
} | ||
function repeat (character, count) { | ||
return Array(count + 1).join(character) | ||
} | ||
var voidElements = ['area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr']; | ||
var blockElements = [ | ||
'address', 'article', 'aside', 'audio', 'blockquote', 'body', 'canvas', | ||
'center', 'dd', 'dir', 'div', 'dl', 'dt', 'fieldset', 'figcaption', | ||
'figure', 'footer', 'form', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', | ||
'header', 'hgroup', 'hr', 'html', 'isindex', 'li', 'main', 'menu', 'nav', | ||
'noframes', 'noscript', 'ol', 'output', 'p', 'pre', 'section', 'table', | ||
'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'ul' | ||
]; | ||
function isVoid(node) { | ||
return voidElements.indexOf(node.nodeName.toLowerCase()) !== -1; | ||
} | ||
function isBlock (node) { | ||
return blockElements.indexOf(node.nodeName.toLowerCase()) !== -1 | ||
} | ||
var voidSelector = voidElements.join(); | ||
function hasVoid(node) { | ||
return node.querySelector && node.querySelector(voidSelector); | ||
} | ||
var voidElements = [ | ||
'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', | ||
'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr' | ||
]; | ||
var rules = {}; | ||
function isVoid (node) { | ||
return voidElements.indexOf(node.nodeName.toLowerCase()) !== -1 | ||
} | ||
rules.paragraph = { | ||
filter: 'p', | ||
var voidSelector = voidElements.join(); | ||
function hasVoid (node) { | ||
return node.querySelector && node.querySelector(voidSelector) | ||
} | ||
replacement: function replacement(content) { | ||
return '\n\n' + content + '\n\n'; | ||
} | ||
}; | ||
var rules = {}; | ||
rules.lineBreak = { | ||
filter: 'br', | ||
rules.paragraph = { | ||
filter: 'p', | ||
replacement: function replacement(content, node, options) { | ||
return options.br + '\n'; | ||
} | ||
}; | ||
replacement: function (content) { | ||
return '\n\n' + content + '\n\n' | ||
} | ||
}; | ||
rules.heading = { | ||
filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'], | ||
rules.lineBreak = { | ||
filter: 'br', | ||
replacement: function replacement(content, node, options) { | ||
var hLevel = Number(node.nodeName.charAt(1)); | ||
replacement: function (content, node, options) { | ||
return options.br + '\n' | ||
} | ||
}; | ||
if (options.headingStyle === 'setext' && hLevel < 3) { | ||
var underline = repeat(hLevel === 1 ? '=' : '-', content.length); | ||
return '\n\n' + content + '\n' + underline + '\n\n'; | ||
} else { | ||
return '\n\n' + repeat('#', hLevel) + ' ' + content + '\n\n'; | ||
} | ||
} | ||
}; | ||
rules.heading = { | ||
filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'], | ||
rules.blockquote = { | ||
filter: 'blockquote', | ||
replacement: function (content, node, options) { | ||
var hLevel = Number(node.nodeName.charAt(1)); | ||
replacement: function replacement(content) { | ||
content = content.replace(/^\n+|\n+$/g, ''); | ||
content = content.replace(/^/gm, '> '); | ||
return '\n\n' + content + '\n\n'; | ||
} | ||
}; | ||
if (options.headingStyle === 'setext' && hLevel < 3) { | ||
var underline = repeat((hLevel === 1 ? '=' : '-'), content.length); | ||
return ( | ||
'\n\n' + content + '\n' + underline + '\n\n' | ||
) | ||
} else { | ||
return '\n\n' + repeat('#', hLevel) + ' ' + content + '\n\n' | ||
} | ||
} | ||
}; | ||
rules.list = { | ||
filter: ['ul', 'ol'], | ||
rules.blockquote = { | ||
filter: 'blockquote', | ||
replacement: function replacement(content, node) { | ||
var parent = node.parentNode; | ||
if (parent.nodeName === 'LI' && parent.lastElementChild === node) { | ||
return '\n' + content; | ||
} else { | ||
return '\n\n' + content + '\n\n'; | ||
} | ||
} | ||
}; | ||
replacement: function (content) { | ||
content = content.replace(/^\n+|\n+$/g, ''); | ||
content = content.replace(/^/gm, '> '); | ||
return '\n\n' + content + '\n\n' | ||
} | ||
}; | ||
rules.listItem = { | ||
filter: 'li', | ||
rules.list = { | ||
filter: ['ul', 'ol'], | ||
replacement: function replacement(content, node, options) { | ||
content = content.replace(/^\n+/, '') // remove leading newlines | ||
.replace(/\n+$/, '\n') // replace trailing newlines with just a single one | ||
.replace(/\n/gm, '\n '); // indent | ||
var prefix = options.bulletListMarker + ' '; | ||
var parent = node.parentNode; | ||
if (parent.nodeName === 'OL') { | ||
var start = parent.getAttribute('start'); | ||
var index = Array.prototype.indexOf.call(parent.children, node); | ||
prefix = (start ? Number(start) + index : index + 1) + '. '; | ||
} | ||
return prefix + content + (node.nextSibling && !/\n$/.test(content) ? '\n' : ''); | ||
} | ||
}; | ||
replacement: function (content, node) { | ||
var parent = node.parentNode; | ||
if (parent.nodeName === 'LI' && parent.lastElementChild === node) { | ||
return '\n' + content | ||
} else { | ||
return '\n\n' + content + '\n\n' | ||
} | ||
} | ||
}; | ||
rules.indentedCodeBlock = { | ||
filter: function filter(node, options) { | ||
return options.codeBlockStyle === 'indented' && node.nodeName === 'PRE' && node.firstChild && node.firstChild.nodeName === 'CODE'; | ||
}, | ||
rules.listItem = { | ||
filter: 'li', | ||
replacement: function replacement(content, node, options) { | ||
return '\n\n ' + node.firstChild.textContent.replace(/\n/g, '\n ') + '\n\n'; | ||
} | ||
}; | ||
replacement: function (content, node, options) { | ||
content = content | ||
.replace(/^\n+/, '') // remove leading newlines | ||
.replace(/\n+$/, '\n') // replace trailing newlines with just a single one | ||
.replace(/\n/gm, '\n '); // indent | ||
var prefix = options.bulletListMarker + ' '; | ||
var parent = node.parentNode; | ||
if (parent.nodeName === 'OL') { | ||
var start = parent.getAttribute('start'); | ||
var index = Array.prototype.indexOf.call(parent.children, node); | ||
prefix = (start ? Number(start) + index : index + 1) + '. '; | ||
} | ||
return ( | ||
prefix + content + (node.nextSibling && !/\n$/.test(content) ? '\n' : '') | ||
) | ||
} | ||
}; | ||
rules.fencedCodeBlock = { | ||
filter: function filter(node, options) { | ||
return options.codeBlockStyle === 'fenced' && node.nodeName === 'PRE' && node.firstChild && node.firstChild.nodeName === 'CODE'; | ||
}, | ||
rules.indentedCodeBlock = { | ||
filter: function (node, options) { | ||
return ( | ||
options.codeBlockStyle === 'indented' && | ||
node.nodeName === 'PRE' && | ||
node.firstChild && | ||
node.firstChild.nodeName === 'CODE' | ||
) | ||
}, | ||
replacement: function replacement(content, node, options) { | ||
var className = node.firstChild.className || ''; | ||
var language = (className.match(/language-(\S+)/) || [null, ''])[1]; | ||
replacement: function (content, node, options) { | ||
return ( | ||
'\n\n ' + | ||
node.firstChild.textContent.replace(/\n/g, '\n ') + | ||
'\n\n' | ||
) | ||
} | ||
}; | ||
return '\n\n' + options.fence + language + '\n' + node.firstChild.textContent + '\n' + options.fence + '\n\n'; | ||
} | ||
}; | ||
rules.fencedCodeBlock = { | ||
filter: function (node, options) { | ||
return ( | ||
options.codeBlockStyle === 'fenced' && | ||
node.nodeName === 'PRE' && | ||
node.firstChild && | ||
node.firstChild.nodeName === 'CODE' | ||
) | ||
}, | ||
rules.horizontalRule = { | ||
filter: 'hr', | ||
replacement: function (content, node, options) { | ||
var className = node.firstChild.className || ''; | ||
var language = (className.match(/language-(\S+)/) || [null, ''])[1]; | ||
replacement: function replacement(content, node, options) { | ||
return '\n\n' + options.hr + '\n\n'; | ||
} | ||
}; | ||
return ( | ||
'\n\n' + options.fence + language + '\n' + | ||
node.firstChild.textContent + | ||
'\n' + options.fence + '\n\n' | ||
) | ||
} | ||
}; | ||
rules.inlineLink = { | ||
filter: function filter(node, options) { | ||
return options.linkStyle === 'inlined' && node.nodeName === 'A' && node.getAttribute('href'); | ||
}, | ||
rules.horizontalRule = { | ||
filter: 'hr', | ||
replacement: function replacement(content, node) { | ||
var href = node.getAttribute('href'); | ||
var title = node.title ? ' "' + node.title + '"' : ''; | ||
return '[' + content + '](' + href + title + ')'; | ||
} | ||
}; | ||
replacement: function (content, node, options) { | ||
return '\n\n' + options.hr + '\n\n' | ||
} | ||
}; | ||
rules.referenceLink = { | ||
filter: function filter(node, options) { | ||
return options.linkStyle === 'referenced' && node.nodeName === 'A' && node.getAttribute('href'); | ||
}, | ||
rules.inlineLink = { | ||
filter: function (node, options) { | ||
return ( | ||
options.linkStyle === 'inlined' && | ||
node.nodeName === 'A' && | ||
node.getAttribute('href') | ||
) | ||
}, | ||
replacement: function replacement(content, node, options) { | ||
var href = node.getAttribute('href'); | ||
var title = node.title ? ' "' + node.title + '"' : ''; | ||
var replacement; | ||
var reference; | ||
replacement: function (content, node) { | ||
var href = node.getAttribute('href'); | ||
var title = node.title ? ' "' + node.title + '"' : ''; | ||
return '[' + content + '](' + href + title + ')' | ||
} | ||
}; | ||
switch (options.linkReferenceStyle) { | ||
case 'collapsed': | ||
replacement = '[' + content + '][]'; | ||
reference = '[' + content + ']: ' + href + title; | ||
break; | ||
case 'shortcut': | ||
replacement = '[' + content + ']'; | ||
reference = '[' + content + ']: ' + href + title; | ||
break; | ||
default: | ||
var id = this.references.length + 1; | ||
replacement = '[' + content + '][' + id + ']'; | ||
reference = '[' + id + ']: ' + href + title; | ||
} | ||
rules.referenceLink = { | ||
filter: function (node, options) { | ||
return ( | ||
options.linkStyle === 'referenced' && | ||
node.nodeName === 'A' && | ||
node.getAttribute('href') | ||
) | ||
}, | ||
this.references.push(reference); | ||
return replacement; | ||
}, | ||
replacement: function (content, node, options) { | ||
var href = node.getAttribute('href'); | ||
var title = node.title ? ' "' + node.title + '"' : ''; | ||
var replacement; | ||
var reference; | ||
references: [], | ||
switch (options.linkReferenceStyle) { | ||
case 'collapsed': | ||
replacement = '[' + content + '][]'; | ||
reference = '[' + content + ']: ' + href + title; | ||
break | ||
case 'shortcut': | ||
replacement = '[' + content + ']'; | ||
reference = '[' + content + ']: ' + href + title; | ||
break | ||
default: | ||
var id = this.references.length + 1; | ||
replacement = '[' + content + '][' + id + ']'; | ||
reference = '[' + id + ']: ' + href + title; | ||
} | ||
append: function append(options) { | ||
var references = ''; | ||
if (this.references.length) { | ||
references = '\n\n' + this.references.join('\n') + '\n\n'; | ||
this.references = []; // Reset references | ||
} | ||
return references; | ||
} | ||
}; | ||
this.references.push(reference); | ||
return replacement | ||
}, | ||
rules.emphasis = { | ||
filter: ['em', 'i'], | ||
references: [], | ||
replacement: function replacement(content, node, options) { | ||
if (!content.trim()) return ''; | ||
return options.emDelimiter + content + options.emDelimiter; | ||
} | ||
}; | ||
append: function (options) { | ||
var references = ''; | ||
if (this.references.length) { | ||
references = '\n\n' + this.references.join('\n') + '\n\n'; | ||
this.references = []; // Reset references | ||
} | ||
return references | ||
} | ||
}; | ||
rules.strong = { | ||
filter: ['strong', 'b'], | ||
rules.emphasis = { | ||
filter: ['em', 'i'], | ||
replacement: function replacement(content, node, options) { | ||
if (!content.trim()) return ''; | ||
return options.strongDelimiter + content + options.strongDelimiter; | ||
} | ||
}; | ||
replacement: function (content, node, options) { | ||
if (!content.trim()) return '' | ||
return options.emDelimiter + content + options.emDelimiter | ||
} | ||
}; | ||
rules.code = { | ||
filter: function filter(node) { | ||
var hasSiblings = node.previousSibling || node.nextSibling; | ||
var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings; | ||
rules.strong = { | ||
filter: ['strong', 'b'], | ||
return node.nodeName === 'CODE' && !isCodeBlock; | ||
}, | ||
replacement: function (content, node, options) { | ||
if (!content.trim()) return '' | ||
return options.strongDelimiter + content + options.strongDelimiter | ||
} | ||
}; | ||
replacement: function replacement(content) { | ||
if (!content.trim()) return ''; | ||
rules.code = { | ||
filter: function (node) { | ||
var hasSiblings = node.previousSibling || node.nextSibling; | ||
var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings; | ||
var delimiter = '`'; | ||
var leadingSpace = ''; | ||
var trailingSpace = ''; | ||
var matches = content.match(/`+/gm); | ||
if (matches) { | ||
if (/^`/.test(content)) leadingSpace = ' '; | ||
if (/`$/.test(content)) trailingSpace = ' '; | ||
while (matches.indexOf(delimiter) !== -1) { | ||
delimiter = delimiter + '`'; | ||
} | ||
} | ||
return node.nodeName === 'CODE' && !isCodeBlock | ||
}, | ||
return delimiter + leadingSpace + content + trailingSpace + delimiter; | ||
} | ||
}; | ||
replacement: function (content) { | ||
if (!content.trim()) return '' | ||
rules.image = { | ||
filter: 'img', | ||
var delimiter = '`'; | ||
var leadingSpace = ''; | ||
var trailingSpace = ''; | ||
var matches = content.match(/`+/gm); | ||
if (matches) { | ||
if (/^`/.test(content)) leadingSpace = ' '; | ||
if (/`$/.test(content)) trailingSpace = ' '; | ||
while (matches.indexOf(delimiter) !== -1) delimiter = delimiter + '`'; | ||
} | ||
replacement: function replacement(content, node) { | ||
var alt = node.alt || ''; | ||
var src = node.getAttribute('src') || ''; | ||
var title = node.title || ''; | ||
var titlePart = title ? ' "' + title + '"' : ''; | ||
return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : ''; | ||
} | ||
}; | ||
return delimiter + leadingSpace + content + trailingSpace + delimiter | ||
} | ||
}; | ||
/** | ||
* Manages a collection of rules used to convert HTML to Markdown | ||
*/ | ||
rules.image = { | ||
filter: 'img', | ||
function Rules(options) { | ||
this.options = options; | ||
this._keep = []; | ||
this._remove = []; | ||
replacement: function (content, node) { | ||
var alt = node.alt || ''; | ||
var src = node.getAttribute('src') || ''; | ||
var title = node.title || ''; | ||
var titlePart = title ? ' "' + title + '"' : ''; | ||
return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : '' | ||
} | ||
}; | ||
this.blankRule = { | ||
replacement: options.blankReplacement | ||
}; | ||
/** | ||
* Manages a collection of rules used to convert HTML to Markdown | ||
*/ | ||
this.keepReplacement = options.keepReplacement; | ||
function Rules (options) { | ||
this.options = options; | ||
this._keep = []; | ||
this._remove = []; | ||
this.defaultRule = { | ||
replacement: options.defaultReplacement | ||
}; | ||
this.blankRule = { | ||
replacement: options.blankReplacement | ||
}; | ||
this.array = []; | ||
for (var key in options.rules) { | ||
this.array.push(options.rules[key]); | ||
} | ||
} | ||
this.keepReplacement = options.keepReplacement; | ||
Rules.prototype = { | ||
add: function add(key, rule) { | ||
this.array.unshift(rule); | ||
}, | ||
this.defaultRule = { | ||
replacement: options.defaultReplacement | ||
}; | ||
keep: function keep(filter) { | ||
this._keep.unshift({ | ||
filter: filter, | ||
replacement: this.keepReplacement | ||
}); | ||
}, | ||
this.array = []; | ||
for (var key in options.rules) this.array.push(options.rules[key]); | ||
} | ||
remove: function remove(filter) { | ||
this._remove.unshift({ | ||
filter: filter, | ||
replacement: function replacement() { | ||
return ''; | ||
} | ||
}); | ||
}, | ||
Rules.prototype = { | ||
add: function (key, rule) { | ||
this.array.unshift(rule); | ||
}, | ||
forNode: function forNode(node) { | ||
if (node.isBlank) return this.blankRule; | ||
var rule; | ||
keep: function (filter) { | ||
this._keep.unshift({ | ||
filter: filter, | ||
replacement: this.keepReplacement | ||
}); | ||
}, | ||
if (rule = findRule(this.array, node, this.options)) return rule; | ||
if (rule = findRule(this._keep, node, this.options)) return rule; | ||
if (rule = findRule(this._remove, node, this.options)) return rule; | ||
remove: function (filter) { | ||
this._remove.unshift({ | ||
filter: filter, | ||
replacement: function () { | ||
return '' | ||
return this.defaultRule; | ||
}, | ||
forEach: function forEach(fn) { | ||
for (var i = 0; i < this.array.length; i++) { | ||
fn(this.array[i], i); | ||
} | ||
} | ||
}); | ||
}, | ||
}; | ||
forNode: function (node) { | ||
if (node.isBlank) return this.blankRule | ||
var rule; | ||
function findRule(rules, node, options) { | ||
for (var i = 0; i < rules.length; i++) { | ||
var rule = rules[i]; | ||
if (filterValue(rule, node, options)) return rule; | ||
} | ||
return void 0; | ||
} | ||
if ((rule = findRule(this.array, node, this.options))) return rule | ||
if ((rule = findRule(this._keep, node, this.options))) return rule | ||
if ((rule = findRule(this._remove, node, this.options))) return rule | ||
function filterValue(rule, node, options) { | ||
var filter = rule.filter; | ||
if (typeof filter === 'string') { | ||
if (filter === node.nodeName.toLowerCase()) return true; | ||
} else if (Array.isArray(filter)) { | ||
if (filter.indexOf(node.nodeName.toLowerCase()) > -1) return true; | ||
} else if (typeof filter === 'function') { | ||
if (filter.call(rule, node, options)) return true; | ||
} else { | ||
throw new TypeError('`filter` needs to be a string, array, or function'); | ||
} | ||
} | ||
return this.defaultRule | ||
}, | ||
/** | ||
* The collapseWhitespace function is adapted from collapse-whitespace | ||
* by Luc Thevenard. | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2014 Luc Thevenard <lucthevenard@gmail.com> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
forEach: function (fn) { | ||
for (var i = 0; i < this.array.length; i++) fn(this.array[i], i); | ||
} | ||
}; | ||
/** | ||
* collapseWhitespace(options) removes extraneous whitespace from an the given element. | ||
* | ||
* @param {Object} options | ||
*/ | ||
function collapseWhitespace(options) { | ||
var element = options.element; | ||
var isBlock = options.isBlock; | ||
var isVoid = options.isVoid; | ||
var isPre = options.isPre || function (node) { | ||
return node.nodeName === 'PRE'; | ||
}; | ||
function findRule (rules, node, options) { | ||
for (var i = 0; i < rules.length; i++) { | ||
var rule = rules[i]; | ||
if (filterValue(rule, node, options)) return rule | ||
} | ||
return void 0 | ||
} | ||
if (!element.firstChild || isPre(element)) return; | ||
function filterValue (rule, node, options) { | ||
var filter = rule.filter; | ||
if (typeof filter === 'string') { | ||
if (filter === node.nodeName.toLowerCase()) return true | ||
} else if (Array.isArray(filter)) { | ||
if (filter.indexOf(node.nodeName.toLowerCase()) > -1) return true | ||
} else if (typeof filter === 'function') { | ||
if (filter.call(rule, node, options)) return true | ||
} else { | ||
throw new TypeError('`filter` needs to be a string, array, or function') | ||
} | ||
} | ||
var prevText = null; | ||
var prevVoid = false; | ||
/** | ||
* The collapseWhitespace function is adapted from collapse-whitespace | ||
* by Luc Thevenard. | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2014 Luc Thevenard <lucthevenard@gmail.com> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
var prev = null; | ||
var node = next(prev, element, isPre); | ||
/** | ||
* collapseWhitespace(options) removes extraneous whitespace from an the given element. | ||
* | ||
* @param {Object} options | ||
*/ | ||
function collapseWhitespace (options) { | ||
var element = options.element; | ||
var isBlock = options.isBlock; | ||
var isVoid = options.isVoid; | ||
var isPre = options.isPre || function (node) { | ||
return node.nodeName === 'PRE' | ||
}; | ||
while (node !== element) { | ||
if (node.nodeType === 3 || node.nodeType === 4) { | ||
// Node.TEXT_NODE or Node.CDATA_SECTION_NODE | ||
var text = node.data.replace(/[ \r\n\t]+/g, ' '); | ||
if (!element.firstChild || isPre(element)) return | ||
if ((!prevText || / $/.test(prevText.data)) && !prevVoid && text[0] === ' ') { | ||
text = text.substr(1); | ||
} | ||
var prevText = null; | ||
var prevVoid = false; | ||
// `text` might be empty at this point. | ||
if (!text) { | ||
node = remove(node); | ||
continue; | ||
} | ||
var prev = null; | ||
var node = next(prev, element, isPre); | ||
node.data = text; | ||
while (node !== element) { | ||
if (node.nodeType === 3 || node.nodeType === 4) { // Node.TEXT_NODE or Node.CDATA_SECTION_NODE | ||
var text = node.data.replace(/[ \r\n\t]+/g, ' '); | ||
prevText = node; | ||
} else if (node.nodeType === 1) { | ||
// Node.ELEMENT_NODE | ||
if (isBlock(node) || node.nodeName === 'BR') { | ||
if (prevText) { | ||
prevText.data = prevText.data.replace(/ $/, ''); | ||
} | ||
if ((!prevText || / $/.test(prevText.data)) && | ||
!prevVoid && text[0] === ' ') { | ||
text = text.substr(1); | ||
} | ||
prevText = null; | ||
prevVoid = false; | ||
} else if (isVoid(node)) { | ||
// Avoid trimming space around non-block, non-BR void elements. | ||
prevText = null; | ||
prevVoid = true; | ||
} | ||
} else { | ||
node = remove(node); | ||
continue; | ||
} | ||
// `text` might be empty at this point. | ||
if (!text) { | ||
node = remove(node); | ||
continue | ||
var nextNode = next(prev, node, isPre); | ||
prev = node; | ||
node = nextNode; | ||
} | ||
node.data = text; | ||
prevText = node; | ||
} else if (node.nodeType === 1) { // Node.ELEMENT_NODE | ||
if (isBlock(node) || node.nodeName === 'BR') { | ||
if (prevText) { | ||
prevText.data = prevText.data.replace(/ $/, ''); | ||
if (prevText) { | ||
prevText.data = prevText.data.replace(/ $/, ''); | ||
if (!prevText.data) { | ||
remove(prevText); | ||
} | ||
prevText = null; | ||
prevVoid = false; | ||
} else if (isVoid(node)) { | ||
// Avoid trimming space around non-block, non-BR void elements. | ||
prevText = null; | ||
prevVoid = true; | ||
} | ||
} else { | ||
node = remove(node); | ||
continue | ||
} | ||
var nextNode = next(prev, node, isPre); | ||
prev = node; | ||
node = nextNode; | ||
} | ||
/** | ||
* remove(node) removes the given node from the DOM and returns the | ||
* next node in the sequence. | ||
* | ||
* @param {Node} node | ||
* @return {Node} node | ||
*/ | ||
function remove(node) { | ||
var next = node.nextSibling || node.parentNode; | ||
if (prevText) { | ||
prevText.data = prevText.data.replace(/ $/, ''); | ||
if (!prevText.data) { | ||
remove(prevText); | ||
node.parentNode.removeChild(node); | ||
return next; | ||
} | ||
} | ||
} | ||
/** | ||
* remove(node) removes the given node from the DOM and returns the | ||
* next node in the sequence. | ||
* | ||
* @param {Node} node | ||
* @return {Node} node | ||
*/ | ||
function remove (node) { | ||
var next = node.nextSibling || node.parentNode; | ||
/** | ||
* next(prev, current, isPre) returns the next node in the sequence, given the | ||
* current and previous nodes. | ||
* | ||
* @param {Node} prev | ||
* @param {Node} current | ||
* @param {Function} isPre | ||
* @return {Node} | ||
*/ | ||
function next(prev, current, isPre) { | ||
if (prev && prev.parentNode === current || isPre(current)) { | ||
return current.nextSibling || current.parentNode; | ||
} | ||
node.parentNode.removeChild(node); | ||
return current.firstChild || current.nextSibling || current.parentNode; | ||
} | ||
return next | ||
} | ||
/* | ||
* Set up window for Node.js | ||
*/ | ||
/** | ||
* next(prev, current, isPre) returns the next node in the sequence, given the | ||
* current and previous nodes. | ||
* | ||
* @param {Node} prev | ||
* @param {Node} current | ||
* @param {Function} isPre | ||
* @return {Node} | ||
*/ | ||
function next (prev, current, isPre) { | ||
if ((prev && prev.parentNode === current) || isPre(current)) { | ||
return current.nextSibling || current.parentNode | ||
} | ||
var root = typeof window !== 'undefined' ? window : {}; | ||
return current.firstChild || current.nextSibling || current.parentNode | ||
} | ||
/* | ||
* Parsing HTML strings | ||
*/ | ||
/* | ||
* Set up window for Node.js | ||
*/ | ||
function canParseHTMLNatively() { | ||
var Parser = root.DOMParser; | ||
var canParse = false; | ||
var root = (typeof window !== 'undefined' ? window : {}); | ||
// Adapted from https://gist.github.com/1129031 | ||
// Firefox/Opera/IE throw errors on unsupported types | ||
try { | ||
// WebKit returns null on unsupported types | ||
if (new Parser().parseFromString('', 'text/html')) { | ||
canParse = true; | ||
} | ||
} catch (e) {} | ||
/* | ||
* Parsing HTML strings | ||
*/ | ||
return canParse; | ||
} | ||
function canParseHTMLNatively () { | ||
var Parser = root.DOMParser; | ||
var canParse = false; | ||
function createHTMLParser() { | ||
var Parser = function Parser() {}; | ||
// Adapted from https://gist.github.com/1129031 | ||
// Firefox/Opera/IE throw errors on unsupported types | ||
try { | ||
// WebKit returns null on unsupported types | ||
if (new Parser().parseFromString('', 'text/html')) { | ||
canParse = true; | ||
{ | ||
if (shouldUseActiveX()) { | ||
Parser.prototype.parseFromString = function (string) { | ||
var doc = new window.ActiveXObject('htmlfile'); | ||
doc.designMode = 'on'; // disable on-page scripts | ||
doc.open(); | ||
doc.write(string); | ||
doc.close(); | ||
return doc; | ||
}; | ||
} else { | ||
Parser.prototype.parseFromString = function (string) { | ||
var doc = document.implementation.createHTMLDocument(''); | ||
doc.open(); | ||
doc.write(string); | ||
doc.close(); | ||
return doc; | ||
}; | ||
} | ||
} | ||
return Parser; | ||
} | ||
} catch (e) {} | ||
return canParse | ||
} | ||
function createHTMLParser () { | ||
var Parser = function () {}; | ||
{ | ||
if (shouldUseActiveX()) { | ||
Parser.prototype.parseFromString = function (string) { | ||
var doc = new window.ActiveXObject('htmlfile'); | ||
doc.designMode = 'on'; // disable on-page scripts | ||
doc.open(); | ||
doc.write(string); | ||
doc.close(); | ||
return doc | ||
}; | ||
} else { | ||
Parser.prototype.parseFromString = function (string) { | ||
var doc = document.implementation.createHTMLDocument(''); | ||
doc.open(); | ||
doc.write(string); | ||
doc.close(); | ||
return doc | ||
}; | ||
function shouldUseActiveX() { | ||
var useActiveX = false; | ||
try { | ||
document.implementation.createHTMLDocument('').open(); | ||
} catch (e) { | ||
if (window.ActiveXObject) useActiveX = true; | ||
} | ||
return useActiveX; | ||
} | ||
} | ||
return Parser | ||
} | ||
function shouldUseActiveX () { | ||
var useActiveX = false; | ||
try { | ||
document.implementation.createHTMLDocument('').open(); | ||
} catch (e) { | ||
if (window.ActiveXObject) useActiveX = true; | ||
} | ||
return useActiveX | ||
} | ||
var HTMLParser = canParseHTMLNatively() ? root.DOMParser : createHTMLParser(); | ||
var HTMLParser = canParseHTMLNatively() ? root.DOMParser : createHTMLParser(); | ||
function RootNode(input) { | ||
var root; | ||
if (typeof input === 'string') { | ||
var doc = htmlParser().parseFromString( | ||
// DOM parsers arrange elements in the <head> and <body>. | ||
// Wrapping in a custom element ensures elements are reliably arranged in | ||
// a single element. | ||
'<x-turndown id="turndown-root">' + input + '</x-turndown>', 'text/html'); | ||
root = doc.getElementById('turndown-root'); | ||
} else { | ||
root = input.cloneNode(true); | ||
} | ||
collapseWhitespace({ | ||
element: root, | ||
isBlock: isBlock, | ||
isVoid: isVoid | ||
}); | ||
function RootNode (input) { | ||
var root; | ||
if (typeof input === 'string') { | ||
var doc = htmlParser().parseFromString( | ||
// DOM parsers arrange elements in the <head> and <body>. | ||
// Wrapping in a custom element ensures elements are reliably arranged in | ||
// a single element. | ||
'<x-turndown id="turndown-root">' + input + '</x-turndown>', | ||
'text/html' | ||
); | ||
root = doc.getElementById('turndown-root'); | ||
} else { | ||
root = input.cloneNode(true); | ||
} | ||
collapseWhitespace({ | ||
element: root, | ||
isBlock: isBlock, | ||
isVoid: isVoid | ||
}); | ||
return root; | ||
} | ||
return root | ||
} | ||
var _htmlParser; | ||
function htmlParser() { | ||
_htmlParser = _htmlParser || new HTMLParser(); | ||
return _htmlParser; | ||
} | ||
var _htmlParser; | ||
function htmlParser () { | ||
_htmlParser = _htmlParser || new HTMLParser(); | ||
return _htmlParser | ||
} | ||
function Node(node) { | ||
node.isBlock = isBlock(node); | ||
node.isCode = node.nodeName.toLowerCase() === 'code' || node.parentNode.isCode; | ||
node.isBlank = isBlank(node); | ||
node.flankingWhitespace = flankingWhitespace(node); | ||
return node; | ||
} | ||
function Node (node) { | ||
node.isBlock = isBlock(node); | ||
node.isCode = node.nodeName.toLowerCase() === 'code' || node.parentNode.isCode; | ||
node.isBlank = isBlank(node); | ||
node.flankingWhitespace = flankingWhitespace(node); | ||
return node | ||
} | ||
function isBlank(node) { | ||
return ['A', 'TH', 'TD'].indexOf(node.nodeName) === -1 && /^\s*$/i.test(node.textContent) && !isVoid(node) && !hasVoid(node); | ||
} | ||
function isBlank (node) { | ||
return ( | ||
['A', 'TH', 'TD'].indexOf(node.nodeName) === -1 && | ||
/^\s*$/i.test(node.textContent) && | ||
!isVoid(node) && | ||
!hasVoid(node) | ||
) | ||
} | ||
function flankingWhitespace(node) { | ||
var leading = ''; | ||
var trailing = ''; | ||
function flankingWhitespace (node) { | ||
var leading = ''; | ||
var trailing = ''; | ||
if (!node.isBlock) { | ||
var hasLeading = /^[ \r\n\t]/.test(node.textContent); | ||
var hasTrailing = /[ \r\n\t]$/.test(node.textContent); | ||
if (!node.isBlock) { | ||
var hasLeading = /^[ \r\n\t]/.test(node.textContent); | ||
var hasTrailing = /[ \r\n\t]$/.test(node.textContent); | ||
if (hasLeading && !isFlankedByWhitespace('left', node)) { | ||
leading = ' '; | ||
} | ||
if (hasTrailing && !isFlankedByWhitespace('right', node)) { | ||
trailing = ' '; | ||
} | ||
} | ||
if (hasLeading && !isFlankedByWhitespace('left', node)) { | ||
leading = ' '; | ||
return { leading: leading, trailing: trailing }; | ||
} | ||
if (hasTrailing && !isFlankedByWhitespace('right', node)) { | ||
trailing = ' '; | ||
} | ||
} | ||
return { leading: leading, trailing: trailing } | ||
} | ||
function isFlankedByWhitespace(side, node) { | ||
var sibling; | ||
var regExp; | ||
var isFlanked; | ||
function isFlankedByWhitespace (side, node) { | ||
var sibling; | ||
var regExp; | ||
var isFlanked; | ||
if (side === 'left') { | ||
sibling = node.previousSibling; | ||
regExp = / $/; | ||
} else { | ||
sibling = node.nextSibling; | ||
regExp = /^ /; | ||
} | ||
if (side === 'left') { | ||
sibling = node.previousSibling; | ||
regExp = / $/; | ||
} else { | ||
sibling = node.nextSibling; | ||
regExp = /^ /; | ||
} | ||
if (sibling) { | ||
if (sibling.nodeType === 3) { | ||
isFlanked = regExp.test(sibling.nodeValue); | ||
} else if (sibling.nodeType === 1 && !isBlock(sibling)) { | ||
isFlanked = regExp.test(sibling.textContent); | ||
if (sibling) { | ||
if (sibling.nodeType === 3) { | ||
isFlanked = regExp.test(sibling.nodeValue); | ||
} else if (sibling.nodeType === 1 && !isBlock(sibling)) { | ||
isFlanked = regExp.test(sibling.textContent); | ||
} | ||
} | ||
return isFlanked; | ||
} | ||
} | ||
return isFlanked | ||
} | ||
var reduce = Array.prototype.reduce; | ||
var leadingNewLinesRegExp = /^\n*/; | ||
var trailingNewLinesRegExp = /\n*$/; | ||
var reduce = Array.prototype.reduce; | ||
var leadingNewLinesRegExp = /^\n*/; | ||
var trailingNewLinesRegExp = /\n*$/; | ||
function TurndownService (options) { | ||
if (!(this instanceof TurndownService)) return new TurndownService(options) | ||
function TurndownService(options) { | ||
if (!(this instanceof TurndownService)) return new TurndownService(options); | ||
var defaults = { | ||
rules: rules, | ||
headingStyle: 'setext', | ||
hr: '* * *', | ||
bulletListMarker: '*', | ||
codeBlockStyle: 'indented', | ||
fence: '```', | ||
emDelimiter: '_', | ||
strongDelimiter: '**', | ||
linkStyle: 'inlined', | ||
linkReferenceStyle: 'full', | ||
br: ' ', | ||
blankReplacement: function (content, node) { | ||
return node.isBlock ? '\n\n' : '' | ||
}, | ||
keepReplacement: function (content, node) { | ||
return node.isBlock ? '\n\n' + node.outerHTML + '\n\n' : node.outerHTML | ||
}, | ||
defaultReplacement: function (content, node) { | ||
return node.isBlock ? '\n\n' + content + '\n\n' : content | ||
var defaults = { | ||
rules: rules, | ||
headingStyle: 'setext', | ||
hr: '* * *', | ||
bulletListMarker: '*', | ||
codeBlockStyle: 'indented', | ||
fence: '```', | ||
emDelimiter: '_', | ||
strongDelimiter: '**', | ||
linkStyle: 'inlined', | ||
linkReferenceStyle: 'full', | ||
br: ' ', | ||
blankReplacement: function blankReplacement(content, node) { | ||
return node.isBlock ? '\n\n' : ''; | ||
}, | ||
keepReplacement: function keepReplacement(content, node) { | ||
return node.isBlock ? '\n\n' + node.outerHTML + '\n\n' : node.outerHTML; | ||
}, | ||
defaultReplacement: function defaultReplacement(content, node) { | ||
return node.isBlock ? '\n\n' + content + '\n\n' : content; | ||
} | ||
}; | ||
this.options = extend({}, defaults, options); | ||
this.rules = new Rules(this.options); | ||
} | ||
}; | ||
this.options = extend({}, defaults, options); | ||
this.rules = new Rules(this.options); | ||
} | ||
TurndownService.prototype = { | ||
/** | ||
* The entry point for converting a string or DOM node to Markdown | ||
* @public | ||
* @param {String|HTMLElement} input The string or DOM node to convert | ||
* @returns A Markdown representation of the input | ||
* @type String | ||
*/ | ||
TurndownService.prototype = { | ||
/** | ||
* The entry point for converting a string or DOM node to Markdown | ||
* @public | ||
* @param {String|HTMLElement} input The string or DOM node to convert | ||
* @returns A Markdown representation of the input | ||
* @type String | ||
*/ | ||
turndown: function (input) { | ||
if (!canConvert(input)) { | ||
throw new TypeError( | ||
input + ' is not a string, or an element/document/fragment node.' | ||
) | ||
} | ||
turndown: function turndown(input) { | ||
if (!canConvert(input)) { | ||
throw new TypeError(input + ' is not a string, or an element/document/fragment node.'); | ||
} | ||
if (input === '') return '' | ||
if (input === '') return ''; | ||
var output = process.call(this, new RootNode(input)); | ||
return postProcess.call(this, output) | ||
}, | ||
var output = process.call(this, new RootNode(input)); | ||
return postProcess.call(this, output); | ||
}, | ||
/** | ||
* Add one or more plugins | ||
* @public | ||
* @param {Function|Array} plugin The plugin or array of plugins to add | ||
* @returns The Turndown instance for chaining | ||
* @type Object | ||
*/ | ||
/** | ||
* Add one or more plugins | ||
* @public | ||
* @param {Function|Array} plugin The plugin or array of plugins to add | ||
* @returns The Turndown instance for chaining | ||
* @type Object | ||
*/ | ||
use: function (plugin) { | ||
if (Array.isArray(plugin)) { | ||
for (var i = 0; i < plugin.length; i++) this.use(plugin[i]); | ||
} else if (typeof plugin === 'function') { | ||
plugin(this); | ||
} else { | ||
throw new TypeError('plugin must be a Function or an Array of Functions') | ||
} | ||
return this | ||
}, | ||
use: function use(plugin) { | ||
if (Array.isArray(plugin)) { | ||
for (var i = 0; i < plugin.length; i++) { | ||
this.use(plugin[i]); | ||
} | ||
} else if (typeof plugin === 'function') { | ||
plugin(this); | ||
} else { | ||
throw new TypeError('plugin must be a Function or an Array of Functions'); | ||
} | ||
return this; | ||
}, | ||
/** | ||
* Adds a rule | ||
* @public | ||
* @param {String} key The unique key of the rule | ||
* @param {Object} rule The rule | ||
* @returns The Turndown instance for chaining | ||
* @type Object | ||
*/ | ||
/** | ||
* Adds a rule | ||
* @public | ||
* @param {String} key The unique key of the rule | ||
* @param {Object} rule The rule | ||
* @returns The Turndown instance for chaining | ||
* @type Object | ||
*/ | ||
addRule: function (key, rule) { | ||
this.rules.add(key, rule); | ||
return this | ||
}, | ||
addRule: function addRule(key, rule) { | ||
this.rules.add(key, rule); | ||
return this; | ||
}, | ||
/** | ||
* Keep a node (as HTML) that matches the filter | ||
* @public | ||
* @param {String|Array|Function} filter The unique key of the rule | ||
* @returns The Turndown instance for chaining | ||
* @type Object | ||
*/ | ||
/** | ||
* Keep a node (as HTML) that matches the filter | ||
* @public | ||
* @param {String|Array|Function} filter The unique key of the rule | ||
* @returns The Turndown instance for chaining | ||
* @type Object | ||
*/ | ||
keep: function (filter) { | ||
this.rules.keep(filter); | ||
return this | ||
}, | ||
keep: function keep(filter) { | ||
this.rules.keep(filter); | ||
return this; | ||
}, | ||
/** | ||
* Remove a node that matches the filter | ||
* @public | ||
* @param {String|Array|Function} filter The unique key of the rule | ||
* @returns The Turndown instance for chaining | ||
* @type Object | ||
*/ | ||
/** | ||
* Remove a node that matches the filter | ||
* @public | ||
* @param {String|Array|Function} filter The unique key of the rule | ||
* @returns The Turndown instance for chaining | ||
* @type Object | ||
*/ | ||
remove: function (filter) { | ||
this.rules.remove(filter); | ||
return this | ||
}, | ||
remove: function remove(filter) { | ||
this.rules.remove(filter); | ||
return this; | ||
}, | ||
/** | ||
* Escapes Markdown syntax | ||
* @public | ||
* @param {String} string The string to escape | ||
* @returns A string with Markdown syntax escaped | ||
* @type String | ||
*/ | ||
/** | ||
* Escapes Markdown syntax | ||
* @public | ||
* @param {String} string The string to escape | ||
* @returns A string with Markdown syntax escaped | ||
* @type String | ||
*/ | ||
escape: function (string) { | ||
return ( | ||
string | ||
escape: function escape(string) { | ||
return string | ||
// Escape backslash escapes! | ||
@@ -790,3 +750,3 @@ .replace(/\\(\S)/g, '\\\\$1') | ||
.replace(/^([-*_] *){3,}$/gm, function (match, character) { | ||
return match.split(character).join('\\' + character) | ||
return match.split(character).join('\\' + character); | ||
}) | ||
@@ -799,3 +759,3 @@ | ||
.replace(/^([^\\\w]*)[*+-] /gm, function (match) { | ||
return match.replace(/([*+-])/g, '\\$1') | ||
return match.replace(/([*+-])/g, '\\$1'); | ||
}) | ||
@@ -808,3 +768,3 @@ | ||
.replace(/\*+(?![*\s\W]).+?\*+/g, function (match) { | ||
return match.replace(/\*/g, '\\*') | ||
return match.replace(/\*/g, '\\*'); | ||
}) | ||
@@ -814,3 +774,3 @@ | ||
.replace(/_+(?![_\s\W]).+?_+/g, function (match) { | ||
return match.replace(/_/g, '\\_') | ||
return match.replace(/_/g, '\\_'); | ||
}) | ||
@@ -820,3 +780,3 @@ | ||
.replace(/`+(?![`\s\W]).+?`+/g, function (match) { | ||
return match.replace(/`/g, '\\`') | ||
return match.replace(/`/g, '\\`'); | ||
}) | ||
@@ -826,126 +786,110 @@ | ||
.replace(/[\[\]]/g, '\\{{{toMarkdown}}}') // eslint-disable-line no-useless-escape | ||
) | ||
} | ||
}; | ||
; | ||
} | ||
}; | ||
/** | ||
* Reduces a DOM node down to its Markdown string equivalent | ||
* @private | ||
* @param {HTMLElement} parentNode The node to convert | ||
* @returns A Markdown representation of the node | ||
* @type String | ||
*/ | ||
/** | ||
* Reduces a DOM node down to its Markdown string equivalent | ||
* @private | ||
* @param {HTMLElement} parentNode The node to convert | ||
* @returns A Markdown representation of the node | ||
* @type String | ||
*/ | ||
function process (parentNode) { | ||
var self = this; | ||
return reduce.call(parentNode.childNodes, function (output, node) { | ||
node = new Node(node); | ||
function process(parentNode) { | ||
var self = this; | ||
return reduce.call(parentNode.childNodes, function (output, node) { | ||
node = new Node(node); | ||
var replacement = ''; | ||
if (node.nodeType === 3) { | ||
replacement = node.isCode ? node.nodeValue : self.escape(node.nodeValue); | ||
} else if (node.nodeType === 1) { | ||
replacement = replacementForNode.call(self, node); | ||
var replacement = ''; | ||
if (node.nodeType === 3) { | ||
replacement = node.isCode ? node.nodeValue : self.escape(node.nodeValue); | ||
} else if (node.nodeType === 1) { | ||
replacement = replacementForNode.call(self, node); | ||
} | ||
return join(output, replacement); | ||
}, ''); | ||
} | ||
return join(output, replacement) | ||
}, '') | ||
} | ||
/** | ||
* Appends strings as each rule requires and trims the output | ||
* @private | ||
* @param {String} output The conversion output | ||
* @returns A trimmed version of the ouput | ||
* @type String | ||
*/ | ||
/** | ||
* Appends strings as each rule requires and trims the output | ||
* @private | ||
* @param {String} output The conversion output | ||
* @returns A trimmed version of the ouput | ||
* @type String | ||
*/ | ||
function postProcess(output) { | ||
var self = this; | ||
this.rules.forEach(function (rule) { | ||
if (typeof rule.append === 'function') { | ||
output = join(output, rule.append(self.options)); | ||
} | ||
}); | ||
function postProcess (output) { | ||
var self = this; | ||
this.rules.forEach(function (rule) { | ||
if (typeof rule.append === 'function') { | ||
output = join(output, rule.append(self.options)); | ||
return output.replace(/^[\t\r\n]+/, '').replace(/[\t\r\n\s]+$/, ''); | ||
} | ||
}); | ||
return output.replace(/^[\t\r\n]+/, '').replace(/[\t\r\n\s]+$/, '') | ||
} | ||
/** | ||
* Converts an element node to its Markdown equivalent | ||
* @private | ||
* @param {HTMLElement} node The node to convert | ||
* @returns A Markdown representation of the node | ||
* @type String | ||
*/ | ||
/** | ||
* Converts an element node to its Markdown equivalent | ||
* @private | ||
* @param {HTMLElement} node The node to convert | ||
* @returns A Markdown representation of the node | ||
* @type String | ||
*/ | ||
function replacementForNode(node) { | ||
var rule = this.rules.forNode(node); | ||
var content = process.call(this, node); | ||
var whitespace = node.flankingWhitespace; | ||
if (whitespace.leading || whitespace.trailing) content = content.trim(); | ||
return whitespace.leading + rule.replacement(content, node, this.options) + whitespace.trailing; | ||
} | ||
function replacementForNode (node) { | ||
var rule = this.rules.forNode(node); | ||
var content = process.call(this, node); | ||
var whitespace = node.flankingWhitespace; | ||
if (whitespace.leading || whitespace.trailing) content = content.trim(); | ||
return ( | ||
whitespace.leading + | ||
rule.replacement(content, node, this.options) + | ||
whitespace.trailing | ||
) | ||
} | ||
/** | ||
* Determines the new lines between the current output and the replacement | ||
* @private | ||
* @param {String} output The current conversion output | ||
* @param {String} replacement The string to append to the output | ||
* @returns The whitespace to separate the current output and the replacement | ||
* @type String | ||
*/ | ||
/** | ||
* Determines the new lines between the current output and the replacement | ||
* @private | ||
* @param {String} output The current conversion output | ||
* @param {String} replacement The string to append to the output | ||
* @returns The whitespace to separate the current output and the replacement | ||
* @type String | ||
*/ | ||
function separatingNewlines(output, replacement) { | ||
var newlines = [output.match(trailingNewLinesRegExp)[0], replacement.match(leadingNewLinesRegExp)[0]].sort(); | ||
var maxNewlines = newlines[newlines.length - 1]; | ||
return maxNewlines.length < 2 ? maxNewlines : '\n\n'; | ||
} | ||
function separatingNewlines (output, replacement) { | ||
var newlines = [ | ||
output.match(trailingNewLinesRegExp)[0], | ||
replacement.match(leadingNewLinesRegExp)[0] | ||
].sort(); | ||
var maxNewlines = newlines[newlines.length - 1]; | ||
return maxNewlines.length < 2 ? maxNewlines : '\n\n' | ||
} | ||
function join(string1, string2) { | ||
var separator = separatingNewlines(string1, string2); | ||
function join (string1, string2) { | ||
var separator = separatingNewlines(string1, string2); | ||
// Remove trailing/leading newlines and replace with separator | ||
string1 = string1.replace(trailingNewLinesRegExp, ''); | ||
string2 = string2.replace(leadingNewLinesRegExp, ''); | ||
// Remove trailing/leading newlines and replace with separator | ||
string1 = string1.replace(trailingNewLinesRegExp, ''); | ||
string2 = string2.replace(leadingNewLinesRegExp, ''); | ||
return string1 + separator + string2; | ||
} | ||
return string1 + separator + string2 | ||
} | ||
/** | ||
* Determines whether an input can be converted | ||
* @private | ||
* @param {String|HTMLElement} input Describe this parameter | ||
* @returns Describe what it returns | ||
* @type String|Object|Array|Boolean|Number | ||
*/ | ||
/** | ||
* Determines whether an input can be converted | ||
* @private | ||
* @param {String|HTMLElement} input Describe this parameter | ||
* @returns Describe what it returns | ||
* @type String|Object|Array|Boolean|Number | ||
*/ | ||
function canConvert(input) { | ||
return input != null && (typeof input === 'string' || input.nodeType && (input.nodeType === 1 || input.nodeType === 9 || input.nodeType === 11)); | ||
} | ||
function canConvert (input) { | ||
return ( | ||
input != null && ( | ||
typeof input === 'string' || | ||
(input.nodeType && ( | ||
input.nodeType === 1 || input.nodeType === 9 || input.nodeType === 11 | ||
)) | ||
) | ||
) | ||
} | ||
return TurndownService; | ||
}(); | ||
return TurndownService; | ||
var MeMarkdown = function MeMarkdown(options, callback) { | ||
}()); | ||
var MeMarkdown = function (options, callback) { | ||
if (typeof options === "function") { | ||
callback = options; | ||
options = {}; | ||
callback = options; | ||
options = {}; | ||
} | ||
@@ -958,31 +902,34 @@ | ||
var toTurnDownOptions = options.toTurnDownOptions = Object(options.toTurnDownOptions); | ||
toTurnDownOptions.converters = toTurnDownOptions.converters || []; | ||
var toTurndownOptions = options.toTurndownOptions = Object(options.toTurndownOptions); | ||
toTurndownOptions.converters = toTurndownOptions.converters || []; | ||
toTurndownOptions.customRules = toTurndownOptions.customRules || []; | ||
if (!options.ignoreBuiltInConverters) { | ||
toTurnDownOptions.converters.push({ | ||
filter: function (node) { | ||
return node.nodeName === "DIV" && !node.attributes.length; | ||
} | ||
, replacement: function (content) { | ||
return content; | ||
} | ||
}); | ||
if (!options.ignoreBuiltinConverters) { | ||
toTurndownOptions.converters.push({ | ||
filter: function filter(node) { | ||
return node.nodeName === "DIV" && !node.attributes.length; | ||
}, | ||
replacement: function replacement(content) { | ||
return content; | ||
} | ||
}); | ||
} | ||
function normalizeList ($elm) { | ||
var $children = $elm.children; | ||
for (var i = 0; i < $children.length; ++i) { | ||
var $cChild = $children[i]; | ||
var $br = $cChild.querySelector("br"); | ||
$br && $br.remove(); | ||
!$cChild.innerHTML.trim() && $cChild.remove(); | ||
var $prevChild = $children[i - 1]; | ||
if (/^UL|OL$/.test($cChild.tagName)) { | ||
try { | ||
$prevChild.appendChild($cChild); | ||
} catch (e) { console.warn(e); } | ||
normalizeList($cChild); | ||
} | ||
function normalizeList($elm) { | ||
var $children = $elm.children; | ||
for (var i = 0; i < $children.length; ++i) { | ||
var $cChild = $children[i]; | ||
var $br = $cChild.querySelector("br"); | ||
$br && $br.remove(); | ||
!$cChild.innerHTML.trim() && $cChild.remove(); | ||
var $prevChild = $children[i - 1]; | ||
if (/^UL|OL$/.test($cChild.tagName)) { | ||
try { | ||
$prevChild.appendChild($cChild); | ||
} catch (e) { | ||
console.warn(e); | ||
} | ||
normalizeList($cChild); | ||
} | ||
} | ||
} | ||
@@ -993,34 +940,47 @@ | ||
// If this instance of medium-editor doesn't have any elements, there's nothing for us to do | ||
if (!this.base.elements || !this.base.elements.length) { | ||
return; | ||
// If this instance of medium-editor doesn't have any elements, there's nothing for us to do | ||
if (!this.base.elements || !this.base.elements.length) { | ||
return; | ||
} | ||
// Element(s) that this instance of medium-editor is attached to is/are stored in .elements | ||
this.element = this.base.elements[0]; | ||
// String.prototype.trimRight is non-standard, this should have the same effect | ||
var rightWhitespace = /[\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF]*$/; | ||
var handler = function () { | ||
var $clone = this.element.cloneNode(true); | ||
var $lists = $clone.querySelectorAll("ul, ol"); | ||
for (var i = 0; i < $lists.length; ++i) { | ||
normalizeList($lists[i]); | ||
} | ||
// Element(s) that this instance of medium-editor is attached to is/are stored in .elements | ||
this.element = this.base.elements[0]; | ||
var turndownService = new TurndownService(options.toTurndownOptions); | ||
// String.prototype.trimRight is non-standard, this should have the same effect | ||
var rightWhitespace = /[\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF]*$/; | ||
toTurndownOptions.customRules.forEach(function (customRule) { | ||
turndownService.addRule(customRule.key, { | ||
filter: customRule.filter, | ||
replacement: customRule.replacement | ||
}); | ||
}); | ||
var handler = function () { | ||
var $clone = this.element.cloneNode(true); | ||
var $lists = $clone.querySelectorAll("ul, ol"); | ||
for (var i = 0; i < $lists.length; ++i) { | ||
normalizeList($lists[i]); | ||
} | ||
callback(turndownService.turndown($clone.innerHTML).split("\n").map(function (c) { | ||
return c.replace(rightWhitespace, ''); | ||
}).join("\n").replace(rightWhitespace, '')); | ||
}.bind(this); | ||
callback( new TurndownService(options.toTurnDownOptions).turndown($clone.innerHTML).split("\n").map(function (c) { | ||
return c.replace(rightWhitespace, ''); | ||
}).join("\n").replace(rightWhitespace, '')); | ||
}.bind(this); | ||
if (options.subscribeToMeEditableInput) { | ||
this.base.subscribe('editableInput', handler); | ||
} else { | ||
options.events.forEach(function (c) { | ||
this.element.addEventListener(c, handler); | ||
this.element.addEventListener(c, handler); | ||
}.bind(this)); | ||
} | ||
handler(); | ||
handler(); | ||
}; | ||
}; | ||
}; | ||
root.MeMarkdown = MeMarkdown; | ||
})(this); | ||
root.MeMarkdown = MeMarkdown; | ||
})(undefined); |
@@ -1,1 +0,268 @@ | ||
!function(e){if("function"!=typeof MediumEditor)throw new Error("Medium Editor is not loaded on the page.");var n=function(){"use strict";function e(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])}return e}function n(e,n){return Array(n+1).join(e)}function t(e){return-1!==S.indexOf(e.nodeName.toLowerCase())}function r(e){return-1!==T.indexOf(e.nodeName.toLowerCase())}function i(e){return e.querySelector&&e.querySelector(A)}function o(e){this.options=e,this._keep=[],this._remove=[],this.blankRule={replacement:e.blankReplacement},this.keepReplacement=e.keepReplacement,this.defaultRule={replacement:e.defaultReplacement},this.array=[];for(var n in e.rules)this.array.push(e.rules[n])}function a(e,n,t){for(var r=0;r<e.length;r++){var i=e[r];if(l(i,n,t))return i}}function l(e,n,t){var r=e.filter;if("string"==typeof r){if(r===n.nodeName.toLowerCase())return!0}else if(Array.isArray(r)){if(r.indexOf(n.nodeName.toLowerCase())>-1)return!0}else{if("function"!=typeof r)throw new TypeError("`filter` needs to be a string, array, or function");if(r.call(e,n,t))return!0}}function u(e){var n=e.element,t=e.isBlock,r=e.isVoid,i=e.isPre||function(e){return"PRE"===e.nodeName};if(n.firstChild&&!i(n)){for(var o=null,a=!1,l=null,u=s(l,n,i);u!==n;){if(3===u.nodeType||4===u.nodeType){var f=u.data.replace(/[ \r\n\t]+/g," ");if(o&&!/ $/.test(o.data)||a||" "!==f[0]||(f=f.substr(1)),!f){u=c(u);continue}u.data=f,o=u}else{if(1!==u.nodeType){u=c(u);continue}t(u)||"BR"===u.nodeName?(o&&(o.data=o.data.replace(/ $/,"")),o=null,a=!1):r(u)&&(o=null,a=!0)}var d=s(l,u,i);l=u,u=d}o&&(o.data=o.data.replace(/ $/,""),o.data||c(o))}}function c(e){var n=e.nextSibling||e.parentNode;return e.parentNode.removeChild(e),n}function s(e,n,t){return e&&e.parentNode===n||t(n)?n.nextSibling||n.parentNode:n.firstChild||n.nextSibling||n.parentNode}function f(){var e=!1;try{document.implementation.createHTMLDocument("").open()}catch(n){window.ActiveXObject&&(e=!0)}return e}function d(e){var n;if("string"==typeof e){n=p().parseFromString('<x-turndown id="turndown-root">'+e+"</x-turndown>","text/html").getElementById("turndown-root")}else n=e.cloneNode(!0);return u({element:n,isBlock:t,isVoid:r}),n}function p(){return O=O||new $}function h(e){return e.isBlock=t(e),e.isCode="code"===e.nodeName.toLowerCase()||e.parentNode.isCode,e.isBlank=m(e),e.flankingWhitespace=g(e),e}function m(e){return-1===["A","TH","TD"].indexOf(e.nodeName)&&/^\s*$/i.test(e.textContent)&&!r(e)&&!i(e)}function g(e){var n="",t="";if(!e.isBlock){var r=/^[ \r\n\t]/.test(e.textContent),i=/[ \r\n\t]$/.test(e.textContent);r&&!v("left",e)&&(n=" "),i&&!v("right",e)&&(t=" ")}return{leading:n,trailing:t}}function v(e,n){var r,i,o;return"left"===e?(r=n.previousSibling,i=/ $/):(r=n.nextSibling,i=/^ /),r&&(3===r.nodeType?o=i.test(r.nodeValue):1!==r.nodeType||t(r)||(o=i.test(r.textContent))),o}function y(n){if(!(this instanceof y))return new y(n);var t={rules:E,headingStyle:"setext",hr:"* * *",bulletListMarker:"*",codeBlockStyle:"indented",fence:"```",emDelimiter:"_",strongDelimiter:"**",linkStyle:"inlined",linkReferenceStyle:"full",br:" ",blankReplacement:function(e,n){return n.isBlock?"\n\n":""},keepReplacement:function(e,n){return n.isBlock?"\n\n"+n.outerHTML+"\n\n":n.outerHTML},defaultReplacement:function(e,n){return n.isBlock?"\n\n"+e+"\n\n":e}};this.options=e({},t,n),this.rules=new o(this.options)}function b(e){var n=this;return L.call(e.childNodes,function(e,t){t=new h(t);var r="";return 3===t.nodeType?r=t.isCode?t.nodeValue:n.escape(t.nodeValue):1===t.nodeType&&(r=w.call(n,t)),C(e,r)},"")}function k(e){var n=this;return this.rules.forEach(function(t){"function"==typeof t.append&&(e=C(e,t.append(n.options)))}),e.replace(/^[\t\r\n]+/,"").replace(/[\t\r\n\s]+$/,"")}function w(e){var n=this.rules.forNode(e),t=b.call(this,e),r=e.flankingWhitespace;return(r.leading||r.trailing)&&(t=t.trim()),r.leading+n.replacement(t,e,this.options)+r.trailing}function N(e,n){var t=[e.match(D)[0],n.match(B)[0]].sort(),r=t[t.length-1];return r.length<2?r:"\n\n"}function C(e,n){var t=N(e,n);return e=e.replace(D,""),n=n.replace(B,""),e+t+n}function x(e){return null!=e&&("string"==typeof e||e.nodeType&&(1===e.nodeType||9===e.nodeType||11===e.nodeType))}var S=["address","article","aside","audio","blockquote","body","canvas","center","dd","dir","div","dl","dt","fieldset","figcaption","figure","footer","form","frameset","h1","h2","h3","h4","h5","h6","header","hgroup","hr","html","isindex","li","main","menu","nav","noframes","noscript","ol","output","p","pre","section","table","tbody","td","tfoot","th","thead","tr","ul"],T=["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"],A=T.join(),E={};E.paragraph={filter:"p",replacement:function(e){return"\n\n"+e+"\n\n"}},E.lineBreak={filter:"br",replacement:function(e,n,t){return t.br+"\n"}},E.heading={filter:["h1","h2","h3","h4","h5","h6"],replacement:function(e,t,r){var i=Number(t.nodeName.charAt(1));if("setext"===r.headingStyle&&i<3){return"\n\n"+e+"\n"+n(1===i?"=":"-",e.length)+"\n\n"}return"\n\n"+n("#",i)+" "+e+"\n\n"}},E.blockquote={filter:"blockquote",replacement:function(e){return e=e.replace(/^\n+|\n+$/g,""),"\n\n"+(e=e.replace(/^/gm,"> "))+"\n\n"}},E.list={filter:["ul","ol"],replacement:function(e,n){var t=n.parentNode;return"LI"===t.nodeName&&t.lastElementChild===n?"\n"+e:"\n\n"+e+"\n\n"}},E.listItem={filter:"li",replacement:function(e,n,t){e=e.replace(/^\n+/,"").replace(/\n+$/,"\n").replace(/\n/gm,"\n ");var r=t.bulletListMarker+" ",i=n.parentNode;if("OL"===i.nodeName){var o=i.getAttribute("start"),a=Array.prototype.indexOf.call(i.children,n);r=(o?Number(o)+a:a+1)+". "}return r+e+(n.nextSibling&&!/\n$/.test(e)?"\n":"")}},E.indentedCodeBlock={filter:function(e,n){return"indented"===n.codeBlockStyle&&"PRE"===e.nodeName&&e.firstChild&&"CODE"===e.firstChild.nodeName},replacement:function(e,n,t){return"\n\n "+n.firstChild.textContent.replace(/\n/g,"\n ")+"\n\n"}},E.fencedCodeBlock={filter:function(e,n){return"fenced"===n.codeBlockStyle&&"PRE"===e.nodeName&&e.firstChild&&"CODE"===e.firstChild.nodeName},replacement:function(e,n,t){var r=n.firstChild.className||"",i=(r.match(/language-(\S+)/)||[null,""])[1];return"\n\n"+t.fence+i+"\n"+n.firstChild.textContent+"\n"+t.fence+"\n\n"}},E.horizontalRule={filter:"hr",replacement:function(e,n,t){return"\n\n"+t.hr+"\n\n"}},E.inlineLink={filter:function(e,n){return"inlined"===n.linkStyle&&"A"===e.nodeName&&e.getAttribute("href")},replacement:function(e,n){return"["+e+"]("+n.getAttribute("href")+(n.title?' "'+n.title+'"':"")+")"}},E.referenceLink={filter:function(e,n){return"referenced"===n.linkStyle&&"A"===e.nodeName&&e.getAttribute("href")},replacement:function(e,n,t){var r,i,o=n.getAttribute("href"),a=n.title?' "'+n.title+'"':"";switch(t.linkReferenceStyle){case"collapsed":r="["+e+"][]",i="["+e+"]: "+o+a;break;case"shortcut":r="["+e+"]",i="["+e+"]: "+o+a;break;default:var l=this.references.length+1;r="["+e+"]["+l+"]",i="["+l+"]: "+o+a}return this.references.push(i),r},references:[],append:function(e){var n="";return this.references.length&&(n="\n\n"+this.references.join("\n")+"\n\n",this.references=[]),n}},E.emphasis={filter:["em","i"],replacement:function(e,n,t){return e.trim()?t.emDelimiter+e+t.emDelimiter:""}},E.strong={filter:["strong","b"],replacement:function(e,n,t){return e.trim()?t.strongDelimiter+e+t.strongDelimiter:""}},E.code={filter:function(e){var n=e.previousSibling||e.nextSibling,t="PRE"===e.parentNode.nodeName&&!n;return"CODE"===e.nodeName&&!t},replacement:function(e){if(!e.trim())return"";var n="`",t="",r="",i=e.match(/`+/gm);if(i)for(/^`/.test(e)&&(t=" "),/`$/.test(e)&&(r=" ");-1!==i.indexOf(n);)n+="`";return n+t+e+r+n}},E.image={filter:"img",replacement:function(e,n){var t=n.alt||"",r=n.getAttribute("src")||"",i=n.title||"",o=i?' "'+i+'"':"";return r?"!["+t+"]("+r+o+")":""}},o.prototype={add:function(e,n){this.array.unshift(n)},keep:function(e){this._keep.unshift({filter:e,replacement:this.keepReplacement})},remove:function(e){this._remove.unshift({filter:e,replacement:function(){return""}})},forNode:function(e){if(e.isBlank)return this.blankRule;var n;return(n=a(this.array,e,this.options))?n:(n=a(this._keep,e,this.options))?n:(n=a(this._remove,e,this.options))?n:this.defaultRule},forEach:function(e){for(var n=0;n<this.array.length;n++)e(this.array[n],n)}};var O,R="undefined"!=typeof window?window:{},$=function(){var e=R.DOMParser,n=!1;try{(new e).parseFromString("","text/html")&&(n=!0)}catch(e){}return n}()?R.DOMParser:function(){var e=function(){};return f()?e.prototype.parseFromString=function(e){var n=new window.ActiveXObject("htmlfile");return n.designMode="on",n.open(),n.write(e),n.close(),n}:e.prototype.parseFromString=function(e){var n=document.implementation.createHTMLDocument("");return n.open(),n.write(e),n.close(),n},e}(),L=Array.prototype.reduce,B=/^\n*/,D=/\n*$/;return y.prototype={turndown:function(e){if(!x(e))throw new TypeError(e+" is not a string, or an element/document/fragment node.");if(""===e)return"";var n=b.call(this,new d(e));return k.call(this,n)},use:function(e){if(Array.isArray(e))for(var n=0;n<e.length;n++)this.use(e[n]);else{if("function"!=typeof e)throw new TypeError("plugin must be a Function or an Array of Functions");e(this)}return this},addRule:function(e,n){return this.rules.add(e,n),this},keep:function(e){return this.rules.keep(e),this},remove:function(e){return this.rules.remove(e),this},escape:function(e){return e.replace(/\\(\S)/g,"\\\\$1").replace(/^(#{1,6} )/gm,"\\$1").replace(/^([-*_] *){3,}$/gm,function(e,n){return e.split(n).join("\\"+n)}).replace(/^(\W* {0,3})(\d+)\. /gm,"$1$2\\. ").replace(/^([^\\\w]*)[*+-] /gm,function(e){return e.replace(/([*+-])/g,"\\$1")}).replace(/^(\W* {0,3})> /gm,"$1\\> ").replace(/\*+(?![*\s\W]).+?\*+/g,function(e){return e.replace(/\*/g,"\\*")}).replace(/_+(?![_\s\W]).+?_+/g,function(e){return e.replace(/_/g,"\\_")}).replace(/`+(?![`\s\W]).+?`+/g,function(e){return e.replace(/`/g,"\\`")}).replace(/[\[\]]/g,"\\{{{toMarkdown}}}")}},y}(),t=function(e,t){function r(e){for(var n=e.children,t=0;t<n.length;++t){var i=n[t],o=i.querySelector("br");o&&o.remove(),!i.innerHTML.trim()&&i.remove();var a=n[t-1];if(/^UL|OL$/.test(i.tagName)){try{a.appendChild(i)}catch(e){console.warn(e)}r(i)}}}"function"==typeof e&&(t=e,e={}),e=Object(e),e.events=e.events||["input","change"],t=t||e.callback||function(){};var i=e.toTurnDownOptions=Object(e.toTurnDownOptions);i.converters=i.converters||[],e.ignoreBuiltInConverters||i.converters.push({filter:function(e){return"DIV"===e.nodeName&&!e.attributes.length},replacement:function(e){return e}}),this.init=function(){if(this.base.elements&&this.base.elements.length){this.element=this.base.elements[0];var i=/[\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF]*$/,o=function(){for(var o=this.element.cloneNode(!0),a=o.querySelectorAll("ul, ol"),l=0;l<a.length;++l)r(a[l]);t(new n(e.toTurnDownOptions).turndown(o.innerHTML).split("\n").map(function(e){return e.replace(i,"")}).join("\n").replace(i,""))}.bind(this);e.events.forEach(function(e){this.element.addEventListener(e,o)}.bind(this)),o()}}};e.MeMarkdown=t}(this); | ||
"use strict"; | ||
!function (e) { | ||
if ("function" != typeof MediumEditor) throw new Error("Medium Editor is not loaded on the page.");var n = function () { | ||
"use strict"; | ||
function e(e) { | ||
for (var n = 1; n < arguments.length; n++) { | ||
var t = arguments[n];for (var r in t) { | ||
t.hasOwnProperty(r) && (e[r] = t[r]); | ||
} | ||
}return e; | ||
}function n(e, n) { | ||
return Array(n + 1).join(e); | ||
}function t(e) { | ||
return -1 !== S.indexOf(e.nodeName.toLowerCase()); | ||
}function r(e) { | ||
return -1 !== T.indexOf(e.nodeName.toLowerCase()); | ||
}function i(e) { | ||
return e.querySelector && e.querySelector(R); | ||
}function o(e) { | ||
this.options = e, this._keep = [], this._remove = [], this.blankRule = { replacement: e.blankReplacement }, this.keepReplacement = e.keepReplacement, this.defaultRule = { replacement: e.defaultReplacement }, this.array = [];for (var n in e.rules) { | ||
this.array.push(e.rules[n]); | ||
} | ||
}function a(e, n, t) { | ||
for (var r = 0; r < e.length; r++) { | ||
var i = e[r];if (l(i, n, t)) return i; | ||
} | ||
}function l(e, n, t) { | ||
var r = e.filter;if ("string" == typeof r) { | ||
if (r === n.nodeName.toLowerCase()) return !0; | ||
} else if (Array.isArray(r)) { | ||
if (r.indexOf(n.nodeName.toLowerCase()) > -1) return !0; | ||
} else { | ||
if ("function" != typeof r) throw new TypeError("`filter` needs to be a string, array, or function");if (r.call(e, n, t)) return !0; | ||
} | ||
}function u(e) { | ||
var n = e.element, | ||
t = e.isBlock, | ||
r = e.isVoid, | ||
i = e.isPre || function (e) { | ||
return "PRE" === e.nodeName; | ||
};if (n.firstChild && !i(n)) { | ||
for (var o = null, a = !1, l = null, u = s(l, n, i); u !== n;) { | ||
if (3 === u.nodeType || 4 === u.nodeType) { | ||
var f = u.data.replace(/[ \r\n\t]+/g, " ");if (o && !/ $/.test(o.data) || a || " " !== f[0] || (f = f.substr(1)), !f) { | ||
u = c(u);continue; | ||
}u.data = f, o = u; | ||
} else { | ||
if (1 !== u.nodeType) { | ||
u = c(u);continue; | ||
}t(u) || "BR" === u.nodeName ? (o && (o.data = o.data.replace(/ $/, "")), o = null, a = !1) : r(u) && (o = null, a = !0); | ||
}var d = s(l, u, i);l = u, u = d; | ||
}o && (o.data = o.data.replace(/ $/, ""), o.data || c(o)); | ||
} | ||
}function c(e) { | ||
var n = e.nextSibling || e.parentNode;return e.parentNode.removeChild(e), n; | ||
}function s(e, n, t) { | ||
return e && e.parentNode === n || t(n) ? n.nextSibling || n.parentNode : n.firstChild || n.nextSibling || n.parentNode; | ||
}function f() { | ||
var e = !1;try { | ||
document.implementation.createHTMLDocument("").open(); | ||
} catch (n) { | ||
window.ActiveXObject && (e = !0); | ||
}return e; | ||
}function d(e) { | ||
var n;if ("string" == typeof e) { | ||
n = p().parseFromString('<x-turndown id="turndown-root">' + e + "</x-turndown>", "text/html").getElementById("turndown-root"); | ||
} else n = e.cloneNode(!0);return u({ element: n, isBlock: t, isVoid: r }), n; | ||
}function p() { | ||
return E = E || new $(); | ||
}function h(e) { | ||
return e.isBlock = t(e), e.isCode = "code" === e.nodeName.toLowerCase() || e.parentNode.isCode, e.isBlank = m(e), e.flankingWhitespace = g(e), e; | ||
}function m(e) { | ||
return -1 === ["A", "TH", "TD"].indexOf(e.nodeName) && /^\s*$/i.test(e.textContent) && !r(e) && !i(e); | ||
}function g(e) { | ||
var n = "", | ||
t = "";if (!e.isBlock) { | ||
var r = /^[ \r\n\t]/.test(e.textContent), | ||
i = /[ \r\n\t]$/.test(e.textContent);r && !v("left", e) && (n = " "), i && !v("right", e) && (t = " "); | ||
}return { leading: n, trailing: t }; | ||
}function v(e, n) { | ||
var r, i, o;return "left" === e ? (r = n.previousSibling, i = / $/) : (r = n.nextSibling, i = /^ /), r && (3 === r.nodeType ? o = i.test(r.nodeValue) : 1 !== r.nodeType || t(r) || (o = i.test(r.textContent))), o; | ||
}function y(n) { | ||
if (!(this instanceof y)) return new y(n);var t = { rules: A, headingStyle: "setext", hr: "* * *", bulletListMarker: "*", codeBlockStyle: "indented", fence: "```", emDelimiter: "_", strongDelimiter: "**", linkStyle: "inlined", linkReferenceStyle: "full", br: " ", blankReplacement: function blankReplacement(e, n) { | ||
return n.isBlock ? "\n\n" : ""; | ||
}, keepReplacement: function keepReplacement(e, n) { | ||
return n.isBlock ? "\n\n" + n.outerHTML + "\n\n" : n.outerHTML; | ||
}, defaultReplacement: function defaultReplacement(e, n) { | ||
return n.isBlock ? "\n\n" + e + "\n\n" : e; | ||
} };this.options = e({}, t, n), this.rules = new o(this.options); | ||
}function b(e) { | ||
var n = this;return L.call(e.childNodes, function (e, t) { | ||
t = new h(t);var r = "";return 3 === t.nodeType ? r = t.isCode ? t.nodeValue : n.escape(t.nodeValue) : 1 === t.nodeType && (r = w.call(n, t)), C(e, r); | ||
}, ""); | ||
}function k(e) { | ||
var n = this;return this.rules.forEach(function (t) { | ||
"function" == typeof t.append && (e = C(e, t.append(n.options))); | ||
}), e.replace(/^[\t\r\n]+/, "").replace(/[\t\r\n\s]+$/, ""); | ||
}function w(e) { | ||
var n = this.rules.forNode(e), | ||
t = b.call(this, e), | ||
r = e.flankingWhitespace;return (r.leading || r.trailing) && (t = t.trim()), r.leading + n.replacement(t, e, this.options) + r.trailing; | ||
}function N(e, n) { | ||
var t = [e.match(M)[0], n.match(B)[0]].sort(), | ||
r = t[t.length - 1];return r.length < 2 ? r : "\n\n"; | ||
}function C(e, n) { | ||
var t = N(e, n);return e = e.replace(M, ""), n = n.replace(B, ""), e + t + n; | ||
}function x(e) { | ||
return null != e && ("string" == typeof e || e.nodeType && (1 === e.nodeType || 9 === e.nodeType || 11 === e.nodeType)); | ||
}var S = ["address", "article", "aside", "audio", "blockquote", "body", "canvas", "center", "dd", "dir", "div", "dl", "dt", "fieldset", "figcaption", "figure", "footer", "form", "frameset", "h1", "h2", "h3", "h4", "h5", "h6", "header", "hgroup", "hr", "html", "isindex", "li", "main", "menu", "nav", "noframes", "noscript", "ol", "output", "p", "pre", "section", "table", "tbody", "td", "tfoot", "th", "thead", "tr", "ul"], | ||
T = ["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"], | ||
R = T.join(), | ||
A = {};A.paragraph = { filter: "p", replacement: function replacement(e) { | ||
return "\n\n" + e + "\n\n"; | ||
} }, A.lineBreak = { filter: "br", replacement: function replacement(e, n, t) { | ||
return t.br + "\n"; | ||
} }, A.heading = { filter: ["h1", "h2", "h3", "h4", "h5", "h6"], replacement: function replacement(e, t, r) { | ||
var i = Number(t.nodeName.charAt(1));if ("setext" === r.headingStyle && i < 3) { | ||
return "\n\n" + e + "\n" + n(1 === i ? "=" : "-", e.length) + "\n\n"; | ||
}return "\n\n" + n("#", i) + " " + e + "\n\n"; | ||
} }, A.blockquote = { filter: "blockquote", replacement: function replacement(e) { | ||
return e = e.replace(/^\n+|\n+$/g, ""), "\n\n" + (e = e.replace(/^/gm, "> ")) + "\n\n"; | ||
} }, A.list = { filter: ["ul", "ol"], replacement: function replacement(e, n) { | ||
var t = n.parentNode;return "LI" === t.nodeName && t.lastElementChild === n ? "\n" + e : "\n\n" + e + "\n\n"; | ||
} }, A.listItem = { filter: "li", replacement: function replacement(e, n, t) { | ||
e = e.replace(/^\n+/, "").replace(/\n+$/, "\n").replace(/\n/gm, "\n ");var r = t.bulletListMarker + " ", | ||
i = n.parentNode;if ("OL" === i.nodeName) { | ||
var o = i.getAttribute("start"), | ||
a = Array.prototype.indexOf.call(i.children, n);r = (o ? Number(o) + a : a + 1) + ". "; | ||
}return r + e + (n.nextSibling && !/\n$/.test(e) ? "\n" : ""); | ||
} }, A.indentedCodeBlock = { filter: function filter(e, n) { | ||
return "indented" === n.codeBlockStyle && "PRE" === e.nodeName && e.firstChild && "CODE" === e.firstChild.nodeName; | ||
}, replacement: function replacement(e, n, t) { | ||
return "\n\n " + n.firstChild.textContent.replace(/\n/g, "\n ") + "\n\n"; | ||
} }, A.fencedCodeBlock = { filter: function filter(e, n) { | ||
return "fenced" === n.codeBlockStyle && "PRE" === e.nodeName && e.firstChild && "CODE" === e.firstChild.nodeName; | ||
}, replacement: function replacement(e, n, t) { | ||
var r = n.firstChild.className || "", | ||
i = (r.match(/language-(\S+)/) || [null, ""])[1];return "\n\n" + t.fence + i + "\n" + n.firstChild.textContent + "\n" + t.fence + "\n\n"; | ||
} }, A.horizontalRule = { filter: "hr", replacement: function replacement(e, n, t) { | ||
return "\n\n" + t.hr + "\n\n"; | ||
} }, A.inlineLink = { filter: function filter(e, n) { | ||
return "inlined" === n.linkStyle && "A" === e.nodeName && e.getAttribute("href"); | ||
}, replacement: function replacement(e, n) { | ||
return "[" + e + "](" + n.getAttribute("href") + (n.title ? ' "' + n.title + '"' : "") + ")"; | ||
} }, A.referenceLink = { filter: function filter(e, n) { | ||
return "referenced" === n.linkStyle && "A" === e.nodeName && e.getAttribute("href"); | ||
}, replacement: function replacement(e, n, t) { | ||
var r, | ||
i, | ||
o = n.getAttribute("href"), | ||
a = n.title ? ' "' + n.title + '"' : "";switch (t.linkReferenceStyle) {case "collapsed": | ||
r = "[" + e + "][]", i = "[" + e + "]: " + o + a;break;case "shortcut": | ||
r = "[" + e + "]", i = "[" + e + "]: " + o + a;break;default: | ||
var l = this.references.length + 1;r = "[" + e + "][" + l + "]", i = "[" + l + "]: " + o + a;}return this.references.push(i), r; | ||
}, references: [], append: function append(e) { | ||
var n = "";return this.references.length && (n = "\n\n" + this.references.join("\n") + "\n\n", this.references = []), n; | ||
} }, A.emphasis = { filter: ["em", "i"], replacement: function replacement(e, n, t) { | ||
return e.trim() ? t.emDelimiter + e + t.emDelimiter : ""; | ||
} }, A.strong = { filter: ["strong", "b"], replacement: function replacement(e, n, t) { | ||
return e.trim() ? t.strongDelimiter + e + t.strongDelimiter : ""; | ||
} }, A.code = { filter: function filter(e) { | ||
var n = e.previousSibling || e.nextSibling, | ||
t = "PRE" === e.parentNode.nodeName && !n;return "CODE" === e.nodeName && !t; | ||
}, replacement: function replacement(e) { | ||
if (!e.trim()) return "";var n = "`", | ||
t = "", | ||
r = "", | ||
i = e.match(/`+/gm);if (i) for (/^`/.test(e) && (t = " "), /`$/.test(e) && (r = " "); -1 !== i.indexOf(n);) { | ||
n += "`"; | ||
}return n + t + e + r + n; | ||
} }, A.image = { filter: "img", replacement: function replacement(e, n) { | ||
var t = n.alt || "", | ||
r = n.getAttribute("src") || "", | ||
i = n.title || "", | ||
o = i ? ' "' + i + '"' : "";return r ? "![" + t + "](" + r + o + ")" : ""; | ||
} }, o.prototype = { add: function add(e, n) { | ||
this.array.unshift(n); | ||
}, keep: function keep(e) { | ||
this._keep.unshift({ filter: e, replacement: this.keepReplacement }); | ||
}, remove: function remove(e) { | ||
this._remove.unshift({ filter: e, replacement: function replacement() { | ||
return ""; | ||
} }); | ||
}, forNode: function forNode(e) { | ||
if (e.isBlank) return this.blankRule;var n;return (n = a(this.array, e, this.options)) ? n : (n = a(this._keep, e, this.options)) ? n : (n = a(this._remove, e, this.options)) ? n : this.defaultRule; | ||
}, forEach: function forEach(e) { | ||
for (var n = 0; n < this.array.length; n++) { | ||
e(this.array[n], n); | ||
} | ||
} };var E, | ||
O = "undefined" != typeof window ? window : {}, | ||
$ = function () { | ||
var e = O.DOMParser, | ||
n = !1;try { | ||
new e().parseFromString("", "text/html") && (n = !0); | ||
} catch (e) {}return n; | ||
}() ? O.DOMParser : function () { | ||
var e = function e() {};return f() ? e.prototype.parseFromString = function (e) { | ||
var n = new window.ActiveXObject("htmlfile");return n.designMode = "on", n.open(), n.write(e), n.close(), n; | ||
} : e.prototype.parseFromString = function (e) { | ||
var n = document.implementation.createHTMLDocument("");return n.open(), n.write(e), n.close(), n; | ||
}, e; | ||
}(), | ||
L = Array.prototype.reduce, | ||
B = /^\n*/, | ||
M = /\n*$/;return y.prototype = { turndown: function turndown(e) { | ||
if (!x(e)) throw new TypeError(e + " is not a string, or an element/document/fragment node.");if ("" === e) return "";var n = b.call(this, new d(e));return k.call(this, n); | ||
}, use: function use(e) { | ||
if (Array.isArray(e)) for (var n = 0; n < e.length; n++) { | ||
this.use(e[n]); | ||
} else { | ||
if ("function" != typeof e) throw new TypeError("plugin must be a Function or an Array of Functions");e(this); | ||
}return this; | ||
}, addRule: function addRule(e, n) { | ||
return this.rules.add(e, n), this; | ||
}, keep: function keep(e) { | ||
return this.rules.keep(e), this; | ||
}, remove: function remove(e) { | ||
return this.rules.remove(e), this; | ||
}, escape: function escape(e) { | ||
return e.replace(/\\(\S)/g, "\\\\$1").replace(/^(#{1,6} )/gm, "\\$1").replace(/^([-*_] *){3,}$/gm, function (e, n) { | ||
return e.split(n).join("\\" + n); | ||
}).replace(/^(\W* {0,3})(\d+)\. /gm, "$1$2\\. ").replace(/^([^\\\w]*)[*+-] /gm, function (e) { | ||
return e.replace(/([*+-])/g, "\\$1"); | ||
}).replace(/^(\W* {0,3})> /gm, "$1\\> ").replace(/\*+(?![*\s\W]).+?\*+/g, function (e) { | ||
return e.replace(/\*/g, "\\*"); | ||
}).replace(/_+(?![_\s\W]).+?_+/g, function (e) { | ||
return e.replace(/_/g, "\\_"); | ||
}).replace(/`+(?![`\s\W]).+?`+/g, function (e) { | ||
return e.replace(/`/g, "\\`"); | ||
}).replace(/[\[\]]/g, "\\{{{toMarkdown}}}"); | ||
} }, y; | ||
}(), | ||
t = function t(e, _t) { | ||
function r(e) { | ||
for (var n = e.children, t = 0; t < n.length; ++t) { | ||
var i = n[t], | ||
o = i.querySelector("br");o && o.remove(), !i.innerHTML.trim() && i.remove();var a = n[t - 1];if (/^UL|OL$/.test(i.tagName)) { | ||
try { | ||
a.appendChild(i); | ||
} catch (e) { | ||
console.warn(e); | ||
}r(i); | ||
} | ||
} | ||
}"function" == typeof e && (_t = e, e = {}), e = Object(e), e.events = e.events || ["input", "change"], _t = _t || e.callback || function () {};var i = e.toTurndownOptions = Object(e.toTurndownOptions);i.converters = i.converters || [], i.customRules = i.customRules || [], e.ignoreBuiltinConverters || i.converters.push({ filter: function filter(e) { | ||
return "DIV" === e.nodeName && !e.attributes.length; | ||
}, replacement: function replacement(e) { | ||
return e; | ||
} }), this.init = function () { | ||
if (this.base.elements && this.base.elements.length) { | ||
this.element = this.base.elements[0];var o = /[\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF]*$/, | ||
a = function () { | ||
for (var a = this.element.cloneNode(!0), l = a.querySelectorAll("ul, ol"), u = 0; u < l.length; ++u) { | ||
r(l[u]); | ||
}var c = new n(e.toTurndownOptions);i.customRules.forEach(function (e) { | ||
c.addRule(e.key, { filter: e.filter, replacement: e.replacement }); | ||
}), _t(c.turndown(a.innerHTML).split("\n").map(function (e) { | ||
return e.replace(o, ""); | ||
}).join("\n").replace(o, "")); | ||
}.bind(this);e.subscribeToMeEditableInput ? this.base.subscribe("editableInput", a) : e.events.forEach(function (e) { | ||
this.element.addEventListener(e, a); | ||
}.bind(this)), a(); | ||
} | ||
}; | ||
};e.MeMarkdown = t; | ||
}(undefined); |
{ | ||
"name": "medium-editor-markdown", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "A Medium Editor extension to add markdown support.", | ||
@@ -5,0 +5,0 @@ "main": "src/medium-editor-md.js", |
101
README.md
@@ -1,131 +0,82 @@ | ||
<!-- Please do not edit this file. Edit the `blah` field in the `package.json` instead. If in doubt, open an issue. --> | ||
[![medium-editor-markdown](http://i.imgur.com/xb6JPkv.png)](#) | ||
# Medium Editor Markdown | ||
[![Support me on Patreon][badge_patreon]][patreon] [![Buy me a book][badge_amazon]][amazon] [![PayPal][badge_paypal_donate]][paypal-donations] [![Ask me anything](https://img.shields.io/badge/ask%20me-anything-1abc9c.svg)](https://github.com/IonicaBizau/ama) [![Version](https://img.shields.io/npm/v/medium-editor-markdown.svg)](https://www.npmjs.com/package/medium-editor-markdown) [![Downloads](https://img.shields.io/npm/dt/medium-editor-markdown.svg)](https://www.npmjs.com/package/medium-editor-markdown) | ||
> A Medium Editor extension to add markdown support. | ||
[Click here](https://github.com/daviferreira/medium-editor) to see the Medium Editor project. | ||
## Usage | ||
The available scripts are: | ||
- me-markdown.no-deps.js | ||
- me-markdown.no-deps.min.js | ||
- me-markdown.standalone.js | ||
- me-markdown.standalone.min.js | ||
The `*.standalone.*` scripts contain all the dependencies included there. | ||
The `*.no-deps.*` scripts contain only the extension code. You will have to include manually [`turndown.js`](https://github.com/domchristie/turndown) on the page, before including the markdown extension. | ||
![medium-editor-markdown](http://i.imgur.com/xb6JPkv.png) | ||
The `*.min.*` scripts are minified. | ||
## Demo | ||
[Click here](http://ionicabizau.github.io/medium-editor-markdown) for a live demo. | ||
# medium-editor-markdown | ||
[![Medium Editor Markdown](http://i.imgur.com/t1taWY0.jpg)](http://ionicabizau.github.io/medium-editor-markdown) | ||
A Medium Editor extension to add markdown support. | ||
## Example | ||
```html | ||
<div class="editor"></div> | ||
<pre class="markdown"></pre> | ||
<script src="path/to/medium-editor.js"></script> | ||
<script src="path/to/me-markdown.standalone.min.js"></script> | ||
<script> | ||
(function () { | ||
var markDownEl = document.querySelector(".markdown"); | ||
new MediumEditor(document.querySelector(".editor"), { | ||
extensions: { | ||
markdown: new MeMarkdown(function (md) { | ||
markDownEl.textContent = md; | ||
}) | ||
} | ||
}); | ||
})(); | ||
</script> | ||
## Installation | ||
```sh | ||
$ npm i medium-editor-markdown | ||
``` | ||
## Building | ||
To rebuild the dist files, run `./build`. | ||
## :memo: Documentation | ||
### `MeMarkdown(options, callback)` | ||
Creates a new instance of `MeMarkdown`. | ||
#### Params | ||
- **Object** `options`: An object containing the following fields: | ||
- `events` (Array): An array with the events when the markdown code will be generated (default: `["input", "change"]`). | ||
- `callback` (Function): The callback function. If the second argument is a function, then it has greater priority. | ||
- `toTurndownOptions` (Object): Options to pass to the markdown converter code. | ||
- `ignoreBuiltinConverters` (Boolean): If `true`, the default converters passed to `toMarkdown` will be ignored. | ||
- **Function** `callback`: The callback function that is called with the markdown code (first argument). | ||
## :yum: How to contribute | ||
Have an idea? Found a bug? See [how to contribute][contributing]. | ||
## :sparkling_heart: Support my projects | ||
I open-source almost everything I can, and I try to reply everyone needing help using these projects. Obviously, | ||
this takes time. You can integrate and use these projects in your applications *for free*! You can even change the source code and redistribute (even resell it). | ||
However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it: | ||
## Documentation | ||
- Starring and sharing the projects you like :rocket: | ||
- [![Buy me a book][badge_amazon]][amazon]—I love books! I will remember you after years if you buy me one. :grin: :book: | ||
- [![PayPal][badge_paypal]][paypal-donations]—You can make one-time donations via PayPal. I'll probably buy a ~~coffee~~ tea. :tea: | ||
- [![Support me on Patreon][badge_patreon]][patreon]—Set up a recurring monthly donation and you will get interesting news about what I'm doing (things that I don't share with everyone). | ||
- **Bitcoin**—You can send me bitcoins at this address (or scanning the code below): `1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6` | ||
![](https://i.imgur.com/z6OQI95.png) | ||
Thanks! :heart: | ||
### `MeMarkdown(options, callback)` | ||
Creates a new instance of `MeMarkdown`. | ||
#### Params | ||
- **Object** `options`: An object containing the following fields: | ||
- `events` (Array): An array with the events when the markdown code will be generated (default: `["input", "change"]`). | ||
- `subscribeToMeEditableInput` (Boolean): If this is true we will respond to the medium editor's custom "editableInput" event | ||
- `callback` (Function): The callback function. If the second argument is a function, then it has greater priority. | ||
- `toTurndownOptions` (Object): Options to pass to the markdown converter code. | ||
- `ignoreBuiltinConverters` (Boolean): If `true`, the default converters passed to `toMarkdown` will be ignored. | ||
- **Function** `callback`: The callback function that is called with the markdown code (first argument). | ||
## :cake: Thanks | ||
- [**@daviferreira**](https://github.com/daviferreira/) who created the [Medium Editor library](https://github.com/daviferreira/medium-editor). | ||
- [**@domchristie**](https://github.com/domchristie/) for creating [turndown](https://github.com/domchristie/turndown). | ||
## :scroll: License | ||
[MIT][license] © [Ionică Bizău][website] | ||
## How to contribute | ||
Have an idea? Found a bug? See [how to contribute][contributing]. | ||
[badge_patreon]: https://ionicabizau.github.io/badges/patreon.svg | ||
[badge_amazon]: https://ionicabizau.github.io/badges/amazon.svg | ||
[badge_paypal]: https://ionicabizau.github.io/badges/paypal.svg | ||
[badge_paypal_donate]: https://ionicabizau.github.io/badges/paypal_donate.svg | ||
[patreon]: https://www.patreon.com/ionicabizau | ||
[amazon]: http://amzn.eu/hRo9sIZ | ||
[paypal-donations]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RVXDDLKKLQRJW | ||
## License | ||
See the [LICENSE][license] file. | ||
[license]: http://showalicense.com/?fullname=Ionic%C4%83%20Biz%C4%83u%20%3Cbizauionica%40gmail.com%3E%20(https%3A%2F%2Fionicabizau.net)&year=2015#license-mit | ||
[website]: https://ionicabizau.net | ||
[license]: /LICENSE | ||
[contributing]: /CONTRIBUTING.md | ||
[docs]: /DOCUMENTATION.md |
@@ -12,2 +12,3 @@ "use strict"; | ||
* - `events` (Array): An array with the events when the markdown code will be generated (default: `["input", "change"]`). | ||
* - `subscribeToMeEditableInput` (Boolean): If this is true we will respond to the medium editor's custom "editableInput" event | ||
* - `callback` (Function): The callback function. If the second argument is a function, then it has greater priority. | ||
@@ -36,9 +37,10 @@ * - `toTurndownOptions` (Object): Options to pass to the markdown converter code. | ||
toTurndownOptions.converters = toTurndownOptions.converters || []; | ||
toTurndownOptions.customRules = toTurndownOptions.customRules || []; | ||
if (!options.ignoreBuiltinConverters) { | ||
toTurndownOptions.converters.push({ | ||
filter: function (node) { | ||
filter: function filter(node) { | ||
return node.nodeName === "DIV" && !node.attributes.length; | ||
} | ||
, replacement: function (content) { | ||
}, | ||
replacement: function replacement(content) { | ||
return content; | ||
@@ -49,3 +51,3 @@ } | ||
function normalizeList ($elm) { | ||
function normalizeList($elm) { | ||
var $children = $elm.children; | ||
@@ -61,3 +63,5 @@ for (var i = 0; i < $children.length; ++i) { | ||
$prevChild.appendChild($cChild); | ||
} catch (e) { console.warn(e); } | ||
} catch (e) { | ||
console.warn(e); | ||
} | ||
normalizeList($cChild); | ||
@@ -89,3 +93,12 @@ } | ||
callback( new TurndownService(options.toTurndownOptions).turndown($clone.innerHTML).split("\n").map(function (c) { | ||
var turndownService = new TurndownService(options.toTurndownOptions); | ||
toTurndownOptions.customRules.forEach(function (customRule) { | ||
turndownService.addRule(customRule.key, { | ||
filter: customRule.filter, | ||
replacement: customRule.replacement | ||
}); | ||
}); | ||
callback(turndownService.turndown($clone.innerHTML).split("\n").map(function (c) { | ||
return c.replace(rightWhitespace, ''); | ||
@@ -95,8 +108,12 @@ }).join("\n").replace(rightWhitespace, '')); | ||
options.events.forEach(function (c) { | ||
this.element.addEventListener(c, handler); | ||
}.bind(this)); | ||
if (options.subscribeToMeEditableInput) { | ||
this.base.subscribe('editableInput', handler); | ||
} else { | ||
options.events.forEach(function (c) { | ||
this.element.addEventListener(c, handler); | ||
}.bind(this)); | ||
} | ||
handler(); | ||
}; | ||
}; | ||
}; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
63172
1311
0
83