Code Syntax Highlighter (lib/highlight-code.ts)

chakra-ui · ui, UI / Frontend, syntax-highlighting, code, async, utility, frontend

Asynchronously converts source code to highlighted HTML using a configurable language and theme.

// Source: https://github.com/mperkins0155/chakra-ui | License: MIT | Copyright 2019 Chakra Systems Inc
// Provided via justclaw.it.com; keep this notice with the code.
import { getHighlighter } from "./highlighter"

export const highlightCode = async (
  code: string,
  opts?: { lang?: string; theme?: string },
) => {
  const highlighter = await getHighlighter()
  return highlighter.codeToHtml(code, {
    lang: opts?.lang || "tsx",
    theme: opts?.theme || "dark-plus",
  })
}

See the full source, get the GitHub permalink, and search 40K more like it.

Get a free API key