Skip to content

How to add custom syntax highlighter to code block? #994

Closed Answered by trangcongthanh
rhmnaulia asked this question in Q&A
Discussion options

You must be logged in to vote

If you're using fields.document, you can pass custom code component for DocumentRenderer

import { DocumentRenderer, DocumentRendererProps } from '@keystatic/core/renderer'

type CodeProps = {
  children: string
  language: string
}

function Code({ children, language }: CodeProps) {
  // add syntax hightlight here
}

function Renderer({ document }: DocumentProps) {
  return (
    <DocumentRenderer document={document} renderers={{ code: Code }} />
  )
}

I'm using shiki, my Code component will looklike:

import { codeToHtml } from 'shiki'

async function Code({ children, language }) {
  const html = await codeToHtml(children, {
    lang,
    theme: 'github-dark'
  })
  return <div dangerousl…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@rhmnaulia
Comment options

Answer selected by rhmnaulia
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants