Skip to content

Commit 5adaf4b

Browse files
committed
fix: handle newlines inside HTML tag brackets
Closes #540
1 parent 9364093 commit 5adaf4b

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

.changeset/fast-months-play.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"markdown-to-jsx": patch
3+
---
4+
5+
Handle newlines inside of HTML tags themselves (not just nested children.)

index.compiler.spec.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3213,6 +3213,7 @@ comment -->`)
32133213
</span>
32143214
`)
32153215
})
3216+
32163217
it('should not fail with lots of \\n in the middle of the text', () => {
32173218
render(
32183219
compiler(
@@ -3362,6 +3363,36 @@ print("hello world")
33623363
</span>
33633364
`)
33643365
})
3366+
3367+
it('#540 multiline attributes are supported', () => {
3368+
render(
3369+
compiler(
3370+
`<p>
3371+
Item detail
3372+
<span
3373+
style="
3374+
color: #fddb67;
3375+
font-size: 11px;
3376+
font-style: normal;
3377+
font-weight: 500;
3378+
line-height: 18px;
3379+
text-decoration-line: underline;
3380+
"
3381+
>debug item 1</span
3382+
>
3383+
</p>`
3384+
)
3385+
)
3386+
3387+
expect(root.innerHTML).toMatchInlineSnapshot(`
3388+
<p>
3389+
Item detail
3390+
<span style="color: rgb(253, 219, 103); font-size: 11px; font-style: normal; font-weight: 500; line-height: 18px; text-decoration-line: underline;">
3391+
debug item 1
3392+
</span>
3393+
</p>
3394+
`)
3395+
})
33653396
})
33663397

33673398
describe('horizontal rules', () => {

index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,7 @@ export function compiler(
14561456
order: Priority.HIGH,
14571457
parse(capture, parse, state) {
14581458
const [, whitespace] = capture[3].match(HTML_LEFT_TRIM_AMOUNT_R)
1459+
14591460
const trimmer = new RegExp(`^${whitespace}`, 'gm')
14601461
const trimmed = capture[3].replace(trimmer, '')
14611462

@@ -1470,7 +1471,7 @@ export function compiler(
14701471
const ast = {
14711472
attrs: attrStringToMap(capture[2]),
14721473
noInnerParse: noInnerParse,
1473-
tag: noInnerParse ? tagName : capture[1],
1474+
tag: (noInnerParse ? tagName : capture[1]).trim(),
14741475
} as {
14751476
attrs: ReturnType<typeof attrStringToMap>
14761477
children?: ReturnType<MarkdownToJSX.NestedParser> | undefined
@@ -1513,7 +1514,7 @@ export function compiler(
15131514
parse(capture /*, parse, state*/) {
15141515
return {
15151516
attrs: attrStringToMap(capture[2] || ''),
1516-
tag: capture[1],
1517+
tag: capture[1].trim(),
15171518
}
15181519
},
15191520
render(node, output, state) {

0 commit comments

Comments
 (0)