Feedify是一款小巧快速的产品要链接的文本转换器,自动将纯文本URL转换为超链接。支持常规链接(http、https、ftp等)、Twitter@username、Twitter#标签和电子邮件地址。
此外,该插件还能够对不安全的字符(如<、>、“、'、&等)进行编码和转义,以防止XSS攻击。
1.在jQuery之后下载并加载jQuery.feedify.js。
<script src="/path/to/cdn/jquery.slim.min.js"></script> <script src="/path/to/jQuery.feedify.js"></script>
2.初始化目标文本上的Feediy。
<!-- Output -->
Hi my twitter account is <a href="http://twitter.com/#!/jqueryscript">@jqueryscript</a>.
I wrote <a href="http://twitter.com/#!/search?q=%23jQuery">#jQuery</a> articles on <a href="https://www.jqueryscript.net">https://www.jqueryscript.net</a>
Contact me at <a href="mailto:[email protected]">[email protected]</a>
This below script is not going to execute because of encoding
<script>alert('hello');</script>
3.使用正则表达式添加您自己的文本模式。
lt : {
regex : /(<)/g,
template : '<'
},
gt : {
regex : /(>)/g,
template : '>'
},
dq : {
regex : /(")/g,
template : '"'
},
sq : {
regex : /(')/g,
template : '''
},
link : {
regex : /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,
template : "<a href=\"$1\">$1</a>"
},
user : {
regex : /((?:^|[^a-zA-Z0-9_!#$%&*@ï¼ ]|RT:?))([@ï¼ ])([a-zA-Z0-9_]{1,20})(\/[a-zA-Z][a-zA-Z0-9_-]{0,24})?/g,
template : '$1<a href="http://twitter.com/#!/$3$4">@$3$4</a>'
},
hash : {
regex : /(^|\s)#(\w+)/g,
template : '$1<a href="http://twitter.com/#!/search?q=%23$2">#$2</a>'
},
email : {
regex : /([a-zA-Z0-9._-][email protected][a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi,
template : '<a href=\"mailto:$1\">$1</a>'
}