v2exMarkdown

为v2ex而生的markdown渲染

目前為 2018-06-13 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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();
})();