-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
161 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
.copyCode { | ||
position: relative; | ||
button { | ||
z-index: 1; | ||
position: absolute; | ||
top: 13px; | ||
right: -10px; | ||
background-color: var(--code-highlight); | ||
border-radius: 5px; | ||
text-transform: uppercase; | ||
font-size: 13px; | ||
padding: .1rem .4rem .2rem; | ||
color: var(--color-bg); | ||
} | ||
} | ||
|
||
button.copyCode::after { | ||
content: "📋"; | ||
} | ||
|
||
.active button.copyCode::after { | ||
content: "☑️"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use client'; | ||
|
||
import { useState } from "react"; | ||
|
||
function ClipboardCopy({ copyText }) { | ||
const [isCopied, setIsCopied] = useState(false); | ||
console.info('copyText passed in:', copyText); | ||
|
||
// This is the function we wrote earlier | ||
async function copyTextToClipboard(text) { | ||
if ('clipboard' in navigator) { | ||
return await navigator.clipboard.writeText(text); | ||
} else { | ||
return document.execCommand('copy', true, text); | ||
} | ||
} | ||
|
||
// onClick handler function for the copy button | ||
const handleCopyClick = () => { | ||
// Asynchronously call copyTextToClipboard | ||
copyTextToClipboard(copyText) | ||
.then(() => { | ||
console.info('Copied successfully: ', copyText); | ||
// If successful, update the isCopied state value | ||
setIsCopied(true); | ||
setTimeout(() => { | ||
setIsCopied(false); | ||
}, 1500); | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
}); | ||
} | ||
|
||
return ( | ||
<div> | ||
{/* Bind our handler function to the onClick button property */} | ||
<button className="copyCode absolute top-3 right-3 text-slate-500 hover:ring-2 hover:ring-slate-300 rounded" onClick={handleCopyClick} aria-label="Copy code to clipboard"> | ||
{isCopied | ||
? | ||
<svg xmlns="http://www.w3.org/2000/svg" className="icon icon-tabler icon-tabler-clipboard-check stroke-slate-500" width="20" height="20" viewBox="0 0 24 24" strokeWidth="1.5" stroke="#000000" fill="none" strokeLinecap="round" strokeLinejoin="round"> | ||
<path stroke="none" d="M0 0h24v24H0z" fill="none" /> | ||
<path d="M9 5h-2a2 2 0 0 0 -2 2v12a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-12a2 2 0 0 0 -2 -2h-2" /> | ||
<path d="M9 3m0 2a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v0a2 2 0 0 1 -2 2h-2a2 2 0 0 1 -2 -2z" /> | ||
<path d="M9 14l2 2l4 -4" /> | ||
</svg> | ||
: | ||
<svg xmlns="http://www.w3.org/2000/svg" className="icon icon-tabler icon-tabler-copy stroke-slate-500" width="20" height="20" viewBox="0 0 24 24" strokeWidth="1.5" fill="none" strokeLinecap="round" strokeLinejoin="round"> | ||
<path stroke="none" d="M0 0h24v24H0z" fill="none" /> | ||
<path d="M7 7m0 2.667a2.667 2.667 0 0 1 2.667 -2.667h8.666a2.667 2.667 0 0 1 2.667 2.667v8.666a2.667 2.667 0 0 1 -2.667 2.667h-8.666a2.667 2.667 0 0 1 -2.667 -2.667z" /> | ||
<path d="M4.012 16.737a2.005 2.005 0 0 1 -1.012 -1.737v-10c0 -1.1 .9 -2 2 -2h10c.75 0 1.158 .385 1.5 1" /> | ||
</svg> | ||
} | ||
<span></span> | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
export default ClipboardCopy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use client'; | ||
|
||
import clsx from "clsx"; | ||
import ClipboardCopy from "./clipboard-copy"; | ||
// import './clipboard-copy.css' | ||
|
||
const CodeBlock = ({ className, children, raw, ...props }) => { | ||
console.log('raw: ', raw); | ||
return ( | ||
<> | ||
<div className="copyCode"> | ||
<pre className={clsx("p-0 my-4 px-4 py-4 bg-transparent rounded overflow-x-auto relative", className)} {...props}> | ||
{children} | ||
<ClipboardCopy copyText={raw}/> | ||
</pre> | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
export default CodeBlock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { visit } from 'unist-util-visit' | ||
|
||
export const preProcess = () => (tree) => { | ||
visit(tree, (node) => { | ||
if (node?.type === 'element' && node?.tagName === 'pre') { | ||
const [codeEl] = node.children | ||
|
||
if (codeEl.tagName !== 'code') return | ||
|
||
node.raw = codeEl.children?.[0].value | ||
// console.info('node preProcessed: ', node); | ||
} | ||
}) | ||
} | ||
|
||
export const postProcess = () => (tree) => { | ||
visit(tree, (node) => { | ||
if (node?.type === 'element' && node?.tagName === 'figure') { | ||
if (!("data-rehype-pretty-code-figure" in node.properties)) { | ||
return; | ||
} | ||
|
||
for (const child of node.children) { | ||
if (child.tagName === "pre") { | ||
child.properties["raw"] = node.raw; | ||
} | ||
} | ||
node.properties['raw'] = node.raw | ||
console.log('PostProcessed: ', node); // here to see if you're getting the raw text | ||
} | ||
}) | ||
} |