Skip to content
Open
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
12 changes: 12 additions & 0 deletions packages/formatters/__tests__/escapers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const testURLs = [
'https://example.com/name_with_underscores',
'https://example.com/name__with__underscores',
'https://example.com/name_with_underscores_and__double__underscores',
'https://*.example.com/globbed/*',
'https://example.com/*before*/>/*after*',
];

describe('Markdown escapers', () => {
Expand Down Expand Up @@ -87,6 +89,16 @@ describe('Markdown escapers', () => {
test('url', () => {
for (const url of testURLs) expect(escapeItalic(url)).toBe(url);
});

test('after-url', () => {
const url = '<https://example.com>';
for (const [input, output] of [
['*hi*', '\\*hi\\*'],
['_hi_', '\\_hi\\_'],
]) {
expect(escapeItalic(url + input)).toBe(url + output);
}
});
});

describe('escapeUnderline', () => {
Expand Down
22 changes: 14 additions & 8 deletions packages/formatters/src/escapers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,21 @@ export function escapeInlineCode(text: string): string {
*/
export function escapeItalic(text: string): string {
let idx = 0;
const newText = text.replaceAll(/(?<=^|[^*])\*([^*]|\*\*|$)/g, (_, match) => {
if (match === '**') return ++idx % 2 ? `\\*${match}` : `${match}\\*`;
return `\\*${match}`;
});
const newText = text.replaceAll(
/(?<=^|[^*])(?<!(?<!<)https?:\/\/\S*|<https?:\/\/[^\s>]*)\*([^*]|\*\*|$)/g,
(_, match) => {
if (match === '**') return ++idx % 2 ? `\\*${match}` : `${match}\\*`;
return `\\*${match}`;
},
);
idx = 0;
return newText.replaceAll(/(?<=^|[^_])(?<!<a?:.+|https?:\/\/\S+)_(?!:\d+>)([^_]|__|$)/g, (_, match) => {
if (match === '__') return ++idx % 2 ? `\\_${match}` : `${match}\\_`;
return `\\_${match}`;
});
return newText.replaceAll(
/(?<=^|[^_])(?<!<a?:.+|(?<!<)https?:\/\/\S*|<https?:\/\/[^\s>]*)_(?!:\d+>)([^_]|__|$)/g,
(_, match) => {
if (match === '__') return ++idx % 2 ? `\\_${match}` : `${match}\\_`;
return `\\_${match}`;
},
);
}

/**
Expand Down