Skip to content

Commit d356419

Browse files
authored
fix: copy markdown should show a single toast notification on trigger (#515)
fix: updated copy markdown component to toast notification
1 parent 71c9f58 commit d356419

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

src/components/CopyMarkdownButton.tsx

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -54,48 +54,52 @@ export function CopyMarkdownButton({
5454
const cached = cache.get(url)
5555

5656
if (cached) {
57-
navigator.clipboard.writeText(cached)
58-
notify(
59-
<div>
60-
<div className="font-medium">Copied markdown</div>
61-
<div className="text-gray-500 dark:text-gray-400 text-xs">
62-
Source content copied from cache
57+
navigator.clipboard.writeText(cached).then(() => {
58+
notify(
59+
<div>
60+
<div className="font-medium">Copied markdown</div>
61+
<div className="text-gray-500 dark:text-gray-400 text-xs">
62+
Source content copied from cache
63+
</div>
6364
</div>
64-
</div>
65-
)
65+
)
66+
})
6667
} else {
6768
fetch(url)
68-
.then((response) => response.text())
69+
.then((response) => {
70+
if (!response.ok) {
71+
throw new Error('Fetch failed')
72+
}
73+
return response.text()
74+
})
6975
.then((content) => {
7076
cache.set(url, content)
71-
return navigator.clipboard.writeText(content)
72-
})
73-
.then(() => {
74-
notify(
75-
<div>
76-
<div className="font-medium">Copied markdown</div>
77-
<div className="text-gray-500 dark:text-gray-400 text-xs">
78-
Source content copied from GitHub
77+
return navigator.clipboard.writeText(content).then(() => {
78+
notify(
79+
<div>
80+
<div className="font-medium">Copied markdown</div>
81+
<div className="text-gray-500 dark:text-gray-400 text-xs">
82+
Source content copied from GitHub
83+
</div>
7984
</div>
80-
</div>
81-
)
85+
)
86+
})
8287
})
8388
.catch(() => {
8489
// fallback: try to copy current page content if available
8590
const pageContent =
8691
document.querySelector('.styled-markdown-content')?.textContent ||
8792
''
88-
return navigator.clipboard.writeText(pageContent)
89-
})
90-
.then(() => {
91-
notify(
92-
<div>
93-
<div className="font-medium">Copied markdown</div>
94-
<div className="text-gray-500 dark:text-gray-400 text-xs">
95-
Fallback: copied rendered page content
93+
navigator.clipboard.writeText(pageContent).then(() => {
94+
notify(
95+
<div>
96+
<div className="font-medium">Copied markdown</div>
97+
<div className="text-gray-500 dark:text-gray-400 text-xs">
98+
Fallback: copied rendered page content
99+
</div>
96100
</div>
97-
</div>
98-
)
101+
)
102+
})
99103
})
100104
}
101105
})

0 commit comments

Comments
 (0)