Skip to content

Commit

Permalink
chore: dark mode code rendering based on useDarkMode hook
Browse files Browse the repository at this point in the history
  • Loading branch information
aegroto committed Nov 19, 2024
1 parent 0c55fef commit c71deb1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
15 changes: 7 additions & 8 deletions components/codeblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,42 @@ import { useMemo, useEffect, useState, Fragment } from 'react'
import { createHighlighter, makeSingletonHighlighter } from 'shiki'
import { toJsxRuntime } from 'hast-util-to-jsx-runtime'
import { jsx, jsxs } from 'react/jsx-runtime'
import './codeblock.module.css'
import useDarkMode from './dark-mode'

const SHIKI_LIGHT_THEME = 'github-light'
const SHIKI_DARK_THEME = 'github-dark'

const getHighlighter = makeSingletonHighlighter(createHighlighter)

export const codeToHast = async ({ code, language }) => {
const codeToHast = async ({ code, language, dark }) => {
const highlighter = await getHighlighter({
themes: [SHIKI_LIGHT_THEME, SHIKI_DARK_THEME],
langs: [language]
})

return highlighter.codeToHast(code, {
lang: language,
themes: {
light: SHIKI_LIGHT_THEME,
dark: SHIKI_DARK_THEME
}
theme: dark ? SHIKI_DARK_THEME : SHIKI_LIGHT_THEME
})
}

export default function CodeBlock ({ code, language }) {
const [hast, setHast] = useState(undefined)
const [darkMode] = useDarkMode()

useEffect(() => {
async function processCode () {
const hast = await codeToHast({
code,
language
language,
dark: darkMode
})

setHast(hast)
}

processCode()
}, [])
}, [darkMode])

const element = useMemo(() => {
if (!hast) {
Expand Down
11 changes: 0 additions & 11 deletions components/codeblock.module.css

This file was deleted.

0 comments on commit c71deb1

Please sign in to comment.