From c798df215e72caf330c04c1a738af20788f7d994 Mon Sep 17 00:00:00 2001 From: Hanzen Shou Date: Sat, 28 Dec 2024 21:03:19 -0800 Subject: [PATCH 1/2] fix: double underscored filenames now render correctly --- .../src/components/common/MarkdownBlock.tsx | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/webview-ui/src/components/common/MarkdownBlock.tsx b/webview-ui/src/components/common/MarkdownBlock.tsx index 37a0278991..4de1f568d1 100644 --- a/webview-ui/src/components/common/MarkdownBlock.tsx +++ b/webview-ui/src/components/common/MarkdownBlock.tsx @@ -50,6 +50,43 @@ const remarkUrlToLink = () => { } } +/** + * Custom remark plugin that prevents filenames with extensions from being parsed as bold text + * For example: __init__.py should not be rendered as bold "init" followed by ".py" + */ +const remarkPreventBoldFilenames = () => { + return (tree: any) => { + visit(tree, "strong", (node: any, index: number | undefined, parent: any) => { + // Only process if there's a next node (potential file extension) + if (!parent || typeof index === "undefined" || index === parent.children.length - 1) return + + const nextNode = parent.children[index + 1] + + // Check if next node is text and starts with . followed by extension + if (nextNode.type !== "text" || !nextNode.value.match(/^\.[a-zA-Z0-9]+/)) return + + // If the strong node has multiple children, something weird is happening + if (node.children?.length !== 1) return + + // Get the text content from inside the strong node + const strongContent = node.children?.[0]?.value + if (!strongContent || typeof strongContent !== "string") return + + // Validate that the strong content is a valid filename + if (!strongContent.match(/^[a-zA-Z0-9_-]+$/)) return + + // Combine into a single text node + const newNode = { + type: "text", + value: `__${strongContent}__${nextNode.value}`, + } + + // Replace both nodes with the combined text node + parent.children.splice(index, 2, newNode) + }) + } +} + const StyledMarkdown = styled.div` pre { background-color: ${CODE_BLOCK_BG_COLOR}; @@ -160,6 +197,7 @@ const MarkdownBlock = memo(({ markdown }: MarkdownBlockProps) => { const { theme } = useExtensionState() const [reactContent, setMarkdown] = useRemark({ remarkPlugins: [ + remarkPreventBoldFilenames, remarkUrlToLink, () => { return (tree) => { From 1ce57f22031758c3a1fbb504ee59688b999c4f22 Mon Sep 17 00:00:00 2001 From: Hanzen Shou Date: Sat, 28 Dec 2024 21:04:25 -0800 Subject: [PATCH 2/2] doc: linked issue to markdown plugin --- webview-ui/src/components/common/MarkdownBlock.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/webview-ui/src/components/common/MarkdownBlock.tsx b/webview-ui/src/components/common/MarkdownBlock.tsx index 4de1f568d1..91e129ceb2 100644 --- a/webview-ui/src/components/common/MarkdownBlock.tsx +++ b/webview-ui/src/components/common/MarkdownBlock.tsx @@ -53,6 +53,7 @@ const remarkUrlToLink = () => { /** * Custom remark plugin that prevents filenames with extensions from being parsed as bold text * For example: __init__.py should not be rendered as bold "init" followed by ".py" + * Solves https://github.com/cline/cline/issues/1028 */ const remarkPreventBoldFilenames = () => { return (tree: any) => {