Skip to content

Commit e621247

Browse files
committed
Add copy button functionality for code blocks and enhance Markdown processing in ContentService
1 parent f32f256 commit e621247

2 files changed

Lines changed: 63 additions & 3 deletions

File tree

Web/Pages/Tips/Details.cshtml

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,42 @@
214214
font-size: 1.1rem;
215215
}
216216
217+
/* Copy button styles for code blocks */
218+
.tip-content pre {
219+
position: relative !important;
220+
}
221+
222+
.tip-content .copy-button {
223+
position: absolute !important;
224+
right: 0.5rem !important;
225+
top: 0.5rem !important;
226+
padding: 0.25rem 0.5rem !important;
227+
font-size: 0.875rem !important;
228+
line-height: 1.5 !important;
229+
color: #6c757d !important;
230+
background-color: transparent !important;
231+
border: 1px solid #6c757d !important;
232+
border-radius: 0.25rem !important;
233+
opacity: 0;
234+
transition: opacity 0.2s ease-in-out !important;
235+
cursor: pointer !important;
236+
}
237+
238+
.tip-content pre:hover .copy-button {
239+
opacity: 1;
240+
}
241+
242+
.tip-content .copy-button:hover {
243+
color: #fff !important;
244+
background-color: #6c757d !important;
245+
}
246+
247+
.tip-content .copy-button.success {
248+
color: #fff !important;
249+
background-color: #198754 !important;
250+
border-color: #198754 !important;
251+
}
252+
217253
.tip-content h1, .tip-content h2, .tip-content h3,
218254
.tip-content h4, .tip-content h5, .tip-content h6 {
219255
margin-top: 2rem;
@@ -334,10 +370,30 @@ document.addEventListener('DOMContentLoaded', function() {
334370
335371
// Re-highlight everything
336372
Prism.highlightAll();
373+
374+
// Handle copy button success states
375+
document.querySelectorAll('.copy-button').forEach(function(button) {
376+
button.addEventListener('click', async function() {
377+
const pre = this.closest('pre');
378+
const code = pre.querySelector('code')?.textContent || pre.textContent;
379+
380+
try {
381+
await navigator.clipboard.writeText(code.trim());
382+
const originalText = button.innerHTML;
383+
button.innerHTML = '<i class="bi bi-check"></i>';
384+
button.classList.add('success');
385+
386+
setTimeout(() => {
387+
button.innerHTML = originalText;
388+
button.classList.remove('success');
389+
}, 2000);
390+
} catch (err) {
391+
console.error('Failed to copy text: ', err);
392+
}
393+
});
394+
});
337395
}
338396
}, 100);
339-
340-
// Note: Copy-to-clipboard functionality is handled by code-clipboard.js
341397
});
342398
343399
document.addEventListener('DOMContentLoaded', function() {

Web/Services/ContentService.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,15 @@ private async Task<List<TipModel>> GetTipsFromAzureTableAsync()
200200
{
201201
try
202202
{
203+
// Convert Markdown content to HTML
204+
var htmlContent = Markdown.ToHtml(entity.Content, _markdownPipeline);
205+
htmlContent = PostProcessCodeBlocks(htmlContent);
206+
203207
var tip = new TipModel
204208
{
205209
Title = entity.Title,
206210
Description = entity.Description,
207-
Content = entity.Content,
211+
Content = htmlContent, // Store the converted HTML
208212
Category = entity.Category,
209213
Tags = entity.Tags?.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList() ?? new List<string>(),
210214
PublishedDate = entity.PublishedDate,

0 commit comments

Comments
 (0)