File size: 1,718 Bytes
0bfe2e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import React from 'react';

interface MarkdownLiteProps {
  children: string;
  className?: string;
}

// Supports [text](url) and `code` only
const MarkdownLite: React.FC<MarkdownLiteProps> = ({ children, className }) => {
  if (!children) return null;
  // Regex for [text](url) and `code`
  const regex = /(`[^`]+`|\[[^\]]+\]\([^\)]+\))/g;
  const parts = children.split(regex);
  const matches = children.match(regex) || [];
  let matchIdx = 0;

  return (
    <span className={className}>
      {parts.map((part, i) => {
        if (i === parts.length - 1 && part === '') return null;
        if (i % 2 === 0) {
          // Plain text
          return part;
        } else {
          const match = matches[matchIdx++];
          if (!match) return null;
          if (match.startsWith('`') && match.endsWith('`')) {
            // Inline code
            return (
              <code
                key={i}
                className="bg-muted px-1 py-0.5 rounded text-[--brand] font-mono text-xs"
              >
                {match.slice(1, -1)}
              </code>
            );
          } else {
            // Link
            const linkMatch = match.match(/\[([^\]]+)\]\(([^\)]+)\)/);
            if (linkMatch) {
              const [, text, url] = linkMatch;
              return (
                <a
                  key={i}
                  href={url}
                  target="_blank"
                  rel="noopener noreferrer"
                  className="text-[--brand] hover:underline"
                >
                  {text}
                </a>
              );
            }
          }
          return match;
        }
      })}
    </span>
  );
};

export default MarkdownLite;