Skip to content

Commit

Permalink
feat(richtext-lexical): multiline string support for objects in MDX p…
Browse files Browse the repository at this point in the history
…arser (#10208)
  • Loading branch information
AlessioGr authored Dec 27, 2024
1 parent 29c5bcd commit 181fc41
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/richtext-lexical/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@
"bson-objectid": "2.0.4",
"dequal": "2.0.3",
"escape-html": "1.0.3",
"json5": "^2.2.3",
"jsox": "1.2.121",
"lexical": "0.20.0",
"mdast-util-from-markdown": "2.0.2",
"mdast-util-mdx-jsx": "3.1.3",
Expand Down
3 changes: 3 additions & 0 deletions packages/richtext-lexical/src/utilities/jsx/declare.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'jsox' {
export const JSOX: any
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import JSON5Import from 'json5'

const JSON5 = ('default' in JSON5Import ? JSON5Import.default : JSON5Import) as typeof JSON5Import
import { JSOX } from 'jsox'

/**
* Turns a JSX props string into an object.
Expand Down Expand Up @@ -82,7 +80,7 @@ function handleArray(propsString: string, startIndex: number): { newIndex: numbe
i++
}

return { newIndex: i, value: JSON5.parse(`[${value}]`) }
return { newIndex: i, value: JSOX.parse(`[${value}]`) }
}

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

function parseObject(objString: string): Record<string, any> {
if (objString[0] !== '{') {
return JSON5.parse(objString)
return JSOX.parse(objString)
}

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

return result
}
Expand Down
19 changes: 18 additions & 1 deletion packages/richtext-lexical/src/utilities/jsx/jsx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,30 @@ describe('jsx', () => {
},
{
// Test if unquoted property keys in objects within arrays are supprted. This is
// supported through the more lenient json5 parser, instead of using JSON.parse()
// supported through the more lenient JSOX parser, instead of using JSON.parse()
input: 'key={[1, 2, { hello: "there" }]}',
inputFromOutput: 'key={[1, 2, { "hello": "there" }]}',
output: {
key: [1, 2, { hello: 'there' }],
},
},
{
// Test if ` strings work
input: `key={[1, 2, { hello: \`there\` }]}`,
inputFromOutput: 'key={[1, 2, { "hello": "there" }]}',
output: {
key: [1, 2, { hello: 'there' }],
},
},
{
// Test if multiline ` strings work
input: `key={[1, 2, { hello: \`Hello
there\` }]}`,
inputFromOutput: 'key={[1, 2, { "hello": "Hello\\nthere" }]}',
output: {
key: [1, 2, { hello: 'Hello\nthere' }],
},
},
]

for (const { input, output } of INPUT_AND_OUTPUT) {
Expand Down
45 changes: 24 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 181fc41

Please sign in to comment.