Skip to content

Commit

Permalink
🐛 fix: Handling invalid Markdown bold (#190)
Browse files Browse the repository at this point in the history
* Update utils.ts

* Update index.tsx

* Update utils.ts

* Create utils.test.ts

* Update index.tsx

* Update type.ts

* Update type.ts

* Update utils.ts

* Update utils.ts

---------

Co-authored-by: CanisMinor <[email protected]>
  • Loading branch information
sxjeru and canisminor1990 authored Aug 6, 2024
1 parent f9cb396 commit 3a21228
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Markdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { CodeFullFeatured, CodeLite } from './CodeBlock';
import type { TypographyProps } from './Typography';
import { useStyles as useMarkdownStyles } from './markdown.style';
import { useStyles } from './style';
import { escapeBrackets, escapeMhchem } from './utils';
import { escapeBrackets, escapeMhchem, fixMarkdownBold } from './utils';

export interface MarkdownProps extends TypographyProps {
allowHtml?: boolean;
Expand Down Expand Up @@ -62,10 +62,10 @@ const Markdown = memo<MarkdownProps>(
const { cx, styles } = useStyles({ fontSize, headerMultiple, lineHeight, marginMultiple });
const { styles: mdStyles } = useMarkdownStyles({ fontSize, headerMultiple, marginMultiple });
const isChatMode = variant === 'chat';

const escapedContent = useMemo(() => {
if (!enableLatex) return children;
return escapeMhchem(escapeBrackets(children));
if (!enableLatex) return fixMarkdownBold(children);
return fixMarkdownBold(escapeMhchem(escapeBrackets(children)));
}, [children, enableLatex]);

const components: Components = useMemo(
Expand Down
32 changes: 32 additions & 0 deletions src/Markdown/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, it } from 'vitest';
import { fixMarkdownBold } from './utils';

describe('fixMarkdownBold', () => {
it('should add space after closing bold markers if needed', () => {
expect(fixMarkdownBold('**123:**456')).toBe('**123:** 456');
expect(fixMarkdownBold('**bold text**')).toBe('**bold text**');
expect(fixMarkdownBold('**bold text** and more')).toBe('**bold text** and more');
expect(fixMarkdownBold('**123**456')).toBe('**123**456');
});

it('should handle multiple bold sections', () => {
expect(fixMarkdownBold('**bold1** **bold2**')).toBe('**bold1** **bold2**');
expect(fixMarkdownBold('**123:**456**789:**123')).toBe('**123:** 456**789:** 123');
});

it('should not affect text without bold markers', () => {
expect(fixMarkdownBold('normal text')).toBe('normal text');
});

it('should not affect empty strings', () => {
expect(fixMarkdownBold('')).toBe('');
});

it('should handle odd number of asterisks', () => {
expect(fixMarkdownBold('*text* *')).toBe('*text* *');
});

it('should handle asterisks within words', () => {
expect(fixMarkdownBold('t*e*st')).toBe('t*e*st');
});
});
35 changes: 35 additions & 0 deletions src/Markdown/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,38 @@ export function escapeBrackets(text: string) {
export function escapeMhchem(text: string) {
return text.replaceAll('$\\ce{', '$\\\\ce{').replaceAll('$\\pu{', '$\\\\pu{');
}

export function fixMarkdownBold(text: string): string {
let count = 0;
let count2 = 0;
let result = '';
for (let i = 0; i < text.length; i++) {
const char = text[i];
if (char === '*') {
count++;
if (count === 2) {
count2++;
}
if (count > 2) {
result += char;
continue;
}
if (count === 2 && count2 % 2 === 0) {
const prevChar = i > 0 ? text[i - 2] : '';
const isPrevCharAlphanumeric = /[a-zA-Z0-9]/.test(prevChar);

if (i + 1 < text.length && text[i + 1] !== ' ' && !isPrevCharAlphanumeric) {
result += '* ';
} else {
result += '*';
}
} else {
result += '*';
}
} else {
result += char;
count = 0;
}
}
return result;
}

0 comments on commit 3a21228

Please sign in to comment.