Skip to content

Commit

Permalink
feat: add option to transform the content copied to the clipboard
Browse files Browse the repository at this point in the history
Use this function to replace placeholder for variables you don't want to display
Useful for example to replace key values in snipped examples
  • Loading branch information
Oriol committed Jun 13, 2023
1 parent 26564f8 commit 8013233
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/mdx/src/mdx-client/code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function mergeCodeConfig<T>(
: showExpandButton,
rows: props.rows,
debug: props.debug ?? props.codeConfig?.debug,
transformCopyContent: props.transformCopyContent
}
return { ...rest, codeConfig }
}
Expand All @@ -68,7 +69,7 @@ export function InnerCode({
onTabClick?: (filename: string) => void
} & Partial<CodeHikeConfig>) {
const { className, style, codeConfig, ...editorProps } =
mergeCodeConfig(props)
mergeCodeConfig(props)

if (
!props.southPanel &&
Expand Down
3 changes: 3 additions & 0 deletions packages/mdx/src/mini-editor/editor-tween.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function EditorTween({
<CopyButton
className="ch-editor-button"
content={northContent}
transformContent={codeConfig.transformCopyContent}
/>
) : undefined}
{showExpandButton ? (
Expand All @@ -117,6 +118,8 @@ function EditorTween({
<CopyButton
className="ch-editor-button"
content={southContent}
transformContent={codeConfig.transformCopyContent}

/>
) : undefined

Expand Down
2 changes: 2 additions & 0 deletions packages/mdx/src/smooth-code/code-tween.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export type CodeConfig = {
horizontalCenter?: boolean
lineNumbers?: boolean
showCopyButton?: boolean
transformCopyContent?: (content: string) => string
showExpandButton?: boolean
staticMediaQuery?: string
rows?: number | "focus" | (number | "focus")[]
Expand Down Expand Up @@ -148,6 +149,7 @@ function AfterDimensions({
<CopyButton
className="ch-code-button"
content={stepInfo?.code?.prev}
transformContent={config.transformCopyContent}
/>
) : undefined}
</Wrapper>
Expand Down
5 changes: 3 additions & 2 deletions packages/mdx/src/smooth-code/copy-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ import React from "react"

export function CopyButton({
content,
transformContent,
style,
className,
}: {
content: string
transformContent?: (content: string) => string
style?: React.CSSProperties
className?: string
}) {
const [copied, setCopied] = React.useState(false)

return (
<button
type="button"
title="Copy code"
className={className}
style={style}
onClick={() => {
copyToClipboard(content)
copyToClipboard(transformContent ? transformContent(content) : content)
setCopied(true)
setTimeout(() => {
setCopied(false)
Expand Down

0 comments on commit 8013233

Please sign in to comment.