This is a viewer only at the moment see the article on how this works.
To update the preview hit Ctrl-Alt-R (or ⌘-Alt-R on Mac) or Enter to refresh. The Save icon lets you save the markdown file to disk
This is a preview from the server running through my markdig pipeline
हाइलाइट के लिए एक प्रतिलिपि बटन
Saturday, 28 September 2024
परिचय
इस साइट में मैं Hightlight का उपयोग करता हूँ।
लक्ष्य
अंतिम बिंदु यह है कि हम साइट पर एक प्रतिलिपि बटन इस तरह है

नोट: इस लेख के लिए सभी श्रेय फराज़ पटनकर जिसको मैं इस एक को बनाने के लिए उपयोग किया करता हूँ-' का लेख-मैं सिर्फ अपने संदर्भ के लिए और दूसरों से साझा करने के लिए इसमें किए गए परिवर्तनों की दस्तावेज़ बनाना चाहता था
विकल्प
यह करने के लिए एक दो तरीके हैं प्रतिलिपि बटन प्लगइन के लिए Higlight.js लेकिन मैंने निर्णय लिया कि मैं बटन और शैली पर अधिक नियंत्रण चाहता हूँ इस लेख प्रतिलिपि बटन जोड़ने के लिए
समस्याएं
हालांकि यह लेख एक महान दृष्टिकोण है लेकिन इसके कुछ मुद्दे थे कि इसे मेरे लिए पूर्ण नहीं हो रहा था
- यह एक फ़ॉन्ट का उपयोग करता है कि मैं नहीं करता हूँ'में साइट पर प्रयोग नहीं करता है (पर फिर हस्तचालित रूप से SVG भी है
// Lucide copy icon
copyButton.innerHTML = `<svg class="h-4 w-4" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-copy"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg>`;
जब यह काम करता है, मैं पहले से ही इस साइट पर BoxIcons का उपयोग कर रहा हूँ जो एक वहाँ पहले से ही प्रतिमा को नक़ल करें.
- यह एक टोस्ट लाइब्रेरी का उपयोग करता है जो मैं नहीं है
// Notify user that the content has been copied
toast.success("Copied to clipboard", {
description: "The code block content has been copied to the clipboard.",
});
- यह कोड ब्लॉक पर y
-अधिप्रवाह को नष्ट करता है और मैं नहीं चाहता था के नीचे प्रतीक रखता है
मेरे अनुकूलन
मुख्य फ़ंक्शन
यह प्लगइन after:highlightElement घटना और कोड ब्लॉक में एक बटन जोड़ता है
तो पहले मैं Faraz के कोड को प्रतिलिपिित किया और फिर निम्नलिखित परिवर्तन कर दिया
- इसे कोड ब्लॉक के अंत में जोड़ने की बजाय मैं इसे शुरू करने के लिए पहले से जोड़ता हूँ
- SVG के बजाय मैं सिर्फ इन वर्गों को घुसाया बटन & सेटिंग पाठ साइज में जोड़ कर BoxIcons संस्करण का उपयोग किया
text-xl. - मैंने टोस्ट अधिसूचना हटा दी और इसे एक सरल से बदल दिया
showToastकार्य जो मैं अपने साइट में है (देखें बाद से - मैं एक जोड़ा
aria-labelऔरtitleपहुँचता के लिए बटन ( और एक अच्छा घुमाने प्रभाव देने के लिए
hljs.addPlugin({
"after:highlightElement": ({ el, text }) => {
const wrapper = el.parentElement;
if (wrapper == null) {
return;
}
/**
* Make the parent relative so we can absolutely
* position the copy button
*/
wrapper.classList.add("relative");
const copyButton = document.createElement("button");
copyButton.classList.add(
"absolute",
"top-2",
"right-1",
"p-2",
"text-gray-500",
"hover:text-gray-700",
"bx",
"bx-copy",
"text-xl",
"cursor-pointer"
);
copyButton.setAttribute("aria-label", "Copy code to clipboard");
copyButton.setAttribute("title", "Copy code to clipboard");
copyButton.onclick = () => {
navigator.clipboard.writeText(text);
// Notify user that the content has been copied
showToast("The code block content has been copied to the clipboard.", 3000, "success");
};
// Append the copy button to the wrapper
wrapper.prepend(copyButton);
},
});
showToast प्रकार्य
यह मेरी परियोजना में जोड़ी गई एक रैसर आंशिक पर निर्भर करता है यह आंशिक का उपयोग करता है डेज़ीयूआई टोस्ट घटक उपयोगकर्ता को संदेश दिखाने के लिए मैं इस दृष्टिकोण को पसंद करता हूँ क्योंकि यह जावास्क्रिप्ट साफ और सरल रखता है और मुझे साइट के बाकी की तरह टोस्ट संदेश शैली प्रदान करता है
<div id="toast" class="toast toast-bottom fixed z-50 hidden overflow-y-hidden">
<div id="toast-message" class="alert">
<div>
<span id="toast-text">Notification message</span>
</div>
</div>
<p class="hidden right-1 bx-copy cursor-pointer alert-success alert-warning alert-error alert-info"></p>
</div>
आप यह भी एक अजीब छिपा हुआ है नोट करेंगे p tag at the bottom, this is just for Tailwind to parse these classes when it builds the site
Javascript फ़ंक्शन सरल है, यह सिर्फ एक सेट समय के लिए टोस्ट संदेश दिखाता है और फिर इसे फिर से छुपा देता है
window.showToast = function(message, duration = 3000, type = 'success') {
const toast = document.getElementById('toast');
const toastText = document.getElementById('toast-text');
const toastMessage = document.getElementById('toast-message');
// Set message and type
toastText.innerText = message;
toastMessage.className = `alert alert-${type}`; // Change alert type (success, warning, error)
// Show the toast
toast.classList.remove('hidden');
// Hide the toast after specified duration
setTimeout(() => {
toast.classList.add('hidden');
}, duration);
}
तब हम इस का उपयोग कर सकते हैं showToast कार्य में copyButton.onclick घटना
showToast("The code block content has been copied to the clipboard.", 3000, "success");
मैं अपने ऊपर इस आंशिक को जोड़ा _Layout.cshtml यह प्रत्येक पृष्ठ पर उपलब्ध है
<partial name="_Toast" />``
अब जब हम ब्लॉग पोस्ट दिखाते हैं कोड ब्लॉक्स है

अंत में
तो यह है कि Faraz के कोड में एक सरल परिवर्तन है यह मेरे लिए काम करने के लिए ' को बनाता है। मैं आशा करता हूँ यह बाहर से किसी और की मदद करता है