Skip to content

Commit 181301c

Browse files
authored
fix(auto-close): handle strike through syntax (#197)
1 parent 95e3cc8 commit 181301c

2 files changed

Lines changed: 22 additions & 16 deletions

File tree

packages/comark/src/internal/parse/auto-close/index.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ function closeInlineMarkersLinear(line: string): string {
207207
// Count markers by scanning
208208
let asteriskCount = 0
209209
let underscoreCount = 0
210-
let tildeCount = 0 // Count individual tildes
210+
let doubleTildeCount = 0 // Count ~~ occurrences (GFM strikethrough delimiter)
211+
let singleTildeCount = 0 // Count standalone ~ (not part of ~~)
211212
let backtickCount = 0
212213
let dollarCount = 0 // Count $ for math
213214
let dollarPairCount = 0 // Count $$ pairs for block math
@@ -298,7 +299,12 @@ function closeInlineMarkersLinear(line: string): string {
298299
}
299300
}
300301
} else if (ch === '~') {
301-
tildeCount++
302+
if (i + 1 < len && line[i + 1] === '~') {
303+
doubleTildeCount++
304+
i++ // Skip second tilde since we counted the pair
305+
} else {
306+
singleTildeCount++
307+
}
302308
} else if (ch === '`') {
303309
backtickCount++
304310
} else if (ch === '$' && prevCh !== '\\') {
@@ -489,19 +495,15 @@ function closeInlineMarkersLinear(line: string): string {
489495
}
490496
}
491497

492-
// Check ~~ (strikethrough)
493-
if (!closingSuffix && tildeCount >= 2) {
494-
const remainder = tildeCount % 4
495-
if (remainder === 2) {
496-
// Two tildes unclosed, close with ~~
497-
closingSuffix = '~~'
498-
if (hasTrailingSpace) shouldTrim = true
499-
} else if (remainder > 2 && remainder < 4) {
500-
// Partial marker like ~~text~ (3 tildes), need 1 more
501-
const needed = 4 - remainder
502-
closingSuffix = '~'.repeat(needed)
503-
if (hasTrailingSpace) shouldTrim = true
504-
}
498+
// Check ~~ (strikethrough) and ~ (single-tilde) separately so that paired
499+
// singles like ~Hello~ are left alone while ~~text and ~Hello both close.
500+
if (!closingSuffix && doubleTildeCount % 2 === 1) {
501+
// A trailing single ~ after an open ~~ is a partial closer (~~text~)
502+
closingSuffix = singleTildeCount === 1 ? '~' : '~~'
503+
if (hasTrailingSpace) shouldTrim = true
504+
} else if (!closingSuffix && singleTildeCount % 2 === 1) {
505+
closingSuffix = '~'
506+
if (hasTrailingSpace) shouldTrim = true
505507
}
506508

507509
// Check ` (code)

packages/comark/test/auto-close.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ Some text with **bold → Some text with **bold**
2121
~~strikethrough text → ~~strikethrough text~~
2222
**bold** and *italic* and \`code\` → **bold** and *italic* and \`code\`
2323
[text](url → [text](url)
24-
$$formula → $$formula$$`
24+
$$formula → $$formula$$
25+
~Hello → ~Hello~
26+
~~Hello → ~~Hello~~
27+
~Hello~ → ~Hello~
28+
~~Hello~~ → ~~Hello~~`
2529

2630
const multilines = `
2731
| Month | Savings

0 commit comments

Comments
 (0)