Last active
September 2, 2016 09:59
-
-
Save tmthrgd/7c573dfdaff3a7927bc984275250b99b to your computer and use it in GitHub Desktop.
Convert HTML to Markdown in DOM Javascript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
(c) Tom Thorogood 2015 | |
https://tomthorogood.co.uk | |
Copyright 2015 Tom Thorogood. All rights reserved. | |
Use of this source code is governed by a | |
Modified BSD License license that can be found in | |
the LICENSE file. | |
*/ | |
window.html2md = function () { | |
var ELEMENTS = { | |
"#text": function text() { | |
// Escape special characters? | |
for (var pn = this.parentNode; pn; pn = pn.parentNode) { | |
if (pn.nodeName === "pre") { | |
return this.textContent; | |
} | |
} | |
return this.parentNode.nodeName === "ol" || this.parentNode.nodeName === "ul" | |
? this.textContent.trim() | |
: this.textContent.replace(/\s+/gm, " "); | |
}, | |
"p": function p() { | |
return this.attributes.length | |
? this.outerHTML | |
: this.parentNode.nodeName === "blockquote" | |
? "\n> " + convert(this) + "\n" | |
: "\n\n" + convert(this) + "\n"; | |
}, | |
"a": function a() { | |
var href = this.getAttribute("href"); | |
var title = this.getAttribute("title"); | |
return this.attributes.length <= 2 && href && (this.attributes.length === 2 ? title : true) && this.children.length === 0 | |
? "[" + this.textContent + "](" + href + (title ? ' "' + title[1] + '"' : "") + ")" | |
: this.outerHTML; | |
}, | |
"h1": hN, | |
"h2": hN, | |
"h3": hN, | |
"h4": hN, | |
"h5": hN, | |
"h6": hN, | |
"br": function br() { | |
return this.attributes.length | |
? this.outerHTML | |
: " \n"; | |
}, | |
"hr": function hr() { | |
return this.attributes.length | |
? this.outerHTML | |
: "\n\n---\n"; | |
}, | |
"b": strong, | |
"strong": strong, | |
"i": em, | |
"em": em, | |
"pre": function pre() { | |
return this.attributes.length || this.children.length !== 1 || this.children[0].nodeName != "code" | |
? this.outerHTML | |
: convert(this); | |
}, | |
"code": function code() { | |
return this.attributes.length | |
? this.outerHTML | |
//: "`" + convert(this) + "`"; | |
: " " + convert(this).split("\n").join("\n "); | |
}, | |
"img": function img() { | |
var src = this.getAttribute("src"); | |
var alt = this.getAttribute("alt"); | |
var title = this.getAttribute("title"); | |
return this.attributes.length <= 3 && src && (this.attributes.length === 2 ? title || alt : true) && (this.attributes.length === 3 ? title && alt : true) && this.children.length === 0 | |
? "![" + alt + "](" + src + (title ? ' "' + title + '"' : "") + ")" | |
: this.outerHTML; | |
}, | |
"ol": xl, | |
"ul": xl, | |
"li": function li() { | |
return this.attributes.length | |
? this.outerHTML | |
: this.parentNode.nodeName === "ol" | |
? (Array.prototype.indexOf.call(this.parentNode.children, this) + 1) + ". " | |
+ convert(this) + "\n" | |
: this.parentNode.nodeName === "ul" | |
? "+ " + convert(this) + "\n" | |
: this.outerHTML; | |
}, | |
"blockquote": function blockquote() { | |
return this.attributes.length | |
? this.outerHTML | |
: this.children.length !== 0 && Array.prototype.find.call(this.children, function isP(elem) { return elem.nodeName === "p"; }) | |
? convert(this) | |
: "\n> " + convert(this) + "\n"; | |
}, | |
}; | |
function hN() { | |
return this.attributes.length | |
? this.outerHTML | |
: "\n\n" + Array((this.nodeName[1]|0) + 1).join("#") + " " + convert(this) + "\n"; | |
}; | |
function strong() { | |
return this.attributes.length | |
? this.outerHTML | |
: "**" + convert(this) + "**"; | |
}; | |
function em() { | |
return this.attributes.length | |
? this.outerHTML | |
: "_" + convert(this) + "_"; | |
}; | |
function xl() { | |
return this.attributes.length | |
? this.outerHTML | |
: "\n" + convert(this) + "\n"; | |
} | |
function convert(elem) { | |
return Array.prototype.map.call(elem.childNodes, function (child) { | |
return child.nodeName in ELEMENTS | |
? ELEMENTS[child.nodeName].call(child) | |
: child.outerHTML; | |
}).join(""); | |
} | |
return function html2md(html) { | |
var doc = document.implementation.createDocument("http://www.w3.org/1999/xhtml", "html", null); | |
var body = document.createElementNS("http://www.w3.org/1999/xhtml", "body"); | |
doc.documentElement.appendChild(body); | |
body.innerHTML = html; | |
return convert(body).replace(/^(?:\r?\n)+|(?:\r?\n)+$/g, ""); | |
} | |
}(); | |
html2md(`<h1>HTML Ipsum Presents</h1> | |
<p><strong>Pellentesque habitant morbi tristique</strong> senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. <em>Aenean ultricies mi vitae est.</em> Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, <code>commodo vitae</code>, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. <a href="#">Donec non enim</a> in turpis pulvinar facilisis. Ut felis.</p> | |
<h2>Header Level 2</h2> | |
<ol> | |
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li> | |
<li>Aliquam tincidunt mauris eu risus.</li> | |
</ol> | |
<blockquote><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus, at luctus turpis elit sit amet quam. Vivamus pretium ornare est.</p></blockquote> | |
<h3>Header Level 3</h3> | |
<ul> | |
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li> | |
<li>Aliquam tincidunt mauris eu risus.</li> | |
</ul> | |
<pre><code> | |
#header h1 a { | |
display: block; | |
width: 300px; | |
height: 80px; | |
} | |
</code></pre>`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright (c) 2015, Tom Thorogood. | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright | |
notice, this list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright | |
notice, this list of conditions and the following disclaimer in the | |
documentation and/or other materials provided with the distribution. | |
* Neither the name of the Tom Thorogood nor the | |
names of its contributors may be used to endorse or promote products | |
derived from this software without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment