Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: markdown code copy listener #93

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions app/_components/_main/markdown-copy-listener.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use client';
import { useEffect } from 'react';

export default function MarkdownCopyListener() {
useEffect(() => {
const copyBtns = document.querySelectorAll('button.copy-code-btn');
copyBtns.forEach((btn) => {
(btn as HTMLButtonElement).addEventListener('click', () => {
const codeBlockContent = btn.parentElement?.nextElementSibling?.textContent;
if (codeBlockContent) {
navigator.clipboard.writeText(codeBlockContent).then(() => {
btn.textContent = 'Copied';
setTimeout(() => {
btn.textContent = 'Copy';
}, 2000);
});
}
});
});

return () => {
copyBtns.forEach((btn) => {
(btn as HTMLButtonElement).removeEventListener('click', () => {});
});
};
}, []);

return null;
}
23 changes: 13 additions & 10 deletions components/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import DOMPurify from 'isomorphic-dompurify';
import hljs from 'highlight.js/lib/core';
import '@/styles/hljs/github-dark.css';
import { escapeText } from '@/lib/utils';
import MarkdownCopyListener from '@/app/_components/_main/markdown-copy-listener';

const languages = {
plaintext: () => import('highlight.js/lib/languages/plaintext'),
Expand Down Expand Up @@ -43,7 +44,7 @@ const Markdown = ({ markdown }: { markdown: string }) => {
</h${depth}>`;
},
code({ text, lang }) {
return `<pre><div class='flex items-center justify-between pb-3 text-xs'><span>${lang}</span><button onclick="navigator.clipboard.writeText(this.parentElement.nextElementSibling.textContent).then(() => { this.innerText = 'Copied'; setTimeout(() => { this.innerText = 'Copy' }, 2000); })">Copy</button></div><div class='flex-1 overflow-x-scroll'><code>${text}</code></div></pre>`;
return `<pre><div class='flex items-center justify-between pb-3 text-xs'><span>${lang}</span><button class='copy-code-btn'>Copy</button></div><div class='flex-1 overflow-x-scroll'><code>${text}</code></div></pre>`;
},
link(args) {
const link = marked.Renderer.prototype.link.call(this, args);
Expand Down Expand Up @@ -76,15 +77,17 @@ const Markdown = ({ markdown }: { markdown: string }) => {
);

return (
<article
className="prose dark:prose-invert prose-pre:rounded-2xl prose-pre:bg-secondary/25"
dangerouslySetInnerHTML={{
__html: DOMPurify.sanitize(marked.parse(markdown) as string, {
USE_PROFILES: { html: true },
ADD_ATTR: ['onclick'],
}),
}}
></article>
<>
<article
className="prose dark:prose-invert prose-pre:rounded-2xl prose-pre:bg-secondary/25"
dangerouslySetInnerHTML={{
__html: DOMPurify.sanitize(marked.parse(markdown) as string, {
USE_PROFILES: { html: true },
}),
}}
></article>
<MarkdownCopyListener />
</>
);
};

Expand Down