Skip to content

Instantly share code, notes, and snippets.

@SukkaW
Last active October 20, 2018 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SukkaW/68fe06dce0778ddfdcbaf4ff1ccf80fa to your computer and use it in GitHub Desktop.
Save SukkaW/68fe06dce0778ddfdcbaf4ff1ccf80fa to your computer and use it in GitHub Desktop.
Remove disq.us from Disqus Message
/* no-disq.us.js
* Author: Sukka (https://skk.moe)
* License: MIT
* Part of DisqusJS (https://suka.js.org/DisqusJS)
*
* @param {string} msg - 传入需要处理的 Disqus Comment rawMessage
* @return {string} - 解析完 disq.us 后的 Message
*/
let removeDisqUs = (msg) => {
// aMatcher - 只处理 Disqus 短链接
var aMatcher = new RegExp(/<a.+?href=\"https:\/\/disq.us(.+?)\".*>/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');
// 去除 disq.us 短链接结尾的鉴权 key 获得原来链接
link = decodeURIComponent(link).replace(/(.*):(.*)/, '$1')
// 将 msg 中对应的 a 标签换成新的
msg = msg.replace(aMatcher, `<a href="${link}" rel="nofollow noopener noreferrer">${link}</a>`)
})
// 在最后添加 target="_blank" 可以生效到全局链接(包括 Disqus CDN 直链)
return msg.replace(/href=/g, 'target="_blank" href=')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment