How to add custom syntax highlighter to code block? #994
-
Hello everyone👋, I've already searched but haven't found any information on how to add a syntax highlighter in KeyStatic. I'm using a Next.js app directory. How can I accomplish this? |
Beta Was this translation helpful? Give feedback.
Answered by
trangcongthanh
Mar 8, 2024
Replies: 1 comment 1 reply
-
If you're using 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 dangerouslySetInnerHTML={{ __html: html }} />
} If you're using I'm not using |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
rhmnaulia
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you're using
fields.document
, you can pass custom code component for DocumentRendererI'm using shiki, my Code component will looklike: