From 3dc20ea2f9cb573ba4015e06669282042709aafe Mon Sep 17 00:00:00 2001 From: patak Date: Sat, 7 Jan 2023 23:42:17 +0100 Subject: [PATCH] fix: preserve paragraphs when editing a status --- composables/content-parse.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/composables/content-parse.ts b/composables/content-parse.ts index 47f82925..73e1df73 100644 --- a/composables/content-parse.ts +++ b/composables/content-parse.ts @@ -1,7 +1,7 @@ // @unimport-disable import type { Emoji } from 'masto' import type { Node } from 'ultrahtml' -import { ELEMENT_NODE, TEXT_NODE, h, parse, render } from 'ultrahtml' +import { DOCUMENT_NODE, ELEMENT_NODE, TEXT_NODE, h, parse, render } from 'ultrahtml' import { findAndReplaceEmojisInText } from '@iconify/utils' import { emojiRegEx, getEmojiAttributes } from '../config/emojis' @@ -79,6 +79,8 @@ export function parseMastodonHTML( transforms.push(replaceCustomEmoji(options.emojis || {})) + transforms.push(transformParagraphs) + return transformSync(parse(html), transforms) } @@ -349,3 +351,10 @@ function transformMarkdown(node: Node) { return node return _markdownProcess(node.value) } + +function transformParagraphs(node: Node): Node | Node[] { + // For top level paragraphs, inject an empty

to preserve status paragraphs in our editor (except for the last one) + if (node.parent?.type === DOCUMENT_NODE && node.name === 'p' && node.parent.children.at(-1) !== node) + return [node, h('p')] + return node +}