diff --git a/src/app/components/editor/input.ts b/src/app/components/editor/input.ts index 4e1043151..1a643c862 100644 --- a/src/app/components/editor/input.ts +++ b/src/app/components/editor/input.ts @@ -399,6 +399,7 @@ export const domToEditorInput = ( domNodes.forEach((node) => { if (isText(node)) { + if (node.data.trim() === '' && node.data.includes('\n')) return; if (lineHolder.length === 0) { // we are inserting first part of line // it may contain block markdown starting data diff --git a/src/app/components/editor/output.ts b/src/app/components/editor/output.ts index 7d24d9cd3..92f3d8542 100644 --- a/src/app/components/editor/output.ts +++ b/src/app/components/editor/output.ts @@ -26,10 +26,17 @@ export type OutputOptions = { * a map of regex patterns to replace nicknames with, used when stripNickname is true */ nickNameReplacement?: Map; + /** + * if true, we are inside a
 block and should not convert newlines to 
+ */ + insidePre?: boolean; }; const textToCustomHtml = (node: Text, opts: OutputOptions): string => { let string = sanitizeText(node.text); + if (!opts.insidePre && !opts.allowBlockMarkdown) { + string = string.replaceAll("\n", "
"); + } if (opts.allowTextFormatting) { if (node.bold) string = `${string}`; if (node.italic) string = `${string}`; @@ -60,8 +67,14 @@ const elementToCustomHtml = (node: CustomElement, children: string): string => { return `${children}`; case BlockType.CodeLine: return `${children}\n`; - case BlockType.CodeBlock: - return `
${children}
`; + case BlockType.CodeBlock: { + const childrenInsidePre = node.children + .map((element, index, array) => + toMatrixCustomHTML(element, { ...opts, insidePre: true }), + ) + .join(""); + return `
${childrenInsidePre}
`; + } case BlockType.QuoteLine: return `${children}
`; case BlockType.BlockQuote: @@ -140,7 +153,7 @@ export const toMatrixCustomHTML = ( allowInlineMarkdown: false, allowBlockMarkdown: false, }) - .replace(/$/, "\n") + .replace(/$/, "\n\n") .replace(/^(\\*)>/, "$1>"); // strip nicknames if needed diff --git a/src/app/features/room/RoomInput.tsx b/src/app/features/room/RoomInput.tsx index db21b3544..2d12fd73f 100644 --- a/src/app/features/room/RoomInput.tsx +++ b/src/app/features/room/RoomInput.tsx @@ -852,7 +852,8 @@ export const RoomInput = forwardRef( } else { // we don't have a formatted body, but we need one content.format = 'org.matrix.custom.html'; - content.formatted_body = `${htmlPrefix}${plainText}`; + const escapedBody = sanitizeText(plainText).replaceAll('\n', '
'); + content.formatted_body = `${htmlPrefix}${escapedBody}`; } } } diff --git a/src/app/features/room/message/MessageEditor.tsx b/src/app/features/room/message/MessageEditor.tsx index e3ea4874a..7806c3329 100644 --- a/src/app/features/room/message/MessageEditor.tsx +++ b/src/app/features/room/message/MessageEditor.tsx @@ -256,7 +256,7 @@ export const MessageEditor = as<'div', MessageEditorProps>( const links = getLinks(editor.children); - if (!customHtmlEqualsPlainText(customHtml, plainText)) { + if (pmpDisplayname || !customHtmlEqualsPlainText(customHtml, plainText)) { newContent.format = 'org.matrix.custom.html'; newContent.formatted_body = customHtml; contentBody.format = 'org.matrix.custom.html'; diff --git a/src/app/plugins/markdown/markdownToHtml.ts b/src/app/plugins/markdown/markdownToHtml.ts index 27e812bfa..1e749ad6a 100644 --- a/src/app/plugins/markdown/markdownToHtml.ts +++ b/src/app/plugins/markdown/markdownToHtml.ts @@ -17,6 +17,7 @@ import { // Configure marked with Matrix extensions const processor = marked.use({ + breaks: true, extensions: [ matrixSpoilerExtension, matrixMathExtension,