Files
5chan/src/components/Post.jsx
T

85 lines
2.1 KiB
React
Raw Normal View History

2023-03-29 13:37:36 +02:00
import React from 'react';
2023-04-21 16:38:14 +02:00
import ReactMarkdown from 'react-markdown';
2023-04-25 21:30:10 +02:00
// import { Link } from 'react-router-dom';
2023-04-27 13:10:23 +02:00
import rehypeSanitize, { defaultSchema } from 'rehype-sanitize';
2023-04-23 18:53:47 +02:00
import breaks from 'remark-breaks';
2023-04-21 16:57:08 +02:00
2023-04-27 13:10:23 +02:00
const customSchema = {
...defaultSchema,
tagNames: [...defaultSchema.tagNames, 'div'],
attributes: {
...defaultSchema.attributes,
div: ['className'],
},
};
2023-04-21 16:38:14 +02:00
const blockquoteToGreentext = () => (tree) => {
tree.children.forEach((node) => {
if (node.type === 'blockquote') {
node.children.forEach((child) => {
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',
},
};
}
});
};
2023-04-27 13:10:23 +02:00
2023-04-25 21:30:10 +02:00
// const createQuotelink = (handlequoteclick, comment, children) => {
// const text = children.map((child) => (typeof child === 'string' ? child : child.props.children)).join('');
// const regex = /(\s|^)(c\/[A-Za-z0-9]{12}|c\/[A-Za-z0-9]{45})(?=\s|$)/g;
// const parts = text.split(regex);
2023-04-21 16:38:14 +02:00
2023-04-25 21:30:10 +02:00
// return parts.flatMap((part, i) => {
// if (regex.test(part)) {
// return [
// <Link
// key={`link-${i}`}
// className="quotelink"
// to=""
// onClick={handlequoteclick}
// >
// {part.trim()}
// </Link>,
// ];
// } else {
// return [part];
// }
// });
// };
2023-04-21 16:38:14 +02:00
const Post = ({ content, handlequoteclick, comment }) => {
2023-04-24 13:09:59 +02:00
const doubleNewlineContent = content?.replaceAll(/\n(?!\n)/g, '\n\n');
2023-03-29 13:37:36 +02:00
2023-04-21 16:38:14 +02:00
return (
<ReactMarkdown
2023-04-25 15:26:46 +02:00
children={doubleNewlineContent}
2023-04-23 18:53:47 +02:00
remarkPlugins={[blockquoteToGreentext, breaks]}
2023-04-27 13:10:23 +02:00
rehypePlugins={[[rehypeSanitize, customSchema]]}
2023-04-21 16:38:14 +02:00
components={{
img: ({ src }) => <span>{src}</span>,
2023-04-21 16:38:14 +02:00
video: () => null,
source: () => null,
gif: () => null,
2023-04-21 16:57:08 +02:00
p: ({ children }) => <div className="custom-paragraph">{children}</div>,
2023-04-21 16:38:14 +02:00
}}
/>
);
2023-03-29 13:37:36 +02:00
};
export default Post;