2024-12-21 14:25:49 +01:00
|
|
|
import React from 'react'
|
|
|
|
import { CopyToClipboard } from '@freesewing/react/components/CopyToClipboard'
|
|
|
|
|
2024-12-22 16:32:46 +01:00
|
|
|
const defaultTitles = {
|
2024-12-21 14:25:49 +01:00
|
|
|
js: 'Javascript',
|
2024-12-22 16:32:46 +01:00
|
|
|
bash: 'Bash commands',
|
|
|
|
sh: 'Shell commands',
|
2024-12-21 14:25:49 +01:00
|
|
|
json: 'JSON',
|
2024-12-22 16:32:46 +01:00
|
|
|
yaml: 'YAML',
|
2024-12-21 14:25:49 +01:00
|
|
|
}
|
|
|
|
|
2024-12-22 16:32:46 +01:00
|
|
|
/**
|
|
|
|
* A React component to highlight code
|
|
|
|
*
|
|
|
|
* @params {object} props - All React props
|
|
|
|
* @params {string} language - The language to highlight
|
|
|
|
* @params {object} children - The React children
|
|
|
|
* @params {bool} raw - Set this to true to not escape tags
|
|
|
|
* @params {string} title - Title for the highlight
|
|
|
|
* @params {string} copy - Content to copy to clipboard
|
|
|
|
*/
|
|
|
|
export const Highlight = ({
|
|
|
|
language = 'txt',
|
|
|
|
children,
|
|
|
|
raw = false,
|
|
|
|
title = false,
|
|
|
|
copy = false,
|
|
|
|
}) => {
|
|
|
|
if (children?.props?.className) {
|
|
|
|
language = children.props.className.split('-').pop()
|
2024-12-21 14:25:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const preProps = {
|
|
|
|
className: `language-${language} hljs text-base lg:text-lg whitespace-break-spaces overflow-scroll pr-4`,
|
|
|
|
}
|
2024-12-22 16:32:46 +01:00
|
|
|
if (raw) preProps.dangerouslySetInnerHTML = { __html: raw }
|
|
|
|
|
|
|
|
const label = title ? title : defaultTitles[language] ? defaultTitles[language] : language
|
2024-12-21 14:25:49 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="hljs my-4">
|
|
|
|
<div
|
|
|
|
className={`
|
|
|
|
flex flex-row justify-between items-center
|
|
|
|
text-xs font-medium text-warning
|
|
|
|
mt-1 border-b border-neutral-content border-opacity-25
|
|
|
|
px-4 py-1 mb-2 lg:text-sm
|
|
|
|
`}
|
|
|
|
>
|
2024-12-22 16:32:46 +01:00
|
|
|
<span>{label}</span>
|
|
|
|
<CopyToClipboard content={copy ? copy : children} label={label} />
|
2024-12-21 14:25:49 +01:00
|
|
|
</div>
|
2024-12-22 16:32:46 +01:00
|
|
|
<pre {...preProps}>{children}</pre>
|
2024-12-21 14:25:49 +01:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|