2024-06-08 16:41:05 +02:00
|
|
|
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
2025-03-21 00:04:30 +01:00
|
|
|
import ReactMarkdown, { Components } from 'react-markdown';
|
2024-03-26 15:07:28 +01:00
|
|
|
import remarkGfm from 'remark-gfm';
|
|
|
|
|
import supersub from 'remark-supersub';
|
2024-05-08 11:10:43 +02:00
|
|
|
import rehypeSanitize, { defaultSchema } from 'rehype-sanitize';
|
2025-03-21 00:04:30 +01:00
|
|
|
import rehypeRaw from 'rehype-raw';
|
2024-06-08 16:41:05 +02:00
|
|
|
import { useDismiss, useFloating, useFocus, useHover, useInteractions, offset, shift, size, autoUpdate, Placement, FloatingPortal } from '@floating-ui/react';
|
2024-05-08 11:10:43 +02:00
|
|
|
import { getLinkMediaInfo, getHasThumbnail } from '../../lib/utils/media-utils';
|
2024-06-15 16:02:24 +02:00
|
|
|
import { isCatalogView } from '../../lib/utils/view-utils';
|
2024-06-08 16:41:05 +02:00
|
|
|
import useIsMobile from '../../hooks/use-is-mobile';
|
2024-05-08 11:10:43 +02:00
|
|
|
import CommentMedia from '../comment-media';
|
2024-06-08 16:41:05 +02:00
|
|
|
import styles from './markdown.module.css';
|
2025-06-04 12:55:55 +02:00
|
|
|
import { Link, useLocation, useParams } from 'react-router-dom';
|
2024-08-31 14:22:02 +02:00
|
|
|
import { canEmbed } from '../embed';
|
2025-10-17 23:30:35 +02:00
|
|
|
import { is5chanLink, transform5chanLinkToInternal, preprocess5chanPatterns } from '../../lib/utils/url-utils';
|
2024-03-26 15:07:28 +01:00
|
|
|
|
2024-06-08 16:41:05 +02:00
|
|
|
interface ContentLinkEmbedProps {
|
|
|
|
|
children: any;
|
|
|
|
|
href: string;
|
|
|
|
|
linkMediaInfo: any;
|
2024-03-26 15:07:28 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-21 00:04:30 +01:00
|
|
|
interface ExtendedComponents extends Components {
|
|
|
|
|
spoiler: React.ComponentType<{ children: React.ReactNode }>;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-08 16:41:05 +02:00
|
|
|
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<Placement>('right');
|
|
|
|
|
const availableWidthRef = useRef<number>(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 (
|
|
|
|
|
<>
|
|
|
|
|
<a href={href} target='_blank' rel='noopener noreferrer'>
|
|
|
|
|
{children}
|
|
|
|
|
</a>{' '}
|
|
|
|
|
[
|
|
|
|
|
<span className={styles.embedButton} onClick={() => setShowMedia(!showMedia)} ref={refs.setReference} {...getReferenceProps()}>
|
|
|
|
|
{showMedia ? t('remove') : isMobile ? t('open') : t('embed')}
|
|
|
|
|
</span>
|
|
|
|
|
]
|
|
|
|
|
{showMedia && (
|
|
|
|
|
<>
|
|
|
|
|
<br />
|
|
|
|
|
<CommentMedia isReply={false} setShowThumbnail={setShowMedia} commentMediaInfo={linkMediaInfo} showThumbnail={false} />
|
|
|
|
|
</>
|
|
|
|
|
)}
|
2024-08-31 14:22:02 +02:00
|
|
|
{getHasThumbnail(linkMediaInfo, href) && (
|
|
|
|
|
<FloatingPortal>
|
|
|
|
|
{isOpen && !isMobile && (
|
|
|
|
|
<div className={styles.floatingEmbed} ref={refs.setFloating} style={floatingStyles} {...getFloatingProps()}>
|
|
|
|
|
<CommentMedia isReply={false} isFloatingEmbed={true} setShowThumbnail={setShowMedia} commentMediaInfo={linkMediaInfo} showThumbnail={true} />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</FloatingPortal>
|
|
|
|
|
)}
|
2024-06-08 16:41:05 +02:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-26 15:07:28 +01:00
|
|
|
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',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-21 00:04:30 +01:00
|
|
|
const spoilerTransform = () => (tree: any) => {
|
|
|
|
|
const visit = (node: any) => {
|
|
|
|
|
if (node.tagName === 'spoiler') {
|
|
|
|
|
node.tagName = 'span';
|
|
|
|
|
node.properties = node.properties || {};
|
|
|
|
|
node.properties.className = 'spoilertext';
|
2025-01-28 15:12:39 +01:00
|
|
|
}
|
2025-03-21 00:04:30 +01:00
|
|
|
|
|
|
|
|
if (node.children) {
|
|
|
|
|
node.children.forEach(visit);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (tree.children) {
|
|
|
|
|
tree.children.forEach(visit);
|
|
|
|
|
}
|
2025-01-28 15:12:39 +01:00
|
|
|
};
|
|
|
|
|
|
2024-06-08 16:41:05 +02:00
|
|
|
interface MarkdownProps {
|
|
|
|
|
content: string;
|
2024-06-15 18:14:48 +02:00
|
|
|
title?: string;
|
2024-06-08 16:41:05 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-04 12:55:55 +02:00
|
|
|
const renderAnchorLink = (children: React.ReactNode, href: string) => {
|
|
|
|
|
if (!href) {
|
|
|
|
|
return <span>{children}</span>;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-17 23:30:35 +02:00
|
|
|
// Check if this is a valid 5chan link that should be handled internally
|
|
|
|
|
if (is5chanLink(href)) {
|
|
|
|
|
const internalPath = transform5chanLinkToInternal(href);
|
2025-06-04 12:55:55 +02:00
|
|
|
if (internalPath) {
|
|
|
|
|
// Check if the link text should be replaced with the internal path
|
|
|
|
|
let shouldReplaceText = false;
|
|
|
|
|
|
|
|
|
|
if (typeof children === 'string') {
|
|
|
|
|
shouldReplaceText = children === href || children.trim() === href.trim();
|
|
|
|
|
} else if (Array.isArray(children) && children.length === 1 && typeof children[0] === 'string') {
|
|
|
|
|
shouldReplaceText = children[0] === href || children[0].trim() === href.trim();
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-07 12:50:58 +01:00
|
|
|
// For display purposes, remove leading slash from paths like "/biz" or board identifiers
|
2025-06-04 12:55:55 +02:00
|
|
|
let displayText: React.ReactNode = children;
|
2025-11-07 12:50:58 +01:00
|
|
|
if (shouldReplaceText && internalPath.match(/^\/[^/]+$/)) {
|
2025-06-04 12:55:55 +02:00
|
|
|
displayText = internalPath.substring(1); // Remove leading slash
|
|
|
|
|
} else if (shouldReplaceText) {
|
|
|
|
|
displayText = internalPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <Link to={internalPath}>{displayText}</Link>;
|
|
|
|
|
} else {
|
2025-10-17 23:30:35 +02:00
|
|
|
console.warn('Failed to transform 5chan link to internal path:', href);
|
2025-06-04 12:55:55 +02:00
|
|
|
return <Link to={href}>{children}</Link>;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle hash routes and internal patterns (including routes that start with /#/)
|
2025-11-07 12:50:58 +01:00
|
|
|
// Support both old format (/p/...) for backward compatibility and new format (/{boardIdentifier}/...)
|
|
|
|
|
if (
|
|
|
|
|
href.startsWith('#/') ||
|
|
|
|
|
href.startsWith('/#/') ||
|
|
|
|
|
href.startsWith('/p/') ||
|
|
|
|
|
href.match(/^\/p\/[^/]+(\/c\/[^/]+)?$/) ||
|
|
|
|
|
href.match(/^\/[^/]+(\/thread\/[^/]+)?$/) ||
|
|
|
|
|
href.match(/^\/[^/]+\/(catalog|description|rules)(\/settings)?$/)
|
|
|
|
|
) {
|
2025-06-04 12:55:55 +02:00
|
|
|
return <Link to={href}>{children}</Link>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// External links
|
|
|
|
|
return (
|
|
|
|
|
<a href={href} target='_blank' rel='noopener noreferrer'>
|
|
|
|
|
{children}
|
|
|
|
|
</a>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-15 21:53:02 +02:00
|
|
|
const Markdown = ({ content, title }: MarkdownProps) => {
|
2024-08-27 18:46:42 +02:00
|
|
|
const remarkPlugins: any[] = [[supersub]];
|
2024-03-26 15:07:28 +01:00
|
|
|
|
2025-03-21 00:04:30 +01:00
|
|
|
if (content && content.length <= MAX_LENGTH_FOR_GFM) {
|
2024-03-26 15:07:28 +01:00
|
|
|
remarkPlugins.push([remarkGfm, { singleTilde: false }]);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-21 00:04:30 +01:00
|
|
|
remarkPlugins.push([blockquoteToGreentext]);
|
|
|
|
|
remarkPlugins.push([spoilerTransform]);
|
|
|
|
|
|
2024-03-26 15:07:28 +01:00
|
|
|
const customSchema = useMemo(
|
|
|
|
|
() => ({
|
|
|
|
|
...defaultSchema,
|
2025-03-21 00:04:30 +01:00
|
|
|
tagNames: [...(defaultSchema.tagNames || []), 'div', 'span', 'spoiler'],
|
2024-03-26 15:07:28 +01:00
|
|
|
attributes: {
|
|
|
|
|
...defaultSchema.attributes,
|
|
|
|
|
div: ['className'],
|
2025-01-28 15:12:39 +01:00
|
|
|
span: ['className'],
|
2025-03-21 00:04:30 +01:00
|
|
|
spoiler: [],
|
2024-03-26 15:07:28 +01:00
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
[],
|
|
|
|
|
);
|
|
|
|
|
|
2024-06-15 16:02:24 +02:00
|
|
|
const isInCatalogView = isCatalogView(useLocation().pathname, useParams());
|
|
|
|
|
|
2025-10-17 23:30:35 +02:00
|
|
|
// Preprocess content to convert plain text 5chan patterns to markdown links
|
|
|
|
|
const processedContent = preprocess5chanPatterns(content || '');
|
2025-06-04 12:55:55 +02:00
|
|
|
|
2024-03-26 15:07:28 +01:00
|
|
|
return (
|
|
|
|
|
<span className={styles.markdown}>
|
2024-06-15 18:14:48 +02:00
|
|
|
{isInCatalogView && title && (
|
|
|
|
|
<span>
|
|
|
|
|
<b>{title}</b>
|
|
|
|
|
{content ? ': ' : ''}
|
|
|
|
|
</span>
|
|
|
|
|
)}
|
2024-03-26 15:07:28 +01:00
|
|
|
<ReactMarkdown
|
2025-06-04 12:55:55 +02:00
|
|
|
children={processedContent}
|
2024-03-26 15:07:28 +01:00
|
|
|
remarkPlugins={remarkPlugins}
|
2025-03-21 00:04:30 +01:00
|
|
|
rehypePlugins={[[rehypeRaw as any], [rehypeSanitize, customSchema]]}
|
|
|
|
|
components={
|
|
|
|
|
{
|
|
|
|
|
p: ({ children }) => <p className={isInCatalogView ? styles.inline : ''}>{children}</p>,
|
|
|
|
|
h1: ({ children }) => <p className={styles.header}>{children}</p>,
|
|
|
|
|
h2: ({ children }) => <p className={styles.header}>{children}</p>,
|
|
|
|
|
h3: ({ children }) => <p className={styles.header}>{children}</p>,
|
|
|
|
|
h4: ({ children }) => <p className={styles.header}>{children}</p>,
|
|
|
|
|
h5: ({ children }) => <p className={styles.header}>{children}</p>,
|
|
|
|
|
h6: ({ children }) => <p className={styles.header}>{children}</p>,
|
|
|
|
|
img: ({ src, alt }) => {
|
|
|
|
|
const displayText = src || alt || 'image';
|
|
|
|
|
return <span>{displayText}</span>;
|
|
|
|
|
},
|
|
|
|
|
video: ({ src }) => <span>{src}</span>,
|
|
|
|
|
iframe: ({ src }) => <span>{src}</span>,
|
|
|
|
|
source: ({ src }) => <span>{src}</span>,
|
|
|
|
|
spoiler: ({ children }) => <span className='spoilertext'>{children}</span>,
|
|
|
|
|
a: ({ href, children }) => {
|
|
|
|
|
if (href && !isInCatalogView) {
|
|
|
|
|
try {
|
|
|
|
|
const linkMediaInfo = getLinkMediaInfo(href);
|
|
|
|
|
const embedUrl = href.startsWith('http') ? new URL(href) : null;
|
|
|
|
|
if ((embedUrl && canEmbed(embedUrl)) || getHasThumbnail(linkMediaInfo, href)) {
|
|
|
|
|
return <ContentLinkEmbed children={children} href={href} linkMediaInfo={linkMediaInfo} />;
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.debug('Invalid URL:', href);
|
2024-11-17 15:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-04 12:55:55 +02:00
|
|
|
return renderAnchorLink(children, href);
|
2025-03-21 00:04:30 +01:00
|
|
|
}
|
2025-06-04 12:55:55 +02:00
|
|
|
|
|
|
|
|
return renderAnchorLink(children, href || '');
|
2025-03-21 00:04:30 +01:00
|
|
|
},
|
|
|
|
|
} as ExtendedComponents
|
|
|
|
|
}
|
2024-03-26 15:07:28 +01:00
|
|
|
/>
|
|
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
};
|
2024-04-07 17:19:59 +02:00
|
|
|
export default React.memo(Markdown);
|