Skip to content

Commit

Permalink
fix: undefined check (#962)
Browse files Browse the repository at this point in the history
| [![PR App][icn]][demo] | RM-10062 |
| :--------------------: | :------: |

## 🧰 Changes

Fixes parsing `undefined` attributes.

## 🧬 QA & Testing

- [Broken on production][prod].
- [Working in this PR app][demo].

[demo]: https://markdown-pr-PR_NUMBER.herokuapp.com
[prod]: https://SUBDOMAIN.readme.io
[icn]:
https://user-images.githubusercontent.com/886627/160426047-1bee9488-305a-4145-bb2b-09d8b757d38a.svg
  • Loading branch information
kellyjosephprice authored Sep 6, 2024
1 parent 88636cd commit cc4a53f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 6 additions & 0 deletions __tests__/compilers/images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ describe('image compiler', () => {

expect(mdx(mdast(doc))).toMatch(doc);
});

it('correctly serializes an Image component with an undefined expression attributes back to MDX', () => {
const doc = '<Image border={undefined} />';

expect(mdx(mdast(doc))).toMatch('![]()');
});
});
8 changes: 7 additions & 1 deletion processor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ export const getHPropKeys = <T>(node: Node): any => {
export const getAttrs = <T>(jsx: MdxJsxFlowElement | MdxJsxTextElement): any =>
jsx.attributes.reduce((memo, attr) => {
if ('name' in attr) {
memo[attr.name] = typeof attr.value !== 'string' ? JSON.parse(attr.value.value) : attr.value;
if (typeof attr.value === 'string') {
memo[attr.name] = attr.value;
} else if (attr.value.value !== 'undefined') {
memo[attr.name] = JSON.parse(attr.value.value);
}
}

return memo;
Expand Down Expand Up @@ -157,6 +161,8 @@ export const toAttributes = (object: Record<string, any>, keys: string[] = []):

if (typeof v === 'string') {
value = v;
} else if (typeof v === 'undefined') {
return;
} else {
/* values can be null, undefined, string, or a expression, eg:
*
Expand Down

0 comments on commit cc4a53f

Please sign in to comment.