@@ -2,35 +2,37 @@ import { encode as HTMLEncode } from "he";
2
2
import markdownIt from "markdown-it" ;
3
3
import hljs from "highlight.js" ;
4
4
import "highlight.js/styles/github-dark-dimmed.min.css" ;
5
+ import { v4 } from "uuid" ;
5
6
6
7
const markdown = markdownIt ( {
7
8
html : true ,
8
9
typographer : true ,
9
10
highlight : function ( str , lang ) {
11
+ const uuid = v4 ( ) ;
10
12
if ( lang && hljs . getLanguage ( lang ) ) {
11
13
try {
12
14
return (
13
- `<div class="whitespace-pre-line w-full rounded-lg bg-black-900 px-4 pt-10 pb-4 relative font-mono font-normal text-sm text-slate-200"><div class="w-full flex items-center absolute top-0 left-0 text-slate-200 bg-stone-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md"><button onclick='window.copySnippet();' class="flex ml-auto gap-2"><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" class="h-4 w-4" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"></path><rect x="8" y="2" width="8" height="4" rx="1" ry="1"></rect></svg>Copy code</button></div><pre class="whitespace-pre-wrap">` +
15
+ `<div class="whitespace-pre-line w-full rounded-lg bg-black-900 px-4 pt-10 pb-4 relative font-mono font-normal text-sm text-slate-200"><div class="w-full flex items-center absolute top-0 left-0 text-slate-200 bg-stone-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md"><button id="code- ${ uuid } " onclick='window.copySnippet(" ${ uuid } " );' class="flex ml-auto gap-2"><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" class="h-4 w-4" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"></path><rect x="8" y="2" width="8" height="4" rx="1" ry="1"></rect></svg>Copy code</button></div><pre class="whitespace-pre-wrap">` +
14
16
hljs . highlight ( lang , str , true ) . value +
15
17
"</pre></div>"
16
18
) ;
17
19
} catch ( __ ) { }
18
20
}
19
21
20
22
return (
21
- `<div class="whitespace-pre-line w-full rounded-lg bg-black-900 px-4 pt-10 pb-4 relative font-mono font-normal text-sm text-slate-200"><div class="w-full flex items-center absolute top-0 left-0 text-slate-200 bg-stone-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md"><button onclick='window.copySnippet();' class="flex ml-auto gap-2"><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" class="h-4 w-4" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"></path><rect x="8" y="2" width="8" height="4" rx="1" ry="1"></rect></svg>Copy code</button></div><pre class="whitespace-pre-wrap">` +
23
+ `<div class="whitespace-pre-line w-full rounded-lg bg-black-900 px-4 pt-10 pb-4 relative font-mono font-normal text-sm text-slate-200"><div class="w-full flex items-center absolute top-0 left-0 text-slate-200 bg-stone-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md"><button id="code- ${ uuid } " onclick='window.copySnippet(" ${ uuid } " );' class="flex ml-auto gap-2"><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" class="h-4 w-4" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"></path><rect x="8" y="2" width="8" height="4" rx="1" ry="1"></rect></svg>Copy code</button></div><pre class="whitespace-pre-wrap">` +
22
24
HTMLEncode ( str ) +
23
25
"</pre></div>"
24
26
) ;
25
27
} ,
26
28
} ) ;
27
29
28
- window . copySnippet = function ( ) {
29
- if ( ! event ?. target ) return false ;
30
-
31
- const target = event ?. target ;
30
+ window . copySnippet = function ( uuid = "" ) {
31
+ const target = document . getElementById ( `code-${ uuid } ` ) ;
32
32
const markdown =
33
- target . parentElement ?. parentElement ?. querySelector ( ".markdown" ) ?. innerText ;
33
+ target . parentElement ?. parentElement ?. querySelector (
34
+ "pre:first-of-type"
35
+ ) ?. innerText ;
34
36
if ( ! markdown ) return false ;
35
37
36
38
window . navigator . clipboard . writeText ( markdown ) ;
0 commit comments