为v2ex而生的markdown渲染
当前为
// ==UserScript==
// @name v2exMarkdown
// @namespace https://github.com/hundan2020/v2exMarkdown
// @version 0.5
// @description 为v2ex而生的markdown渲染
// @author hundan,ccsiyu
// @match https://*.v2ex.com/t/*
// @require https://cdn.staticfile.org/showdown/1.8.6/showdown.js
// @require https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js
// @grant none
// ==/UserScript==
(function () {
var preFix = function(rawReply){
var fixedReply = rawReply;
fixedReply = fixedReply.replace(/([^#]#|^#)(\d{1,3}\s)>/ig, '#'); // 避免楼层号加粗
fixedReply = fixedReply.replace(/<a target="_blank" href="(\S+?)"><img src="(\S+?)" class="embedded_image"><\/a>/ig, ''); // 正常显示的图片处理
fixedReply = fixedReply.replace(/\s*@<a href="\/member\/(\S+?)">(\S+?)<\/a>/ig, '@[$1](/member/$2)'); // 论坛内@处理
fixedReply = fixedReply.replace(/\s*<a target="_blank" href="(\S+?)" rel="nofollow">(\S+?)<\/a>\s*/ig, '$2'); // 链接处理
fixedReply = fixedReply.replace(/(\n)?<br *\/?>/ig, "\n"); // 换行处理,避免多行代码无法正常工作
fixedReply = fixedReply.replace(/&/ig, '&'); // 实体转义回去,由showdown处理
fixedReply = fixedReply.replace(/</ig, '<');
fixedReply = fixedReply.replace(/>/ig, '>');
return fixedReply;
}
var endFix = function(markedReply){
var fixedReply = markedReply;
fixedReply = fixedReply.replace(/\n/ig, '<br />');
fixedReply = fixedReply.replace(/(<\/ul>|<\/li>|<\/p>|<\/table>|<\/h\d>)\s*<br\s*\/?>/ig, '$1');
fixedReply = fixedReply.replace(/<br\s*\/?>(<li>|<ul>|<p>|<table>|<h\d>)/ig, '$1');
fixedReply = fixedReply.replace(/(<\/?table>|<\/?tbody>|<\/?thead>|<\/?tr>|<\/?th>|<\/?td>)<br\s*\/?>/ig, '$1');
fixedReply = fixedReply.replace(/(<br\s*\/?>\s*){2,}/ig, '<br />');
fixedReply = fixedReply.replace(/@\[(\S+)\]\(\/member\/\S+\)/ig, '@$1');
return fixedReply;
}
var processMarkdown = function(){
$("div.reply_content").each(function () {
var reply = $(this)[0];
var rawReply = reply.innerHTML;
var converter = new showdown.Converter({
omitExtraWLInCodeBlocks: true,
parseImgDimensions: true,
simplifiedAutoLink: true,
literalMidWordUnderscores: true,
strikethrough: true,
tables: true,
ghCodeBlocks: true,
tasklists: true,
smoothLivePreview: true,
ghCompatibleHeaderId: true,
encodeEmails: true,
emoji: true
});
var markedReply = converter.makeHtml(preFix(rawReply));
reply.innerHTML = endFix(markedReply);
});
}
processMarkdown();
console.log("\n\n\n Thanks for using my script~\n\n\n\n");
})();