Skip to content

Commit bbd7d4f

Browse files
dcramercursoragentgetsantry[bot]Copilot
authored
Fix copy button output on logging page (#13972)
The copy logic within the `Expandable` component in `src/components/expandable/index.tsx` was refined to prevent unwanted inline code from being copied. Previously, the copy button selected all `<code>` elements, including inline code snippets like `.cursorrules` and `rules.md` from explanatory text. The changes implement a more selective approach: * **Primary Selection**: The component now first attempts to copy content from `pre code` elements, which typically contain main code blocks. * **Fallback Filtering**: If no `pre code` blocks are found, a fallback mechanism filters all `<code>` elements. It excludes: * Code elements found within paragraphs. * Short code snippets (less than 100 characters), which are likely inline. * **Final Fallback**: If no suitable code blocks are identified, the entire text content of the expandable section is copied. This ensures that only the intended code content is copied, omitting extraneous inline text. --------- Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com> Co-authored-by: Copilot <[email protected]>
1 parent e1e7194 commit bbd7d4f

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/components/expandable/index.tsx

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,35 @@ export function Expandable({
8080

8181
emit('Copy Expandable Content', {props: {page: window.location.pathname, title}});
8282

83-
// Attempt to get text from markdown code blocks if they exist
84-
const codeBlocks = contentRef.current.querySelectorAll('code');
83+
// First, try to get text from main code blocks (those inside pre elements)
84+
const preCodeBlocks = contentRef.current.querySelectorAll('pre code');
8585
let contentToCopy = '';
8686

87-
if (codeBlocks.length > 0) {
88-
// If there are code blocks, concatenate their text content
89-
codeBlocks.forEach(block => {
90-
// Exclude code elements within other code elements (e.g. inline code in a block)
91-
if (!block.closest('code')?.parentElement?.closest('code')) {
92-
contentToCopy += (block.textContent || '') + '\n';
93-
}
87+
if (preCodeBlocks.length > 0) {
88+
// If there are pre code blocks, concatenate their text content
89+
preCodeBlocks.forEach(block => {
90+
contentToCopy += (block.textContent || '') + '\n';
9491
});
9592
contentToCopy = contentToCopy.trim();
93+
} else {
94+
// Fallback: Look for large standalone code blocks (not inline code)
95+
const allCodeBlocks = contentRef.current.querySelectorAll('code');
96+
const largeCodeBlocks = Array.from(allCodeBlocks).filter((block: Element) => {
97+
// Skip inline code (usually short and inside paragraphs)
98+
const isInlineCode =
99+
block.closest('p') !== null && (block.textContent?.length || 0) < 100;
100+
return !isInlineCode;
101+
});
102+
103+
if (largeCodeBlocks.length > 0) {
104+
contentToCopy = largeCodeBlocks
105+
.map((block: Element) => block.textContent || '')
106+
.join('\n')
107+
.trim();
108+
}
96109
}
97110

98-
// Fallback to the whole content if no code blocks or if they are empty
111+
// Final fallback to the whole content if no code blocks or if they are empty
99112
if (!contentToCopy && contentRef.current.textContent) {
100113
contentToCopy = contentRef.current.textContent.trim();
101114
}

0 commit comments

Comments
 (0)