mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix(markdown): users couldn't include empty lines in the post content
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import ReactMarkdown from 'react-markdown';
|
||||
import remarkBreaks from 'remark-breaks';
|
||||
import remarkGfm from 'remark-gfm';
|
||||
import supersub from 'remark-supersub';
|
||||
import rehypeSanitize, { defaultSchema } from 'rehype-sanitize';
|
||||
@@ -128,7 +129,7 @@ interface MarkdownProps {
|
||||
}
|
||||
|
||||
const Markdown = ({ content, title }: MarkdownProps) => {
|
||||
const remarkPlugins: any[] = [[supersub]];
|
||||
const remarkPlugins: any[] = [[remarkBreaks, supersub]];
|
||||
|
||||
if (content && content.length <= MAX_LENGTH_FOR_GFM) {
|
||||
remarkPlugins.push([remarkGfm, { singleTilde: false }]);
|
||||
@@ -150,6 +151,14 @@ const Markdown = ({ content, title }: MarkdownProps) => {
|
||||
|
||||
const isInCatalogView = isCatalogView(useLocation().pathname, useParams());
|
||||
|
||||
// retain empty lines
|
||||
const processedContent = useMemo(() => {
|
||||
let md = content;
|
||||
md = md.replace(/```[\s\S]*?```/g, (m) => m.replace(/\n/g, '\n '));
|
||||
md = md.replace(/(?<=\n\n)(?![*-])\n/g, ' \n ');
|
||||
return md;
|
||||
}, [content]);
|
||||
|
||||
return (
|
||||
<span className={styles.markdown}>
|
||||
{isInCatalogView && title && (
|
||||
@@ -159,7 +168,7 @@ const Markdown = ({ content, title }: MarkdownProps) => {
|
||||
</span>
|
||||
)}
|
||||
<ReactMarkdown
|
||||
children={content.replace(/\n/g, '\n\n')} // single newlines would be rendered as spaces
|
||||
children={processedContent}
|
||||
remarkPlugins={remarkPlugins}
|
||||
rehypePlugins={[[rehypeSanitize, customSchema]]}
|
||||
components={{
|
||||
|
||||
@@ -322,7 +322,7 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid:
|
||||
wrap='soft'
|
||||
ref={textRef}
|
||||
onChange={(e) => {
|
||||
const content = e.target.value.replace(/\n/g, '\n\n');
|
||||
const content = e.target.value;
|
||||
isInPostView ? setPublishReplyOptions({ content }) : setSubmitStore({ content });
|
||||
}}
|
||||
/>
|
||||
|
||||
@@ -154,7 +154,7 @@ const ReplyModal = ({ closeModal, showReplyModal, parentCid, postCid, scrollY, s
|
||||
};
|
||||
|
||||
const handleContentChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||||
const contentWithoutPrefix = e.target.value.slice(contentPrefix.length).replace(/\n/g, '\n\n');
|
||||
const contentWithoutPrefix = e.target.value.slice(contentPrefix.length);
|
||||
if (textRef.current && textRef.current.value !== contentWithoutPrefix) {
|
||||
setPublishReplyOptions({ content: contentWithoutPrefix });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user