fix: preserve paragraphs when editing a status

zio/stable
patak 2023-01-07 23:42:17 +01:00
parent 1817afdb23
commit 3dc20ea2f9
1 changed files with 10 additions and 1 deletions

View File

@ -1,7 +1,7 @@
// @unimport-disable // @unimport-disable
import type { Emoji } from 'masto' import type { Emoji } from 'masto'
import type { Node } from 'ultrahtml' 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 { findAndReplaceEmojisInText } from '@iconify/utils'
import { emojiRegEx, getEmojiAttributes } from '../config/emojis' import { emojiRegEx, getEmojiAttributes } from '../config/emojis'
@ -79,6 +79,8 @@ export function parseMastodonHTML(
transforms.push(replaceCustomEmoji(options.emojis || {})) transforms.push(replaceCustomEmoji(options.emojis || {}))
transforms.push(transformParagraphs)
return transformSync(parse(html), transforms) return transformSync(parse(html), transforms)
} }
@ -349,3 +351,10 @@ function transformMarkdown(node: Node) {
return node return node
return _markdownProcess(node.value) return _markdownProcess(node.value)
} }
function transformParagraphs(node: Node): Node | Node[] {
// For top level paragraphs, inject an empty <p> 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
}