From 7ec29ba7e649f8682bbc82980687391f83b0fa2a Mon Sep 17 00:00:00 2001 From: zhangtao Date: Tue, 22 Oct 2024 21:38:39 +0800 Subject: [PATCH] fix code context description parsing to filename & range --- gui/src/components/mainInput/resolveInput.ts | 7 +++++-- gui/src/components/markdown/CodeBlockToolbar.tsx | 1 + gui/src/components/markdown/CodeSnippetPreview.tsx | 6 +++--- gui/src/components/markdown/PreWithToolbar.tsx | 1 + gui/src/components/markdown/StyledMarkdownPreview.tsx | 10 ++++++---- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/gui/src/components/mainInput/resolveInput.ts b/gui/src/components/mainInput/resolveInput.ts index f57c03f4a..4b2d606d6 100644 --- a/gui/src/components/mainInput/resolveInput.ts +++ b/gui/src/components/mainInput/resolveInput.ts @@ -57,9 +57,12 @@ async function resolveEditorContent( } } else if (p.type === "codeBlock") { if (!p.attrs.item.editing) { + let meta = p.attrs.item.description.split(" "); + let relativePath = meta[0] || ""; + let extName = relativePath.split(".").slice(-1)[0]; const text = - "```" + - p.attrs.item.description + + "\n\n" + + "```" + extName + " " + p.attrs.item.description + "\n" + p.attrs.item.content + "\n```"; diff --git a/gui/src/components/markdown/CodeBlockToolbar.tsx b/gui/src/components/markdown/CodeBlockToolbar.tsx index aecaa1b84..2973fc535 100644 --- a/gui/src/components/markdown/CodeBlockToolbar.tsx +++ b/gui/src/components/markdown/CodeBlockToolbar.tsx @@ -86,6 +86,7 @@ interface CodeBlockToolBarProps { language: string | undefined; isNextCodeBlock: boolean; filepath?: string; + range?: string; } const terminalLanguages = ["bash", "sh"]; diff --git a/gui/src/components/markdown/CodeSnippetPreview.tsx b/gui/src/components/markdown/CodeSnippetPreview.tsx index 2a7b6d970..3c783c59a 100644 --- a/gui/src/components/markdown/CodeSnippetPreview.tsx +++ b/gui/src/components/markdown/CodeSnippetPreview.tsx @@ -95,7 +95,7 @@ function CodeSnippetPreview(props: CodeSnippetPreviewProps) { .split(")")[0] .split("-"); ideMessenger.ide.showLines( - props.item.description, + props.item.description.split(" ")[0], parseInt(lines[0]) - 1, parseInt(lines[1]) - 1, ); @@ -145,8 +145,8 @@ function CodeSnippetPreview(props: CodeSnippetPreviewProps) { > diff --git a/gui/src/components/markdown/PreWithToolbar.tsx b/gui/src/components/markdown/PreWithToolbar.tsx index ec9297d54..f626dfb90 100644 --- a/gui/src/components/markdown/PreWithToolbar.tsx +++ b/gui/src/components/markdown/PreWithToolbar.tsx @@ -36,6 +36,7 @@ function PreWithToolbar(props: { language: string | null; codeBlockIndex: number; filepath?: string | undefined; + range?: string; }) { const uiConfig = useUIConfig(); const toolbarBottom = uiConfig?.codeBlockToolbarPosition == "bottom"; diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index 1695b7d19..a744b371d 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -126,14 +126,15 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( visit(tree, "code", (node: any) => { if (!node.lang) { node.lang === "javascript"; - } else if (node.lang.includes(".")) { - node.lang = node.lang.split(".").slice(-1)[0]; } if (node.meta) { node.data = node.data || {}; node.data.hProperties = node.data.hProperties || {}; - node.data.hProperties.filepath = node.meta; + + let meta = node.meta.split(" "); + node.data.hProperties.filepath = meta[0]; + node.data.hProperties.range = meta[1]; } }); }; @@ -172,13 +173,14 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( ); }, pre: ({ node, ...preProps }) => { - const { className, filepath } = preProps?.children?.[0]?.props; + const { className, filepath, range } = preProps?.children?.[0]?.props; return props.showCodeBorder ? (