Skip to content

Commit cb94a3f

Browse files
quantizorInnei
authored andcommitted
fix: handle newlines inside HTML tag brackets (quantizor#557)
* refactor: trim style keys * fix: handle newlines inside HTML tag brackets Closes quantizor#540 Signed-off-by: Innei <[email protected]>
1 parent e2cc34c commit cb94a3f

File tree

3 files changed

+102
-5
lines changed

3 files changed

+102
-5
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: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2981,6 +2981,7 @@ comment -->`)
29812981
</span>
29822982
`)
29832983
})
2984+
29842985
it('should not fail with lots of \\n in the middle of the text', () => {
29852986
render(
29862987
compiler(
@@ -3069,6 +3070,97 @@ print("hello world")
30693070
</div>
30703071
`)
30713072
})
3073+
3074+
it('#444 switching list formats regression test', () => {
3075+
render(
3076+
compiler(
3077+
`
3078+
1. One
3079+
2. Two
3080+
3. Three
3081+
3082+
* Red
3083+
* Green
3084+
* Blue
3085+
`
3086+
)
3087+
)
3088+
3089+
expect(root.innerHTML).toMatchInlineSnapshot(`
3090+
<div>
3091+
<ol start="1">
3092+
<li>
3093+
One
3094+
</li>
3095+
<li>
3096+
Two
3097+
</li>
3098+
<li>
3099+
Three
3100+
</li>
3101+
</ol>
3102+
<ul>
3103+
<li>
3104+
Red
3105+
</li>
3106+
<li>
3107+
Green
3108+
</li>
3109+
<li>
3110+
Blue
3111+
</li>
3112+
</ul>
3113+
</div>
3114+
`)
3115+
})
3116+
3117+
it('#466 list-like syntax inside link regression test', () => {
3118+
render(
3119+
compiler(
3120+
'Hello, I think that [6. Markdown](http://daringfireball.net/projects/markdown/) lets you write content in a really natural way.'
3121+
)
3122+
)
3123+
3124+
expect(root.innerHTML).toMatchInlineSnapshot(`
3125+
<span>
3126+
Hello, I think that
3127+
<a href="http://daringfireball.net/projects/markdown/">
3128+
6. Markdown
3129+
</a>
3130+
lets you write content in a really natural way.
3131+
</span>
3132+
`)
3133+
})
3134+
3135+
it('#540 multiline attributes are supported', () => {
3136+
render(
3137+
compiler(
3138+
`<p>
3139+
Item detail
3140+
<span
3141+
style="
3142+
color: #fddb67;
3143+
font-size: 11px;
3144+
font-style: normal;
3145+
font-weight: 500;
3146+
line-height: 18px;
3147+
text-decoration-line: underline;
3148+
"
3149+
>debug item 1</span
3150+
>
3151+
</p>`
3152+
)
3153+
)
3154+
3155+
expect(root.innerHTML).toMatchInlineSnapshot(`
3156+
<p>
3157+
Item detail
3158+
<span style="color: rgb(253, 219, 103); font-size: 11px; font-style: normal; font-weight: 500; line-height: 18px; text-decoration-line: underline;">
3159+
debug item 1
3160+
</span>
3161+
</p>
3162+
`)
3163+
})
30723164
})
30733165

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

index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ const HEADING_SETEXT_R = /^([^\n]+)\n *(=|-){3,} *(?:\n *)+\n/
416416
* ([^ >/]+)
417417
*
418418
* 3. Ignore a space after the starting tag and capture the attribute portion of the tag (capture 2)
419-
* ?([^>]*)\/{0}>
419+
* ?([^>]*)>
420420
*
421421
* 4. Ensure a matching closing tag is present in the rest of the input string
422422
* (?=[\s\S]*<\/\1>)
@@ -429,7 +429,7 @@ const HEADING_SETEXT_R = /^([^\n]+)\n *(=|-){3,} *(?:\n *)+\n/
429429
* \n*
430430
*/
431431
const HTML_BLOCK_ELEMENT_R =
432-
/^ *(?!<[a-z][^ >/]* ?\/>)<([a-z][^ >/]*) ?([^>]*)\/{0}>\n?(\s*(?:<\1[^>]*?>[\s\S]*?<\/\1>|(?!<\1)[\s\S])*?)<\/\1>\n*/i
432+
/^ *(?!<[a-z][^ >/]* ?\/>)<([a-z][^ >/]*) ?([^>]*)>\n?(\s*(?:<\1[^>]*?>[\s\S]*?<\/\1>|(?!<\1)[\s\S])*?)<\/\1>\n*/i
433433

434434
const HTML_CHAR_CODE_R = /&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-fA-F]{1,6});/gi
435435

@@ -865,9 +865,9 @@ function attributeValueToJSXPropValue(key: string, value: string): any {
865865

866866
// snake-case to camelCase
867867
// also handles PascalCasing vendor prefixes
868-
const camelCasedKey = key.replace(/(-[a-z])/g, substr =>
869-
substr[1].toUpperCase()
870-
)
868+
const camelCasedKey = key
869+
.trim()
870+
.replace(/(-[a-z])/g, substr => substr[1].toUpperCase())
871871

872872
// key.length + 1 to skip over the colon
873873
styles[camelCasedKey] = kvPair.slice(key.length + 1).trim()

0 commit comments

Comments
 (0)