medium-editor-markdown
Advanced tools
Comparing version 2.4.1 to 2.5.0
@@ -18,2 +18,33 @@ (function (root) { | ||
var toMarkdownOptions = options.toMarkdownOptions = Object(options.toMarkdownOptions); | ||
toMarkdownOptions.converters = toMarkdownOptions.converters || []; | ||
if (!options.ignoreBuiltInConverters) { | ||
toMarkdownOptions.converters.push({ | ||
filter: function (node) { | ||
return node.nodeName === "DIV" && !node.attributes.length; | ||
} | ||
, replacement: function (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); | ||
} | ||
} | ||
} | ||
// Called by medium-editor during init | ||
@@ -23,3 +54,5 @@ 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.base.elements || !this.base.elements.length) { | ||
return; | ||
} | ||
@@ -30,5 +63,11 @@ // Element(s) that this instance of medium-editor is attached to is/are stored in .elements | ||
var handler = function () { | ||
callback(toMarkdown(this.element.innerHTML).split("\n").map(function (c) { | ||
return c.trim(); | ||
}).join("\n").trim()); | ||
var $clone = this.element.cloneNode(true); | ||
var $lists = $clone.querySelectorAll("ul, ol"); | ||
for (var i = 0; i < $lists.length; ++i) { | ||
normalizeList($lists[i]); | ||
} | ||
callback(toMarkdown($clone.innerHTML, options.toMarkdownOptions).split("\n").map(function (c) { | ||
return c.trimRight(); | ||
}).join("\n").trimRight()); | ||
}.bind(this); | ||
@@ -35,0 +74,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(n){if("function"!=typeof MediumEditor)throw new Error("Medium Editor is not loaded on the page.");var t=function(n,t){"function"==typeof n&&(t=n,n={}),n=Object(n),n.events=n.events||["input","change"],t=t||n.callback||function(){},this.init=function(){if(this.base.elements&&this.base.elements.length){this.element=this.base.elements[0];var e=function(){t(toMarkdown(this.element.innerHTML).split("\n").map(function(n){return n.trim()}).join("\n").trim())}.bind(this);n.events.forEach(function(n){this.element.addEventListener(n,e)}.bind(this)),e()}}};n.MeMarkdown=t}(this); | ||
!function(e){if("function"!=typeof MediumEditor)throw new Error("Medium Editor is not loaded on the page.");var t=function(e,t){function n(e){for(var t=e.children,r=0;r<t.length;++r){var i=t[r],o=i.querySelector("br");o&&o.remove(),!i.innerHTML.trim()&&i.remove();var s=t[r-1];if(/^UL|OL$/.test(i.tagName)){try{s.appendChild(i)}catch(e){console.warn(e)}n(i)}}}"function"==typeof e&&(t=e,e={}),e=Object(e),e.events=e.events||["input","change"],t=t||e.callback||function(){};var r=e.toMarkdownOptions=Object(e.toMarkdownOptions);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=function(){for(var r=this.element.cloneNode(!0),i=r.querySelectorAll("ul, ol"),o=0;o<i.length;++o)n(i[o]);t(toMarkdown(r.innerHTML,e.toMarkdownOptions).split("\n").map(function(e){return e.trimRight()}).join("\n").trimRight())}.bind(this);e.events.forEach(function(e){this.element.addEventListener(e,r)}.bind(this)),r()}}};e.MeMarkdown=t}(this); |
@@ -10,3 +10,3 @@ (function (root) { | ||
* | ||
* Copyright 2011-15, Dom Christie | ||
* Copyright 2011+, Dom Christie | ||
* Licenced under the MIT licence | ||
@@ -16,40 +16,25 @@ * | ||
'use strict'; | ||
'use strict' | ||
var toMarkdown; | ||
var converters; | ||
var mdConverters = require('./lib/md-converters'); | ||
var gfmConverters = require('./lib/gfm-converters'); | ||
var collapse = require('collapse-whitespace'); | ||
var toMarkdown | ||
var converters | ||
var mdConverters = require('./lib/md-converters') | ||
var gfmConverters = require('./lib/gfm-converters') | ||
var HtmlParser = require('./lib/html-parser') | ||
var collapse = require('collapse-whitespace') | ||
/* | ||
* Set up window and document for Node.js | ||
*/ | ||
var _window = (typeof window !== 'undefined' ? window : this), _document; | ||
if (typeof document === 'undefined') { | ||
_document = require('jsdom').jsdom(); | ||
} | ||
else { | ||
_document = document; | ||
} | ||
/* | ||
* Utilities | ||
*/ | ||
function trim(string) { | ||
return string.replace(/^[ \r\n\t]+|[ \r\n\t]+$/g, ''); | ||
} | ||
var blocks = ['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', | ||
'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 blocks.indexOf(node.nodeName.toLowerCase()) !== -1; | ||
function isBlock (node) { | ||
return blocks.indexOf(node.nodeName.toLowerCase()) !== -1 | ||
} | ||
@@ -60,51 +45,14 @@ | ||
'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr' | ||
]; | ||
] | ||
function isVoid(node) { | ||
return voids.indexOf(node.nodeName.toLowerCase()) !== -1; | ||
function isVoid (node) { | ||
return voids.indexOf(node.nodeName.toLowerCase()) !== -1 | ||
} | ||
/* | ||
* Parsing HTML strings | ||
*/ | ||
function canParseHtml() { | ||
var Parser = _window.DOMParser, canParse = false; | ||
// 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) {} | ||
return canParse; | ||
function htmlToDom (string) { | ||
var tree = new HtmlParser().parseFromString(string, 'text/html') | ||
collapse(tree.documentElement, isBlock) | ||
return tree | ||
} | ||
function createHtmlParser() { | ||
var Parser = function () {}; | ||
Parser.prototype.parseFromString = function (string) { | ||
var newDoc = _document.implementation.createHTMLDocument(''); | ||
if (string.toLowerCase().indexOf('<!doctype') > -1) { | ||
newDoc.documentElement.innerHTML = string; | ||
} | ||
else { | ||
newDoc.body.innerHTML = string; | ||
} | ||
return newDoc; | ||
}; | ||
return Parser; | ||
} | ||
var HtmlParser = canParseHtml() ? _window.DOMParser : createHtmlParser(); | ||
function htmlToDom(string) { | ||
var tree = new HtmlParser().parseFromString(string, 'text/html'); | ||
collapse(tree, isBlock); | ||
return tree; | ||
} | ||
/* | ||
@@ -114,17 +62,19 @@ * Flattens DOM tree into single array | ||
function bfsOrder(node) { | ||
var inqueue = [node], | ||
outqueue = [], | ||
elem, children, i; | ||
function bfsOrder (node) { | ||
var inqueue = [node] | ||
var outqueue = [] | ||
var elem | ||
var children | ||
var i | ||
while (inqueue.length > 0) { | ||
elem = inqueue.shift(); | ||
outqueue.push(elem); | ||
children = elem.childNodes; | ||
for (i = 0 ; i < children.length; i++) { | ||
if (children[i].nodeType === 1) { inqueue.push(children[i]); } | ||
elem = inqueue.shift() | ||
outqueue.push(elem) | ||
children = elem.childNodes | ||
for (i = 0; i < children.length; i++) { | ||
if (children[i].nodeType === 1) inqueue.push(children[i]) | ||
} | ||
} | ||
outqueue.shift(); | ||
return outqueue; | ||
outqueue.shift() | ||
return outqueue | ||
} | ||
@@ -136,14 +86,12 @@ | ||
function getContent(node) { | ||
var text = ''; | ||
function getContent (node) { | ||
var text = '' | ||
for (var i = 0; i < node.childNodes.length; i++) { | ||
if (node.childNodes[i].nodeType === 1) { | ||
text += node.childNodes[i]._replacement; | ||
} | ||
else if (node.childNodes[i].nodeType === 3) { | ||
text += node.childNodes[i].data; | ||
} | ||
else { continue; } | ||
text += node.childNodes[i]._replacement | ||
} else if (node.childNodes[i].nodeType === 3) { | ||
text += node.childNodes[i].data | ||
} else continue | ||
} | ||
return text; | ||
return text | ||
} | ||
@@ -155,60 +103,59 @@ | ||
function outer(node, content) { | ||
return node.cloneNode(false).outerHTML.replace('><', '>'+ content +'<'); | ||
function outer (node, content) { | ||
return node.cloneNode(false).outerHTML.replace('><', '>' + content + '<') | ||
} | ||
function canConvert(node, filter) { | ||
function canConvert (node, filter) { | ||
if (typeof filter === 'string') { | ||
return filter === node.nodeName.toLowerCase(); | ||
return filter === node.nodeName.toLowerCase() | ||
} | ||
if (Array.isArray(filter)) { | ||
return filter.indexOf(node.nodeName.toLowerCase()) !== -1; | ||
return filter.indexOf(node.nodeName.toLowerCase()) !== -1 | ||
} else if (typeof filter === 'function') { | ||
return filter.call(toMarkdown, node) | ||
} else { | ||
throw new TypeError('`filter` needs to be a string, array, or function') | ||
} | ||
else if (typeof filter === 'function') { | ||
return filter.call(toMarkdown, node); | ||
} | ||
else { | ||
throw new TypeError('`filter` needs to be a string, array, or function'); | ||
} | ||
} | ||
function isFlankedByWhitespace(side, node) { | ||
var sibling, regExp, isFlanked; | ||
function isFlankedByWhitespace (side, node) { | ||
var sibling | ||
var regExp | ||
var isFlanked | ||
if (side === 'left') { | ||
sibling = node.previousSibling; | ||
regExp = / $/; | ||
sibling = node.previousSibling | ||
regExp = / $/ | ||
} else { | ||
sibling = node.nextSibling | ||
regExp = /^ / | ||
} | ||
else { | ||
sibling = node.nextSibling; | ||
regExp = /^ /; | ||
} | ||
if (sibling) { | ||
if (sibling.nodeType === 3) { | ||
isFlanked = regExp.test(sibling.nodeValue); | ||
isFlanked = regExp.test(sibling.nodeValue) | ||
} else if (sibling.nodeType === 1 && !isBlock(sibling)) { | ||
isFlanked = regExp.test(sibling.textContent) | ||
} | ||
else if(sibling.nodeType === 1 && !isBlock(sibling)) { | ||
isFlanked = regExp.test(sibling.textContent); | ||
} | ||
} | ||
return isFlanked; | ||
return isFlanked | ||
} | ||
function flankingWhitespace(node) { | ||
var leading = '', trailing = ''; | ||
function flankingWhitespace (node) { | ||
var leading = '' | ||
var trailing = '' | ||
if (!isBlock(node)) { | ||
var hasLeading = /^[ \r\n\t]/.test(node.innerHTML), | ||
hasTrailing = /[ \r\n\t]$/.test(node.innerHTML); | ||
var hasLeading = /^[ \r\n\t]/.test(node.innerHTML) | ||
var hasTrailing = /[ \r\n\t]$/.test(node.innerHTML) | ||
if (hasLeading && !isFlankedByWhitespace('left', node)) { | ||
leading = ' '; | ||
leading = ' ' | ||
} | ||
if (hasTrailing && !isFlankedByWhitespace('right', node)) { | ||
trailing = ' '; | ||
trailing = ' ' | ||
} | ||
} | ||
return { leading: leading, trailing: trailing }; | ||
return { leading: leading, trailing: trailing } | ||
} | ||
@@ -221,13 +168,14 @@ | ||
function process(node) { | ||
var replacement, content = getContent(node); | ||
function process (node) { | ||
var replacement | ||
var content = getContent(node) | ||
// Remove blank nodes | ||
if (!isVoid(node) && !/A/.test(node.nodeName) && /^\s*$/i.test(content)) { | ||
node._replacement = ''; | ||
return; | ||
if (!isVoid(node) && !/A|TH|TD/.test(node.nodeName) && /^\s*$/i.test(content)) { | ||
node._replacement = '' | ||
return | ||
} | ||
for (var i = 0; i < converters.length; i++) { | ||
var converter = converters[i]; | ||
var converter = converters[i] | ||
@@ -238,41 +186,41 @@ if (canConvert(node, converter.filter)) { | ||
'`replacement` needs to be a function that returns a string' | ||
); | ||
) | ||
} | ||
var whitespace = flankingWhitespace(node); | ||
var whitespace = flankingWhitespace(node) | ||
if (whitespace.leading || whitespace.trailing) { | ||
content = trim(content); | ||
content = content.trim() | ||
} | ||
replacement = whitespace.leading + | ||
converter.replacement.call(toMarkdown, content, node) + | ||
whitespace.trailing; | ||
break; | ||
converter.replacement.call(toMarkdown, content, node) + | ||
whitespace.trailing | ||
break | ||
} | ||
} | ||
node._replacement = replacement; | ||
node._replacement = replacement | ||
} | ||
toMarkdown = function (input, options) { | ||
options = options || {}; | ||
options = options || {} | ||
if (typeof input !== 'string') { | ||
throw new TypeError(input + ' is not a string'); | ||
throw new TypeError(input + ' is not a string') | ||
} | ||
// Escape potential ol triggers | ||
input = input.replace(/(\d+)\. /g, '$1\\. '); | ||
input = input.replace(/(\d+)\. /g, '$1\\. ') | ||
var clone = htmlToDom(input).body, | ||
nodes = bfsOrder(clone), | ||
output; | ||
var clone = htmlToDom(input).body | ||
var nodes = bfsOrder(clone) | ||
var output | ||
converters = mdConverters.slice(0); | ||
converters = mdConverters.slice(0) | ||
if (options.gfm) { | ||
converters = gfmConverters.concat(converters); | ||
converters = gfmConverters.concat(converters) | ||
} | ||
if (options.converters) { | ||
converters = options.converters.concat(converters); | ||
converters = options.converters.concat(converters) | ||
} | ||
@@ -282,29 +230,28 @@ | ||
for (var i = nodes.length - 1; i >= 0; i--) { | ||
process(nodes[i]); | ||
process(nodes[i]) | ||
} | ||
output = getContent(clone); | ||
output = getContent(clone) | ||
return output.replace(/^[\t\r\n]+|[\t\r\n\s]+$/g, '') | ||
.replace(/\n\s+\n/g, '\n\n') | ||
.replace(/\n{3,}/g, '\n\n'); | ||
}; | ||
.replace(/\n\s+\n/g, '\n\n') | ||
.replace(/\n{3,}/g, '\n\n') | ||
} | ||
toMarkdown.isBlock = isBlock; | ||
toMarkdown.isVoid = isVoid; | ||
toMarkdown.trim = trim; | ||
toMarkdown.outer = outer; | ||
toMarkdown.isBlock = isBlock | ||
toMarkdown.isVoid = isVoid | ||
toMarkdown.outer = outer | ||
module.exports = toMarkdown; | ||
module.exports = toMarkdown | ||
},{"./lib/gfm-converters":2,"./lib/md-converters":3,"collapse-whitespace":4,"jsdom":7}],2:[function(require,module,exports){ | ||
'use strict'; | ||
},{"./lib/gfm-converters":2,"./lib/html-parser":3,"./lib/md-converters":4,"collapse-whitespace":7}],2:[function(require,module,exports){ | ||
'use strict' | ||
function cell(content, node) { | ||
var index = Array.prototype.indexOf.call(node.parentNode.childNodes, node); | ||
var prefix = ' '; | ||
if (index === 0) { prefix = '| '; } | ||
return prefix + content + ' |'; | ||
function cell (content, node) { | ||
var index = Array.prototype.indexOf.call(node.parentNode.childNodes, node) | ||
var prefix = ' ' | ||
if (index === 0) prefix = '| ' | ||
return prefix + content + ' |' | ||
} | ||
var highlightRegEx = /highlight highlight-(\S+)/; | ||
var highlightRegEx = /highlight highlight-(\S+)/ | ||
@@ -315,3 +262,3 @@ module.exports = [ | ||
replacement: function () { | ||
return '\n'; | ||
return '\n' | ||
} | ||
@@ -322,3 +269,3 @@ }, | ||
replacement: function (content) { | ||
return '~~' + content + '~~'; | ||
return '~~' + content + '~~' | ||
} | ||
@@ -329,6 +276,6 @@ }, | ||
filter: function (node) { | ||
return node.type === 'checkbox' && node.parentNode.nodeName === 'LI'; | ||
return node.type === 'checkbox' && node.parentNode.nodeName === 'LI' | ||
}, | ||
replacement: function (content, node) { | ||
return (node.checked ? '[x]' : '[ ]') + ' '; | ||
return (node.checked ? '[x]' : '[ ]') + ' ' | ||
} | ||
@@ -340,3 +287,3 @@ }, | ||
replacement: function (content, node) { | ||
return cell(content, node); | ||
return cell(content, node) | ||
} | ||
@@ -348,16 +295,16 @@ }, | ||
replacement: function (content, node) { | ||
var borderCells = ''; | ||
var alignMap = { left: ':--', right: '--:', center: ':-:' }; | ||
var borderCells = '' | ||
var alignMap = { left: ':--', right: '--:', center: ':-:' } | ||
if (node.parentNode.nodeName === 'THEAD') { | ||
for (var i = 0; i < node.childNodes.length; i++) { | ||
var align = node.childNodes[i].attributes.align; | ||
var border = '---'; | ||
var align = node.childNodes[i].attributes.align | ||
var border = '---' | ||
if (align) { border = alignMap[align.value] || border; } | ||
if (align) border = alignMap[align.value] || border | ||
borderCells += cell(border, node.childNodes[i]); | ||
borderCells += cell(border, node.childNodes[i]) | ||
} | ||
} | ||
return '\n' + content + (borderCells ? '\n' + borderCells : ''); | ||
return '\n' + content + (borderCells ? '\n' + borderCells : '') | ||
} | ||
@@ -369,3 +316,3 @@ }, | ||
replacement: function (content) { | ||
return '\n\n' + content + '\n\n'; | ||
return '\n\n' + content + '\n\n' | ||
} | ||
@@ -377,3 +324,3 @@ }, | ||
replacement: function (content) { | ||
return content; | ||
return content | ||
} | ||
@@ -386,7 +333,7 @@ }, | ||
return node.nodeName === 'PRE' && | ||
node.firstChild && | ||
node.firstChild.nodeName === 'CODE'; | ||
node.firstChild && | ||
node.firstChild.nodeName === 'CODE' | ||
}, | ||
replacement: function(content, node) { | ||
return '\n\n```\n' + node.firstChild.textContent + '\n```\n\n'; | ||
replacement: function (content, node) { | ||
return '\n\n```\n' + node.firstChild.textContent + '\n```\n\n' | ||
} | ||
@@ -399,8 +346,8 @@ }, | ||
return node.nodeName === 'PRE' && | ||
node.parentNode.nodeName === 'DIV' && | ||
highlightRegEx.test(node.parentNode.className); | ||
node.parentNode.nodeName === 'DIV' && | ||
highlightRegEx.test(node.parentNode.className) | ||
}, | ||
replacement: function (content, node) { | ||
var language = node.parentNode.className.match(highlightRegEx)[1]; | ||
return '\n\n```' + language + '\n' + node.textContent + '\n```\n\n'; | ||
var language = node.parentNode.className.match(highlightRegEx)[1] | ||
return '\n\n```' + language + '\n' + node.textContent + '\n```\n\n' | ||
} | ||
@@ -412,13 +359,91 @@ }, | ||
return node.nodeName === 'DIV' && | ||
highlightRegEx.test(node.className); | ||
highlightRegEx.test(node.className) | ||
}, | ||
replacement: function (content) { | ||
return '\n\n' + content + '\n\n'; | ||
return '\n\n' + content + '\n\n' | ||
} | ||
} | ||
]; | ||
] | ||
},{}],3:[function(require,module,exports){ | ||
'use strict'; | ||
/* | ||
* Set up window for Node.js | ||
*/ | ||
var _window = (typeof window !== 'undefined' ? window : this) | ||
/* | ||
* Parsing HTML strings | ||
*/ | ||
function canParseHtmlNatively () { | ||
var Parser = _window.DOMParser | ||
var canParse = false | ||
// 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) {} | ||
return canParse | ||
} | ||
function createHtmlParser () { | ||
var Parser = function () {} | ||
// For Node.js environments | ||
if (typeof document === 'undefined') { | ||
var jsdom = require('jsdom') | ||
Parser.prototype.parseFromString = function (string) { | ||
return jsdom.jsdom(string, { | ||
features: { | ||
FetchExternalResources: [], | ||
ProcessExternalResources: false | ||
} | ||
}) | ||
} | ||
} else { | ||
if (!shouldUseActiveX()) { | ||
Parser.prototype.parseFromString = function (string) { | ||
var doc = document.implementation.createHTMLDocument('') | ||
doc.open() | ||
doc.write(string) | ||
doc.close() | ||
return doc | ||
} | ||
} else { | ||
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 | ||
} | ||
} | ||
} | ||
return Parser | ||
} | ||
function shouldUseActiveX () { | ||
var useActiveX = false | ||
try { | ||
document.implementation.createHTMLDocument('').open() | ||
} catch (e) { | ||
if (window.ActiveXObject) useActiveX = true | ||
} | ||
return useActiveX | ||
} | ||
module.exports = canParseHtmlNatively() ? _window.DOMParser : createHtmlParser() | ||
},{"jsdom":6}],4:[function(require,module,exports){ | ||
'use strict' | ||
module.exports = [ | ||
@@ -428,3 +453,3 @@ { | ||
replacement: function (content) { | ||
return '\n\n' + content + '\n\n'; | ||
return '\n\n' + content + '\n\n' | ||
} | ||
@@ -436,3 +461,3 @@ }, | ||
replacement: function () { | ||
return ' \n'; | ||
return ' \n' | ||
} | ||
@@ -442,10 +467,10 @@ }, | ||
{ | ||
filter: ['h1', 'h2', 'h3', 'h4','h5', 'h6'], | ||
replacement: function(content, node) { | ||
var hLevel = node.nodeName.charAt(1); | ||
var hPrefix = ''; | ||
for(var i = 0; i < hLevel; i++) { | ||
hPrefix += '#'; | ||
filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'], | ||
replacement: function (content, node) { | ||
var hLevel = node.nodeName.charAt(1) | ||
var hPrefix = '' | ||
for (var i = 0; i < hLevel; i++) { | ||
hPrefix += '#' | ||
} | ||
return '\n\n' + hPrefix + ' ' + content + '\n\n'; | ||
return '\n\n' + hPrefix + ' ' + content + '\n\n' | ||
} | ||
@@ -457,3 +482,3 @@ }, | ||
replacement: function () { | ||
return '\n\n* * *\n\n'; | ||
return '\n\n* * *\n\n' | ||
} | ||
@@ -465,3 +490,3 @@ }, | ||
replacement: function (content) { | ||
return '_' + content + '_'; | ||
return '_' + content + '_' | ||
} | ||
@@ -473,3 +498,3 @@ }, | ||
replacement: function (content) { | ||
return '**' + content + '**'; | ||
return '**' + content + '**' | ||
} | ||
@@ -481,9 +506,9 @@ }, | ||
filter: function (node) { | ||
var hasSiblings = node.previousSibling || node.nextSibling; | ||
var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings; | ||
var hasSiblings = node.previousSibling || node.nextSibling | ||
var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings | ||
return node.nodeName === 'CODE' && !isCodeBlock; | ||
return node.nodeName === 'CODE' && !isCodeBlock | ||
}, | ||
replacement: function(content) { | ||
return '`' + content + '`'; | ||
replacement: function (content) { | ||
return '`' + content + '`' | ||
} | ||
@@ -494,7 +519,7 @@ }, | ||
filter: function (node) { | ||
return node.nodeName === 'A' && node.getAttribute('href'); | ||
return node.nodeName === 'A' && node.getAttribute('href') | ||
}, | ||
replacement: function(content, node) { | ||
var titlePart = node.title ? ' "'+ node.title +'"' : ''; | ||
return '[' + content + '](' + node.getAttribute('href') + titlePart + ')'; | ||
replacement: function (content, node) { | ||
var titlePart = node.title ? ' "' + node.title + '"' : '' | ||
return '[' + content + '](' + node.getAttribute('href') + titlePart + ')' | ||
} | ||
@@ -505,8 +530,8 @@ }, | ||
filter: 'img', | ||
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 + ')' : ''; | ||
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 + ')' : '' | ||
} | ||
@@ -518,6 +543,6 @@ }, | ||
filter: function (node) { | ||
return node.nodeName === 'PRE' && node.firstChild.nodeName === 'CODE'; | ||
return node.nodeName === 'PRE' && node.firstChild.nodeName === 'CODE' | ||
}, | ||
replacement: function(content, node) { | ||
return '\n\n ' + node.firstChild.textContent.replace(/\n/g, '\n ') + '\n\n'; | ||
replacement: function (content, node) { | ||
return '\n\n ' + node.firstChild.textContent.replace(/\n/g, '\n ') + '\n\n' | ||
} | ||
@@ -529,6 +554,6 @@ }, | ||
replacement: function (content) { | ||
content = this.trim(content); | ||
content = content.replace(/\n{3,}/g, '\n\n'); | ||
content = content.replace(/^/gm, '> '); | ||
return '\n\n' + content + '\n\n'; | ||
content = content.trim() | ||
content = content.replace(/\n{3,}/g, '\n\n') | ||
content = content.replace(/^/gm, '> ') | ||
return '\n\n' + content + '\n\n' | ||
} | ||
@@ -540,9 +565,9 @@ }, | ||
replacement: function (content, node) { | ||
content = content.replace(/^\s+/, '').replace(/\n/gm, '\n '); | ||
var prefix = '* '; | ||
var parent = node.parentNode; | ||
var index = Array.prototype.indexOf.call(parent.children, node) + 1; | ||
content = content.replace(/^\s+/, '').replace(/\n/gm, '\n ') | ||
var prefix = '* ' | ||
var parent = node.parentNode | ||
var index = Array.prototype.indexOf.call(parent.children, node) + 1 | ||
prefix = /ol/i.test(parent.nodeName) ? index + '. ' : '* '; | ||
return prefix + content; | ||
prefix = /ol/i.test(parent.nodeName) ? index + '. ' : '* ' | ||
return prefix + content | ||
} | ||
@@ -554,11 +579,11 @@ }, | ||
replacement: function (content, node) { | ||
var strings = []; | ||
var strings = [] | ||
for (var i = 0; i < node.childNodes.length; i++) { | ||
strings.push(node.childNodes[i]._replacement); | ||
strings.push(node.childNodes[i]._replacement) | ||
} | ||
if (/li/i.test(node.parentNode.nodeName)) { | ||
return '\n' + strings.join('\n'); | ||
return '\n' + strings.join('\n') | ||
} | ||
return '\n\n' + strings.join('\n') + '\n\n'; | ||
return '\n\n' + strings.join('\n') + '\n\n' | ||
} | ||
@@ -569,6 +594,6 @@ }, | ||
filter: function (node) { | ||
return this.isBlock(node); | ||
return this.isBlock(node) | ||
}, | ||
replacement: function (content, node) { | ||
return '\n\n' + this.outer(node, content) + '\n\n'; | ||
return '\n\n' + this.outer(node, content) + '\n\n' | ||
} | ||
@@ -580,10 +605,57 @@ }, | ||
filter: function () { | ||
return true; | ||
return true | ||
}, | ||
replacement: function (content, node) { | ||
return this.outer(node, content); | ||
return this.outer(node, content) | ||
} | ||
} | ||
] | ||
},{}],5:[function(require,module,exports){ | ||
/** | ||
* This file automatically generated from `build.js`. | ||
* Do not manually edit. | ||
*/ | ||
module.exports = [ | ||
"address", | ||
"article", | ||
"aside", | ||
"audio", | ||
"blockquote", | ||
"canvas", | ||
"dd", | ||
"div", | ||
"dl", | ||
"fieldset", | ||
"figcaption", | ||
"figure", | ||
"footer", | ||
"form", | ||
"h1", | ||
"h2", | ||
"h3", | ||
"h4", | ||
"h5", | ||
"h6", | ||
"header", | ||
"hgroup", | ||
"hr", | ||
"main", | ||
"nav", | ||
"noscript", | ||
"ol", | ||
"output", | ||
"p", | ||
"pre", | ||
"section", | ||
"table", | ||
"tfoot", | ||
"ul", | ||
"video" | ||
]; | ||
},{}],4:[function(require,module,exports){ | ||
},{}],6:[function(require,module,exports){ | ||
},{}],7:[function(require,module,exports){ | ||
'use strict'; | ||
@@ -726,48 +798,4 @@ | ||
},{"block-elements":5,"void-elements":6}],5:[function(require,module,exports){ | ||
},{"block-elements":5,"void-elements":8}],8:[function(require,module,exports){ | ||
/** | ||
* This file automatically generated from `build.js`. | ||
* Do not manually edit. | ||
*/ | ||
module.exports = [ | ||
"address", | ||
"article", | ||
"aside", | ||
"audio", | ||
"blockquote", | ||
"canvas", | ||
"dd", | ||
"div", | ||
"dl", | ||
"fieldset", | ||
"figcaption", | ||
"figure", | ||
"footer", | ||
"form", | ||
"h1", | ||
"h2", | ||
"h3", | ||
"h4", | ||
"h5", | ||
"h6", | ||
"header", | ||
"hgroup", | ||
"hr", | ||
"main", | ||
"nav", | ||
"noscript", | ||
"ol", | ||
"output", | ||
"p", | ||
"pre", | ||
"section", | ||
"table", | ||
"tfoot", | ||
"ul", | ||
"video" | ||
]; | ||
},{}],6:[function(require,module,exports){ | ||
/** | ||
* This file automatically generated from `pre-publish.js`. | ||
@@ -796,4 +824,2 @@ * Do not manually edit. | ||
},{}],7:[function(require,module,exports){ | ||
},{}]},{},[1])(1) | ||
@@ -814,2 +840,33 @@ }); | ||
var toMarkdownOptions = options.toMarkdownOptions = Object(options.toMarkdownOptions); | ||
toMarkdownOptions.converters = toMarkdownOptions.converters || []; | ||
if (!options.ignoreBuiltInConverters) { | ||
toMarkdownOptions.converters.push({ | ||
filter: function (node) { | ||
return node.nodeName === "DIV" && !node.attributes.length; | ||
} | ||
, replacement: function (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); | ||
} | ||
} | ||
} | ||
// Called by medium-editor during init | ||
@@ -819,3 +876,5 @@ 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.base.elements || !this.base.elements.length) { | ||
return; | ||
} | ||
@@ -826,5 +885,11 @@ // Element(s) that this instance of medium-editor is attached to is/are stored in .elements | ||
var handler = function () { | ||
callback(toMarkdown(this.element.innerHTML).split("\n").map(function (c) { | ||
return c.trim(); | ||
}).join("\n").trim()); | ||
var $clone = this.element.cloneNode(true); | ||
var $lists = $clone.querySelectorAll("ul, ol"); | ||
for (var i = 0; i < $lists.length; ++i) { | ||
normalizeList($lists[i]); | ||
} | ||
callback(toMarkdown($clone.innerHTML, options.toMarkdownOptions).split("\n").map(function (c) { | ||
return c.trimRight(); | ||
}).join("\n").trimRight()); | ||
}.bind(this); | ||
@@ -831,0 +896,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(e){if("function"!=typeof MediumEditor)throw new Error("Medium Editor is not loaded on the page.");!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var n;n="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,n.toMarkdown=e()}}(function(){return function e(n,t,r){function i(a,c){if(!t[a]){if(!n[a]){var l="function"==typeof require&&require;if(!c&&l)return l(a,!0);if(o)return o(a,!0);var u=new Error("Cannot find module '"+a+"'");throw u.code="MODULE_NOT_FOUND",u}var f=t[a]={exports:{}};n[a][0].call(f.exports,function(e){var t=n[a][1][e];return i(t?t:e)},f,f.exports,e,n,t,r)}return t[a].exports}for(var o="function"==typeof require&&require,a=0;a<r.length;a++)i(r[a]);return i}({1:[function(e,n,t){"use strict";function r(e){return e.replace(/^[ \r\n\t]+|[ \r\n\t]+$/g,"")}function i(e){return-1!==C.indexOf(e.nodeName.toLowerCase())}function o(e){return-1!==E.indexOf(e.nodeName.toLowerCase())}function a(){var e=x.DOMParser,n=!1;try{(new e).parseFromString("","text/html")&&(n=!0)}catch(t){}return n}function c(){var e=function(){};return e.prototype.parseFromString=function(e){var n=N.implementation.createHTMLDocument("");return e.toLowerCase().indexOf("<!doctype")>-1?n.documentElement.innerHTML=e:n.body.innerHTML=e,n},e}function l(e){var n=(new k).parseFromString(e,"text/html");return w(n,i),n}function u(e){for(var n,t,r,i=[e],o=[];i.length>0;)for(n=i.shift(),o.push(n),t=n.childNodes,r=0;r<t.length;r++)1===t[r].nodeType&&i.push(t[r]);return o.shift(),o}function f(e){for(var n="",t=0;t<e.childNodes.length;t++)if(1===e.childNodes[t].nodeType)n+=e.childNodes[t]._replacement;else{if(3!==e.childNodes[t].nodeType)continue;n+=e.childNodes[t].data}return n}function d(e,n){return e.cloneNode(!1).outerHTML.replace("><",">"+n+"<")}function s(e,n){if("string"==typeof n)return n===e.nodeName.toLowerCase();if(Array.isArray(n))return-1!==n.indexOf(e.nodeName.toLowerCase());if("function"==typeof n)return n.call(g,e);throw new TypeError("`filter` needs to be a string, array, or function")}function p(e,n){var t,r,o;return"left"===e?(t=n.previousSibling,r=/ $/):(t=n.nextSibling,r=/^ /),t&&(3===t.nodeType?o=r.test(t.nodeValue):1!==t.nodeType||i(t)||(o=r.test(t.textContent))),o}function m(e){var n="",t="";if(!i(e)){var r=/^[ \r\n\t]/.test(e.innerHTML),o=/[ \r\n\t]$/.test(e.innerHTML);r&&!p("left",e)&&(n=" "),o&&!p("right",e)&&(t=" ")}return{leading:n,trailing:t}}function h(e){var n,t=f(e);if(!o(e)&&!/A/.test(e.nodeName)&&/^\s*$/i.test(t))return void(e._replacement="");for(var i=0;i<v.length;i++){var a=v[i];if(s(e,a.filter)){if("function"!=typeof a.replacement)throw new TypeError("`replacement` needs to be a function that returns a string");var c=m(e);(c.leading||c.trailing)&&(t=r(t)),n=c.leading+a.replacement.call(g,t,e)+c.trailing;break}}e._replacement=n}var g,v,N,b=e("./lib/md-converters"),y=e("./lib/gfm-converters"),w=e("collapse-whitespace"),x="undefined"!=typeof window?window:this;N="undefined"==typeof document?e("jsdom").jsdom():document;var C=["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"],E=["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"],k=a()?x.DOMParser:c();g=function(e,n){if(n=n||{},"string"!=typeof e)throw new TypeError(e+" is not a string");e=e.replace(/(\d+)\. /g,"$1\\. ");var t,r=l(e).body,i=u(r);v=b.slice(0),n.gfm&&(v=y.concat(v)),n.converters&&(v=n.converters.concat(v));for(var o=i.length-1;o>=0;o--)h(i[o]);return t=f(r),t.replace(/^[\t\r\n]+|[\t\r\n\s]+$/g,"").replace(/\n\s+\n/g,"\n\n").replace(/\n{3,}/g,"\n\n")},g.isBlock=i,g.isVoid=o,g.trim=r,g.outer=d,n.exports=g},{"./lib/gfm-converters":2,"./lib/md-converters":3,"collapse-whitespace":4,jsdom:7}],2:[function(e,n,t){"use strict";function r(e,n){var t=Array.prototype.indexOf.call(n.parentNode.childNodes,n),r=" ";return 0===t&&(r="| "),r+e+" |"}var i=/highlight highlight-(\S+)/;n.exports=[{filter:"br",replacement:function(){return"\n"}},{filter:["del","s","strike"],replacement:function(e){return"~~"+e+"~~"}},{filter:function(e){return"checkbox"===e.type&&"LI"===e.parentNode.nodeName},replacement:function(e,n){return(n.checked?"[x]":"[ ]")+" "}},{filter:["th","td"],replacement:function(e,n){return r(e,n)}},{filter:"tr",replacement:function(e,n){var t="",i={left:":--",right:"--:",center:":-:"};if("THEAD"===n.parentNode.nodeName)for(var o=0;o<n.childNodes.length;o++){var a=n.childNodes[o].attributes.align,c="---";a&&(c=i[a.value]||c),t+=r(c,n.childNodes[o])}return"\n"+e+(t?"\n"+t:"")}},{filter:"table",replacement:function(e){return"\n\n"+e+"\n\n"}},{filter:["thead","tbody","tfoot"],replacement:function(e){return e}},{filter:function(e){return"PRE"===e.nodeName&&e.firstChild&&"CODE"===e.firstChild.nodeName},replacement:function(e,n){return"\n\n```\n"+n.firstChild.textContent+"\n```\n\n"}},{filter:function(e){return"PRE"===e.nodeName&&"DIV"===e.parentNode.nodeName&&i.test(e.parentNode.className)},replacement:function(e,n){var t=n.parentNode.className.match(i)[1];return"\n\n```"+t+"\n"+n.textContent+"\n```\n\n"}},{filter:function(e){return"DIV"===e.nodeName&&i.test(e.className)},replacement:function(e){return"\n\n"+e+"\n\n"}}]},{}],3:[function(e,n,t){"use strict";n.exports=[{filter:"p",replacement:function(e){return"\n\n"+e+"\n\n"}},{filter:"br",replacement:function(){return" \n"}},{filter:["h1","h2","h3","h4","h5","h6"],replacement:function(e,n){for(var t=n.nodeName.charAt(1),r="",i=0;t>i;i++)r+="#";return"\n\n"+r+" "+e+"\n\n"}},{filter:"hr",replacement:function(){return"\n\n* * *\n\n"}},{filter:["em","i"],replacement:function(e){return"_"+e+"_"}},{filter:["strong","b"],replacement:function(e){return"**"+e+"**"}},{filter:function(e){var n=e.previousSibling||e.nextSibling,t="PRE"===e.parentNode.nodeName&&!n;return"CODE"===e.nodeName&&!t},replacement:function(e){return"`"+e+"`"}},{filter:function(e){return"A"===e.nodeName&&e.getAttribute("href")},replacement:function(e,n){var t=n.title?' "'+n.title+'"':"";return"["+e+"]("+n.getAttribute("href")+t+")"}},{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+")":""}},{filter:function(e){return"PRE"===e.nodeName&&"CODE"===e.firstChild.nodeName},replacement:function(e,n){return"\n\n "+n.firstChild.textContent.replace(/\n/g,"\n ")+"\n\n"}},{filter:"blockquote",replacement:function(e){return e=this.trim(e),e=e.replace(/\n{3,}/g,"\n\n"),e=e.replace(/^/gm,"> "),"\n\n"+e+"\n\n"}},{filter:"li",replacement:function(e,n){e=e.replace(/^\s+/,"").replace(/\n/gm,"\n ");var t="* ",r=n.parentNode,i=Array.prototype.indexOf.call(r.children,n)+1;return t=/ol/i.test(r.nodeName)?i+". ":"* ",t+e}},{filter:["ul","ol"],replacement:function(e,n){for(var t=[],r=0;r<n.childNodes.length;r++)t.push(n.childNodes[r]._replacement);return/li/i.test(n.parentNode.nodeName)?"\n"+t.join("\n"):"\n\n"+t.join("\n")+"\n\n"}},{filter:function(e){return this.isBlock(e)},replacement:function(e,n){return"\n\n"+this.outer(n,e)+"\n\n"}},{filter:function(){return!0},replacement:function(e,n){return this.outer(n,e)}}]},{}],4:[function(e,n,t){"use strict";function r(e){return!(!e||!u[e.nodeName])}function i(e){return!(!e||!l[e.nodeName])}function o(e,n){if(e.firstChild&&"PRE"!==e.nodeName){"function"!=typeof n&&(n=r);for(var t=null,o=!1,l=null,u=c(l,e);u!==e;){if(3===u.nodeType){var f=u.data.replace(/[ \r\n\t]+/g," ");if(t&&!/ $/.test(t.data)||o||" "!==f[0]||(f=f.substr(1)),!f){u=a(u);continue}u.data=f,t=u}else{if(1!==u.nodeType){u=a(u);continue}n(u)||"BR"===u.nodeName?(t&&(t.data=t.data.replace(/ $/,"")),t=null,o=!1):i(u)&&(t=null,o=!0)}var d=c(l,u);l=u,u=d}t&&(t.data=t.data.replace(/ $/,""),t.data||a(t))}}function a(e){var n=e.nextSibling||e.parentNode;return e.parentNode.removeChild(e),n}function c(e,n){return e&&e.parentNode===n||"PRE"===n.nodeName?n.nextSibling||n.parentNode:n.firstChild||n.nextSibling||n.parentNode}var l=e("void-elements");Object.keys(l).forEach(function(e){l[e.toUpperCase()]=1});var u={};e("block-elements").forEach(function(e){u[e.toUpperCase()]=1}),n.exports=o},{"block-elements":5,"void-elements":6}],5:[function(e,n,t){n.exports=["address","article","aside","audio","blockquote","canvas","dd","div","dl","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","header","hgroup","hr","main","nav","noscript","ol","output","p","pre","section","table","tfoot","ul","video"]},{}],6:[function(e,n,t){n.exports={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuitem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0}},{}],7:[function(e,n,t){},{}]},{},[1])(1)});var n=function(e,n){"function"==typeof e&&(n=e,e={}),e=Object(e),e.events=e.events||["input","change"],n=n||e.callback||function(){},this.init=function(){if(this.base.elements&&this.base.elements.length){this.element=this.base.elements[0];var t=function(){n(toMarkdown(this.element.innerHTML).split("\n").map(function(e){return e.trim()}).join("\n").trim())}.bind(this);e.events.forEach(function(e){this.element.addEventListener(e,t)}.bind(this)),t()}}};e.MeMarkdown=n}(this); | ||
!function(e){if("function"!=typeof MediumEditor)throw new Error("Medium Editor is not loaded on the page.");!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var n;n="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,n.toMarkdown=e()}}(function(){return function e(n,t,r){function o(a,c){if(!t[a]){if(!n[a]){var l="function"==typeof require&&require;if(!c&&l)return l(a,!0);if(i)return i(a,!0);var u=new Error("Cannot find module '"+a+"'");throw u.code="MODULE_NOT_FOUND",u}var f=t[a]={exports:{}};n[a][0].call(f.exports,function(e){var t=n[a][1][e];return o(t?t:e)},f,f.exports,e,n,t,r)}return t[a].exports}for(var i="function"==typeof require&&require,a=0;a<r.length;a++)o(r[a]);return o}({1:[function(e,n,t){"use strict";function r(e){return N.indexOf(e.nodeName.toLowerCase())!==-1}function o(e){return y.indexOf(e.nodeName.toLowerCase())!==-1}function i(e){var n=(new g).parseFromString(e,"text/html");return b(n.documentElement,r),n}function a(e){for(var n,t,r,o=[e],i=[];o.length>0;)for(n=o.shift(),i.push(n),t=n.childNodes,r=0;r<t.length;r++)1===t[r].nodeType&&o.push(t[r]);return i.shift(),i}function c(e){for(var n="",t=0;t<e.childNodes.length;t++)if(1===e.childNodes[t].nodeType)n+=e.childNodes[t]._replacement;else{if(3!==e.childNodes[t].nodeType)continue;n+=e.childNodes[t].data}return n}function l(e,n){return e.cloneNode(!1).outerHTML.replace("><",">"+n+"<")}function u(e,n){if("string"==typeof n)return n===e.nodeName.toLowerCase();if(Array.isArray(n))return n.indexOf(e.nodeName.toLowerCase())!==-1;if("function"==typeof n)return n.call(p,e);throw new TypeError("`filter` needs to be a string, array, or function")}function f(e,n){var t,o,i;return"left"===e?(t=n.previousSibling,o=/ $/):(t=n.nextSibling,o=/^ /),t&&(3===t.nodeType?i=o.test(t.nodeValue):1!==t.nodeType||r(t)||(i=o.test(t.textContent))),i}function d(e){var n="",t="";if(!r(e)){var o=/^[ \r\n\t]/.test(e.innerHTML),i=/[ \r\n\t]$/.test(e.innerHTML);o&&!f("left",e)&&(n=" "),i&&!f("right",e)&&(t=" ")}return{leading:n,trailing:t}}function s(e){var n,t=c(e);if(!o(e)&&!/A|TH|TD/.test(e.nodeName)&&/^\s*$/i.test(t))return void(e._replacement="");for(var r=0;r<m.length;r++){var i=m[r];if(u(e,i.filter)){if("function"!=typeof i.replacement)throw new TypeError("`replacement` needs to be a function that returns a string");var a=d(e);(a.leading||a.trailing)&&(t=t.trim()),n=a.leading+i.replacement.call(p,t,e)+a.trailing;break}}e._replacement=n}var p,m,h=e("./lib/md-converters"),v=e("./lib/gfm-converters"),g=e("./lib/html-parser"),b=e("collapse-whitespace"),N=["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"],y=["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"];p=function(e,n){if(n=n||{},"string"!=typeof e)throw new TypeError(e+" is not a string");e=e.replace(/(\d+)\. /g,"$1\\. ");var t,r=i(e).body,o=a(r);m=h.slice(0),n.gfm&&(m=v.concat(m)),n.converters&&(m=n.converters.concat(m));for(var l=o.length-1;l>=0;l--)s(o[l]);return t=c(r),t.replace(/^[\t\r\n]+|[\t\r\n\s]+$/g,"").replace(/\n\s+\n/g,"\n\n").replace(/\n{3,}/g,"\n\n")},p.isBlock=r,p.isVoid=o,p.outer=l,n.exports=p},{"./lib/gfm-converters":2,"./lib/html-parser":3,"./lib/md-converters":4,"collapse-whitespace":7}],2:[function(e,n,t){"use strict";function r(e,n){var t=Array.prototype.indexOf.call(n.parentNode.childNodes,n),r=" ";return 0===t&&(r="| "),r+e+" |"}var o=/highlight highlight-(\S+)/;n.exports=[{filter:"br",replacement:function(){return"\n"}},{filter:["del","s","strike"],replacement:function(e){return"~~"+e+"~~"}},{filter:function(e){return"checkbox"===e.type&&"LI"===e.parentNode.nodeName},replacement:function(e,n){return(n.checked?"[x]":"[ ]")+" "}},{filter:["th","td"],replacement:function(e,n){return r(e,n)}},{filter:"tr",replacement:function(e,n){var t="",o={left:":--",right:"--:",center:":-:"};if("THEAD"===n.parentNode.nodeName)for(var i=0;i<n.childNodes.length;i++){var a=n.childNodes[i].attributes.align,c="---";a&&(c=o[a.value]||c),t+=r(c,n.childNodes[i])}return"\n"+e+(t?"\n"+t:"")}},{filter:"table",replacement:function(e){return"\n\n"+e+"\n\n"}},{filter:["thead","tbody","tfoot"],replacement:function(e){return e}},{filter:function(e){return"PRE"===e.nodeName&&e.firstChild&&"CODE"===e.firstChild.nodeName},replacement:function(e,n){return"\n\n```\n"+n.firstChild.textContent+"\n```\n\n"}},{filter:function(e){return"PRE"===e.nodeName&&"DIV"===e.parentNode.nodeName&&o.test(e.parentNode.className)},replacement:function(e,n){var t=n.parentNode.className.match(o)[1];return"\n\n```"+t+"\n"+n.textContent+"\n```\n\n"}},{filter:function(e){return"DIV"===e.nodeName&&o.test(e.className)},replacement:function(e){return"\n\n"+e+"\n\n"}}]},{}],3:[function(e,n,t){function r(){var e=a.DOMParser,n=!1;try{(new e).parseFromString("","text/html")&&(n=!0)}catch(e){}return n}function o(){var n=function(){};if("undefined"==typeof document){var t=e("jsdom");n.prototype.parseFromString=function(e){return t.jsdom(e,{features:{FetchExternalResources:[],ProcessExternalResources:!1}})}}else i()?n.prototype.parseFromString=function(e){var n=new window.ActiveXObject("htmlfile");return n.designMode="on",n.open(),n.write(e),n.close(),n}:n.prototype.parseFromString=function(e){var n=document.implementation.createHTMLDocument("");return n.open(),n.write(e),n.close(),n};return n}function i(){var e=!1;try{document.implementation.createHTMLDocument("").open()}catch(n){window.ActiveXObject&&(e=!0)}return e}var a="undefined"!=typeof window?window:this;n.exports=r()?a.DOMParser:o()},{jsdom:6}],4:[function(e,n,t){"use strict";n.exports=[{filter:"p",replacement:function(e){return"\n\n"+e+"\n\n"}},{filter:"br",replacement:function(){return" \n"}},{filter:["h1","h2","h3","h4","h5","h6"],replacement:function(e,n){for(var t=n.nodeName.charAt(1),r="",o=0;o<t;o++)r+="#";return"\n\n"+r+" "+e+"\n\n"}},{filter:"hr",replacement:function(){return"\n\n* * *\n\n"}},{filter:["em","i"],replacement:function(e){return"_"+e+"_"}},{filter:["strong","b"],replacement:function(e){return"**"+e+"**"}},{filter:function(e){var n=e.previousSibling||e.nextSibling,t="PRE"===e.parentNode.nodeName&&!n;return"CODE"===e.nodeName&&!t},replacement:function(e){return"`"+e+"`"}},{filter:function(e){return"A"===e.nodeName&&e.getAttribute("href")},replacement:function(e,n){var t=n.title?' "'+n.title+'"':"";return"["+e+"]("+n.getAttribute("href")+t+")"}},{filter:"img",replacement:function(e,n){var t=n.alt||"",r=n.getAttribute("src")||"",o=n.title||"",i=o?' "'+o+'"':"";return r?"!["+t+"]("+r+i+")":""}},{filter:function(e){return"PRE"===e.nodeName&&"CODE"===e.firstChild.nodeName},replacement:function(e,n){return"\n\n "+n.firstChild.textContent.replace(/\n/g,"\n ")+"\n\n"}},{filter:"blockquote",replacement:function(e){return e=e.trim(),e=e.replace(/\n{3,}/g,"\n\n"),e=e.replace(/^/gm,"> "),"\n\n"+e+"\n\n"}},{filter:"li",replacement:function(e,n){e=e.replace(/^\s+/,"").replace(/\n/gm,"\n ");var t="* ",r=n.parentNode,o=Array.prototype.indexOf.call(r.children,n)+1;return t=/ol/i.test(r.nodeName)?o+". ":"* ",t+e}},{filter:["ul","ol"],replacement:function(e,n){for(var t=[],r=0;r<n.childNodes.length;r++)t.push(n.childNodes[r]._replacement);return/li/i.test(n.parentNode.nodeName)?"\n"+t.join("\n"):"\n\n"+t.join("\n")+"\n\n"}},{filter:function(e){return this.isBlock(e)},replacement:function(e,n){return"\n\n"+this.outer(n,e)+"\n\n"}},{filter:function(){return!0},replacement:function(e,n){return this.outer(n,e)}}]},{}],5:[function(e,n,t){n.exports=["address","article","aside","audio","blockquote","canvas","dd","div","dl","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","header","hgroup","hr","main","nav","noscript","ol","output","p","pre","section","table","tfoot","ul","video"]},{}],6:[function(e,n,t){},{}],7:[function(e,n,t){"use strict";function r(e){return!(!e||!u[e.nodeName])}function o(e){return!(!e||!l[e.nodeName])}function i(e,n){if(e.firstChild&&"PRE"!==e.nodeName){"function"!=typeof n&&(n=r);for(var t=null,i=!1,l=null,u=c(l,e);u!==e;){if(3===u.nodeType){var f=u.data.replace(/[ \r\n\t]+/g," ");if(t&&!/ $/.test(t.data)||i||" "!==f[0]||(f=f.substr(1)),!f){u=a(u);continue}u.data=f,t=u}else{if(1!==u.nodeType){u=a(u);continue}n(u)||"BR"===u.nodeName?(t&&(t.data=t.data.replace(/ $/,"")),t=null,i=!1):o(u)&&(t=null,i=!0)}var d=c(l,u);l=u,u=d}t&&(t.data=t.data.replace(/ $/,""),t.data||a(t))}}function a(e){var n=e.nextSibling||e.parentNode;return e.parentNode.removeChild(e),n}function c(e,n){return e&&e.parentNode===n||"PRE"===n.nodeName?n.nextSibling||n.parentNode:n.firstChild||n.nextSibling||n.parentNode}var l=e("void-elements");Object.keys(l).forEach(function(e){l[e.toUpperCase()]=1});var u={};e("block-elements").forEach(function(e){u[e.toUpperCase()]=1}),n.exports=i},{"block-elements":5,"void-elements":8}],8:[function(e,n,t){n.exports={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuitem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0}},{}]},{},[1])(1)});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 a=n[r-1];if(/^UL|OL$/.test(o.tagName)){try{a.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.toMarkdownOptions=Object(e.toMarkdownOptions);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=function(){for(var r=this.element.cloneNode(!0),o=r.querySelectorAll("ul, ol"),i=0;i<o.length;++i)t(o[i]);n(toMarkdown(r.innerHTML,e.toMarkdownOptions).split("\n").map(function(e){return e.trimRight()}).join("\n").trimRight())}.bind(this);e.events.forEach(function(e){this.element.addEventListener(e,r)}.bind(this)),r()}}};e.MeMarkdown=n}(this); |
{ | ||
"name": "medium-editor-markdown", | ||
"version": "2.4.1", | ||
"version": "2.5.0", | ||
"description": "A Medium Editor extension to add markdown support.", | ||
@@ -16,11 +16,13 @@ "main": "src/medium-editor-md.js", | ||
"@prevuelta (http://www.pablorevuelta.com/)", | ||
"okmttdhr (http://okmttdhr.github.io/)" | ||
"okmttdhr (http://okmttdhr.github.io/)", | ||
"Anna Yafi (https://github.com/annayafi/)" | ||
], | ||
"license": "MIT", | ||
"dependencies": { | ||
"barbe": "^2.0.0", | ||
"to-markdown": "^2.0.1", | ||
"to-markdown": "^3.0.1" | ||
}, | ||
"devDependencies": { | ||
"barbe": "^3.0.6", | ||
"uglify-js": "^2.5.0" | ||
}, | ||
"devDependencies": {}, | ||
"repository": { | ||
@@ -123,2 +125,3 @@ "type": "git", | ||
"menu/", | ||
"scripts/", | ||
"cli.js", | ||
@@ -125,0 +128,0 @@ "index.js" |
[![medium-editor-markdown](http://i.imgur.com/xb6JPkv.png)](#) | ||
# Medium Editor Markdown [![PayPal](https://img.shields.io/badge/%24-paypal-f39c12.svg)][paypal-donations] [![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) [![Get help on Codementor](https://cdn.codementor.io/badges/get_help_github.svg)](https://www.codementor.io/johnnyb?utm_source=github&utm_medium=button&utm_term=johnnyb&utm_campaign=github) | ||
# Medium Editor Markdown | ||
[![PayPal](https://img.shields.io/badge/%24-paypal-f39c12.svg)][paypal-donations] [![AMA](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) [![Get help on Codementor](https://cdn.codementor.io/badges/get_help_github.svg)](https://www.codementor.io/johnnyb?utm_source=github&utm_medium=button&utm_term=johnnyb&utm_campaign=github) | ||
> A Medium Editor extension to add markdown support. | ||
@@ -57,16 +59,2 @@ | ||
## :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. | ||
- **Function** `callback`: The callback function that is called with the markdown code (first argument). | ||
## :yum: How to contribute | ||
@@ -73,0 +61,0 @@ Have an idea? Found a bug? See [how to contribute][contributing]. |
@@ -0,1 +1,3 @@ | ||
"use strict"; | ||
/** | ||
@@ -11,2 +13,4 @@ * MeMarkdown | ||
* - `callback` (Function): The callback function. If the second argument is a function, then it has greater priority. | ||
* - `toMarkdownOptions` (Object): Options to pass to the markdown converter code. | ||
* - `ignorebuiltinconverters` (Boolean): If `true`, the default converters passed to `toMarkdown` will be ignored. | ||
* | ||
@@ -30,2 +34,33 @@ * @param {Function} callback The callback function that is called with the markdown code (first argument). | ||
var toMarkdownOptions = options.toMarkdownOptions = Object(options.toMarkdownOptions); | ||
toMarkdownOptions.converters = toMarkdownOptions.converters || []; | ||
if (!options.ignoreBuiltInConverters) { | ||
toMarkdownOptions.converters.push({ | ||
filter: function (node) { | ||
return node.nodeName === "DIV" && !node.attributes.length; | ||
} | ||
, replacement: function (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); | ||
} | ||
} | ||
} | ||
// Called by medium-editor during init | ||
@@ -35,3 +70,5 @@ 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.base.elements || !this.base.elements.length) { | ||
return; | ||
} | ||
@@ -42,5 +79,11 @@ // Element(s) that this instance of medium-editor is attached to is/are stored in .elements | ||
var handler = function () { | ||
callback(toMarkdown(this.element.innerHTML).split("\n").map(function (c) { | ||
return c.trim(); | ||
}).join("\n").trim()); | ||
var $clone = this.element.cloneNode(true); | ||
var $lists = $clone.querySelectorAll("ul, ol"); | ||
for (var i = 0; i < $lists.length; ++i) { | ||
normalizeList($lists[i]); | ||
} | ||
callback(toMarkdown($clone.innerHTML, options.toMarkdownOptions).split("\n").map(function (c) { | ||
return c.trimRight(); | ||
}).join("\n").trimRight()); | ||
}.bind(this); | ||
@@ -47,0 +90,0 @@ |
47379
1
900
2
80
+ Addedacorn@4.0.13(transitive)
+ Addedacorn-globals@3.1.0(transitive)
+ Addedcontent-type-parser@1.0.2(transitive)
+ Addedhtml-encoding-sniffer@1.0.2(transitive)
+ Addedjsdom@9.12.0(transitive)
+ Addedto-markdown@3.1.1(transitive)
+ Addedwebidl-conversions@4.0.2(transitive)
+ Addedwhatwg-encoding@1.0.5(transitive)
+ Addedwhatwg-url@4.8.0(transitive)
- Removedbarbe@^2.0.0
- Removeduglify-js@^2.5.0
- Removedacorn@2.7.0(transitive)
- Removedacorn-globals@1.0.9(transitive)
- Removedalign-text@0.1.4(transitive)
- Removedbarbe@2.2.1(transitive)
- Removedcamelcase@1.2.1(transitive)
- Removedcenter-align@0.1.3(transitive)
- Removedcliui@2.1.0(transitive)
- Removeddecamelize@1.2.0(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedjsdom@8.5.0(transitive)
- Removedkind-of@3.2.2(transitive)
- Removedlazy-cache@1.0.4(transitive)
- Removedlongest@1.0.1(transitive)
- Removedregex-escape@3.4.10(transitive)
- Removedrepeat-string@1.6.1(transitive)
- Removedright-align@0.1.3(transitive)
- Removedsource-map@0.5.7(transitive)
- Removedto-markdown@2.1.0(transitive)
- Removeduglify-js@2.8.29(transitive)
- Removeduglify-to-browserify@1.0.2(transitive)
- Removedwhatwg-url@2.0.1(transitive)
- Removedwindow-size@0.1.0(transitive)
- Removedwordwrap@0.0.2(transitive)
- Removedyargs@3.10.0(transitive)
Updatedto-markdown@^3.0.1