Skip to content

Commit 2d9e570

Browse files
committed
replace trimend with more compatible variant
1 parent cf7693c commit 2d9e570

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

index.compiler.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ describe('inline textual elements', () => {
480480

481481
render(
482482
compiler(
483-
'*This should not misinterpret the asterisk ~~*~~ in the strikethrough.*'
483+
String.raw`*This should not misinterpret the asterisk ~~\*~~ in the strikethrough.*`
484484
)
485485
)
486486

@@ -512,7 +512,7 @@ describe('inline textual elements', () => {
512512

513513
render(
514514
compiler(
515-
'_This should not misinterpret the under_score that forms part of a word._'
515+
`_This should not misinterpret the under\\_score that forms part of a word._`
516516
)
517517
)
518518

index.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ const TABLE_RIGHT_ALIGN = /^ *-+: *$/
296296
* and therefore miss content that should have been included.
297297
*/
298298
const INLINE_SKIP_R =
299-
'((?:\\[.*?\\][([].*?[)\\]]|<.*?>(?:.*?<.*?>)?|`.*?`|~~.*?~~|==.*?==|.|\\n)*?)'
299+
'((?:\\[.*?\\][([].*?[)\\]]|<.*?>(?:.*?<.*?>)?|`.*?`|\\\\\\1|[\\s\\S])+?)'
300300

301301
/**
302302
* Detect a sequence like **foo** or __foo__. Note that bold has a higher priority
@@ -307,17 +307,17 @@ const TEXT_BOLD_R = new RegExp(`^([*_])\\1${INLINE_SKIP_R}\\1\\1(?!\\1)`)
307307
/**
308308
* Detect a sequence like *foo* or _foo_.
309309
*/
310-
const TEXT_EMPHASIZED_R = new RegExp(`^([*_])${INLINE_SKIP_R}\\1(?!\\1|\\w)`)
310+
const TEXT_EMPHASIZED_R = new RegExp(`^([*_])${INLINE_SKIP_R}\\1(?!\\1)`)
311311

312312
/**
313313
* Detect a sequence like ==foo==.
314314
*/
315-
const TEXT_MARKED_R = new RegExp(`^==${INLINE_SKIP_R}==`)
315+
const TEXT_MARKED_R = new RegExp(`^(==)${INLINE_SKIP_R}\\1`)
316316

317317
/**
318318
* Detect a sequence like ~~foo~~.
319319
*/
320-
const TEXT_STRIKETHROUGHED_R = new RegExp(`^~~${INLINE_SKIP_R}~~`)
320+
const TEXT_STRIKETHROUGHED_R = new RegExp(`^(~~)${INLINE_SKIP_R}\\1`)
321321

322322
const TEXT_ESCAPED_R = /^\\([^0-9A-Za-z\s])/
323323
const TEXT_UNESCAPE_R = /\\([^0-9A-Za-z\s])/g
@@ -575,6 +575,10 @@ const BLOCK_SYNTAXES = [
575575
HTML_SELF_CLOSING_ELEMENT_R,
576576
]
577577

578+
function trimEnd(str: string) {
579+
return str.replace(/\s*$/, '')
580+
}
581+
578582
function containsBlockSyntax(input: string) {
579583
return BLOCK_SYNTAXES.some(r => r.test(input))
580584
}
@@ -979,12 +983,14 @@ function matchParagraph(source: string, state: MarkdownToJSX.State) {
979983
return !!line.trim()
980984
})
981985

982-
const captured = match.trimEnd()
986+
const captured = trimEnd(match)
983987
if (captured == '') {
984988
return null
985989
}
986990

987-
return [match, captured]
991+
// parseCaptureInline expects the inner content to be at index 2
992+
// because index 1 is the delimiter for text formatting syntaxes
993+
return [match, , captured]
988994
}
989995

990996
export function sanitizer(url: string): string {
@@ -1074,7 +1080,7 @@ const parseCaptureInline: MarkdownToJSX.Parser<{
10741080
children: MarkdownToJSX.ParserResult[]
10751081
}> = (capture, parse, state: MarkdownToJSX.State) => {
10761082
return {
1077-
children: parseInline(parse, capture[1], state),
1083+
children: parseInline(parse, capture[2], state),
10781084
}
10791085
}
10801086

@@ -1225,7 +1231,7 @@ export function compiler(
12251231
parser(
12261232
inline
12271233
? input
1228-
: `${input.trimEnd().replace(TRIM_STARTING_NEWLINES, '')}\n\n`,
1234+
: `${trimEnd(input).replace(TRIM_STARTING_NEWLINES, '')}\n\n`,
12291235
{
12301236
inline,
12311237
}

0 commit comments

Comments
 (0)