|
1 | 1 | (function () { |
2 | | - var MOON = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/></svg>'; |
3 | | - var SUN = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="5"/><line x1="12" y1="1" x2="12" y2="3"/><line x1="12" y1="21" x2="12" y2="23"/><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/><line x1="1" y1="12" x2="3" y2="12"/><line x1="21" y1="12" x2="23" y2="12"/><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/></svg>'; |
| 2 | + var MOON = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/></svg>'; |
| 3 | + var SUN = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="5"/><line x1="12" y1="1" x2="12" y2="3"/><line x1="12" y1="21" x2="12" y2="23"/><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/><line x1="1" y1="12" x2="3" y2="12"/><line x1="21" y1="12" x2="23" y2="12"/><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/></svg>'; |
4 | 4 |
|
5 | 5 | function isDark() { |
6 | 6 | return document.documentElement.classList.contains('dark'); |
|
28 | 28 |
|
29 | 29 | buildTOC(); |
30 | 30 | if (typeof hljs !== 'undefined') hljs.highlightAll(); |
| 31 | + decorateCodeBlocks(); |
31 | 32 | }); |
32 | 33 |
|
| 34 | + var LANG_LABELS = { |
| 35 | + bash: 'shell', |
| 36 | + sh: 'shell', |
| 37 | + python: 'python', |
| 38 | + typescript: 'ts', |
| 39 | + javascript: 'js', |
| 40 | + json: 'json', |
| 41 | + }; |
| 42 | + |
| 43 | + function decorateCodeBlocks() { |
| 44 | + var blocks = document.querySelectorAll('pre > code'); |
| 45 | + blocks.forEach(function (code) { |
| 46 | + var pre = code.parentElement; |
| 47 | + |
| 48 | + var match = (code.className || '').match(/language-([\w-]+)/); |
| 49 | + if (match) { |
| 50 | + var tag = document.createElement('span'); |
| 51 | + tag.className = 'code-lang'; |
| 52 | + tag.textContent = LANG_LABELS[match[1]] || match[1]; |
| 53 | + pre.appendChild(tag); |
| 54 | + } |
| 55 | + |
| 56 | + var btn = document.createElement('button'); |
| 57 | + btn.className = 'code-copy'; |
| 58 | + btn.type = 'button'; |
| 59 | + btn.textContent = 'copy'; |
| 60 | + btn.setAttribute('aria-label', 'Copy code to clipboard'); |
| 61 | + btn.addEventListener('click', function () { |
| 62 | + copyText(code.innerText, function (ok) { |
| 63 | + btn.textContent = ok ? 'copied' : 'failed'; |
| 64 | + btn.classList.add('copied'); |
| 65 | + setTimeout(function () { |
| 66 | + btn.textContent = 'copy'; |
| 67 | + btn.classList.remove('copied'); |
| 68 | + }, 1600); |
| 69 | + }); |
| 70 | + }); |
| 71 | + pre.appendChild(btn); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + function copyText(text, done) { |
| 76 | + if (navigator.clipboard && navigator.clipboard.writeText) { |
| 77 | + navigator.clipboard.writeText(text).then( |
| 78 | + function () { done(true); }, |
| 79 | + function () { done(fallbackCopy(text)); } |
| 80 | + ); |
| 81 | + } else { |
| 82 | + done(fallbackCopy(text)); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + function fallbackCopy(text) { |
| 87 | + var ta = document.createElement('textarea'); |
| 88 | + ta.value = text; |
| 89 | + ta.style.position = 'fixed'; |
| 90 | + ta.style.opacity = '0'; |
| 91 | + document.body.appendChild(ta); |
| 92 | + ta.select(); |
| 93 | + var ok = false; |
| 94 | + try { |
| 95 | + ok = document.execCommand('copy'); |
| 96 | + } catch (e) { |
| 97 | + ok = false; |
| 98 | + } |
| 99 | + document.body.removeChild(ta); |
| 100 | + return ok; |
| 101 | + } |
| 102 | + |
33 | 103 | function buildTOC() { |
34 | 104 | var list = document.getElementById('toc-list'); |
35 | 105 | var tocEl = document.querySelector('.toc'); |
|
0 commit comments