From 0edcc1634db0ce51748f451f4a59ac8af5a81c66 Mon Sep 17 00:00:00 2001 From: Shane Date: Sun, 13 Aug 2023 15:34:34 +0800 Subject: [PATCH 1/4] feat: remove leading spaces and specific characters when copying shell code in `SourceCode` --- src/client/theme-default/builtins/SourceCode/index.tsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/client/theme-default/builtins/SourceCode/index.tsx b/src/client/theme-default/builtins/SourceCode/index.tsx index 78313ea4c3..22a148b143 100644 --- a/src/client/theme-default/builtins/SourceCode/index.tsx +++ b/src/client/theme-default/builtins/SourceCode/index.tsx @@ -28,10 +28,16 @@ const SourceCode: FC = (props) => { const [isCopied, setIsCopied] = useState(false); const { themeConfig } = useSiteData(); + let text = ''; + const isShell = /shellscript|shell|bash|sh|zsh/.test(lang); + if (isShell) { + text = children.replace(/^ *(\$|>) /gm, ''); + } + return (
{ setIsCopied(true); clearTimeout(timer.current); From d83e5d16fa570478d8ac866aba17da467dfcd409 Mon Sep 17 00:00:00 2001 From: Shane Date: Sun, 13 Aug 2023 21:07:31 +0800 Subject: [PATCH 2/4] Update src/client/theme-default/builtins/SourceCode/index.tsx Co-authored-by: Peach --- src/client/theme-default/builtins/SourceCode/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/theme-default/builtins/SourceCode/index.tsx b/src/client/theme-default/builtins/SourceCode/index.tsx index 22a148b143..0fa9c8098f 100644 --- a/src/client/theme-default/builtins/SourceCode/index.tsx +++ b/src/client/theme-default/builtins/SourceCode/index.tsx @@ -31,7 +31,7 @@ const SourceCode: FC = (props) => { let text = ''; const isShell = /shellscript|shell|bash|sh|zsh/.test(lang); if (isShell) { - text = children.replace(/^ *(\$|>) /gm, ''); + text = children.replace(/^(\$|>)\s/gm, ''); } return ( From afb169c9778449424074d3caf4136774206e3f06 Mon Sep 17 00:00:00 2001 From: Shane Date: Sun, 13 Aug 2023 21:28:52 +0800 Subject: [PATCH 3/4] perf: use `useEffect` to avoid unnecessary execution of code --- .../theme-default/builtins/SourceCode/index.tsx | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/client/theme-default/builtins/SourceCode/index.tsx b/src/client/theme-default/builtins/SourceCode/index.tsx index 0fa9c8098f..dc1e8f6193 100644 --- a/src/client/theme-default/builtins/SourceCode/index.tsx +++ b/src/client/theme-default/builtins/SourceCode/index.tsx @@ -4,7 +4,7 @@ import classNames from 'classnames'; import { useSiteData } from 'dumi'; import Highlight, { defaultProps, type Language } from 'prism-react-renderer'; import 'prism-themes/themes/prism-one-light.css'; -import React, { useRef, useState, type FC } from 'react'; +import React, { useRef, useState, useEffect, type FC } from 'react'; import { CopyToClipboard } from 'react-copy-to-clipboard'; import './index.less'; @@ -29,11 +29,13 @@ const SourceCode: FC = (props) => { const { themeConfig } = useSiteData(); let text = ''; - const isShell = /shellscript|shell|bash|sh|zsh/.test(lang); - if (isShell) { - text = children.replace(/^(\$|>)\s/gm, ''); - } - + useEffect(() => { + const isShell = /shellscript|shell|bash|sh|zsh/.test(lang); + if (isShell) { + text = children.replace(/^(\$|>)\s/gm, ''); + } + }, [lang]); + return (
Date: Wed, 16 Aug 2023 16:51:09 +0800 Subject: [PATCH 4/4] Update index.tsx - The initialValue of `text` should be the value of `children`. - Here, `text` should be managed within the component's state, otherwise the new value of `text` will not be rendered even if the effect re-runs. - `children` should also be a dependency. --- src/client/theme-default/builtins/SourceCode/index.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/client/theme-default/builtins/SourceCode/index.tsx b/src/client/theme-default/builtins/SourceCode/index.tsx index dc1e8f6193..d62c00207d 100644 --- a/src/client/theme-default/builtins/SourceCode/index.tsx +++ b/src/client/theme-default/builtins/SourceCode/index.tsx @@ -26,15 +26,16 @@ const SourceCode: FC = (props) => { const { children = '', lang, highlightLines = [] } = props; const timer = useRef(); const [isCopied, setIsCopied] = useState(false); + const [text, setText] = useState(children); const { themeConfig } = useSiteData(); - let text = ''; useEffect(() => { const isShell = /shellscript|shell|bash|sh|zsh/.test(lang); if (isShell) { - text = children.replace(/^(\$|>)\s/gm, ''); + const text = children.replace(/^(\$|>)\s/gm, ''); + setText(text); } - }, [lang]); + }, [lang, children]); return (