|
2 | 2 | {{$filename := .Get 1}}
|
3 | 3 | {{$content := printf "./content/embeds/%s" $file | readFile}}
|
4 | 4 |
|
5 |
| -<!-- Container for YAML content and download button --> |
6 |
| -<div class="yaml-embed-container relative"> |
7 |
| - <!-- Download button positioned in top-right --> |
8 |
| - {{ if $filename }} |
9 |
| - <div class="absolute top-2 right-2 z-10"> |
10 |
| - <button onclick="downloadYaml('{{ $filename }}', this)" class="download-yaml-btn bg-redis-yellow-500 hover:bg-redis-ink-900 hover:text-white text-redis-ink-900 text-xs font-mono px-3 py-1 rounded border border-redis-pen-600 transition-colors flex items-center gap-1" title="Download YAML file"> |
11 |
| - <svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24" fill="currentColor"> |
12 |
| - <path d="M12 16l-6-6h4V4h4v6h4l-6 6zm-6 2h12v2H6v-2z"/> |
13 |
| - </svg> |
14 |
| - Download |
15 |
| - </button> |
16 |
| - </div> |
17 |
| - {{ end }} |
18 |
| - |
| 5 | +<!-- Container for YAML content --> |
| 6 | +<div class="yaml-embed-container"{{ if $filename }} data-download-filename="{{ $filename }}"{{ end }}> |
19 | 7 | <!-- YAML content -->
|
20 | 8 | <div class="yaml-content">
|
21 | 9 | {{ $content | markdownify }}
|
22 | 10 | </div>
|
23 | 11 | </div>
|
24 | 12 |
|
| 13 | +{{ if $filename }} |
| 14 | +<script> |
| 15 | +function downloadYaml(filename, button) { |
| 16 | + try { |
| 17 | + // Get the YAML content from the code block |
| 18 | + const container = button.closest('.yaml-embed-container') || button.closest('div.highlight').parentElement; |
| 19 | + const codeElement = container.querySelector('code'); |
| 20 | + |
| 21 | + if (!codeElement) { |
| 22 | + console.error('Could not find YAML content to download'); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + // Extract the YAML content (remove any syntax highlighting spans) |
| 27 | + let yamlContent = ''; |
| 28 | + |
| 29 | + // Try different methods to get clean text content |
| 30 | + if (codeElement.textContent) { |
| 31 | + yamlContent = codeElement.textContent.trim(); |
| 32 | + } else if (codeElement.innerText) { |
| 33 | + yamlContent = codeElement.innerText.trim(); |
| 34 | + } else { |
| 35 | + // Fallback for complex highlighting - look for line elements |
| 36 | + const lines = codeElement.querySelectorAll('.cl, .line'); |
| 37 | + if (lines.length > 0) { |
| 38 | + yamlContent = Array.from(lines).map(line => line.textContent || line.innerText || '').join('\n'); |
| 39 | + } else { |
| 40 | + // Last resort - get all text content |
| 41 | + yamlContent = codeElement.textContent || codeElement.innerText || ''; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if (!yamlContent.trim()) { |
| 46 | + console.error('No YAML content found to download'); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + // Create and trigger download |
| 51 | + const blob = new Blob([yamlContent], { type: 'text/yaml;charset=utf-8' }); |
| 52 | + const url = window.URL.createObjectURL(blob); |
| 53 | + const a = document.createElement('a'); |
| 54 | + a.href = url; |
| 55 | + a.download = filename; |
| 56 | + a.style.display = 'none'; |
| 57 | + document.body.appendChild(a); |
| 58 | + a.click(); |
| 59 | + document.body.removeChild(a); |
| 60 | + window.URL.revokeObjectURL(url); |
| 61 | + |
| 62 | + // Visual feedback |
| 63 | + const originalText = button.innerHTML; |
| 64 | + button.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="currentColor"><path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/></svg>'; |
| 65 | + button.disabled = true; |
| 66 | + |
| 67 | + setTimeout(() => { |
| 68 | + button.innerHTML = originalText; |
| 69 | + button.disabled = false; |
| 70 | + }, 2000); |
| 71 | + |
| 72 | + } catch (error) { |
| 73 | + console.error('Error downloading YAML file:', error); |
| 74 | + |
| 75 | + // Show error feedback |
| 76 | + const originalText = button.innerHTML; |
| 77 | + button.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"/></svg>'; |
| 78 | + button.disabled = true; |
| 79 | + |
| 80 | + setTimeout(() => { |
| 81 | + button.innerHTML = originalText; |
| 82 | + button.disabled = false; |
| 83 | + }, 3000); |
| 84 | + } |
| 85 | +} |
| 86 | +</script> |
| 87 | +{{ end }} |
| 88 | + |
25 | 89 | {{ if $filename }}
|
26 | 90 | <script>
|
27 | 91 | function downloadYaml(filename, button) {
|
|
0 commit comments