Skip to content

Commit c798df2

Browse files
committed
fix: double underscored filenames now render correctly
1 parent 35b9e4c commit c798df2

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

webview-ui/src/components/common/MarkdownBlock.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,43 @@ const remarkUrlToLink = () => {
5050
}
5151
}
5252

53+
/**
54+
* Custom remark plugin that prevents filenames with extensions from being parsed as bold text
55+
* For example: __init__.py should not be rendered as bold "init" followed by ".py"
56+
*/
57+
const remarkPreventBoldFilenames = () => {
58+
return (tree: any) => {
59+
visit(tree, "strong", (node: any, index: number | undefined, parent: any) => {
60+
// Only process if there's a next node (potential file extension)
61+
if (!parent || typeof index === "undefined" || index === parent.children.length - 1) return
62+
63+
const nextNode = parent.children[index + 1]
64+
65+
// Check if next node is text and starts with . followed by extension
66+
if (nextNode.type !== "text" || !nextNode.value.match(/^\.[a-zA-Z0-9]+/)) return
67+
68+
// If the strong node has multiple children, something weird is happening
69+
if (node.children?.length !== 1) return
70+
71+
// Get the text content from inside the strong node
72+
const strongContent = node.children?.[0]?.value
73+
if (!strongContent || typeof strongContent !== "string") return
74+
75+
// Validate that the strong content is a valid filename
76+
if (!strongContent.match(/^[a-zA-Z0-9_-]+$/)) return
77+
78+
// Combine into a single text node
79+
const newNode = {
80+
type: "text",
81+
value: `__${strongContent}__${nextNode.value}`,
82+
}
83+
84+
// Replace both nodes with the combined text node
85+
parent.children.splice(index, 2, newNode)
86+
})
87+
}
88+
}
89+
5390
const StyledMarkdown = styled.div`
5491
pre {
5592
background-color: ${CODE_BLOCK_BG_COLOR};
@@ -160,6 +197,7 @@ const MarkdownBlock = memo(({ markdown }: MarkdownBlockProps) => {
160197
const { theme } = useExtensionState()
161198
const [reactContent, setMarkdown] = useRemark({
162199
remarkPlugins: [
200+
remarkPreventBoldFilenames,
163201
remarkUrlToLink,
164202
() => {
165203
return (tree) => {

0 commit comments

Comments
 (0)