From 93122eee20cb6586026c1ffac04d9787861cc2f3 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 20 Dec 2023 05:20:39 +0100 Subject: [PATCH] feat: allow explicitly mark code element as `.vp-copy-ignore` (#3360) --- src/client/app/composables/copyCode.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/client/app/composables/copyCode.ts b/src/client/app/composables/copyCode.ts index 0a07c281..2c458bb4 100644 --- a/src/client/app/composables/copyCode.ts +++ b/src/client/app/composables/copyCode.ts @@ -16,13 +16,15 @@ export function useCopyCode() { parent.className ) - let text = '' + const ignoredNodes = ['.vp-copy-ignore', '.diff.remove'] - sibling - .querySelectorAll('span.line:not(.diff.remove)') - .forEach((node) => (text += (node.textContent || '') + '\n')) + // Clone the node and remove the ignored nodes + const clone = sibling.cloneNode(true) as HTMLElement + clone + .querySelectorAll(ignoredNodes.join(',')) + .forEach((node) => node.remove()) - text = (text || sibling.textContent || '').slice(0, -1) + let text = clone.textContent || '' if (isShell) { text = text.replace(/^ *(\$|>) /gm, '').trim()