Skip to content

Commit

Permalink
Fix virtual code when JSX child starts with > (#448)
Browse files Browse the repository at this point in the history
This fixes an offset issue if the child of a JSX element in the MDX
starts with a `>` character. Typically these are blockquotes.

Refs https://github.com/orgs/mdx-js/discussions/2489
  • Loading branch information
remcohaszing authored Jun 5, 2024
1 parent f600a05 commit 52e38fa
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .changeset/wild-kangaroos-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@mdx-js/language-service': patch
'@mdx-js/language-server': patch
'@mdx-js/typescript-plugin': patch
'vscode-mdx': patch
---

Fix virtual code if a child of a JSX node starts with a `>` character.
3 changes: 2 additions & 1 deletion packages/language-service/lib/virtual-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,8 @@ function getEmbeddedCodes(mdx, ast, checkMdx, jsxImportSource) {
case 'mdxJsxFlowElement':
case 'mdxJsxTextElement': {
if (node.children.length > 0) {
end = mdx.lastIndexOf('>', getNodeStartOffset(node.children[0])) + 1
end =
mdx.lastIndexOf('>', getNodeStartOffset(node.children[0]) - 1) + 1
}

updateMarkdownFromOffsets(start, end)
Expand Down
121 changes: 121 additions & 0 deletions packages/language-service/test/language-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,127 @@ test('create virtual code w/ mdxJsxFlowElement w/ children', () => {
])
})

test('create virtual code w/ mdxJsxFlowElement w/ blockquote child', () => {
const plugin = createMdxLanguagePlugin()

const snapshot = snapshotFromLines('<div>', '>', '</div>', '<div>></div>', '')

const code = plugin.createVirtualCode?.('/test.mdx', 'mdx', snapshot)

assert.ok(code instanceof VirtualMdxCode)
assert.equal(code.id, 'mdx')
assert.equal(code.languageId, 'mdx')
assert.ifError(code.error)
assert.equal(code.snapshot, snapshot)
assert.deepEqual(code.mappings, [
{
sourceOffsets: [0],
generatedOffsets: [0],
lengths: [snapshot.getLength()],
data: {
completion: true,
format: true,
navigation: true,
semantic: true,
structure: true,
verification: true
}
}
])
assert.deepEqual(code.embeddedCodes, [
{
id: 'jsx',
languageId: 'javascriptreact',
mappings: [
{
sourceOffsets: [0, 8, 15, 21],
generatedOffsets: [779, 804, 822, 841],
lengths: [5, 6, 5, 6],
data: {
completion: true,
format: false,
navigation: true,
semantic: true,
structure: true,
verification: true
}
}
],
snapshot: snapshotFromLines(
'/* @jsxRuntime automatic',
'@jsxImportSource react */',
'',
'/**',
' * @internal',
' * **Do not use.** This function is generated by MDX for internal use.',
' *',
' * @param {{readonly [K in keyof MDXContentProps]: MDXContentProps[K]}} props',
' * The [props](https://mdxjs.com/docs/using-mdx/#props) that have been passed to the MDX component.',
' */',
'function _createMdxContent(props) {',
' /**',
' * @internal',
' * **Do not use.** This variable is generated by MDX for internal use.',
' */',
' const _components = {',
' // @ts-ignore',
' .../** @type {0 extends 1 & MDXProvidedComponents ? {} : MDXProvidedComponents} */ ({}),',
' ...props.components,',
' /** The [props](https://mdxjs.com/docs/using-mdx/#props) that have been passed to the MDX component. */',
' props',
' }',
' _components',
' return <>',
' <div>',
' <>',
' </>',
' </div>',
' <>',
' <div>',
" {''}",
' </div>',
' </>',
' </>',
'}',
'',
'/**',
' * Render the MDX contents.',
' *',
' * @param {{readonly [K in keyof MDXContentProps]: MDXContentProps[K]}} props',
' * The [props](https://mdxjs.com/docs/using-mdx/#props) that have been passed to the MDX component.',
' */',
'export default function MDXContent(props) {',
' return <_createMdxContent {...props} />',
'}',
'',
'// @ts-ignore',
'/** @typedef {(0 extends 1 & Props ? {} : Props) & {components?: {}}} MDXContentProps */',
''
)
},
{
id: 'md',
languageId: 'markdown',
mappings: [
{
sourceOffsets: [5, 14, 20, 27],
generatedOffsets: [0, 10, 18, 26],
lengths: [3, 1, 1, 1],
data: {
completion: true,
format: false,
navigation: true,
semantic: true,
structure: true,
verification: true
}
}
],
snapshot: snapshotFromLines('', '>', '<!---->', '<!---->><!---->', '')
}
])
})

test('create virtual code w/ mdxJsxFlowElement w/o children', () => {
const plugin = createMdxLanguagePlugin()

Expand Down

0 comments on commit 52e38fa

Please sign in to comment.