From 0c4838e45c3e472567d6ce0f4800d785fee66143 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Tue, 20 Aug 2024 10:52:08 +0200 Subject: [PATCH] Fix copy code edge case --- web/cm_plugins/code_copy.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/web/cm_plugins/code_copy.ts b/web/cm_plugins/code_copy.ts index 2f1a5333..5691097e 100644 --- a/web/cm_plugins/code_copy.ts +++ b/web/cm_plugins/code_copy.ts @@ -26,7 +26,7 @@ class CodeCopyWidget extends WidgetType { toDOM() { const wrap = document.createElement("span"); - wrap.setAttribute("aria-hidden", "true"); + // wrap.setAttribute("aria-hidden", "true"); wrap.className = "sb-actions"; const button = wrap.appendChild(document.createElement("button")); @@ -69,10 +69,10 @@ function codeCopyDecoration( to, enter: (node) => { if (node.name == "FencedCode") { - const textNode = node.node.getChild("CodeText"); + const textNodes = node.node.getChildren("CodeText"); const infoNode = node.node.getChild("CodeInfo"); - if (!textNode) { + if (textNodes.length === 0) { return; } @@ -87,7 +87,11 @@ function codeCopyDecoration( return; } - const text = view.state.doc.sliceString(textNode.from, textNode.to); + // Accumulate the text content of the code block + let text = ""; + for (const textNode of textNodes) { + text += view.state.doc.sliceString(textNode.from, textNode.to); + } const deco = Decoration.widget({ widget: new CodeCopyWidget(text, client), side: 0,