Skip to content

Commit ba19a2f

Browse files
fix download button
1 parent 3d1f5c5 commit ba19a2f

File tree

3 files changed

+111
-29
lines changed

3 files changed

+111
-29
lines changed

assets/css/index.css

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -521,25 +521,17 @@ select {
521521
@apply w-10 h-10;
522522
}
523523

524-
/* YAML embed container styles */
525-
.yaml-embed-container {
526-
@apply relative;
527-
}
528-
529-
.yaml-embed-container .download-yaml-btn {
530-
@apply absolute top-2 right-2 z-10;
531-
@apply bg-redis-yellow-500 hover:bg-redis-ink-900 hover:text-white;
532-
@apply text-redis-ink-900 text-xs font-mono px-3 py-1 rounded;
533-
@apply border border-redis-pen-600 transition-colors;
534-
@apply flex items-center gap-1;
524+
/* YAML embed download button styles */
525+
.download-yaml-btn {
526+
@apply transition-colors;
535527
}
536528

537-
.yaml-embed-container .download-yaml-btn:disabled {
529+
.download-yaml-btn:disabled {
538530
@apply opacity-75 cursor-not-allowed;
539531
}
540532

541-
.yaml-embed-container .download-yaml-btn svg {
542-
@apply w-3 h-3;
533+
.download-yaml-btn svg {
534+
@apply w-4 h-4;
543535
}
544536

545537
#download-redis > h3,

layouts/partials/scripts.html

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
} else {
1414
wrapper.style = 'position:absolute;top:10px;right:10px;z-index:1;display:flex;align-items:center;gap:6px;';
1515
}
16+
17+
// Check if this is a YAML embed with download functionality
18+
const yamlContainer = block.closest('.yaml-embed-container');
19+
const downloadFilename = yamlContainer ? yamlContainer.getAttribute('data-download-filename') : null;
20+
1621
// Create the copy button
1722
const button = document.createElement('button');
1823
button.innerHTML = `
@@ -51,9 +56,30 @@
5156
});
5257
});
5358

54-
// Append button and message
59+
// Append copy button
5560
wrapper.appendChild(button);
5661
wrapper.appendChild(tooltipContainer);
62+
63+
// Add download button if this is a YAML embed
64+
if (downloadFilename) {
65+
const downloadButton = document.createElement('button');
66+
downloadButton.className = 'download-yaml-btn text-neutral-400 hover:text-slate-100 bg-slate-600 h-7 w-7 p-1 rounded rounded-mx';
67+
downloadButton.title = 'Download YAML file';
68+
downloadButton.innerHTML = `
69+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
70+
<path d="M12 16l-6-6h4V4h4v6h4l-6 6zm-6 2h12v2H6v-2z"/>
71+
</svg>
72+
`;
73+
74+
downloadButton.addEventListener('click', () => {
75+
if (typeof downloadYaml === 'function') {
76+
downloadYaml(downloadFilename, downloadButton);
77+
}
78+
});
79+
80+
wrapper.appendChild(downloadButton);
81+
}
82+
5783
block.style.position = 'relative';
5884
block.appendChild(wrapper);
5985
});

layouts/shortcodes/embed-yaml.html

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,90 @@
22
{{$filename := .Get 1}}
33
{{$content := printf "./content/embeds/%s" $file | readFile}}
44

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 }}>
197
<!-- YAML content -->
208
<div class="yaml-content">
219
{{ $content | markdownify }}
2210
</div>
2311
</div>
2412

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+
2589
{{ if $filename }}
2690
<script>
2791
function downloadYaml(filename, button) {

0 commit comments

Comments
 (0)