Skip to content

Commit e5e9a15

Browse files
committed
fix(markdown): handle spaces inside bold markers
1 parent ed2cd04 commit e5e9a15

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

packages/markdown/src/preprocess.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ describe("preprocessMarkdown", () => {
1414
expect(preprocessMarkdown('**"text"**')).toBe('"**text**"');
1515
});
1616

17+
it("trims invalid spaces immediately inside bold markers", () => {
18+
expect(
19+
preprocessMarkdown(
20+
"** 쇼핑몰 기본디자인 (base)**: 현재는 사용 중이지 않은 기본 디자인입니다.",
21+
),
22+
).toBe("**쇼핑몰 기본디자인 (base)**: 현재는 사용 중이지 않은 기본 디자인입니다.");
23+
});
24+
25+
it("trims trailing spaces immediately before bold closing markers", () => {
26+
expect(preprocessMarkdown("**쇼핑몰 기본디자인 (base) **입니다")).toBe(
27+
"**쇼핑몰 기본디자인 (base)** 입니다",
28+
);
29+
});
30+
1731
it("adds space between bold closing and Korean particle", () => {
1832
expect(preprocessMarkdown("**(text)**이며")).toBe("**(text)** 이며");
1933
});

packages/markdown/src/preprocess.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
11
/**
22
* Preprocess markdown content to fix rendering issues with Korean text.
3-
* Version 0.1.1
3+
* Version 0.1.3
44
*
55
* 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: `~` → `\~`
1011
*/
1112
export function preprocessMarkdown(content: string): string {
1213
if (!content) return "";
1314

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
1523
// **'text'** -> '**text**'
16-
let processed = content.replace(/\*\*'(.+?)'\*\*/g, "'**$1**'");
24+
processed = processed.replace(/\*\*'(.+?)'\*\*/g, "'**$1**'");
1725

18-
// 2. Move double quotes outside of bold markers
26+
// 3. Move double quotes outside of bold markers
1927
// **"text"** -> "**text**"
2028
processed = processed.replace(/\*\*"([^"]+)"\*\*/g, '"**$1**"');
2129

22-
// 3. Handle cases where bold closing ** is followed by Korean character
30+
// 4. Handle cases where bold closing ** is followed by Korean character
2331
// **text**에서 -> **text** 에서
2432
processed = processed.replace(/(\*\*[^*]+?\*\*)(?=[-])/g, "$1 ");
2533

26-
// 4. Escape tildes to prevent accidental strikethrough
34+
// 5. Escape tildes to prevent accidental strikethrough
2735
processed = processed.replace(/~/g, "\\~");
2836

2937
return processed;

0 commit comments

Comments
 (0)