308308 }
309309
310310 .copy-button {
311- position : absolute;
312- top : 5px ;
313- right : 5px ;
314311 padding : 5px 10px ;
315312 background : # 667eea ;
316313 color : white;
317314 border : none;
318315 border-radius : 4px ;
319316 font-size : 12px ;
320317 cursor : pointer;
318+ transition : all 0.3s ;
319+ }
320+
321+ .markdown-content pre .copy-button ,
322+ .markdown-content pre .lang-label {
321323 opacity : 0 ;
322324 transition : opacity 0.3s ;
323325 }
324326
325- .markdown-content pre : hover .copy-button {
327+ .markdown-content pre : hover .copy-button ,
328+ .markdown-content pre : hover .lang-label {
326329 opacity : 1 ;
327330 }
328331
@@ -603,6 +606,29 @@ <h2>❌ 載入失敗</h2>
603606 const codeBlocks = document . querySelectorAll ( '.markdown-content pre' ) ;
604607
605608 codeBlocks . forEach ( ( block , index ) => {
609+ // 獲取程式碼語言類型
610+ const codeElement = block . querySelector ( 'code' ) ;
611+ const classList = codeElement . className . split ( ' ' ) ;
612+ let language = '' ;
613+ classList . forEach ( cls => {
614+ if ( cls . startsWith ( 'language-' ) ) {
615+ language = cls . replace ( 'language-' , '' ) . toUpperCase ( ) ;
616+ }
617+ } ) ;
618+
619+ // 創建按鈕容器
620+ const buttonContainer = document . createElement ( 'div' ) ;
621+ buttonContainer . style . cssText = 'position: absolute; top: 5px; right: 5px; display: flex; gap: 5px; align-items: center;' ;
622+
623+ // 如果有語言標示,添加語言標籤
624+ if ( language ) {
625+ const langLabel = document . createElement ( 'span' ) ;
626+ langLabel . className = 'lang-label' ;
627+ langLabel . style . cssText = 'font-size: 12px; color: #666; background: #f0f0f0; padding: 2px 8px; border-radius: 3px;' ;
628+ langLabel . textContent = language ;
629+ buttonContainer . appendChild ( langLabel ) ;
630+ }
631+
606632 // 創建複製按鈕
607633 const button = document . createElement ( 'button' ) ;
608634 button . className = 'copy-button' ;
@@ -612,7 +638,10 @@ <h2>❌ 載入失敗</h2>
612638 // 複製功能
613639 button . addEventListener ( 'click' , async function ( ) {
614640 const code = block . querySelector ( 'code' ) ;
615- const textToCopy = code . textContent || code . innerText ;
641+ let textToCopy = code . textContent || code . innerText ;
642+
643+ // 移除多餘的空白
644+ textToCopy = textToCopy . trim ( ) ;
616645
617646 try {
618647 await navigator . clipboard . writeText ( textToCopy ) ;
@@ -656,8 +685,11 @@ <h2>❌ 載入失敗</h2>
656685 }
657686 } ) ;
658687
659- // 添加按鈕到程式碼區塊
660- block . appendChild ( button ) ;
688+ // 添加按鈕到容器
689+ buttonContainer . appendChild ( button ) ;
690+
691+ // 添加容器到程式碼區塊
692+ block . appendChild ( buttonContainer ) ;
661693 } ) ;
662694 }
663695 </ script >
0 commit comments