use XMLSerializer for toMastoApiHtml - fixes #556

the `inline` bit is not pretty, but does the job
This commit is contained in:
dakkar 2024-08-06 10:13:53 +01:00
parent 265afa9097
commit 61c13241ba

View file

@ -6,7 +6,7 @@
import { URL } from 'node:url';
import { Inject, Injectable } from '@nestjs/common';
import * as parse5 from 'parse5';
import { Window, XMLSerializer } from 'happy-dom';
import { Window, DocumentFragment, XMLSerializer } from 'happy-dom';
import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js';
import { intersperse } from '@/misc/prelude/array.js';
@ -483,6 +483,8 @@ export class MfmService {
const doc = window.document;
const body = doc.createElement('p');
async function appendChildren(children: mfm.MfmNode[], targetElement: any): Promise<void> {
if (children) {
for (const child of await Promise.all(children.map(async (x) => await (handlers as any)[x.type](x)))) targetElement.appendChild(child);
@ -661,7 +663,7 @@ export class MfmService {
},
};
await appendChildren(nodes, doc.body);
await appendChildren(nodes, body);
if (quoteUri !== null) {
const a = doc.createElement('a');
@ -675,9 +677,15 @@ export class MfmService {
quote.innerHTML += 'RE: ';
quote.appendChild(a);
doc.body.appendChild(quote);
body.appendChild(quote);
}
return inline ? doc.body.innerHTML : `<p>${doc.body.innerHTML}</p>`;
let result = new XMLSerializer().serializeToString(body);
if (inline) {
result = result.replace(/^<p>/,'').replace(/<\/p>$/,'');
}
return result;
}
}