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