diff --git a/src/disqus.js b/src/disqus.js index 126e76e..90aea1f 100644 --- a/src/disqus.js +++ b/src/disqus.js @@ -317,8 +317,8 @@ function DisqusJS(config) { /* * parseCommentData(data) - 解析评论列表 * - * @param {Object} data - 评论列表 JSON - * @return {Object} - 解析后的评论列表数据 + * @param {Array} data - 评论列表 JSON + * @return {Array} - 解析后的评论列表数据 */ let parseCommentData = (data) => { var topLevelComments = [], @@ -370,13 +370,13 @@ function DisqusJS(config) { /* * renderCommentData(data) - 渲染评论列表 * - * @param {Object} data - 从 getComment() 获取到的 JSON + * @param {Array} data - 从 getComment() 获取到的 JSON */ let renderComment = (data) => { /* * processData(data) - 处理评论列表 * - * @param {Object} data - 解析后的评论列表 JSON + * @param {Array} data - 解析后的评论列表 JSON */ let processData = (data) => { // 处理 Disqus Profile URL @@ -408,6 +408,27 @@ function DisqusJS(config) { return data; } + /* + * removeDisqUs(msg) - 将 comment 中的短链接 disq.us 去除 + * @param {string} msg - 评论信息 + * @return {string} msg - 经过处理的评论信息 + */ + let removeDisqUs = (msg) => { + // aMatcher - 只处理 Disqus 短链接 + var aMatcher = new RegExp(//gi), + hrefMatcher = new RegExp(/href=\"(?[^\"]*)\"/gi) + let link = (msg.match(aMatcher) || []) + link.map((link) => { + // (.*) 是贪婪处理,会一致匹配到最后,可以用于匹配最后一次 + link = link.match(hrefMatcher)[0].replace(/href=\"https:\/\/disq.us\/url\?url=/g, '').replace(/(.*)"/, '$1'); + link = decodeURIComponent(link).replace(/(.*):(.*)/, '$1') + msg = msg.replace(aMatcher, `${link}`) + }) + + // 在最后添加 target="_blank" 可以生效到全局链接(包括 Disqus CDN 直链) + return msg.replace(/href=/g, 'target="_blank" href=') + } + let renderPostItem = (s) => { /*
@@ -426,7 +447,7 @@ function DisqusJS(config) {
*/ - let html = `
${s.avatarEl}
${s.authorEl}
${s.message}
` + let html = `
${s.avatarEl}
${s.authorEl}
${removeDisqUs(s.message)}
` return html; }