Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix: Ignore ** in code blocks #196

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
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
33 changes: 33 additions & 0 deletions src/Markdown/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,37 @@ describe('fixMarkdownBold', () => {
it('should handle asterisks within words', () => {
expect(fixMarkdownBold('t*e*st')).toBe('t*e*st');
});

it('should ignore bold markers inside inline code', () => {
expect(fixMarkdownBold('This is `**not bold**`')).toBe('This is `**not bold**`');
expect(fixMarkdownBold('`**code**` and **bold**')).toBe('`**code**` and **bold**');
expect(fixMarkdownBold('`**` and **bold**')).toBe('`**` and **bold**');
});

it('should ignore bold markers inside code blocks', () => {
const codeBlock = `
\`\`\`
**123:**456**789:**123
\`\`\`
`;
expect(fixMarkdownBold(codeBlock)).toBe(codeBlock);
});

it('should handle text with mixed content in code blocks', () => {
const text = `
**bold text**
\`\`\`
n**0.5
\`\`\`
More **bold text**
`;
const expected = `
**bold text**
\`\`\`
n**0.5
\`\`\`
More **bold text**
`;
expect(fixMarkdownBold(text)).toBe(expected);
});
});
18 changes: 17 additions & 1 deletion src/Markdown/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,25 @@ export function fixMarkdownBold(text: string): string {
let count = 0;
let count2 = 0;
let result = '';
let inCodeBlock = false;
let inInlineCode = false;

for (let i = 0; i < text.length; i++) {
const char = text[i];
if (char === '*') {

if (text.slice(i, i + 3) === '```') {
inCodeBlock = !inCodeBlock;
result += '```';
i += 2;
continue;
}
if (char === '`') {
inInlineCode = !inInlineCode;
result += '`';
continue;
}

if (char === '*' && !inInlineCode && !inCodeBlock) {
count++;
if (count === 2) {
count2++;
Expand Down
Loading