refactor Post.jsx with memoization

This commit is contained in:
plebeius.eth
2023-06-21 08:47:31 +02:00
parent 1f0eada8a9
commit 6b1bb46fa2
+10 -11
View File
@@ -1,21 +1,24 @@
import React from 'react';
import React, { useMemo } from 'react';
import ReactMarkdown from 'react-markdown';
import { Link } from 'react-router-dom';
import rehypeSanitize, { defaultSchema } from 'rehype-sanitize';
import breaks from 'remark-breaks';
const customSchema = {
const Post = ({ content }) => {
const doubleNewlineContent = useMemo(() => content?.replaceAll(/\n(?!\n)/g, '\n\n'), [content]);
const customSchema = useMemo(() => ({
...defaultSchema,
tagNames: [...defaultSchema.tagNames, 'div'],
attributes: {
...defaultSchema.attributes,
div: ['className'],
},
};
}), []);
const blockquoteToGreentext = () => (tree) => {
const blockquoteToGreentext = () => (tree) => {
tree.children.forEach((node) => {
if (node.type === 'blockquote') {
node.children.forEach((child) => {
@@ -36,10 +39,10 @@ const blockquoteToGreentext = () => (tree) => {
};
}
});
};
};
const createQuotelink = (children) => {
const createQuotelink = (children) => {
const regexC = /(c\/[A-Za-z0-9]{46}|c\/[A-Za-z0-9]{12})/g;
const regexP = /(p\/([A-Za-z0-9]{52}|[A-Za-z0-9-.]*\.eth))/g;
const regexU = /(u\/([A-Za-z0-9]{52}|[A-Za-z0-9-.]*\.eth))/g;
@@ -88,13 +91,9 @@ const createQuotelink = (children) => {
});
return parts;
};
};
const Post = ({ content }) => {
const doubleNewlineContent = content?.replaceAll(/\n(?!\n)/g, '\n\n');
return (
<ReactMarkdown
children={doubleNewlineContent}