File tree Expand file tree Collapse file tree 1 file changed +24
-6
lines changed Expand file tree Collapse file tree 1 file changed +24
-6
lines changed Original file line number Diff line number Diff line change @@ -12,15 +12,33 @@ export function CopyForLLMButton({markdownPath}: Props) {
1212 const [ copied , setCopied ] = useState ( false ) ;
1313
1414 async function handleClick ( ) {
15+ let didCopy = false ;
16+
17+ // First attempt: fetch markdown file and copy its contents
1518 try {
16- // Attempt to fetch the markdown content and copy it
1719 const response = await fetch ( markdownPath ) ;
18- const text = await response . text ( ) ;
19- await navigator . clipboard . writeText ( text ) ;
20+ if ( response . ok ) {
21+ const text = await response . text ( ) ;
22+ await navigator . clipboard . writeText ( text ) ;
23+ didCopy = true ;
24+ }
2025 } catch ( err ) {
21- // Fallback: copy the markdown url itself
22- await navigator . clipboard . writeText ( window . location . origin + markdownPath ) ;
23- } finally {
26+ // network error handled below in fallback
27+ console . error ( err ) ;
28+ }
29+
30+ // Fallback: copy the markdown URL if first attempt failed
31+ if ( ! didCopy ) {
32+ try {
33+ await navigator . clipboard . writeText ( window . location . origin + markdownPath ) ;
34+ didCopy = true ;
35+ } catch ( err ) {
36+ console . error ( 'Failed to copy markdown URL as fallback' , err ) ;
37+ }
38+ }
39+
40+ // Visual feedback only when something was actually copied
41+ if ( didCopy ) {
2442 setCopied ( true ) ;
2543 setTimeout ( ( ) => setCopied ( false ) , 1200 ) ;
2644 }
You can’t perform that action at this time.
0 commit comments