mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-04-27 11:18:30 -04:00
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import classNames from "classnames";
|
|
import CopyButton from "../CopyButton/CopyButton";
|
|
|
|
import styles from "./CodeBlock.module.css";
|
|
|
|
type CodeBlockProps = JSX.IntrinsicElements["code"] & {
|
|
forceBlock?: boolean;
|
|
};
|
|
|
|
const CodeBlock = ({ forceBlock, className, children, ...rest }: CodeBlockProps) => {
|
|
// detect if this input has already been touched by prism.js via rehype
|
|
const prismEnabled = className?.split(" ").includes("code-highlight");
|
|
|
|
if (prismEnabled || forceBlock) {
|
|
// full multi-line code blocks with copy-to-clipboard button
|
|
// automatic if highlighted by prism, otherwise can be forced (rather than inlined) with `forceBlock={true}`
|
|
return (
|
|
<div className={styles.block}>
|
|
<CopyButton source={children} className={styles.copy_btn} />
|
|
<code
|
|
className={classNames(
|
|
styles.code,
|
|
prismEnabled && styles.highlight,
|
|
className?.replace("code-highlight", "").trim()
|
|
)}
|
|
{...rest}
|
|
>
|
|
{children}
|
|
</code>
|
|
</div>
|
|
);
|
|
} else {
|
|
// inline code in paragraphs, headings, etc. (not highlighted)
|
|
return (
|
|
<code className={classNames(styles.code, styles.inline, className)} {...rest}>
|
|
{children}
|
|
</code>
|
|
);
|
|
}
|
|
};
|
|
|
|
export default CodeBlock;
|