Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion frontend/components/design-system-components/MessageMarkdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,25 @@ const markdownComponents: any = {
}
} as const

// Normalize ordered list markers so that lines starting with "1) " are
// converted to the CommonMark style "1. ". This helps react-markdown parse
// them as ordered lists. We avoid transforming content inside fenced code blocks.
const normalizeOrderedListMarkers = (markdown: string) => {
let inCodeFence = false
const fenceRegex = /^\s*```/
return markdown
.split('\n')
.map(line => {
if (fenceRegex.test(line)) {
inCodeFence = !inCodeFence
return line
}
if (inCodeFence) return line
return line.replace(/^(\s*)(\d+)\)\s+/, '$1$2. ')
})
.join('\n')
}

const MessageMarkdown = ({
children,
className,
Expand All @@ -291,6 +310,7 @@ const MessageMarkdown = ({
allowHtml?: boolean
textSize?: MarkdownTextSize
}) => {
const normalizedChildren = normalizeOrderedListMarkers(children)
return (
<MarkdownContext.Provider value={textSize}>
<MemoizedReactMarkdown
Expand All @@ -307,7 +327,7 @@ const MessageMarkdown = ({
}
components={markdownComponents}
>
{children}
{normalizedChildren}
</MemoizedReactMarkdown>
</MarkdownContext.Provider>
)
Expand Down