From a524a9cea8d9cbbfe55fadd9da208aa97035ff80 Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Fri, 13 Jun 2025 20:43:46 -0400 Subject: [PATCH 1/3] 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; } From 8d628aa50b92a2fbefd4ab0ff397dfe5cd0a2815 Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Fri, 13 Jun 2025 21:08:20 -0400 Subject: [PATCH 2/3] avoid duplicating all the childNodes.length checks --- packages/backend/src/core/MfmService.ts | 83 +++++++++++-------------- 1 file changed, 35 insertions(+), 48 deletions(-) diff --git a/packages/backend/src/core/MfmService.ts b/packages/backend/src/core/MfmService.ts index adea0c467d..57a1ae5c08 100644 --- a/packages/backend/src/core/MfmService.ts +++ b/packages/backend/src/core/MfmService.ts @@ -72,12 +72,17 @@ export class MfmService { return; } - switch (node.tagName) { - case 'br': { - text += '\n'; - break; - } + if (node.tagName === 'br') { + text += '\n'; + return; + } + // Don't produce invalid empty MFM + if (node.childNodes.length < 1) { + return; + } + + switch (node.tagName) { case 'a': { const txt = getText(node); const rel = node.attribs.rel; @@ -127,60 +132,48 @@ export class MfmService { } case 'h1': { - if (node.childNodes.length > 0) { - text += '**【'; - appendChildren(node.childNodes); - text += '】**\n'; - } + text += '**【'; + appendChildren(node.childNodes); + text += '】**\n'; break; } case 'h2': case 'h3': { - if (node.childNodes.length > 0) { - text += '**'; - appendChildren(node.childNodes); - text += '**\n'; - } + text += '**'; + appendChildren(node.childNodes); + text += '**\n'; break; } case 'b': case 'strong': { - if (node.childNodes.length > 0) { - text += '**'; - appendChildren(node.childNodes); - text += '**'; - } + text += '**'; + appendChildren(node.childNodes); + text += '**'; break; } case 'small': { - if (node.childNodes.length > 0) { - text += ''; - appendChildren(node.childNodes); - text += ''; - } + text += ''; + appendChildren(node.childNodes); + text += ''; break; } case 's': case 'del': { - if (node.childNodes.length > 0) { - text += '~~'; - appendChildren(node.childNodes); - text += '~~'; - } + text += '~~'; + appendChildren(node.childNodes); + text += '~~'; break; } case 'i': case 'em': { - if (node.childNodes.length > 0) { - text += ''; - appendChildren(node.childNodes); - text += ''; - } + text += ''; + appendChildren(node.childNodes); + text += ''; break; } @@ -235,11 +228,9 @@ export class MfmService { // inline code () case 'code': { - if (node.childNodes.length > 0) { - text += '`'; - appendChildren(node.childNodes); - text += '`'; - } + text += '`'; + appendChildren(node.childNodes); + text += '`'; break; } @@ -256,10 +247,8 @@ export class MfmService { case 'h4': case 'h5': case 'h6': { - if (node.childNodes.length > 0) { - text += '\n\n'; - appendChildren(node.childNodes); - } + text += '\n\n'; + appendChildren(node.childNodes); break; } @@ -271,10 +260,8 @@ export class MfmService { case 'li': case 'dt': case 'dd': { - if (node.childNodes.length > 0) { - text += '\n'; - appendChildren(node.childNodes); - } + text += '\n'; + appendChildren(node.childNodes); break; } From a5f5de46dabc9a250f9c5908aa384e38887b056e Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Fri, 13 Jun 2025 23:23:35 -0400 Subject: [PATCH 3/3] fix conversion of empty links to mfm URL --- packages/backend/src/core/MfmService.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/backend/src/core/MfmService.ts b/packages/backend/src/core/MfmService.ts index 57a1ae5c08..839cdf534c 100644 --- a/packages/backend/src/core/MfmService.ts +++ b/packages/backend/src/core/MfmService.ts @@ -72,17 +72,11 @@ export class MfmService { return; } - if (node.tagName === 'br') { - text += '\n'; - return; - } - - // Don't produce invalid empty MFM - if (node.childNodes.length < 1) { - return; - } - switch (node.tagName) { + case 'br': { + text += '\n'; + return; + } case 'a': { const txt = getText(node); const rel = node.attribs.rel; @@ -128,9 +122,16 @@ export class MfmService { text += generateLink(); } - break; + return; } + } + // Don't produce invalid empty MFM + if (node.childNodes.length < 1) { + return; + } + + switch (node.tagName) { case 'h1': { text += '**【'; appendChildren(node.childNodes);