Skip to content

Commit 009d370

Browse files
fix(docs): escape backticks in type or default value columns (#6025)
1 parent 8ff3048 commit 009d370

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

src/compiler/docs/readme/markdown-props.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export const propsToMarkdown = (props: d.JsonDocsProp[]) => {
1919
getPropertyField(prop),
2020
getAttributeField(prop),
2121
getDocsField(prop),
22-
`\`${prop.type}\``,
23-
`\`${prop.default}\``,
22+
getTypeField(prop),
23+
getDefaultValueField(prop),
2424
]);
2525
});
2626

@@ -46,3 +46,11 @@ const getDocsField = (prop: d.JsonDocsProp) => {
4646
: ''
4747
}${prop.docs}`;
4848
};
49+
50+
const getTypeField = (prop: d.JsonDocsProp) => {
51+
return prop.type.includes('`') ? `\`\` ${prop.type} \`\`` : `\`${prop.type}\``;
52+
};
53+
54+
const getDefaultValueField = (prop: d.JsonDocsProp) => {
55+
return prop.default?.includes('`') ? `\`\` ${prop.default} \`\`` : `\`${prop.default}\``;
56+
};

src/compiler/docs/test/markdown-props.spec.ts

+78
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,84 @@ describe('markdown props', () => {
3737
| \`hello\` | \`hello\` | This is a prop | \`boolean \\| string\` | \`false\` |
3838
| \`hello\` | -- | This is a prop | \`boolean \\| string\` | \`false\` |
3939
40+
`);
41+
});
42+
43+
it('escapes template literal types', () => {
44+
const markdown = propsToMarkdown([
45+
{
46+
name: 'width',
47+
attr: 'width',
48+
docs: 'Width of the button',
49+
default: 'undefined',
50+
type: '`${number}px` | `${number}%`',
51+
mutable: false,
52+
optional: false,
53+
required: false,
54+
reflectToAttr: false,
55+
docsTags: [],
56+
values: [],
57+
},
58+
]).join('\n');
59+
60+
expect(markdown).toEqual(`## Properties
61+
62+
| Property | Attribute | Description | Type | Default |
63+
| -------- | --------- | ------------------- | ----------------------------------- | ----------- |
64+
| \`width\` | \`width\` | Width of the button | \`\` \`\${number}px\` \\| \`\${number}%\` \`\` | \`undefined\` |
65+
66+
`);
67+
});
68+
69+
it('escapes backticks in default value', () => {
70+
const markdown = propsToMarkdown([
71+
{
72+
name: 'quote',
73+
attr: 'quote',
74+
docs: 'Quote character',
75+
default: "'`'",
76+
type: 'string',
77+
mutable: false,
78+
optional: false,
79+
required: false,
80+
reflectToAttr: false,
81+
docsTags: [],
82+
values: [],
83+
},
84+
]).join('\n');
85+
86+
expect(markdown).toEqual(`## Properties
87+
88+
| Property | Attribute | Description | Type | Default |
89+
| -------- | --------- | --------------- | -------- | --------- |
90+
| \`quote\` | \`quote\` | Quote character | \`string\` | \`\` '\`' \`\` |
91+
92+
`);
93+
});
94+
95+
it('outputs `undefined` in default column when `prop.default` is undefined', () => {
96+
const markdown = propsToMarkdown([
97+
{
98+
name: 'first',
99+
attr: 'first',
100+
docs: 'First name',
101+
default: undefined,
102+
type: 'string',
103+
mutable: false,
104+
optional: false,
105+
required: false,
106+
reflectToAttr: false,
107+
docsTags: [],
108+
values: [],
109+
},
110+
]).join('\n');
111+
112+
expect(markdown).toBe(`## Properties
113+
114+
| Property | Attribute | Description | Type | Default |
115+
| -------- | --------- | ----------- | -------- | ----------- |
116+
| \`first\` | \`first\` | First name | \`string\` | \`undefined\` |
117+
40118
`);
41119
});
42120
});

0 commit comments

Comments
 (0)