From 98d2ef62ff95d2f953a54592a42cdbbfe1601c71 Mon Sep 17 00:00:00 2001 From: Sanjay Soundarajan Date: Thu, 30 Jun 2022 19:07:33 -0700 Subject: [PATCH] fix: prevent line numbers from being copied. #884 --- src/client/theme-default/composables/copy-code.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/client/theme-default/composables/copy-code.ts b/src/client/theme-default/composables/copy-code.ts index 35aca1c10579..e7413ffce347 100644 --- a/src/client/theme-default/composables/copy-code.ts +++ b/src/client/theme-default/composables/copy-code.ts @@ -76,7 +76,18 @@ function handleElement(el: HTMLElement) { parent.classList.contains('language-sh') || parent.classList.contains('language-bash') - let { innerText: text = '' } = parent + // Select the element inside the whole code block. + // The element is currently nested inside the
 tag.
+    const codeBlockBody = parent.querySelector(
+      'pre > code'
+    ) as HTMLElement | null
+
+    let text: string = '' // Give it a default value
+
+    // if the code block body is not null, then we can get the text from it
+    if (codeBlockBody) {
+      text = codeBlockBody?.innerText ?? ''
+    }
 
     if (isShell) {
       text = text.replace(/^ *\$ /gm, '')