-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: fix mispeliing * install shiki * feat: code highlighter from shiki * import CodeBlock * doc: simple tests section * style: shiki styling * fix: wrong file ext * fix: misspell on filename * fix: import filename
- Loading branch information
Showing
6 changed files
with
171 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useTheme } from "@/hooks/useTheme"; | ||
import { useEffect, useState } from "react"; | ||
import { codeToHtml } from "shiki"; | ||
|
||
export default function CodeBlock({ | ||
code, | ||
lang, | ||
}: { | ||
code: string; | ||
lang: string; | ||
}) { | ||
const { theme } = useTheme(); | ||
const [htmlOutput, setHtmlOutput] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
const fetchCode = async () => { | ||
try { | ||
const out = await codeToHtml(code, { | ||
lang: lang || "plaintext", | ||
themes: { | ||
dark: "houston", | ||
light: "one-light", | ||
}, | ||
}); | ||
setHtmlOutput(out); | ||
} catch (error) { | ||
console.error("Failed to generate code block HTML:", error); | ||
setHtmlOutput(null); | ||
} | ||
}; | ||
|
||
fetchCode(); | ||
}, [code, theme, lang]); | ||
|
||
return ( | ||
<div | ||
className="my-5 shiki-codeblock" | ||
dangerouslySetInnerHTML={{ __html: htmlOutput || "" }} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,31 @@ | ||
:root { | ||
--nextra-primary-hue: -213deg; | ||
--nextra-primary-saturation: 80%; | ||
--shiki-codeblock-bg: #fff; | ||
} | ||
|
||
.dark { | ||
--nextra-primary-hue: 167deg; | ||
--shiki-codeblock-bg: #000; | ||
} | ||
|
||
body { | ||
font-family: "Kanit", sans-serif; | ||
} | ||
|
||
.shiki-codeblock pre { | ||
padding: 1rem; | ||
border-radius: 1rem; | ||
background-color: var(--shiki-codeblock-bg) !important; | ||
overflow: auto; | ||
} | ||
|
||
html.dark .shiki, | ||
html.dark .shiki span { | ||
color: var(--shiki-dark) !important; | ||
background-color: var(--shiki-dark-bg) !important; | ||
/* Optional, if you also want font styles */ | ||
font-style: var(--shiki-dark-font-style) !important; | ||
font-weight: var(--shiki-dark-font-weight) !important; | ||
text-decoration: var(--shiki-dark-text-decoration) !important; | ||
} |