From 738864df5304e68827b9d9af2ccc4502d4ace74a Mon Sep 17 00:00:00 2001 From: "moxey.eth" Date: Mon, 15 Jan 2024 11:01:15 +1100 Subject: [PATCH] fix: twoslash clipboard --- .changeset/smooth-days-shop.md | 5 +++++ src/app/hooks/useCopyCode.ts | 13 ++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 .changeset/smooth-days-shop.md diff --git a/.changeset/smooth-days-shop.md b/.changeset/smooth-days-shop.md new file mode 100644 index 00000000..b0fbe0f8 --- /dev/null +++ b/.changeset/smooth-days-shop.md @@ -0,0 +1,5 @@ +--- +"vocs": patch +--- + +Fixed Twoslash leaking into clipboard. diff --git a/src/app/hooks/useCopyCode.ts b/src/app/hooks/useCopyCode.ts index cbaee3a2..2664d6f2 100644 --- a/src/app/hooks/useCopyCode.ts +++ b/src/app/hooks/useCopyCode.ts @@ -14,13 +14,12 @@ export function useCopyCode() { function copy() { setCopied(true) - const lines = ref.current?.querySelectorAll('[data-line]:not(.diff.remove)') - - let text = '' - for (const line of lines ?? []) text += `${line.textContent || ''}\n` - text = text.slice(0, -1) - - navigator.clipboard.writeText(text) + const node = ref.current?.cloneNode(true) as HTMLPreElement + const nodesToRemove = node?.querySelectorAll( + 'button,[data-line].diff.remove,.twoslash-popup-info-hover,.twoslash-popup-info,.twoslash-meta-line,.twoslash-tag-line', + ) + for (const node of nodesToRemove ?? []) node.remove() + navigator.clipboard.writeText(node?.textContent as string) } return {