Skip to content

Commit

Permalink
查看备忘: 链接 变超链接
Browse files Browse the repository at this point in the history
  • Loading branch information
yc-2018 committed Sep 22, 2024
1 parent 905616c commit e1b535d
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/pages/MemoDrawer/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,30 @@ const MemoDrawer = () => {
*/
const formatTime = strTime => strTime?.replace('T00:00:00', ' ').replace('T', ' ')


/**
* 定义函数来检测和转换 URL 为链接
*
* @author Yc
* @since 2024/9/23 1:27
*/
const linkify = (text) => {
const urlRegex = /(https?:\/\/[^\s]+)/g;

return text.split(urlRegex).map((part, index) => {
// 如果匹配到的是 URL,将其转换为 <a> 标签
if (part.match(urlRegex)) {
return (
<a key={index} href={part} target="_blank" rel="noopener noreferrer">
{part}
</a>
);
}
// 否则,返回普通文本
return part;
});
};

/** 处理待办列表的操作 */
const listHandleAction = async event => {

Expand Down Expand Up @@ -278,7 +302,7 @@ const MemoDrawer = () => {
style={{height: '70vh', border: '1px solid #ccc', borderRadius: '6px', padding: 9, overflow: 'auto'}}
>
<pre style={{whiteSpace: 'pre-wrap', fontSize: '14px', margin: 0, fontFamily: 'unset'}}>
{itemObj.content}
{linkify(itemObj.content)}
</pre>
</div>,
})
Expand Down

0 comments on commit e1b535d

Please sign in to comment.