Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdoc formatter - update to skip printing undefined attributes #432

Merged
merged 5 commits into from
Aug 9, 2023

Conversation

matv-stripe
Copy link
Contributor

See test - the Markdoc parser does not support undefined passed into as an attribute. When generating Markdoc this can cause issues because the data source might have undefined values.

Let me know thoughts around this, I am fine with not merging it.

r? @mfix-stripe

@matv-stripe matv-stripe changed the title Markdoc formatter - update to skip over undefined values Markdoc formatter - update to skip printing undefined attributes Aug 9, 2023
@rpaul-stripe
Copy link
Contributor

I want to let @mfix-stripe weigh in before merging, but this looks good to me.

Copy link
Contributor

@mfix-stripe mfix-stripe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/formatter.ts Outdated
if (Ast.isAst(v)) {
return format(v);
}
if (v === null) {
return 'null';
}
if (v === undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just move this to the top of this function.

src/formatter.ts Outdated
Comment on lines 78 to 84
if (formattedValue !== undefined) {
return `${a.name}=${formattedValue}`;
} else {
// The Markdoc parser does not support undefined attribute
// values. Filter those values out.
return undefined;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (formattedValue !== undefined) {
return `${a.name}=${formattedValue}`;
} else {
// The Markdoc parser does not support undefined attribute
// values. Filter those values out.
return undefined;
}
if (formattedValue !== undefined) {
return `${a.name}=${formattedValue}`;
}
// The Markdoc parser does not support undefined attribute
// values. Filter those values out.
return undefined;

src/formatter.ts Outdated
Comment on lines 273 to 274
const attributes = [...formatAttributes(n)];
const tag = [open + n.tag, ...attributes.filter((v) => v !== undefined)];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const attributes = [...formatAttributes(n)];
const tag = [open + n.tag, ...attributes.filter((v) => v !== undefined)];
const attributes = [...formatAttributes(n)].filter((v) => v !== undefined);
const tag = [open + n.tag, ...attributes];

@matv-stripe
Copy link
Contributor Author

ptal @mfix-stripe

Copy link
Contributor

@mfix-stripe mfix-stripe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PTAL @matv-stripe — one more thing, sorry!

src/formatter.ts Outdated
Comment on lines 78 to 83
if (formattedValue !== undefined) {
return `${a.name}=${formattedValue}`;
}
// The Markdoc parser does not support undefined attribute
// values. Filter those values out.
return undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, I just realized. Let's just move if (formattedValue === undefined) return undefined to the first branch of this function, and leave return ${a.name}=${formattedValue}; as the fallback.

@matv-stripe
Copy link
Contributor Author

ty for the feedback! ptal @mfix-stripe

Copy link
Contributor

@mfix-stripe mfix-stripe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TY!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants