diff --git a/packages/backend/src/mfm/to-html.ts b/packages/backend/src/mfm/to-html.ts index c32363574..d59c60687 100644 --- a/packages/backend/src/mfm/to-html.ts +++ b/packages/backend/src/mfm/to-html.ts @@ -10,44 +10,44 @@ import { intersperse } from '@/prelude/array.js'; // from the text will be extracted. export async function toHtml(mfmText: string, mentions?: string[]): Promise { const nodes = mfm.parse(mfmText); - if (nodes == null) { + if (nodes.length === 0) { return null; } const doc = new JSDOM('').window.document; - const handlers: { [K in mfm.MfmNode['type']]: (node: mfm.NodeType) => any } = { - bold(node) { + const handlers: { [K in mfm.MfmNode['type']]: (node: mfm.NodeType) => Promise } = { + async bold(node) { const el = doc.createElement('b'); appendChildren(node.children, el); return el; }, - small(node) { + async small(node) { const el = doc.createElement('small'); appendChildren(node.children, el); return el; }, - strike(node) { + async strike(node) { const el = doc.createElement('del'); appendChildren(node.children, el); return el; }, - italic(node) { + async italic(node) { const el = doc.createElement('i'); appendChildren(node.children, el); return el; }, - fn(node) { + async fn(node) { const el = doc.createElement('i'); appendChildren(node.children, el); return el; }, - blockCode(node) { + async blockCode(node) { const pre = doc.createElement('pre'); const inner = doc.createElement('code'); inner.textContent = node.props.code; @@ -55,21 +55,21 @@ export async function toHtml(mfmText: string, mentions?: string[]): Promise doc.createTextNode(x)); @@ -149,14 +149,14 @@ export async function toHtml(mfmText: string, mentions?: string[]): Promise 0) { - for (const child of children.map(x => (handlers as any)[x.type](x))) targetElement.appendChild(child); + async function appendChildren(children: mfm.MfmNode[], targetElement: HTMLElement): Promise { + type HandlerFunc = (node: mfm.MfmNode) => Promise; + const htmlChildren = await Promise.all(children.map(x => (handlers[x.type] as HandlerFunc)(x))); + + for (const child of htmlChildren) { + targetElement.appendChild(child); } } - appendChildren(nodes, doc.body); + await appendChildren(nodes, doc.body); return `

${doc.body.innerHTML}

`; }