mirror of https://github.com/mindoc-org/mindoc.git
commit
a7304381b1
|
@ -88,7 +88,18 @@ $(function () {
|
|||
saveDocument(false);
|
||||
} else if (name === "template") {
|
||||
$("#documentTemplateModal").modal("show");
|
||||
} else if (name === "sidebar") {
|
||||
} else if (name=="word2md"){
|
||||
$("#word2mdform")[0].reset();
|
||||
$("#output").html("");
|
||||
$("#messages").html("");
|
||||
$("#word2md").modal("show");
|
||||
|
||||
}else if (name=="Pasteoffice"){
|
||||
$("#Pasteofficeform")[0].reset();
|
||||
$("#Pastearea").innerText="";
|
||||
$("#officeoutmd").val("");
|
||||
$("#Pasteoffice").modal("show");
|
||||
}else if (name === "sidebar") {
|
||||
$("#manualCategory").toggle(0, "swing", function () {
|
||||
var $then = $("#manualEditorContainer");
|
||||
var left = parseInt($then.css("left"));
|
||||
|
@ -425,6 +436,221 @@ $(function () {
|
|||
|
||||
loadDocument(selected);
|
||||
}).on("move_node.jstree", jstree_save);
|
||||
|
||||
//对html进行预处理
|
||||
function firstfilter(str) {
|
||||
//处理一下html,忽略从WORD粘贴时特殊情况下尾部乱码
|
||||
if (/(<html[\s\S]*<\/html>)/gi.test(str)) {
|
||||
str = str.match(/(<html[\s\S]*<\/html>)/gi)[0];
|
||||
}
|
||||
//去掉头部描述
|
||||
return str.replace(/<head>[\s\S]*<\/head>/gi, "")
|
||||
//去掉注释
|
||||
.replace(/<!--[\s\S]*?-->/ig, "")
|
||||
//去掉隐藏元素
|
||||
.replace(/<([a-z0-9]*)[^>]*\s*display:none[\s\S]*?><\/\1>/gi, '');
|
||||
}
|
||||
|
||||
|
||||
//去除冗余属性和标签
|
||||
function filterPasteWord(str) {
|
||||
return str.replace(/[\t\r\n]+/g, ' ').replace(/<!--[\s\S]*?-->/ig, "")
|
||||
//转换图片
|
||||
.replace(/<v:shape [^>]*>[\s\S]*?.<\/v:shape>/gi,
|
||||
function (str) {
|
||||
//opera能自己解析出image所这里直接返回空
|
||||
if (!!window.opera && window.opera.version) {
|
||||
return '';
|
||||
}
|
||||
try {
|
||||
//有可能是bitmap占为图,无用,直接过滤掉,主要体现在粘贴excel表格中
|
||||
if (/Bitmap/i.test(str)) {
|
||||
return '';
|
||||
}
|
||||
var src = str.match(/src=\s*"([^"]*)"/i)[1];
|
||||
return '<img src="' + src + '" />';
|
||||
} catch (e) {
|
||||
return '';
|
||||
}
|
||||
})
|
||||
|
||||
//针对wps添加的多余标签处理
|
||||
.replace(/<\/?div[^>]*>/g, '')
|
||||
.replace(/<\/?span[^>]*>/g, '')
|
||||
.replace(/<\/?font[^>]*>/g, '')
|
||||
.replace(/<\/?col[^>]*>/g, '')
|
||||
.replace(/<\/?(span|div|o:p|v:.*?|input|label)[\s\S]*?>/g, '')
|
||||
//去掉所有属性,需要保留单元格合并
|
||||
//.replace(/<([a-zA-Z]+)\s*[^><]*>/g, "<$1>")
|
||||
//去掉多余的属性
|
||||
.replace(/v:\w+=(["']?)[^'"]+\1/g, '')
|
||||
.replace(/<(!|script[^>]*>.*?<\/script(?=[>\s])|\/?(\?xml(:\w+)?|xml|meta|link|style|\w+:\w+)(?=[\s\/>]))[^>]*>/gi, "").replace(/<p [^>]*class="?MsoHeading"?[^>]*>(.*?)<\/p>/gi, "<p><strong>$1</strong></p>")
|
||||
//去掉多余的属性
|
||||
.replace(/\s+(class|lang|align)\s*=\s*(['"]?)([\w-]+)\2/gi, '')
|
||||
//清除多余的font/span不能匹配 有可能是空格
|
||||
.replace(/<(font|span)[^>]*>(\s*)<\/\1>/gi,
|
||||
function (a, b, c) {
|
||||
return c.replace(/[\t\r\n ]+/g, " ");
|
||||
})
|
||||
//去掉style属性
|
||||
.replace(/(<[a-z][^>]*)\sstyle=(["'])([^\2]*?)\2/gi, "$1")
|
||||
// 去除不带引号的属性
|
||||
.replace(/(class|border|cellspacing|MsoNormalTable|valign|width|center| |x:str|height|x:num|cellpadding)(=[^ \f\n\r\t\v>]*)?/g, "")
|
||||
// 去除多余空格
|
||||
.replace(/(\S+)(\s+)/g, function (match, p1, p2) {
|
||||
return p1 + ' ';
|
||||
})
|
||||
.replace(/(\s)(>|<)/g, function (match, p1, p2) {
|
||||
return p2;
|
||||
})
|
||||
//处理表格中的p标签
|
||||
.replace(/(<table[^>]*[\s\S]*?><\/table>)/gi, function (a) {
|
||||
//嵌套表格不处理
|
||||
if (a.match(/(<table>)/gi).length > 1) {
|
||||
return a
|
||||
|
||||
}
|
||||
if (!/<thead>/i.test(a) && !/(rowspan|colspan)/i.test(a)) {
|
||||
//没有表头,将第一行作为表头
|
||||
//修复,当表格只有一行时,正则错误
|
||||
const firstrow = "<table><thead>" + a.match(/<tr>[\s\S]*?(<\/tr>)/i)[0] + "</thead>";
|
||||
a = a.replace(/<tr>[\s\S]*?(<\/tr>)/i, "")
|
||||
.replace(/<table>/, firstrow);
|
||||
} else if (/<thead>/i.test(a) && /(rowspan|colspan)/i.test(a)) {
|
||||
a=a.replace(/<thead>/, "");
|
||||
}
|
||||
return a.replace(/<\/p><p>/ig, "<br/>")
|
||||
.replace(/<\/?p[^>]*>/ig, '')
|
||||
//.replace(/<td><\/td>/ig,"<td> </td>")
|
||||
.replace(/<td> <\/td>/g, "<td></td>")
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
//判断粘贴的内容是否来自office
|
||||
function isWordDocument(str) {
|
||||
return /(class="?Mso|style="[^"]*\bmso\-|w:WordDocument|<(v|o):|lang=)/ig.test(str) || /\"urn:schemas-microsoft-com:office:office/ig.test(str);
|
||||
}
|
||||
|
||||
//excel表格处理
|
||||
function pasteClipboardHtml(html) {
|
||||
const startFramgmentStr = '<!--StartFragment-->';
|
||||
const endFragmentStr = '<!--EndFragment-->';
|
||||
const startFragmentIndex = html.indexOf(startFramgmentStr);
|
||||
const endFragmentIndex = html.lastIndexOf(endFragmentStr);
|
||||
|
||||
if (startFragmentIndex > -1 && endFragmentIndex > -1) {
|
||||
html = html.slice(startFragmentIndex + startFramgmentStr.length, endFragmentIndex);
|
||||
}
|
||||
|
||||
// Wrap with <tr> if html contains dangling <td> tags
|
||||
// Dangling <td> tag is that tag does not have <tr> as parent node.
|
||||
if (/<\/td>((?!<\/tr>)[\s\S])*$/i.test(html)) {
|
||||
html = '<tr>' + html + '</tr>';
|
||||
}
|
||||
// Wrap with <table> if html contains dangling <tr> tags
|
||||
// Dangling <tr> tag is that tag does not have <table> as parent node.
|
||||
if (/<\/tr>((?!<\/table>)[\s\S])*$/i.test(html)) {
|
||||
html = '<table>' + html + '</table>';
|
||||
}
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
//将html转换为markdown
|
||||
function html2md(str) {
|
||||
var gfm = turndownPluginGfm.gfm;
|
||||
var turndownService = new TurndownService({
|
||||
headingStyle: 'atx',
|
||||
hr: '- - -',
|
||||
bulletListMarker: '-',
|
||||
codeBlockStyle: 'indented',
|
||||
fence: '```',
|
||||
emDelimiter: '_',
|
||||
strongDelimiter: '**'
|
||||
});
|
||||
turndownService.use(gfm);
|
||||
turndownService.keep(['sub', 'sup']);//保留标签
|
||||
str=firstfilter(str);
|
||||
//str=pasteClipboardHtml(str);
|
||||
str=filterPasteWord(str);
|
||||
return turndownService.turndown(str);
|
||||
}
|
||||
|
||||
//将word转换的html转换为markdown,并插入编辑器
|
||||
$("#btnhtml2md").click(function (e) {
|
||||
e.preventDefault();
|
||||
if ($(this).hasClass("disabled"))
|
||||
return false;
|
||||
var str = $("#output").html();
|
||||
var cm = window.editor.cm;
|
||||
var cursor = cm.getCursor();
|
||||
var selection = cm.getSelection();
|
||||
cm.replaceSelection(html2md(str)+"\n\n");
|
||||
$("#btnhtml2md").removeClass("disabled");
|
||||
$("#word2md").modal('hide');
|
||||
cm.focus();
|
||||
});
|
||||
//粘贴Pastearea自动获得焦点
|
||||
$('#Pasteoffice').on('shown.bs.modal', function (event) {
|
||||
var modal = $(this);
|
||||
var form=modal.find("#Pasteofficeform");
|
||||
var renameInput =form.find("#Pastearea");
|
||||
//获得焦点时文本全选
|
||||
renameInput.focus(function () {
|
||||
this.select();
|
||||
});
|
||||
renameInput.focus();
|
||||
});
|
||||
|
||||
//粘贴解析
|
||||
$("#Pastearea")[0].addEventListener('paste', function (e) {
|
||||
var clipboard = e.clipboardData;
|
||||
if (!(clipboard && clipboard.items)) {
|
||||
return;
|
||||
}
|
||||
for (var i = 0, len = clipboard.items.length; i < len; i++) {
|
||||
var item = clipboard.items[i];
|
||||
if (item.kind === "string" && item.type === "text/html") {
|
||||
item.getAsString(function (str) {
|
||||
if (/<img [^>]*src=['"]([^'"]+)[^>]*>/gi.test(str)) {
|
||||
layer.msg("粘贴的内容中包含有图片,建议使用word转markdown模块处理!");
|
||||
}
|
||||
var markdown = html2md(str);
|
||||
$("#officeoutmd").val(markdown);
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
//解析html源码为markdown
|
||||
$("#HtmlToMarkdown").click(function (e) {
|
||||
e.preventDefault();
|
||||
var str = $("#Pastearea").val();
|
||||
var markdown = html2md(str);
|
||||
$("#officeoutmd").val(markdown);
|
||||
|
||||
});
|
||||
|
||||
|
||||
//将html转换为markdown,并插入编辑器
|
||||
$("#office2md").click(function (e) {
|
||||
e.preventDefault();
|
||||
if ($(this).hasClass("disabled"))
|
||||
return false;
|
||||
var str = $("#officeoutmd").val();
|
||||
var cm = window.editor.cm;
|
||||
var cursor = cm.getCursor();
|
||||
var selection = cm.getSelection();
|
||||
cm.replaceSelection(str+"\n\n");
|
||||
$("#office2md").removeClass("disabled");
|
||||
$("#Pasteoffice").modal('hide');
|
||||
cm.focus();
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* 打开文档模板
|
||||
*/
|
||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,172 @@
|
|||
var turndownPluginGfm = (function (exports) {
|
||||
'use strict';
|
||||
|
||||
var highlightRegExp = /highlight-(?:text|source)-([a-z0-9]+)/;
|
||||
|
||||
function highlightedCodeBlock (turndownService) {
|
||||
turndownService.addRule('highlightedCodeBlock', {
|
||||
filter: function (node) {
|
||||
var firstChild = node.firstChild;
|
||||
return (
|
||||
node.nodeName === 'DIV' &&
|
||||
highlightRegExp.test(node.className) &&
|
||||
firstChild &&
|
||||
firstChild.nodeName === 'PRE'
|
||||
)
|
||||
},
|
||||
replacement: function (content, node, options) {
|
||||
var className = node.className || '';
|
||||
var language = (className.match(highlightRegExp) || [null, ''])[1];
|
||||
|
||||
return (
|
||||
'\n\n' + options.fence + language + '\n' +
|
||||
node.firstChild.textContent +
|
||||
'\n' + options.fence + '\n\n'
|
||||
)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function strikethrough (turndownService) {
|
||||
turndownService.addRule('strikethrough', {
|
||||
filter: ['del', 's', 'strike'],
|
||||
replacement: function (content) {
|
||||
return '~' + content + '~'
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var indexOf = Array.prototype.indexOf;
|
||||
var every = Array.prototype.every;
|
||||
var rules = {};
|
||||
|
||||
rules.tableCell = {
|
||||
filter: ['th', 'td'],
|
||||
replacement: function (content, node) {
|
||||
return cell(content, node)
|
||||
}
|
||||
};
|
||||
|
||||
rules.tableRow = {
|
||||
filter: 'tr',
|
||||
replacement: function (content, node) {
|
||||
var borderCells = '';
|
||||
var alignMap = { left: ':--', right: '--:', center: ':-:' };
|
||||
|
||||
if (isHeadingRow(node)) {
|
||||
for (var i = 0; i < node.childNodes.length; i++) {
|
||||
var border = '---';
|
||||
var align = (
|
||||
node.childNodes[i].getAttribute('align') || ''
|
||||
).toLowerCase();
|
||||
|
||||
if (align) border = alignMap[align] || border;
|
||||
|
||||
borderCells += cell(border, node.childNodes[i]);
|
||||
}
|
||||
}
|
||||
return '\n' + content + (borderCells ? '\n' + borderCells : '')
|
||||
}
|
||||
};
|
||||
|
||||
rules.table = {
|
||||
// Only convert tables with a heading row.
|
||||
// Tables with no heading row are kept using `keep` (see below).
|
||||
filter: function (node) {
|
||||
return node.nodeName === 'TABLE' && isHeadingRow(node.rows[0])
|
||||
},
|
||||
|
||||
replacement: function (content) {
|
||||
// Ensure there are no blank lines
|
||||
content = content.replace('\n\n', '\n');
|
||||
return '\n\n' + content + '\n\n'
|
||||
}
|
||||
};
|
||||
|
||||
rules.tableSection = {
|
||||
filter: ['thead', 'tbody', 'tfoot'],
|
||||
replacement: function (content) {
|
||||
return content
|
||||
}
|
||||
};
|
||||
|
||||
// A tr is a heading row if:
|
||||
// - the parent is a THEAD
|
||||
// - or if its the first child of the TABLE or the first TBODY (possibly
|
||||
// following a blank THEAD)
|
||||
// - and every cell is a TH
|
||||
function isHeadingRow (tr) {
|
||||
var parentNode = tr.parentNode;
|
||||
return (
|
||||
parentNode.nodeName === 'THEAD' ||
|
||||
(
|
||||
parentNode.firstChild === tr &&
|
||||
(parentNode.nodeName === 'TABLE' || isFirstTbody(parentNode)) &&
|
||||
every.call(tr.childNodes, function (n) { return n.nodeName === 'TH' })
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
function isFirstTbody (element) {
|
||||
var previousSibling = element.previousSibling;
|
||||
return (
|
||||
element.nodeName === 'TBODY' && (
|
||||
!previousSibling ||
|
||||
(
|
||||
previousSibling.nodeName === 'THEAD' &&
|
||||
/^\s*$/i.test(previousSibling.textContent)
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
//修复,当表格中有换行时,解析不正确,修复表格为空时,增加3个空格,确保能正确解析
|
||||
function cell(content, node) {
|
||||
var index = indexOf.call(node.parentNode.childNodes, node);
|
||||
var prefix = ' ';
|
||||
content = content.replace("\n", "<br/>")
|
||||
if (index === 0)
|
||||
prefix = '| ';
|
||||
let filteredContent = content.trim().replace(/\n\r/g, '<br/>').replace(/\n/g, "<br/>");
|
||||
filteredContent = filteredContent.replace(/\|+/g, '\\|');
|
||||
while (filteredContent.length < 3)
|
||||
filteredContent += ' ';
|
||||
return prefix + filteredContent + ' |'
|
||||
}
|
||||
|
||||
function tables (turndownService) {
|
||||
turndownService.keep(function (node) {
|
||||
return node.nodeName === 'TABLE' && !isHeadingRow(node.rows[0])
|
||||
});
|
||||
for (var key in rules) turndownService.addRule(key, rules[key]);
|
||||
}
|
||||
|
||||
function taskListItems (turndownService) {
|
||||
turndownService.addRule('taskListItems', {
|
||||
filter: function (node) {
|
||||
return node.type === 'checkbox' && node.parentNode.nodeName === 'LI'
|
||||
},
|
||||
replacement: function (content, node) {
|
||||
return (node.checked ? '[x]' : '[ ]') + ' '
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function gfm (turndownService) {
|
||||
turndownService.use([
|
||||
highlightedCodeBlock,
|
||||
strikethrough,
|
||||
tables,
|
||||
taskListItems
|
||||
]);
|
||||
}
|
||||
|
||||
exports.gfm = gfm;
|
||||
exports.highlightedCodeBlock = highlightedCodeBlock;
|
||||
exports.strikethrough = strikethrough;
|
||||
exports.tables = tables;
|
||||
exports.taskListItems = taskListItems;
|
||||
|
||||
return exports;
|
||||
|
||||
}({}));
|
|
@ -0,0 +1,909 @@
|
|||
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
|
||||
}
|
||||
|
||||
function repeat (character, count) {
|
||||
return Array(count + 1).join(character)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
var voidElements = [
|
||||
'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input',
|
||||
'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'
|
||||
];
|
||||
|
||||
function isVoid (node) {
|
||||
return voidElements.indexOf(node.nodeName.toLowerCase()) !== -1
|
||||
}
|
||||
|
||||
var voidSelector = voidElements.join();
|
||||
function hasVoid (node) {
|
||||
return node.querySelector && node.querySelector(voidSelector)
|
||||
}
|
||||
|
||||
var rules = {};
|
||||
|
||||
rules.paragraph = {
|
||||
filter: 'p',
|
||||
|
||||
replacement: function (content) {
|
||||
return '\n\n' + content + '\n\n'
|
||||
}
|
||||
};
|
||||
|
||||
rules.lineBreak = {
|
||||
filter: 'br',
|
||||
|
||||
replacement: function (content, node, options) {
|
||||
return options.br + '\n'
|
||||
}
|
||||
};
|
||||
|
||||
rules.heading = {
|
||||
filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
|
||||
|
||||
replacement: function (content, node, options) {
|
||||
var hLevel = Number(node.nodeName.charAt(1));
|
||||
|
||||
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.blockquote = {
|
||||
filter: 'blockquote',
|
||||
|
||||
replacement: function (content) {
|
||||
content = content.replace(/^\n+|\n+$/g, '');
|
||||
content = content.replace(/^/gm, '> ');
|
||||
return '\n\n' + content + '\n\n'
|
||||
}
|
||||
};
|
||||
|
||||
rules.list = {
|
||||
filter: ['ul', 'ol'],
|
||||
|
||||
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.listItem = {
|
||||
filter: 'li',
|
||||
|
||||
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.indentedCodeBlock = {
|
||||
filter: function (node, options) {
|
||||
return (
|
||||
options.codeBlockStyle === 'indented' &&
|
||||
node.nodeName === 'PRE' &&
|
||||
node.firstChild &&
|
||||
node.firstChild.nodeName === 'CODE'
|
||||
)
|
||||
},
|
||||
|
||||
replacement: function (content, node, options) {
|
||||
return (
|
||||
'\n\n ' +
|
||||
node.firstChild.textContent.replace(/\n/g, '\n ') +
|
||||
'\n\n'
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
rules.fencedCodeBlock = {
|
||||
filter: function (node, options) {
|
||||
return (
|
||||
options.codeBlockStyle === 'fenced' &&
|
||||
node.nodeName === 'PRE' &&
|
||||
node.firstChild &&
|
||||
node.firstChild.nodeName === 'CODE'
|
||||
)
|
||||
},
|
||||
|
||||
replacement: function (content, node, options) {
|
||||
var className = node.firstChild.className || '';
|
||||
var language = (className.match(/language-(\S+)/) || [null, ''])[1];
|
||||
|
||||
return (
|
||||
'\n\n' + options.fence + language + '\n' +
|
||||
node.firstChild.textContent +
|
||||
'\n' + options.fence + '\n\n'
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
rules.horizontalRule = {
|
||||
filter: 'hr',
|
||||
|
||||
replacement: function (content, node, options) {
|
||||
return '\n\n' + options.hr + '\n\n'
|
||||
}
|
||||
};
|
||||
|
||||
rules.inlineLink = {
|
||||
filter: function (node, options) {
|
||||
return (
|
||||
options.linkStyle === 'inlined' &&
|
||||
node.nodeName === 'A' &&
|
||||
node.getAttribute('href')
|
||||
)
|
||||
},
|
||||
|
||||
replacement: function (content, node) {
|
||||
var href = node.getAttribute('href');
|
||||
var title = node.title ? ' "' + node.title + '"' : '';
|
||||
return '[' + content + '](' + href + title + ')'
|
||||
}
|
||||
};
|
||||
|
||||
rules.referenceLink = {
|
||||
filter: function (node, options) {
|
||||
return (
|
||||
options.linkStyle === 'referenced' &&
|
||||
node.nodeName === 'A' &&
|
||||
node.getAttribute('href')
|
||||
)
|
||||
},
|
||||
|
||||
replacement: function (content, node, options) {
|
||||
var href = node.getAttribute('href');
|
||||
var title = node.title ? ' "' + node.title + '"' : '';
|
||||
var replacement;
|
||||
var reference;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
this.references.push(reference);
|
||||
return replacement
|
||||
},
|
||||
|
||||
references: [],
|
||||
|
||||
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.emphasis = {
|
||||
filter: ['em', 'i'],
|
||||
|
||||
replacement: function (content, node, options) {
|
||||
if (!content.trim()) return ''
|
||||
return options.emDelimiter + content + options.emDelimiter
|
||||
}
|
||||
};
|
||||
|
||||
rules.strong = {
|
||||
filter: ['strong', 'b'],
|
||||
|
||||
replacement: function (content, node, options) {
|
||||
if (!content.trim()) return ''
|
||||
return options.strongDelimiter + content + options.strongDelimiter
|
||||
}
|
||||
};
|
||||
|
||||
rules.code = {
|
||||
filter: function (node) {
|
||||
var hasSiblings = node.previousSibling || node.nextSibling;
|
||||
var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings;
|
||||
|
||||
return node.nodeName === 'CODE' && !isCodeBlock
|
||||
},
|
||||
|
||||
replacement: function (content) {
|
||||
if (!content.trim()) return ''
|
||||
|
||||
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 delimiter + leadingSpace + content + trailingSpace + delimiter
|
||||
}
|
||||
};
|
||||
|
||||
rules.image = {
|
||||
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 + ')' : ''
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Manages a collection of rules used to convert HTML to Markdown
|
||||
*/
|
||||
|
||||
function Rules (options) {
|
||||
this.options = options;
|
||||
this._keep = [];
|
||||
this._remove = [];
|
||||
|
||||
this.blankRule = {
|
||||
replacement: options.blankReplacement
|
||||
};
|
||||
|
||||
this.keepReplacement = options.keepReplacement;
|
||||
|
||||
this.defaultRule = {
|
||||
replacement: options.defaultReplacement
|
||||
};
|
||||
|
||||
this.array = [];
|
||||
for (var key in options.rules) this.array.push(options.rules[key]);
|
||||
}
|
||||
|
||||
Rules.prototype = {
|
||||
add: function (key, rule) {
|
||||
this.array.unshift(rule);
|
||||
},
|
||||
|
||||
keep: function (filter) {
|
||||
this._keep.unshift({
|
||||
filter: filter,
|
||||
replacement: this.keepReplacement
|
||||
});
|
||||
},
|
||||
|
||||
remove: function (filter) {
|
||||
this._remove.unshift({
|
||||
filter: filter,
|
||||
replacement: function () {
|
||||
return ''
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
forNode: function (node) {
|
||||
if (node.isBlank) return this.blankRule
|
||||
var rule;
|
||||
|
||||
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
|
||||
|
||||
return this.defaultRule
|
||||
},
|
||||
|
||||
forEach: function (fn) {
|
||||
for (var i = 0; i < this.array.length; i++) fn(this.array[i], i);
|
||||
}
|
||||
};
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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'
|
||||
};
|
||||
|
||||
if (!element.firstChild || isPre(element)) return
|
||||
|
||||
var prevText = null;
|
||||
var prevVoid = false;
|
||||
|
||||
var prev = null;
|
||||
var node = next(prev, element, isPre);
|
||||
|
||||
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 ((!prevText || / $/.test(prevText.data)) &&
|
||||
!prevVoid && text[0] === ' ') {
|
||||
text = text.substr(1);
|
||||
}
|
||||
|
||||
// `text` might be empty at this point.
|
||||
if (!text) {
|
||||
node = remove(node);
|
||||
continue
|
||||
}
|
||||
|
||||
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(/ $/, '');
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (prevText) {
|
||||
prevText.data = prevText.data.replace(/ $/, '');
|
||||
if (!prevText.data) {
|
||||
remove(prevText);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
node.parentNode.removeChild(node);
|
||||
|
||||
return next
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
|
||||
return current.firstChild || current.nextSibling || current.parentNode
|
||||
}
|
||||
|
||||
/*
|
||||
* Set up window for Node.js
|
||||
*/
|
||||
|
||||
var root = (typeof window !== 'undefined' ? window : {});
|
||||
|
||||
/*
|
||||
* Parsing HTML strings
|
||||
*/
|
||||
|
||||
function canParseHTMLNatively () {
|
||||
var Parser = root.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 () {};
|
||||
|
||||
{
|
||||
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
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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 isBlank (node) {
|
||||
//如果要忽略掉空白的表,请删除'TABLE','THEAD','TBODY','TR'
|
||||
return (
|
||||
['A', 'TABLE','THEAD','TBODY','TR','TH', 'TD', 'IFRAME', 'SCRIPT', 'AUDIO', 'VIDEO'].indexOf(node.nodeName) === -1 &&
|
||||
/^\s*$/i.test(node.textContent) &&
|
||||
!isVoid(node) &&
|
||||
!hasVoid(node)
|
||||
)
|
||||
}
|
||||
|
||||
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 (hasLeading && !isFlankedByWhitespace('left', node)) {
|
||||
leading = ' ';
|
||||
}
|
||||
if (hasTrailing && !isFlankedByWhitespace('right', node)) {
|
||||
trailing = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
return { leading: leading, trailing: trailing }
|
||||
}
|
||||
|
||||
function isFlankedByWhitespace (side, node) {
|
||||
var sibling;
|
||||
var regExp;
|
||||
var isFlanked;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
return isFlanked
|
||||
}
|
||||
|
||||
var reduce = Array.prototype.reduce;
|
||||
var leadingNewLinesRegExp = /^\n*/;
|
||||
var trailingNewLinesRegExp = /\n*$/;
|
||||
var escapes = [
|
||||
[/\\/g, '\\\\'],
|
||||
[/\*/g, '\\*'],
|
||||
[/^-/g, '\\-'],
|
||||
[/^\+ /g, '\\+ '],
|
||||
[/^(=+)/g, '\\$1'],
|
||||
[/^(#{1,6}) /g, '\\$1 '],
|
||||
[/`/g, '\\`'],
|
||||
[/^~~~/g, '\\~~~'],
|
||||
[/\[/g, '\\['],
|
||||
[/\]/g, '\\]'],
|
||||
[/^>/g, '\\>'],
|
||||
[/_/g, '\\_'],
|
||||
[/^(\d+)\. /g, '$1\\. ']
|
||||
];
|
||||
|
||||
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
|
||||
}
|
||||
};
|
||||
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
|
||||
*/
|
||||
|
||||
turndown: function (input) {
|
||||
if (!canConvert(input)) {
|
||||
throw new TypeError(
|
||||
input + ' is not a string, or an element/document/fragment node.'
|
||||
)
|
||||
}
|
||||
|
||||
if (input === '') return ''
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
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
|
||||
},
|
||||
|
||||
/**
|
||||
* 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
|
||||
},
|
||||
|
||||
/**
|
||||
* 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
|
||||
},
|
||||
|
||||
/**
|
||||
* 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
|
||||
},
|
||||
|
||||
/**
|
||||
* Escapes Markdown syntax
|
||||
* @public
|
||||
* @param {String} string The string to escape
|
||||
* @returns A string with Markdown syntax escaped
|
||||
* @type String
|
||||
*/
|
||||
|
||||
escape: function (string) {
|
||||
return escapes.reduce(function (accumulator, escape) {
|
||||
return accumulator.replace(escape[0], escape[1])
|
||||
}, 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);
|
||||
|
||||
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)
|
||||
}, '')
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
});
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
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);
|
||||
|
||||
// Remove trailing/leading newlines and replace with separator
|
||||
string1 = string1.replace(trailingNewLinesRegExp, '');
|
||||
string2 = string2.replace(leadingNewLinesRegExp, '');
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
function canConvert (input) {
|
||||
return (
|
||||
input != null && (
|
||||
typeof input === 'string' ||
|
||||
(input.nodeType && (
|
||||
input.nodeType === 1 || input.nodeType === 9 || input.nodeType === 11
|
||||
))
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
return TurndownService;
|
||||
|
||||
}());
|
|
@ -0,0 +1,132 @@
|
|||
(function () {
|
||||
var options = {
|
||||
convertImage: mammoth.images.imgElement(function (image) {
|
||||
var result = "";
|
||||
return image.read("base64").then(function (imageBuffer, $callback) {
|
||||
var fileName = Date.parse(new Date());
|
||||
|
||||
switch (image.contentType) {
|
||||
case "image/png":
|
||||
fileName += ".png";
|
||||
break;
|
||||
case "image/jpg":
|
||||
fileName += ".jpg";
|
||||
break
|
||||
case "image/jpeg":
|
||||
fileName += ".jpeg";
|
||||
break;
|
||||
case "image/gif":
|
||||
fileName += ".gif";
|
||||
break;
|
||||
default:
|
||||
layer.msg("不支持的图片格式");
|
||||
return;
|
||||
}
|
||||
var form = new FormData();
|
||||
form.append('editormd-image-file', base64ToBlob(imageBuffer, image.contentType), fileName);
|
||||
|
||||
var layerIndex = 0;
|
||||
|
||||
return {
|
||||
src: _ajax(window.imageUploadURL, form, function (ret) {
|
||||
if (ret.success == 1) {
|
||||
//return ret.url;
|
||||
//cm.replaceSelection("![](" + ret.url + ")");
|
||||
}
|
||||
console.log(ret.message);
|
||||
})
|
||||
};
|
||||
|
||||
});
|
||||
})
|
||||
};
|
||||
var _ajax = function (url, data, callback) {
|
||||
$.ajax({
|
||||
"type": 'post',
|
||||
"cache": false,
|
||||
"url": url,
|
||||
"data": data,
|
||||
"dateType": "json",
|
||||
"processData": false,
|
||||
"contentType": false,
|
||||
"mimeType": "multipart/form-data",
|
||||
async: false,
|
||||
success: function (ret) {
|
||||
callback(JSON.parse(ret));
|
||||
result = JSON.parse(ret).url;
|
||||
},
|
||||
error: function (err) {
|
||||
console.log('请求失败')
|
||||
}
|
||||
})
|
||||
return result;
|
||||
};
|
||||
function base64ToBlob(base64, mime) {
|
||||
mime = mime || "";
|
||||
const sliceSize = 1024;
|
||||
const byteChars = window.atob(base64);
|
||||
const byteArrays = [];
|
||||
for (
|
||||
let offset = 0, len = byteChars.length;
|
||||
offset < len;
|
||||
offset += sliceSize) {
|
||||
const slice = byteChars.slice(offset, offset + sliceSize);
|
||||
|
||||
const byteNumbers = new Array(slice.length);
|
||||
for (let i = 0; i < slice.length; i++) {
|
||||
byteNumbers[i] = slice.charCodeAt(i);
|
||||
}
|
||||
|
||||
const byteArray = new Uint8Array(byteNumbers);
|
||||
|
||||
byteArrays.push(byteArray);
|
||||
}
|
||||
|
||||
return new Blob(byteArrays, {
|
||||
type: mime
|
||||
});
|
||||
}
|
||||
document.getElementById("document")
|
||||
.addEventListener("change", handleFileSelect, false);
|
||||
|
||||
function handleFileSelect(event) {
|
||||
readFileInputEventAsArrayBuffer(event, function (arrayBuffer) {
|
||||
mammoth.convertToHtml({
|
||||
arrayBuffer: arrayBuffer
|
||||
}, options)
|
||||
.then(displayResult)
|
||||
.done();
|
||||
});
|
||||
}
|
||||
|
||||
function displayResult(result) {
|
||||
document.getElementById("output").innerHTML = result.value;
|
||||
|
||||
var messageHtml = result.messages.map(function (message) {
|
||||
return '<li class="' + message.type + '">' + escapeHtml(message.message) + "</li>";
|
||||
}).join("");
|
||||
|
||||
document.getElementById("messages").innerHTML = "<ul>" + messageHtml + "</ul>";
|
||||
}
|
||||
|
||||
function readFileInputEventAsArrayBuffer(event, callback) {
|
||||
var file = event.target.files[0];
|
||||
|
||||
var reader = new FileReader();
|
||||
|
||||
reader.onload = function (loadEvent) {
|
||||
var arrayBuffer = loadEvent.target.result;
|
||||
callback(arrayBuffer);
|
||||
};
|
||||
|
||||
reader.readAsArrayBuffer(file);
|
||||
}
|
||||
|
||||
function escapeHtml(value) {
|
||||
return value
|
||||
.replace(/&/g, '&')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>');
|
||||
}
|
||||
})();
|
|
@ -0,0 +1 @@
|
|||
word2md.js是使用官方的demo修改的,增加了上传图片处理。
|
|
@ -51,6 +51,8 @@
|
|||
</div>
|
||||
<div class="editormd-group">
|
||||
<a href="javascript:;" id="markdown-save" data-toggle="tooltip" data-title="保存" class="disabled save"><i class="fa fa-save" aria-hidden="true" name="save"></i></a>
|
||||
<a href="javascript:;" data-toggle="tooltip" data-title="word转换为markdown"><i class="fa fa-file-word-o item" name="word2md" aria-hidden="true"></i></a>
|
||||
<a href="javascript:;" data-toggle="tooltip" data-title="从office粘贴转换"><i class="fa fa-file-excel-o item" name="Pasteoffice" aria-hidden="true"></i></a>
|
||||
</div>
|
||||
<div class="editormd-group">
|
||||
<a href="javascript:;" data-toggle="tooltip" data-title="撤销 (Ctrl-Z)"><i class="fa fa-undo first" name="undo" unselectable="on"></i></a>
|
||||
|
@ -231,6 +233,64 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<!-- word2md -->
|
||||
<div class="modal fade" id="word2md" tabindex="-1" role="dialog" aria-labelledby="ModalReplaceLabel">
|
||||
<div class="modal-dialog" role="document">
|
||||
<div class="modal-content">
|
||||
<form class="form-horizontal" id="word2mdform">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
||||
<span aria-hidden="true">×</span></button>
|
||||
<h1 class="modal-title" id="ModalReplaceLabel" style="text-align:center;">word转换为markdown</h1>
|
||||
<input id="document" type="file" accept=".docx"/></div>
|
||||
<div class="modal-body" style="overflow-y:auto; height:400px;">
|
||||
<div id="output" class="well">
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<h3 style="margin-top:0px;text-align:center;">信息提示</h3>
|
||||
<div id="messages" class="well" align="left">
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-primary" id="btnhtml2md" >确定</button>
|
||||
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button></div>
|
||||
</div>
|
||||
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<!-- Pasteoffice -->
|
||||
<div class="modal fade" id="Pasteoffice" tabindex="-1" role="dialog" aria-labelledby="ModalReplaceLabel">
|
||||
<div class="modal-dialog" role="document">
|
||||
<div class="modal-content">
|
||||
<form class="form-horizontal" id="Pasteofficeform">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
||||
<span aria-hidden="true">×</span></button>
|
||||
<h1 class="modal-title" id="ModalReplaceLabel" style="text-align:center;">将office/html内容粘贴在下方</h1>
|
||||
</div>
|
||||
<div class="modal-body" style="overflow-y:auto; height:400px;">
|
||||
<textarea name="Pastearea" id="Pastearea" style="height:100px;width:100%"></textarea>
|
||||
<h2 style="margin-top:0px;text-align:center;">结果预览</h2>
|
||||
<textarea id="officeoutmd" style="height:200px;width:100%"></textarea>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
|
||||
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-info" id="HtmlToMarkdown" >解析html源码</button>
|
||||
<button type="button" class="btn btn-primary" id="office2md" >确定</button>
|
||||
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button></div>
|
||||
</div>
|
||||
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="modal fade" id="documentTemplateModal" tabindex="-1" role="dialog" aria-labelledby="请选择模板类型" aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
|
@ -292,6 +352,13 @@
|
|||
<script src="{{cdnjs "/static/js/jquery.form.js"}}" type="text/javascript"></script>
|
||||
<script src="{{cdnjs "/static/js/editor.js"}}" type="text/javascript"></script>
|
||||
<script src="{{cdnjs "/static/js/markdown.js"}}" type="text/javascript"></script>
|
||||
|
||||
<script src="/static/word2md/turndown.js"></script>
|
||||
<script src="/static/word2md/turndown-plugin-gfm.js"></script>
|
||||
<script src="/static/word2md/mammoth.browser.js"></script>
|
||||
<script src="/static/word2md/word2md.js"></script>
|
||||
|
||||
|
||||
<script type="text/javascript">
|
||||
$(function () {
|
||||
|
||||
|
|
Loading…
Reference in New Issue