v2exMarkdown

为v2ex而生的markdown渲染

目前为 2018-06-13 提交的版本。查看 最新版本

// ==UserScript==
// @name         v2exMarkdown
// @namespace    https://github.com/hundan2020/v2exMarkdown
// @version      0.4
// @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, '![]($2)'); // 正常显示的图片处理
		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(/&amp;/ig, '&'); // 实体转义回去,由showdown处理
        fixedReply = fixedReply.replace(/&lt;/ig, '<');
        fixedReply = fixedReply.replace(/&gt;/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));
            console.log(rawReply);
            console.log(preFix(rawReply));
            console.log(markedReply);
            console.log(endFix(markedReply));
			reply.innerHTML = endFix(markedReply);
		});
	}
	processMarkdown();
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址