Skip to content

Commit

Permalink
Merge branch 'master'
Browse files Browse the repository at this point in the history
  • Loading branch information
yc-2018 committed Sep 23, 2024
2 parents dd34a22 + 51ce9d0 commit cd23c4b
Showing 1 changed file with 46 additions and 5 deletions.
51 changes: 46 additions & 5 deletions src/pages/MemoDrawer/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
Drawer, List, Skeleton, Button, Tag,
Spin, Tooltip, Select, Divider,
Badge, Space, Dropdown, App, DatePicker,
Switch
Switch, Popover
} from "antd";
import moment from 'moment';

Expand Down Expand Up @@ -251,19 +251,60 @@ const MemoDrawer = () => {
* @author Yc
* @since 2024/9/23 1:27
*/
const linkify = (text) => {
const linkifyContent = (text) => {
// 正则表达式匹配不同类型的 URL
const urlRegex = /(https?:\/\/[^\s]+)/g;
const imageRegex = /\.(jpeg|jpg|gif|png|bmp|webp|svg)$/i; // 匹配图片格式
const videoRegex = /\.(mp4|webm|ogg|mov)$/i; // 匹配视频格式

return text.split(urlRegex).map((part, index) => {
// 如果匹配到的是 URL,将其转换为 <a> 标签
if (part.match(urlRegex)) {
// 检查是否是图片格式
if (imageRegex.test(part)) {
return (
<Popover
content={
<div style={{maxWidth: 400, maxHeight: 400}}>
<img
key={index}
src={part}
alt="Embedded content"
style={{width: '100%', maxHeight: '100%', display: 'block', margin: '10px 0'}}
/>
</div>
}
>
<a key={index} href={part} target="_blank" rel="noopener noreferrer">{part}</a>
</Popover>
);
}
// 检查是否是视频格式
if (videoRegex.test(part)) {
return (
<Popover
content={
<div style={{maxWidth: 600, maxHeight: 600}}>
<video
key={index}
controls
src={part}
style={{width: '100%', display: 'block', margin: '10px 0'}}
/>
</div>
}
>
<a key={index} href={part} target="_blank" rel="noopener noreferrer">{part}</a>
</Popover>
);
}
// 对其他链接格式使用 <a> 标签
return (
<a key={index} href={part} target="_blank" rel="noopener noreferrer">
{part}
</a>
);
}
// 否则,返回普通文本
// 返回普通文本
return part;
});
};
Expand Down Expand Up @@ -302,7 +343,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'}}>
{linkify(itemObj.content)}
{linkifyContent(itemObj.content)}
</pre>
</div>,
})
Expand Down

0 comments on commit cd23c4b

Please sign in to comment.