SkillMarkdown.tsx 3.63 KB
import type { ReactNode } from 'react';

function renderInline(text: string, keyPrefix: string) {
  const nodes: ReactNode[] = [];
  const pattern = /(`[^`]+`)|(\[[^\]]+\]\([^)]+\))/g;
  let lastIndex = 0;
  let match: RegExpExecArray | null;

  match = pattern.exec(text);
  while (match) {
    if (match.index > lastIndex) {
      nodes.push(text.slice(lastIndex, match.index));
    }

    const token = match[0];
    if (token.startsWith('`')) {
      nodes.push(
        <code
          key={`${keyPrefix}-code-${match.index}`}
          className="rounded bg-[var(--gl-color-bg-page)] px-1.5 py-0.5 font-mono text-[13px]"
        >
          {token.slice(1, -1)}
        </code>,
      );
    } else {
      const link = /^\[([^\]]+)\]\(([^)]+)\)$/.exec(token);
      if (link) {
        nodes.push(
          <a
            key={`${keyPrefix}-link-${match.index}`}
            className="text-[var(--gl-color-primary)] underline"
            href={link[2]}
            rel="noreferrer"
            target="_blank"
          >
            {link[1]}
          </a>,
        );
      }
    }

    lastIndex = match.index + token.length;
    match = pattern.exec(text);
  }

  if (lastIndex < text.length) {
    nodes.push(text.slice(lastIndex));
  }

  return nodes;
}

function renderParagraph(text: string, key: string) {
  if (text.startsWith('### ')) return <h4 key={key} className="m-0 text-base font-semibold">{text.slice(4)}</h4>;
  if (text.startsWith('## ')) return <h3 key={key} className="m-0 text-lg font-semibold">{text.slice(3)}</h3>;
  if (text.startsWith('# ')) return <h2 key={key} className="m-0 text-xl font-semibold">{text.slice(2)}</h2>;
  if (text.startsWith('> ')) return <blockquote key={key} className="m-0 border-l-4 border-[var(--gl-color-border-secondary)] pl-3 text-[var(--gl-color-text-secondary)]">{renderInline(text.slice(2), key)}</blockquote>;
  return <p key={key} className="m-0 leading-[var(--gl-line-height-md)]">{renderInline(text, key)}</p>;
}

export function SkillMarkdown({ markdown }: { markdown: string }) {
  const lines = markdown.split('\n');
  const nodes: ReactNode[] = [];
  let index = 0;

  while (index < lines.length) {
    const line = lines[index];

    if (!line.trim()) {
      index += 1;
      continue;
    }

    if (line.startsWith('```')) {
      const codeLines: string[] = [];
      index += 1;
      while (index < lines.length && !lines[index].startsWith('```')) {
        codeLines.push(lines[index]);
        index += 1;
      }
      nodes.push(
        <pre
          key={`code-${index}`}
          className="m-0 overflow-x-auto rounded-[var(--gl-radius-sm)] bg-[#0f172a] p-3 text-[13px] leading-6 text-[#e2e8f0]"
          data-testid="skill-markdown-codeblock"
        >
          <code>{codeLines.join('\n')}</code>
        </pre>,
      );
      index += 1;
      continue;
    }

    if (line.startsWith('- ')) {
      const items: string[] = [];
      while (index < lines.length && lines[index].startsWith('- ')) {
        items.push(lines[index].slice(2));
        index += 1;
      }
      nodes.push(
        <ul key={`list-${index}`} className="m-0 grid gap-1 pl-5">
          {items.map((item, itemIndex) => <li key={`${index}-${itemIndex}`}>{renderInline(item, `list-${index}-${itemIndex}`)}</li>)}
        </ul>,
      );
      continue;
    }

    const paragraphLines = [line];
    index += 1;
    while (index < lines.length && lines[index].trim() && !lines[index].startsWith('- ') && !lines[index].startsWith('```')) {
      paragraphLines.push(lines[index]);
      index += 1;
    }
    nodes.push(renderParagraph(paragraphLines.join(' '), `paragraph-${index}`));
  }

  return <div className="grid gap-3">{nodes}</div>;
}