Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new <CodeBlock /> react component #43

Closed
julianbenegas opened this issue Jan 17, 2024 · 1 comment
Closed

new <CodeBlock /> react component #43

julianbenegas opened this issue Jan 17, 2024 · 1 comment

Comments

@julianbenegas
Copy link
Member

it's curretly way too hard to add a code snippet into your website. choosing the syntax highlighter is the worse.

I suggest: an Async Server Component which uses shikiji to highlight code, well integrated with BaseHub's API for code snippet blocks, and which exposes primitives (potentially ala radix, or ala our RichText) to:

  • have line numbers
  • have file names
  • have "copy code" button
  • highlight specific lines

RichText style is more fit for syntax highlighting because the code follows a strict order.

An initial api design could be something like this:

<CodeSnippet.Root language="" theme="" code={code}>
  <header>
     <p>filename.tsx</p>
     <CodeSnippet.CopyButton>Copy to clipboard</CodeSnippet.CopyButton>
  </header>
  <pre>
    <CodeSnippet.Highlighter components={{
      line: ({ children, number, isHighlighted }) => <span>{}</span>,
      token: ({ type, children, style }) => {
        // type can be "keyword", "string", "comment", etc... see how they do it in https://prismjs.com/
        return <span style={style}>{children}</span>
      }
    }} />
  </pre>

</CodeSnippet.Root>

<CodeSnippet.Highlighter>'s components are optional. If you set theme, they'll come with styles, and you're always free to add your own if you want.
you can do <CodeSnippet.Root language="" theme="" code={code} highlightedLines={[1, 4]} /> alone and it should work i guess. Meaning, if no children sent, will render default Highlighter.

@julianbenegas
Copy link
Member Author

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant