Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

2.8 KB
Raw Blame
import { useMemo } from 'react'

import CopyButton from '~/ui/CopyButton'

const highlightHTML = (code: string): React.ReactNode[] => {
  const parts: React.ReactNode[] = []
  const regex =
    /(<\/?[a-zA-Z][a-zA-Z0-9]*)|(\s[a-zA-Z-]+(?==))|("[^"]*")|(&lt;\/?[a-zA-Z][a-zA-Z0-9]*)|(\/>|>)|(\/\/.*$)|(['"]https?:\/\/[^'"]*['"])|(swetrix\.\w+)|(document\.\w+)|(\b(?:function|var|const|let|document|window)\b)/gm
  let lastIndex = 0

  const matches = [...code.matchAll(regex)]

  for (const match of matches) {
    const [fullMatch] = match
    const index = match.index!

    if (index > lastIndex) {
      parts.push(code.slice(lastIndex, index))
    }

    if (fullMatch.startsWith('<') || fullMatch.startsWith('&lt;')) {
      parts.push(
        <span key={index} className='text-rose-600 dark:text-rose-400'>
          {fullMatch}
        </span>,
      )
    } else if (fullMatch.startsWith('"') || fullMatch.startsWith("'")) {
      parts.push(
        <span key={index} className='text-emerald-600 dark:text-emerald-400'>
          {fullMatch}
        </span>,
      )
    } else if (fullMatch === '/>' || fullMatch === '>') {
      parts.push(
        <span key={index} className='text-rose-600 dark:text-rose-400'>
          {fullMatch}
        </span>,
      )
    } else if (fullMatch.startsWith('//')) {
      parts.push(
        <span key={index} className='text-gray-400 dark:text-slate-500'>
          {fullMatch}
        </span>,
      )
    } else if (
      fullMatch.match(/^swetrix\.\w+/) ||
      fullMatch.match(/^document\.\w+/)
    ) {
      parts.push(
        <span key={index} className='text-sky-600 dark:text-sky-400'>
          {fullMatch}
        </span>,
      )
    } else if (fullMatch.match(/^\s[a-zA-Z-]+$/)) {
      parts.push(
        <span key={index} className='text-amber-600 dark:text-amber-400'>
          {fullMatch}
        </span>,
      )
    } else {
      parts.push(
        <span key={index} className='text-violet-600 dark:text-violet-400'>
          {fullMatch}
        </span>,
      )
    }

    lastIndex = index + fullMatch.length
  }

  if (lastIndex < code.length) {
    parts.push(code.slice(lastIndex))
  }

  return parts
}

interface CodeBlockProps {
  code: string
  onCopy?: () => void
}

const CodeBlock = ({ code, onCopy }: CodeBlockProps) => {
  const highlighted = useMemo(() => highlightHTML(code), [code])

  return (
    <div className='group relative'>
      <pre className='overflow-x-auto rounded-lg bg-white p-4 pr-12 font-mono text-xs leading-relaxed text-gray-800 ring-1 ring-gray-200 dark:bg-slate-950 dark:text-gray-200 dark:ring-slate-800'>
        <code>{highlighted}</code>
      </pre>
      <CopyButton
        value={code}
        onCopy={onCopy}
        className='absolute top-2.5 right-2.5'
      />
    </div>
  )
}

export default CodeBlock