我的许多用户都输入纯 URL,而不是使用 Textile 格式来创建链接。我希望 Textile 能够将 URL 链接到 URL。我真的不需要担心 XSS 或任何恶意行为,因为它是一个只有很少用户组的内部网络。实现此功能的最佳方法是什么?
我正在使用 RedCloth 的 Ruby 版本。
最佳答案
这个怎么样?
def urls2links( s )
s.gsub(
/([^:])((https?|ftp)\:\/\/[^\s)'"]+[^.,)\s])(. |, |[\s]|\) | )/,
'\1<a href="\2" target="_blank">\2</a>\4')
end
def textile2html( s )
textilize(urls2links(s)).html_safe
end
使用此文本进行测试:
Plain urls converted to external links by my textile2html helper:
* http://www.google.com
* http://www.google.com,
* http://www.google.com.
关于ruby - 如何让 Textile 从纯 URL 中创建链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3410823/