0

我正在尝试将 html 字符串呈现到基于 Azure 通信服务的聊天应用程序中。样板代码取自 Azure Samples GitHub 存储库https://github.com/Azure-Samples/communication-services-web-chat-hero

我有格式的字符串: "str1</br>str2</br>str3</br>".

我想要的是我想在应用程序的 ChatArea 组件中将此字符串呈现为 html,使其看起来像

str1
str2
str3

我还在 sideEffects.ts 文件的 sendMessageHelper 方法中将 SendMessageOptions.type 设置为“html”,但仍然仅将输出作为字符串获取。唯一的区别是现在我得到了没有/标签的br净化字符串。

任何帮助将不胜感激。提前非常感谢。

4

1 回答 1

1

默认情况下,此示例通常假设消息是单行字符串,并且 react 不会自动处理\n字符或<br />.

要在渲染消息的ChatThread中执行多行操作,您需要确保生成适当的 JSX:

要支持多行,其中行由您分隔,\n您需要更新

{renderHyperlink(message.content.message)}

类似于:

{message.content.message.split(/\n/g).map((line: string) => <p>{renderHyperlink(line)}</p>)}

更多信息和其他解决方案可以在这里找到:How to add a <br> tag in reactjs between two strings?

要支持从消息中呈现任意 HTML,您需要修改此行以将消息字符串加载为 html。但是,在应用程序中使用任意发送的 html 可能很危险,恶意用户可能会嵌入恶意脚本 html 或脚本,因此请避免这样做。有关更多信息,请搜索跨站点脚本攻击:https ://owasp.org/www-community/attacks/xss/ 。

于 2021-05-31T21:47:43.047 回答