import EntityLoadState from '@/components/EntityLoadState'; import { useState, useEffect } from 'react'; import { httpClient } from '@/app/infra/http/HttpClient'; import { useTranslation } from 'react-i18next'; import ReactMarkdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; import rehypeRaw from 'rehype-raw'; import rehypeSanitize from 'rehype-sanitize'; import rehypeHighlight from 'rehype-highlight'; import rehypeSlug from 'rehype-slug'; import rehypeAutolinkHeadings from 'rehype-autolink-headings'; import { getAPILanguageCode } from '@/i18n/I18nProvider'; import '@/styles/github-markdown.css'; import { useAuthenticatedPluginAsset } from '@/hooks/useAuthenticatedPluginResource'; function AuthenticatedReadmeImage({ author, name, filepath, alt, ...props }: { author: string; name: string; filepath: string; alt?: string; } & React.ImgHTMLAttributes) { const { url, error } = useAuthenticatedPluginAsset(author, name, filepath); if (error) return ( {alt || filepath} ); if (!url) return ( ); return ( {alt ); } function PluginReadmeImage({ author, name, src, alt, ...props }: { author: string; name: string; src?: string; alt?: string; } & React.ImgHTMLAttributes) { const imageSrc = typeof src === 'string' ? src : ''; if (!imageSrc || /^(https?:\/\/|data:)/i.test(imageSrc)) { return ( {alt ); } let filepath = imageSrc.replace(/^(\.\/|\/)+/, ''); filepath = filepath.replace(/^assets\//, ''); return ( ); } export default function PluginReadme({ pluginAuthor, pluginName, }: { pluginAuthor: string; pluginName: string; }) { const { t } = useTranslation(); const [readme, setReadme] = useState(''); const [loadFailed, setLoadFailed] = useState(false); const [loadAttempt, setLoadAttempt] = useState(0); const [isLoadingReadme, setIsLoadingReadme] = useState(true); const language = getAPILanguageCode(); useEffect(() => { // Fetch plugin README setLoadFailed(false); setIsLoadingReadme(true); httpClient .getPluginReadme(pluginAuthor, pluginName, language) .then((res) => { setReadme(res.readme); }) .catch(() => { setLoadFailed(true); setReadme(''); }) .finally(() => { setIsLoadingReadme(false); }); }, [pluginAuthor, pluginName, language, loadAttempt]); if (loadFailed) return ( setLoadAttempt((n) => n + 1)} /> ); return (
{isLoadingReadme ? ( ) : readme ? (
    {children}
, ol: ({ children }) => (
    {children}
), li: ({ children }) =>
  • {children}
  • , img: ({ src, alt, ...props }) => ( ), }} > {readme}
    ) : (
    {t('plugins.noReadme')}
    )}
    ); }