import { Modal, ModalContent, ModalHeader, ModalBody, ModalFooter } from '@heroui/modal'; import { Button } from '@heroui/button'; import { Chip } from '@heroui/chip'; import { Avatar } from '@heroui/avatar'; import { Tooltip } from '@heroui/tooltip'; import { Spinner } from '@heroui/spinner'; import { IoMdCheckmarkCircle, IoMdOpen, IoMdDownload } from 'react-icons/io'; import { MdUpdate } from 'react-icons/md'; import { useState, useEffect } from 'react'; import { PluginStoreItem } from '@/types/plugin-store'; import { InstallStatus } from '@/components/display_card/plugin_store_card'; import TailwindMarkdown from '@/components/tailwind_markdown'; interface PluginDetailModalProps { isOpen: boolean; onClose: () => void; plugin: PluginStoreItem | null; installStatus?: InstallStatus; installedVersion?: string; onInstall?: () => void; } /** 提取作者头像 URL */ function getAuthorAvatar (homepage?: string, downloadUrl?: string): string | undefined { // 1. 尝试从 downloadUrl 提取 GitHub 用户名 if (downloadUrl) { try { const url = new URL(downloadUrl); if (url.hostname === 'github.com' || url.hostname === 'www.github.com') { const parts = url.pathname.split('/').filter(Boolean); if (parts.length >= 1) { return `https://github.com/${parts[0]}.png`; } } } catch { // 忽略解析错误 } } // 2. 尝试从 homepage 提取 if (homepage) { try { const url = new URL(homepage); if (url.hostname === 'github.com' || url.hostname === 'www.github.com') { const parts = url.pathname.split('/').filter(Boolean); if (parts.length >= 1) { return `https://github.com/${parts[0]}.png`; } } else { return `https://api.iowen.cn/favicon/${url.hostname}.png`; } } catch { // 忽略解析错误 } } return undefined; } /** 提取 GitHub 仓库信息 */ function extractGitHubRepo (url?: string): { owner: string; repo: string; } | null { if (!url) return null; try { const urlObj = new URL(url); if (urlObj.hostname === 'github.com' || urlObj.hostname === 'www.github.com') { const parts = urlObj.pathname.split('/').filter(Boolean); if (parts.length >= 2) { return { owner: parts[0], repo: parts[1] }; } } } catch { // 忽略解析错误 } return null; } /** 从 GitHub API 获取 README */ async function fetchGitHubReadme (owner: string, repo: string): Promise { const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/readme`, { headers: { Accept: 'application/vnd.github.v3.raw', }, }); if (!response.ok) { throw new Error('Failed to fetch README'); } return response.text(); } /** 清理 README 中的 HTML 标签,保留 Markdown */ function cleanReadmeHtml (content: string): string { // 移除 HTML 注释 let cleaned = content.replace(//g, ''); // 保留常见的 Markdown 友好的 HTML 标签(img, br),其他的移除标签但保留内容 // 移除 style 和 script 标签及其内容 cleaned = cleaned.replace(/]*>[\s\S]*?<\/style>/gi, ''); cleaned = cleaned.replace(/]*>[\s\S]*?<\/script>/gi, ''); // 将其他 HTML 标签替换为空格或换行(保留内容) // 保留 img 标签(转为 markdown)- 尝试提取 alt 和 src 属性 cleaned = cleaned.replace(/]*\\bsrc=["']([^"']+)["'][^>]*\\balt=["']([^"']+)["'][^>]*>/gi, '![$2]($1)'); cleaned = cleaned.replace(/]*\\balt=["']([^"']+)["'][^>]*\\bsrc=["']([^"']+)["'][^>]*>/gi, '![$1]($2)'); cleaned = cleaned.replace(/]+src=["']([^"']+)["'][^>]*>/gi, '![]($1)'); // 移除其他 HTML 标签,但保留内容 cleaned = cleaned.replace(/<\/?[^>]+(>|$)/g, ''); // 清理多余的空行(超过2个连续换行) cleaned = cleaned.replace(/\n{3,}/g, '\n\n'); return cleaned.trim(); } export default function PluginDetailModal ({ isOpen, onClose, plugin, installStatus = 'not-installed', installedVersion, onInstall, }: PluginDetailModalProps) { const [readme, setReadme] = useState(''); const [readmeLoading, setReadmeLoading] = useState(false); const [readmeError, setReadmeError] = useState(false); // 获取 GitHub 仓库信息(需要在 hooks 之前计算) const githubRepo = plugin ? extractGitHubRepo(plugin.homepage) : null; // 当模态框打开且有 GitHub 链接时,获取 README useEffect(() => { if (!isOpen || !githubRepo) { setReadme(''); setReadmeError(false); return; } const loadReadme = async () => { setReadmeLoading(true); setReadmeError(false); try { const content = await fetchGitHubReadme(githubRepo.owner, githubRepo.repo); // 清理 HTML 标签后再设置 setReadme(cleanReadmeHtml(content)); } catch (error) { console.error('Failed to fetch README:', error); setReadmeError(true); } finally { setReadmeLoading(false); } }; loadReadme(); }, [isOpen, githubRepo?.owner, githubRepo?.repo]); if (!plugin) return null; const { name, version, author, description, tags, homepage, downloadUrl, minVersion } = plugin; const avatarUrl = getAuthorAvatar(homepage, downloadUrl) || `https://avatar.vercel.sh/${encodeURIComponent(name)}`; return ( {(onModalClose) => ( <> {/* 插件头部信息 */}

{name}

{homepage && ( )}

by {author || '未知作者'}

{/* 标签和版本信息 */}
v{version} {tags?.map((tag) => ( {tag} ))} {installStatus === 'update-available' && installedVersion && ( 可更新 )} {installStatus === 'installed' && ( } > 已安装 )}
{/* 插件描述 */}

插件描述

{description || '暂无描述'}

{/* 插件信息 */}

插件信息

最新版本: v{version}
{installedVersion && (
已安装版本: v{installedVersion}
)} {minVersion && (
最低要求版本: v{minVersion}
)}
插件 ID: {plugin.id}
{downloadUrl && (
下载地址:
)}
{/* GitHub README 显示 */} {githubRepo && ( <>

详情

{readmeLoading && (
)} {readmeError && (

无法加载 README

)} {!readmeLoading && !readmeError && readme && (
)}
)}
{installStatus === 'installed' ? ( ) : installStatus === 'update-available' ? ( ) : ( )} )}
); }