with
*/
var PRETTY_PRINT = 1;
/** Wrap the converted Markdown text with a . */
var WRAP_GLOBAL_DIV = false;
var ignore_BR = false;
var areastyle = 'font-size: small; overflow: auto; font-family: monospace; width:100%; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box;';
/** "Main function" */
$(function() {
// Output simulation UI as HTML
$('#regexhtml2md').html('\
HTML-Input
\
\
\
\
\
To convert, press Ctrl Enter or click on the "Convert" button.\
\
\
\
\
\
\
');
$('#rhtml2md-input').keydown(function (event) {
if (event.keyCode === 10 || event.keyCode == 13 && event.ctrlKey) {
// Ctrl-Enter pressed
convert(event);
}
});
// Bind start button
$("#rhtml2md-convert").click(function(event) {
convert(event)
});
$('#rhtml2md-input').focusin(function(event) {
$(this).select();
});
$('#rhtml2md-input').select();
convert(null);
});
/**
* Converts basic HTML to Markdown Extra with regex.
*
* @param event JavaScript event
*
* @return false
*/
function convert(event) {
if (event !== null) event.preventDefault();
var input = $('#rhtml2md-input').val();
ignore_BR = $('#rhtml2md-br').attr('checked');
var converted = regexConvert(input, ignore_BR);
// Write conversion output
$('#rhtml2md-output').html('\
Markdown Extra Output
\
\
Show terms of service\
\
');
$('#rhtml2md-output-legal-link').click(function () {
if ($('#rhtml2md-output-legal-text').is(':visible')) {
$(this).html($(this).html().replace(/Hide/, 'Show'));
} else {
$(this).html($(this).html().replace(/Show/, 'Hide'));
}
// Do it afterwards as the operation is async
$('#rhtml2md-output-legal-text').slideToggle('slow');
});
$('#rhtml2md-output-md').text(converted);
$('#rhtml2md-output-md').focusin(function(event) {
$(this).select();
});
$('#rhtml2md-output-md').select();
// Copy to clipboard, ref: https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand
document.execCommand("copy", false, null);
if (typeof Drupal != 'undefined') {
// Run attached Drupal behaviors
Drupal.attachBehaviors($('#regexhtml2md').get());
}
return false;
}
/**
* Regex replacer function inserting a warning comment if nested lists
* are detected.
*
* @return the replaced string
*/
function ulOlNestingDetector(match, p1, p2, offset, string) {
if (p2.match(/<(ul|ol).*?>[\s\S]*?<\/\1>/)) {
return '\n\n' + match + '\n';
} else {
return match;
}
}
/**
* Regex replacer function inserting a warning comment if nested blockquotes
* are detected.
*
* @return the replaced string
*/
function blockquoteNestingDetector(match, p1, p2, offset, string) {
if (p2.match(/<(blockquote).*?>[\s\S]*?<\/\1>/)) {
return '\n\n' + match + '\n';
} else {
return match;
}
}
/**
* Main string based conversion function.
*
* @param input string to convert
*
* @return the converted string
*/
function regexConvert(input) {
var converted = '';
var s = input;
var pat = /\s*(?:<(code)>|<(\?)php)([\s\S]+?)(?:<\/\1>|^\2>)\s*/mi; //[\s\S] = dotall; ? = non-greedy match
//var pat = /\s*(?:<(code)>)([\s\S]+?)(?:<\/\1>)\s*/mi; //[\s\S] = dotall; ? = non-greedy match
for (var i; (i = s.search(pat)) !== -1; ) {
converted += mdConvert(s.substring(0, i));
var m = s.match(pat)[0];
switch (PRETTY_PRINT) {
case 1 :
converted += m.replace(/^\s*\s*$/gim, '\n\n~~~~ {.prettycode .lang-js}')
.replace(/^\s*<\/code>\s*$/gim, '~~~~\n\n')
.replace(/<\/?code>/gim, '`')
.replace(/^<\?php/im, '\n/im, '?>\n');
break;
case 2 :
converted += m.replace(/^\s*\s*/gim, '\n\n\n')
.replace(/^\s*<\/code>/gim, '
\n\n\n')
.replace(/^<\?php/im, '\n/im, '?>\n');
break;
case 0 :
default :
converted += '\n\n' + m + '\n\n';
}
s = s.substring(i + m.length);
}
converted += mdConvert(s);
if (WRAP_GLOBAL_DIV) converted = '\n' + converted + '\n';
return converted;
}
/**
* Replaces HTML with Markdown markup.
*
* @param input string to convert
*
* @return the converted string
*/
function mdConvert(input) {
var converted = input;
converted = converted.replace(/<(ul|ol).*?>([\s\S]*?)<\/\1>/igm, ulOlNestingDetector);
converted = convertBlockquote(convertOl(converted)).replace(//igm, '\n').replace(/<\/p>/igm, '\n')
.replace(/><(strong|b)>/igm, '> **').replace(/<\/(strong|b)>/igm, '**')
.replace(/><(em|i)>/igm, '> _').replace(/<\/(em|i)>/igm, '_')
.replace(/<\/?ul>/igm, '\n')
.replace(/
/igm, '* ').replace(/<\/li>/igm, '')
.replace(/(.*?)<\/h1>/igm, '\n# $2 {#$1}\n')
.replace(/(.*?)<\/h2>/igm, '\n## $2 {#$1}\n')
.replace(/(.*?)<\/h3>/igm, '\n### $2 {#$1}\n')
.replace(/(.*?)<\/h4>/igm, '\n#### $2 {#$1}\n')
.replace(/(.*?)<\/h5>/igm, '\n##### $2 {#$1}\n')
.replace(/(.*?)<\/h6>/igm, '\n###### $2 {#$1}\n')
.replace(//igm, '\n# ').replace(//igm, '\n## ').replace(//igm, '\n### ').replace(//igm, '\n#### ').replace(//igm, '\n##### ').replace(//igm, '\n###### ').replace(/<\/h[123456]>/igm, '\n')
.replace(/([^<]+)<\/a>/igm, '[$2]($1)')
.replace(/([^<]+)<\/a>/igm, '[$3]($1 "$2")')
.replace(/
/, '\n- - -\n')
.replace(/\s*
]*)>/ig, '\n\n')
.replace(/\s*]*)>.*?<\/colgroup>\s*/igm, '')
.replace(/\s*<\/?thead([^>]*)>\s*/ig, '')
.replace(/\s*<\/?tbody([^>]*)>\s*/ig, '')
.replace(/\s* \s*/igm, '\n')
.replace(/<\/th>\s* /igm, ' | ')
.replace(/<\/th>\s*<\/tr>/igm, '\n - | -\n')
.replace(/\s* \s*/igm, '\n')
.replace(/<\/td>\s* /igm, ' | ')
.replace(/<\/td>\s*<\/tr>/igm, '')
.replace(/\s*<\/table>/ig, '\n<\/div>')
.replace(/<\/?font[^>]*>/ig, '')
;
converted = ignore_BR ? converted.replace(/
/gm, '
\n')
: converted.replace(/
/gm, ' \n');
converted = converted.replace(/\n{3,}/gm, '\n\n');
converted = converted.replace(/(^\n+|\n+$)/g, '');
return converted;
}
/** Convert ol > li to 1. */
function convertOl(str) {
var r = str;
var pat = /([\s\S]*?)<\/ol>/mi; //[\s\S] = dotall; ? = non-greedy match
var lipat = /- /i;
for (var mat; (mat = r.match(pat)) !== null; ) {
mat = mat[1].replace(/<\/li>/igm, '')/*.replace(/
- /igm, '1. ')*/;
for (var c = 1; mat.search(lipat) !== -1; c++) {
mat = mat.replace(lipat, c + '. ');
}
r = r.replace(pat, '\n' + mat + '\n');
}
return r;
}
/** Convert simple blockquote. */
function convertBlockquote(str) {
var r = str;
//r = r.replace(/<(blockquote).*?>([\s\S]*?)<\/\1>/igm, blockquoteNestingDetector);
var pat = /
\n?([\s\S]*?)\n?<\/blockquote>/mi; //[\s\S] = dotall; ? = non-greedy match
for (var mat; (mat = r.match(pat)) !== null; ) {
mat = mat[1].replace(/\n/gm, '\n> ').replace(//igm, '\n> ').replace(/<\/p>/igm, '\n> \n> ')
.replace(/(\n> ?){3,}/gm,'\n> \n> ');
r = r.replace(pat, '\n' + mat + '\n');
}
return r;
}
})(jQuery);