Skip to content

Commit

Permalink
feat: allow spaces in variables
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjosephprice committed Dec 5, 2024
1 parent 2fe39ae commit e37096f
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 6 deletions.
8 changes: 8 additions & 0 deletions __tests__/lib/mdast/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import tablesJson from './tables/out.json';
import variablesMdx from './variables/in.mdx?raw';
import variablesJson from './variables/out.json';

import variablesWithSpacesMdx from './variables-with-spaces/in.mdx?raw';
import variablesWithSpacesJson from './variables-with-spaces/out.json';

import inlineImagesMdx from './images/inline/in.mdx?raw';
import inlineImagesJson from './images/inline/out.json';

Expand All @@ -28,6 +31,11 @@ describe('mdast transformer', async () => {
expect(mdast(variablesMdx)).toStrictEqualExceptPosition(variablesJson);
});

it.only('parses variables with spaces', () => {
// @ts-ignore
expect(mdast(variablesWithSpacesMdx)).toStrictEqualExceptPosition(variablesWithSpacesJson);
});

it('parses inline images', () => {
// @ts-ignore
expect(mdast(inlineImagesMdx)).toStrictEqualExceptPosition(inlineImagesJson);
Expand Down
1 change: 1 addition & 0 deletions __tests__/lib/mdast/variables-with-spaces/in.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, { user['this is cursed'] }
72 changes: 72 additions & 0 deletions __tests__/lib/mdast/variables-with-spaces/out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"children": [
{
"children": [
{
"position": {
"end": {
"column": 8,
"line": 1,
"offset": 7
},
"start": {
"column": 1,
"line": 1,
"offset": 0
}
},
"type": "text",
"value": "Hello, "
},
{
"data": {
"hName": "Variable",
"hProperties": {
"name": "this is cursed"
}
},
"position": {
"end": {
"column": 19,
"line": 1,
"offset": 18
},
"start": {
"column": 8,
"line": 1,
"offset": 7
}
},
"type": "readme-variable",
"value": "{ user['this is cursed'] }"
}
],
"position": {
"end": {
"column": 19,
"line": 1,
"offset": 18
},
"start": {
"column": 1,
"line": 1,
"offset": 0
}
},
"type": "paragraph"
}
],
"position": {
"end": {
"column": 1,
"line": 2,
"offset": 19
},
"start": {
"column": 1,
"line": 1,
"offset": 0
}
},
"type": "root"
}
2 changes: 1 addition & 1 deletion __tests__/lib/mdast/variables/in.mdx
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Hello, { user.name }
Hello, {user.name}
2 changes: 1 addition & 1 deletion __tests__/lib/mdast/variables/out.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
}
},
"type": "readme-variable",
"value": "{ user.name }"
"value": "{user.name}"
}
],
"position": {
Expand Down
18 changes: 14 additions & 4 deletions processor/transform/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ const variables =
visit(tree, (node, index, parent) => {
if (!['mdxFlowExpression', 'mdxTextExpression'].includes(node.type) || !('value' in node)) return;

const match = node.value.match(/^\s*user\.(?<value>\S*)\s*$/);
if (!match) return;
// @ts-expect-error - estree is not defined on our mdx types?!
if (node.data.estree.type !== 'Program') return;
// @ts-expect-error - estree is not defined on our mdx types?!
const [expression] = node.data.estree.body;
if (
!expression ||
expression.type !== 'ExpressionStatement' ||
expression.expression.property?.type !== 'Literal'
)
return;

const name = expression.expression.property.value;

let variable = asMdx
? ({
Expand All @@ -23,7 +33,7 @@ const variables =
{
type: 'mdxJsxAttribute',
name: 'name',
value: match.groups.value,
value: name,
},
],
children: [],
Expand All @@ -34,7 +44,7 @@ const variables =
data: {
hName: 'Variable',
hProperties: {
name: match.groups.value,
name,
},
},
value: `{${node.value}}`,
Expand Down

0 comments on commit e37096f

Please sign in to comment.