|
| 1 | +const shellCommands = new Set([ |
| 2 | + 'apt', |
| 3 | + 'curl', |
| 4 | + 'dnf', |
| 5 | + 'echo', |
| 6 | + 'flatpak', |
| 7 | + 'install', |
| 8 | + 'rpm', |
| 9 | + 'sudo', |
| 10 | + 'tee', |
| 11 | + 'wget', |
| 12 | + 'zypper', |
| 13 | +]); |
| 14 | + |
| 15 | +const tokenPattern = |
| 16 | + /"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|https?:\/\/[^\s"'<>]+|\\(?=\n)|--?[a-zA-Z0-9][\w-]*|(?:\/|\.\.?\/)[\w./*-]+|\b\d+\b|[|>]+|\b[\w-]+\b/g; |
| 17 | + |
| 18 | +function escapeHtml(value) { |
| 19 | + return value |
| 20 | + .replaceAll('&', '&') |
| 21 | + .replaceAll('<', '<') |
| 22 | + .replaceAll('>', '>'); |
| 23 | +} |
| 24 | + |
| 25 | +function highlightShell(source) { |
| 26 | + let cursor = 0; |
| 27 | + let result = ''; |
| 28 | + |
| 29 | + for (const match of source.matchAll(tokenPattern)) { |
| 30 | + const token = match[0]; |
| 31 | + let type; |
| 32 | + |
| 33 | + result += escapeHtml(source.slice(cursor, match.index)); |
| 34 | + |
| 35 | + if (token.startsWith('"') || token.startsWith("'")) { |
| 36 | + type = 'string'; |
| 37 | + } else if (token.startsWith('http') || token.startsWith('/') || token.startsWith('.')) { |
| 38 | + type = 'path'; |
| 39 | + } else if (token.startsWith('-')) { |
| 40 | + type = 'option'; |
| 41 | + } else if (/^\d+$/.test(token)) { |
| 42 | + type = 'number'; |
| 43 | + } else if (token === '\\' || token.includes('|') || token.includes('>')) { |
| 44 | + type = 'operator'; |
| 45 | + } else if (shellCommands.has(token)) { |
| 46 | + type = 'command'; |
| 47 | + } |
| 48 | + |
| 49 | + result += type |
| 50 | + ? `<span class="token-${type}">${escapeHtml(token)}</span>` |
| 51 | + : escapeHtml(token); |
| 52 | + cursor = match.index + token.length; |
| 53 | + } |
| 54 | + |
| 55 | + return result + escapeHtml(source.slice(cursor)); |
| 56 | +} |
| 57 | + |
| 58 | +function copyFallback(value) { |
| 59 | + const textarea = document.createElement('textarea'); |
| 60 | + textarea.value = value; |
| 61 | + textarea.style.position = 'fixed'; |
| 62 | + textarea.style.opacity = '0'; |
| 63 | + document.body.append(textarea); |
| 64 | + textarea.select(); |
| 65 | + const copied = document.execCommand('copy'); |
| 66 | + textarea.remove(); |
| 67 | + |
| 68 | + if (!copied) { |
| 69 | + throw new Error('Could not copy code'); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +async function copyText(value) { |
| 74 | + if (navigator.clipboard && window.isSecureContext) { |
| 75 | + await navigator.clipboard.writeText(value); |
| 76 | + } else { |
| 77 | + copyFallback(value); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function enhanceHorizontalScroll(pre, container) { |
| 82 | + const track = document.createElement('div'); |
| 83 | + track.className = 'code-scrollbar'; |
| 84 | + track.hidden = true; |
| 85 | + |
| 86 | + const thumb = document.createElement('div'); |
| 87 | + thumb.className = 'code-scrollbar__thumb'; |
| 88 | + thumb.tabIndex = 0; |
| 89 | + thumb.setAttribute('role', 'scrollbar'); |
| 90 | + thumb.setAttribute('aria-label', 'Code block horizontal scroll'); |
| 91 | + thumb.setAttribute('aria-orientation', 'horizontal'); |
| 92 | + thumb.setAttribute('aria-valuemin', '0'); |
| 93 | + thumb.setAttribute('aria-valuemax', '100'); |
| 94 | + track.append(thumb); |
| 95 | + container.append(track); |
| 96 | + |
| 97 | + function refresh() { |
| 98 | + const maxScroll = pre.scrollWidth - pre.clientWidth; |
| 99 | + if (maxScroll <= 0) { |
| 100 | + track.hidden = true; |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + track.hidden = false; |
| 105 | + const thumbWidth = Math.max(36, Math.round(track.clientWidth * (pre.clientWidth / pre.scrollWidth))); |
| 106 | + const maxThumbOffset = track.clientWidth - thumbWidth; |
| 107 | + const thumbOffset = maxThumbOffset <= 0 ? 0 : (pre.scrollLeft / maxScroll) * maxThumbOffset; |
| 108 | + thumb.style.width = `${thumbWidth}px`; |
| 109 | + thumb.style.transform = `translateX(${thumbOffset}px)`; |
| 110 | + thumb.setAttribute('aria-valuenow', String(Math.round((pre.scrollLeft / maxScroll) * 100))); |
| 111 | + } |
| 112 | + |
| 113 | + pre.addEventListener('scroll', refresh, { passive: true }); |
| 114 | + window.addEventListener('resize', refresh, { passive: true }); |
| 115 | + if ('ResizeObserver' in window) { |
| 116 | + const observer = new ResizeObserver(refresh); |
| 117 | + observer.observe(pre); |
| 118 | + observer.observe(track); |
| 119 | + } |
| 120 | + |
| 121 | + let startX = 0; |
| 122 | + let startScroll = 0; |
| 123 | + |
| 124 | + thumb.addEventListener('pointerdown', (event) => { |
| 125 | + startX = event.clientX; |
| 126 | + startScroll = pre.scrollLeft; |
| 127 | + thumb.setPointerCapture(event.pointerId); |
| 128 | + track.classList.add('is-dragging'); |
| 129 | + event.preventDefault(); |
| 130 | + }); |
| 131 | + |
| 132 | + thumb.addEventListener('pointermove', (event) => { |
| 133 | + if (!thumb.hasPointerCapture(event.pointerId)) return; |
| 134 | + const maxScroll = pre.scrollWidth - pre.clientWidth; |
| 135 | + const maxThumbOffset = Math.max(1, track.clientWidth - thumb.offsetWidth); |
| 136 | + pre.scrollLeft = startScroll + ((event.clientX - startX) / maxThumbOffset) * maxScroll; |
| 137 | + }); |
| 138 | + |
| 139 | + function endDrag(event) { |
| 140 | + if (thumb.hasPointerCapture(event.pointerId)) thumb.releasePointerCapture(event.pointerId); |
| 141 | + track.classList.remove('is-dragging'); |
| 142 | + } |
| 143 | + |
| 144 | + thumb.addEventListener('pointerup', endDrag); |
| 145 | + thumb.addEventListener('pointercancel', endDrag); |
| 146 | + |
| 147 | + track.addEventListener('pointerdown', (event) => { |
| 148 | + if (event.target !== track) return; |
| 149 | + const maxScroll = pre.scrollWidth - pre.clientWidth; |
| 150 | + const maxThumbOffset = Math.max(1, track.clientWidth - thumb.offsetWidth); |
| 151 | + const targetOffset = event.clientX - track.getBoundingClientRect().left - thumb.offsetWidth / 2; |
| 152 | + pre.scrollLeft = (Math.min(maxThumbOffset, Math.max(0, targetOffset)) / maxThumbOffset) * maxScroll; |
| 153 | + }); |
| 154 | + |
| 155 | + thumb.addEventListener('keydown', (event) => { |
| 156 | + const step = Math.max(48, Math.round(pre.clientWidth * 0.25)); |
| 157 | + if (event.key === 'ArrowLeft') pre.scrollLeft -= step; |
| 158 | + else if (event.key === 'ArrowRight') pre.scrollLeft += step; |
| 159 | + else if (event.key === 'Home') pre.scrollLeft = 0; |
| 160 | + else if (event.key === 'End') pre.scrollLeft = pre.scrollWidth; |
| 161 | + else return; |
| 162 | + event.preventDefault(); |
| 163 | + }); |
| 164 | + |
| 165 | + requestAnimationFrame(refresh); |
| 166 | +} |
| 167 | + |
| 168 | +for (const code of document.querySelectorAll('code.language-shell')) { |
| 169 | + const source = code.textContent; |
| 170 | + code.innerHTML = highlightShell(source); |
| 171 | + |
| 172 | + const pre = code.parentElement; |
| 173 | + const container = document.createElement('div'); |
| 174 | + container.className = 'code-block'; |
| 175 | + pre.before(container); |
| 176 | + container.append(pre); |
| 177 | + enhanceHorizontalScroll(pre, container); |
| 178 | + |
| 179 | + const button = document.createElement('button'); |
| 180 | + button.className = 'copy-button'; |
| 181 | + button.type = 'button'; |
| 182 | + button.setAttribute('aria-label', 'Copy code'); |
| 183 | + button.title = 'Copy code'; |
| 184 | + button.innerHTML = ` |
| 185 | + <svg class="copy-icon" aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"> |
| 186 | + <rect width="14" height="14" x="8" y="8" rx="2"></rect> |
| 187 | + <path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"></path> |
| 188 | + </svg> |
| 189 | + <svg class="check-icon" aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"> |
| 190 | + <path d="m5 12 4 4L19 6"></path> |
| 191 | + </svg> |
| 192 | + `; |
| 193 | + |
| 194 | + let resetTimer; |
| 195 | + button.addEventListener('click', async () => { |
| 196 | + try { |
| 197 | + await copyText(source); |
| 198 | + button.dataset.copied = ''; |
| 199 | + button.setAttribute('aria-label', 'Copied'); |
| 200 | + button.title = 'Copied'; |
| 201 | + clearTimeout(resetTimer); |
| 202 | + resetTimer = setTimeout(() => { |
| 203 | + delete button.dataset.copied; |
| 204 | + button.setAttribute('aria-label', 'Copy code'); |
| 205 | + button.title = 'Copy code'; |
| 206 | + }, 2000); |
| 207 | + } catch { |
| 208 | + button.setAttribute('aria-label', 'Copy failed'); |
| 209 | + button.title = 'Copy failed'; |
| 210 | + } |
| 211 | + }); |
| 212 | + |
| 213 | + container.append(button); |
| 214 | +} |
0 commit comments