Skip to content

Commit 181fc41

Browse files
authored
feat(richtext-lexical): multiline string support for objects in MDX parser (#10208)
1 parent 29c5bcd commit 181fc41

File tree

5 files changed

+50
-29
lines changed

5 files changed

+50
-29
lines changed

packages/richtext-lexical/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@
362362
"bson-objectid": "2.0.4",
363363
"dequal": "2.0.3",
364364
"escape-html": "1.0.3",
365-
"json5": "^2.2.3",
365+
"jsox": "1.2.121",
366366
"lexical": "0.20.0",
367367
"mdast-util-from-markdown": "2.0.2",
368368
"mdast-util-mdx-jsx": "3.1.3",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module 'jsox' {
2+
export const JSOX: any
3+
}

packages/richtext-lexical/src/utilities/jsx/extractPropsFromJSXPropsString.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import JSON5Import from 'json5'
2-
3-
const JSON5 = ('default' in JSON5Import ? JSON5Import.default : JSON5Import) as typeof JSON5Import
1+
import { JSOX } from 'jsox'
42

53
/**
64
* Turns a JSX props string into an object.
@@ -82,7 +80,7 @@ function handleArray(propsString: string, startIndex: number): { newIndex: numbe
8280
i++
8381
}
8482

85-
return { newIndex: i, value: JSON5.parse(`[${value}]`) }
83+
return { newIndex: i, value: JSOX.parse(`[${value}]`) }
8684
}
8785

8886
function handleQuotedString(
@@ -120,10 +118,10 @@ function handleObject(propsString: string, startIndex: number): { newIndex: numb
120118

121119
function parseObject(objString: string): Record<string, any> {
122120
if (objString[0] !== '{') {
123-
return JSON5.parse(objString)
121+
return JSOX.parse(objString)
124122
}
125123

126-
const result = JSON5.parse(objString.replace(/(\w+):/g, '"$1":'))
124+
const result = JSOX.parse(objString.replace(/(\w+):/g, '"$1":'))
127125

128126
return result
129127
}

packages/richtext-lexical/src/utilities/jsx/jsx.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,30 @@ describe('jsx', () => {
118118
},
119119
{
120120
// Test if unquoted property keys in objects within arrays are supprted. This is
121-
// supported through the more lenient json5 parser, instead of using JSON.parse()
121+
// supported through the more lenient JSOX parser, instead of using JSON.parse()
122122
input: 'key={[1, 2, { hello: "there" }]}',
123123
inputFromOutput: 'key={[1, 2, { "hello": "there" }]}',
124124
output: {
125125
key: [1, 2, { hello: 'there' }],
126126
},
127127
},
128+
{
129+
// Test if ` strings work
130+
input: `key={[1, 2, { hello: \`there\` }]}`,
131+
inputFromOutput: 'key={[1, 2, { "hello": "there" }]}',
132+
output: {
133+
key: [1, 2, { hello: 'there' }],
134+
},
135+
},
136+
{
137+
// Test if multiline ` strings work
138+
input: `key={[1, 2, { hello: \`Hello
139+
there\` }]}`,
140+
inputFromOutput: 'key={[1, 2, { "hello": "Hello\\nthere" }]}',
141+
output: {
142+
key: [1, 2, { hello: 'Hello\nthere' }],
143+
},
144+
},
128145
]
129146

130147
for (const { input, output } of INPUT_AND_OUTPUT) {

pnpm-lock.yaml

Lines changed: 24 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)