Fix issue #4769: '[Bug]: Markdown numbering is rendered weird'

This commit is contained in:
openhands 2024-11-05 23:24:45 +00:00
parent e497438085
commit dd846ca11d
3 changed files with 34 additions and 3 deletions

View File

@ -75,7 +75,7 @@ export function ChatMessage({
ul,
ol,
}}
remarkPlugins={[remarkGfm]}
remarkPlugins={[[remarkGfm, { listNumbering: true }]]}
>
{message}
</Markdown>

View File

@ -13,10 +13,28 @@ export function ul({
// Custom component to render <ol> in markdown
export function ol({
children,
ordered,
start,
}: React.ClassAttributes<HTMLElement> &
React.HTMLAttributes<HTMLElement> &
ExtraProps) {
ExtraProps & {
ordered?: boolean;
start?: number;
}) {
return (
<ol className="list-decimal ml-5 pl-2 whitespace-normal">{children}</ol>
<ol className="ml-5 pl-2 whitespace-normal" style={{ listStyle: 'none' }}>
{React.Children.map(children, (child, index) => {
if (React.isValidElement(child)) {
const originalNumber = child.props?.value || (start || 1) + index;
return React.cloneElement(child, {
...child.props,
style: { counterReset: `list-item ${originalNumber}` },
className: 'custom-list-item',
'data-number': originalNumber,
});
}
return child;
})}
</ol>
);
}

View File

@ -52,3 +52,16 @@ code {
.markdown-body th, .markdown-body td {
padding: 0.1rem 1rem;
}
.custom-list-item {
position: relative;
padding-left: 1rem;
}
.custom-list-item::before {
content: attr(data-number) ".";
position: absolute;
left: -1.5rem;
width: 1.5rem;
text-align: right;
}