From be28fae40f575cb66e753adb62dab895a9487f94 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Wed, 11 Oct 2023 22:23:14 +0200 Subject: [PATCH] [mastodon-client] Fix code block rendering This doesn't work in all clients, but it works in more than the previous solution & matches what glitch-soc is doing. --- .../backend/src/server/api/mastodon/helpers/mfm.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/server/api/mastodon/helpers/mfm.ts b/packages/backend/src/server/api/mastodon/helpers/mfm.ts index 9e8201ae8..c5ecc3bc2 100644 --- a/packages/backend/src/server/api/mastodon/helpers/mfm.ts +++ b/packages/backend/src/server/api/mastodon/helpers/mfm.ts @@ -68,7 +68,15 @@ export class MfmHelpers { blockCode(node) { const pre = doc.createElement("pre"); const inner = doc.createElement("code"); - inner.textContent = node.props.code; + + const nodes = node.props.code + .split(/\r\n|\r|\n/) + .map((x) => doc.createTextNode(x)); + + for (const x of intersperse("br", nodes)) { + inner.appendChild(x === "br" ? doc.createElement("br") : x); + } + pre.appendChild(inner); return pre; },