From a524a9cea8d9cbbfe55fadd9da208aa97035ff80 Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Fri, 13 Jun 2025 20:43:46 -0400 Subject: [PATCH] skip empty elements in MfmService.fromHtml --- packages/backend/src/core/MfmService.ts | 68 ++++++++++++++++--------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/packages/backend/src/core/MfmService.ts b/packages/backend/src/core/MfmService.ts index 551b25394a..adea0c467d 100644 --- a/packages/backend/src/core/MfmService.ts +++ b/packages/backend/src/core/MfmService.ts @@ -127,48 +127,60 @@ export class MfmService { } case 'h1': { - text += '**【'; - appendChildren(node.childNodes); - text += '】**\n'; + if (node.childNodes.length > 0) { + text += '**【'; + appendChildren(node.childNodes); + text += '】**\n'; + } break; } case 'h2': case 'h3': { - text += '**'; - appendChildren(node.childNodes); - text += '**\n'; + if (node.childNodes.length > 0) { + text += '**'; + appendChildren(node.childNodes); + text += '**\n'; + } break; } case 'b': case 'strong': { - text += '**'; - appendChildren(node.childNodes); - text += '**'; + if (node.childNodes.length > 0) { + text += '**'; + appendChildren(node.childNodes); + text += '**'; + } break; } case 'small': { - text += ''; - appendChildren(node.childNodes); - text += ''; + if (node.childNodes.length > 0) { + text += ''; + appendChildren(node.childNodes); + text += ''; + } break; } case 's': case 'del': { - text += '~~'; - appendChildren(node.childNodes); - text += '~~'; + if (node.childNodes.length > 0) { + text += '~~'; + appendChildren(node.childNodes); + text += '~~'; + } break; } case 'i': case 'em': { - text += ''; - appendChildren(node.childNodes); - text += ''; + if (node.childNodes.length > 0) { + text += ''; + appendChildren(node.childNodes); + text += ''; + } break; } @@ -223,9 +235,11 @@ export class MfmService { // inline code () case 'code': { - text += '`'; - appendChildren(node.childNodes); - text += '`'; + if (node.childNodes.length > 0) { + text += '`'; + appendChildren(node.childNodes); + text += '`'; + } break; } @@ -242,8 +256,10 @@ export class MfmService { case 'h4': case 'h5': case 'h6': { - text += '\n\n'; - appendChildren(node.childNodes); + if (node.childNodes.length > 0) { + text += '\n\n'; + appendChildren(node.childNodes); + } break; } @@ -255,8 +271,10 @@ export class MfmService { case 'li': case 'dt': case 'dd': { - text += '\n'; - appendChildren(node.childNodes); + if (node.childNodes.length > 0) { + text += '\n'; + appendChildren(node.childNodes); + } break; }