Skip to content

Commit

Permalink
fix: 修复auto、dark模式下在刷新后,code模块未同步的情况 (#227)
Browse files Browse the repository at this point in the history
Co-authored-by: 橙林 <chenglin@dian.so>
  • Loading branch information
BARMPlus and 橙林 authored Mar 7, 2023
1 parent 79cee57 commit c615d81
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions packages/website/components/Markdown/Code.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dynamic from "next/dynamic";
import { useContext, useMemo } from "react";
import { useContext, useState, useEffect } from "react";
import CopyToClipboard from "react-copy-to-clipboard";
import toast from "react-hot-toast";
import { CodeComponent, CodeProps } from "react-markdown/lib/ast-to-react";
Expand All @@ -10,13 +10,10 @@ import { ThemeContext } from "../../utils/themeContext";
export function CodeBlock(props: { children: any; match: any }) {
const code = props.children.replace(/\n$/, "");
const { theme } = useContext(ThemeContext);
const [curMode, setCurMode] = useState(undefined);

const curModeInfo = useMemo(() => {
const mode = theme.includes("dark") ? dark : (light as any);
return {
mode,
key: Date.now(),
};
useEffect(() => {
setCurMode(theme.includes("dark") ? dark : (light as any));
}, [theme]);

return (
Expand Down Expand Up @@ -58,8 +55,7 @@ export function CodeBlock(props: { children: any; match: any }) {
</div>

<SyntaxHighlighter
key={curModeInfo.key}
style={curModeInfo.mode}
style={curMode}
language={props.match?.length ? props.match[1] : undefined}
wrapLines={true}
lineProps={() => {
Expand Down

0 comments on commit c615d81

Please sign in to comment.