我将 linkify-react 模块与主题标签插件结合使用
import Linkify from 'linkifyjs/react';
import * as linkify from 'linkifyjs';
import hashtag from 'linkifyjs/plugins/hashtag';
hashtag(linkify);
我没有找到任何解决方案让主题标签链接直接在 jsx 组件中工作,这可能吗?
<Linkify options={linkifyOptions} >{content}</Linkify>
我正在尝试使用该插件。通过该插件,我检索内容中所有主题标签的数组
const pTag = linkify.find(p.content || '');
// here the output
{
"type": "hashtag",
"value": "#tag",
"href": "#tag"
}
如何用文本中的链接替换所有主题标签?我已经尝试过这个解决方案,但不起作用
pTag.forEach( (tag) => {
content.replace(tag, 'example.com/'+tag);
})
最佳答案
您可以根据 Linkify 文档使用 formatHref 属性将 URL 添加到每个主题标签 https://soapbox.github.io/linkifyjs/docs/options.html
var linkifyOptions =
{
formatHref: function (href, type) {
if (type === 'hashtag') {
href = 'https://twitter.com/hashtag/' + href.substring(1);
}
return href;
}
}
var content = 'Linkify is #super #rad2015';
return <Linkify options={linkifyOptions}>{content}</Linkify>;
在此处查看完整代码
关于reactjs - 如何在 react 中链接主题标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57116494/