import React, { useEffect, useMemo, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import ReactMarkdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; import supersub from 'remark-supersub'; import rehypeSanitize, { defaultSchema } from 'rehype-sanitize'; import { useDismiss, useFloating, useFocus, useHover, useInteractions, offset, shift, size, autoUpdate, Placement, FloatingPortal } from '@floating-ui/react'; import { getLinkMediaInfo, getHasThumbnail } from '../../lib/utils/media-utils'; import { isCatalogView } from '../../lib/utils/view-utils'; import useIsMobile from '../../hooks/use-is-mobile'; import CommentMedia from '../comment-media'; import styles from './markdown.module.css'; import { useLocation, useParams } from 'react-router-dom'; import { canEmbed } from '../embed'; interface ContentLinkEmbedProps { children: any; href: string; linkMediaInfo: any; } const ContentLinkEmbed = ({ children, href, linkMediaInfo }: ContentLinkEmbedProps) => { const { t } = useTranslation(); const isMobile = useIsMobile(); const [isOpen, setIsOpen] = useState(false); const [showMedia, setShowMedia] = useState(false); const placementRef = useRef('right'); const availableWidthRef = useRef(0); const { refs, floatingStyles, update, context } = useFloating({ open: isOpen, onOpenChange: setIsOpen, placement: placementRef.current, middleware: [ shift({ padding: 10 }), offset({ mainAxis: 5 }), size({ apply({ availableWidth, elements }) { availableWidthRef.current = availableWidth; if (availableWidth >= 250) { elements.floating.style.maxWidth = `${availableWidth - 12}px`; } else if (placementRef.current === 'right') { placementRef.current = 'left'; } }, }), ], whileElementsMounted: autoUpdate, }); const hover = useHover(context, { move: false }); const focus = useFocus(context); const dismiss = useDismiss(context); const { getReferenceProps, getFloatingProps } = useInteractions([hover, focus, dismiss]); useEffect(() => { const handleResize = () => { const availableWidth = availableWidthRef.current; if (availableWidth >= 250) { placementRef.current = 'right'; } else { placementRef.current = 'left'; } update(); }; window.addEventListener('resize', handleResize); return () => { window.removeEventListener('resize', handleResize); }; }, [update]); return ( <> {children} {' '} [ setShowMedia(!showMedia)} ref={refs.setReference} {...getReferenceProps()}> {showMedia ? t('remove') : isMobile ? t('open') : t('embed')} ] {showMedia && ( <>
)} {getHasThumbnail(linkMediaInfo, href) && ( {isOpen && !isMobile && (
)}
)} ); }; const MAX_LENGTH_FOR_GFM = 10000; // remarkGfm lags with large content const blockquoteToGreentext = () => (tree: any) => { tree.children.forEach((node: any) => { if (node.type === 'blockquote') { node.children.forEach((child: any) => { if (child.type === 'paragraph' && child.children.length > 0) { const prefix = { type: 'text', value: '>', }; child.children.unshift(prefix); } }); node.type = 'div'; node.data = { hName: 'div', hProperties: { className: 'greentext', }, }; } }); }; interface MarkdownProps { content: string; title?: string; } const Markdown = ({ content, title }: MarkdownProps) => { const remarkPlugins: any[] = [[supersub]]; if (content && content.length <= MAX_LENGTH_FOR_GFM) { remarkPlugins.push([remarkGfm, { singleTilde: false }]); } const customSchema = useMemo( () => ({ ...defaultSchema, tagNames: [...(defaultSchema.tagNames || []), 'div'], attributes: { ...defaultSchema.attributes, div: ['className'], }, }), [], ); remarkPlugins.push([blockquoteToGreentext]); const isInCatalogView = isCatalogView(useLocation().pathname, useParams()); return ( {isInCatalogView && title && ( {title} {content ? ': ' : ''} )}

{children}

, img: ({ src }) => {src}, video: ({ src }) => {src}, iframe: ({ src }) => {src}, source: ({ src }) => {src}, hr: () => null, a: ({ href, children }) => { if (href && !isInCatalogView) { const linkMediaInfo = getLinkMediaInfo(href); const embedUrl = new URL(href); if (canEmbed(embedUrl) || getHasThumbnail(linkMediaInfo, href)) { return ; } else { return ( {children} ); } } }, }} />
); }; export default React.memo(Markdown);