|
1 | 1 | /** |
2 | 2 | * Preprocess markdown content to fix rendering issues with Korean text. |
3 | | - * Version 0.1.1 |
| 3 | + * Version 0.1.3 |
4 | 4 | * |
5 | 5 | * Handles: |
6 | | - * 1. Bold markers wrapping quoted text: `**'text'**` → `'**text**'` |
7 | | - * 2. Bold markers wrapping double-quoted text: `**"text"**` → `"**text**"` |
8 | | - * 3. Bold closing `**` followed by Korean characters: `**text**에서` → `**text** 에서` |
9 | | - * 4. Tilde escaping to prevent accidental strikethrough: `~` → `\~` |
| 6 | + * 1. Trim invalid inner spaces around bold text: `** text **` → `**text**` |
| 7 | + * 2. Bold markers wrapping quoted text: `**'text'**` → `'**text**'` |
| 8 | + * 3. Bold markers wrapping double-quoted text: `**"text"**` → `"**text**"` |
| 9 | + * 4. Bold closing `**` followed by Korean characters: `**text**에서` → `**text** 에서` |
| 10 | + * 5. Tilde escaping to prevent accidental strikethrough: `~` → `\~` |
10 | 11 | */ |
11 | 12 | export function preprocessMarkdown(content: string): string { |
12 | 13 | if (!content) return ""; |
13 | 14 |
|
14 | | - // 1. Move single quotes outside of bold markers |
| 15 | + // 1. Trim invalid inner spaces around bold markers |
| 16 | + // ** text ** -> **text** |
| 17 | + let processed = content.replace(/\*\*([^*]+?)\*\*/g, (match, inner: string) => { |
| 18 | + const trimmed = inner.trim(); |
| 19 | + return trimmed && trimmed !== inner ? `**${trimmed}**` : match; |
| 20 | + }); |
| 21 | + |
| 22 | + // 2. Move single quotes outside of bold markers |
15 | 23 | // **'text'** -> '**text**' |
16 | | - let processed = content.replace(/\*\*'(.+?)'\*\*/g, "'**$1**'"); |
| 24 | + processed = processed.replace(/\*\*'(.+?)'\*\*/g, "'**$1**'"); |
17 | 25 |
|
18 | | - // 2. Move double quotes outside of bold markers |
| 26 | + // 3. Move double quotes outside of bold markers |
19 | 27 | // **"text"** -> "**text**" |
20 | 28 | processed = processed.replace(/\*\*"([^"]+)"\*\*/g, '"**$1**"'); |
21 | 29 |
|
22 | | - // 3. Handle cases where bold closing ** is followed by Korean character |
| 30 | + // 4. Handle cases where bold closing ** is followed by Korean character |
23 | 31 | // **text**에서 -> **text** 에서 |
24 | 32 | processed = processed.replace(/(\*\*[^*]+?\*\*)(?=[가-힣])/g, "$1 "); |
25 | 33 |
|
26 | | - // 4. Escape tildes to prevent accidental strikethrough |
| 34 | + // 5. Escape tildes to prevent accidental strikethrough |
27 | 35 | processed = processed.replace(/~/g, "\\~"); |
28 | 36 |
|
29 | 37 | return processed; |
|
0 commit comments