fix: regex for disq.us

feat/fix/umd
SukkaW 7 years ago
parent 2a4de46d71
commit 1c9369e5b9

@ -450,18 +450,20 @@ function DisqusJS(config) {
*/ */
let removeDisqUs = (msg) => { let removeDisqUs = (msg) => {
// aMatcher - 只处理 Disqus 短链接 // aMatcher - 只处理 Disqus 短链接
var aMatcher = new RegExp(/<a.+?href=\"https:\/\/disq\.us(.+?)\".*>(.+)<\/a>/gi), var aMatcher = new RegExp(/<a(.*?)href="https:\/\/disq\.us(.+?)"(.+?)>(.+?)<\/a>/gi),
hrefMatcher = new RegExp(/href=\"(.+?)\"/gi) hrefMatcher = new RegExp(/href=\"(.+?)\"/gi)
let link = (msg.match(aMatcher) || []) let link = (msg.match(aMatcher) || []);
link.map((link) => { link.map((olink) => {
// (.*) 是贪婪处理,会一致匹配到最后,可以用于匹配最后一次 // (.*) 是贪婪处理,会一致匹配到最后,可以用于匹配最后一次
link = link.match(hrefMatcher)[0].replace(/href=\"https:\/\/disq.us\/url\?url=/g, '').replace(/(.*)"/, '$1'); let link = olink.match(hrefMatcher)[0].replace(/href=\"https:\/\/disq.us\/url\?url=/g, '').replace(/(.*)"/, '$1');
link = decodeURIComponent(link).replace(/(.*):(.*)cuid=(.*)/, '$1') link = decodeURIComponent(link);
msg = msg.replace(aMatcher, `<a href="${link}" rel="nofollow noopener noreferrer">${link}</a>`) link = link.replace(/(.*):(.*)cuid=(.*)/, '$1')
msg = msg.replace(olink, `<a href="${link}" rel="nofollow noopener noreferrer">${link}</a>`)
}) })
// 在最后添加 target="_blank" 可以生效到全局链接(包括 Disqus CDN 直链) // 在最后添加 target="_blank" 可以生效到全局链接(包括 Disqus CDN 直链)
return msg.replace(/href=/g, 'target="_blank" href=') msg = msg.replace(/href=/g, `target="_blank" href=`)
return msg
} }
let renderPostItem = (s) => { let renderPostItem = (s) => {
@ -610,4 +612,4 @@ function DisqusJS(config) {
} }
} }
export default DisqusJS; export default DisqusJS;

Loading…
Cancel
Save