diff options
| author | Hazelnoot <acomputerdog@gmail.com> | 2025-06-13 21:08:20 -0400 |
|---|---|---|
| committer | Hazelnoot <acomputerdog@gmail.com> | 2025-06-13 21:08:20 -0400 |
| commit | 8d628aa50b92a2fbefd4ab0ff397dfe5cd0a2815 (patch) | |
| tree | f7a71105caa2aeeead81ce08964cabd6e27dab0e /packages | |
| parent | skip empty elements in MfmService.fromHtml (diff) | |
| download | sharkey-8d628aa50b92a2fbefd4ab0ff397dfe5cd0a2815.tar.gz sharkey-8d628aa50b92a2fbefd4ab0ff397dfe5cd0a2815.tar.bz2 sharkey-8d628aa50b92a2fbefd4ab0ff397dfe5cd0a2815.zip | |
avoid duplicating all the childNodes.length checks
Diffstat (limited to 'packages')
| -rw-r--r-- | packages/backend/src/core/MfmService.ts | 83 |
1 files 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 += '<small>'; - appendChildren(node.childNodes); - text += '</small>'; - } + text += '<small>'; + appendChildren(node.childNodes); + text += '</small>'; 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 += '<i>'; - appendChildren(node.childNodes); - text += '</i>'; - } + text += '<i>'; + appendChildren(node.childNodes); + text += '</i>'; break; } @@ -235,11 +228,9 @@ export class MfmService { // inline code (<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; } |