Skip to content

Commit b1f44a2

Browse files
committed
add test
1 parent 678a15c commit b1f44a2

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

src/generators/legacy-json/utils/__tests__/parseList.test.mjs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,50 @@ describe('parseList', () => {
123123
parseList(section, nodes);
124124
assert.ok(Array.isArray(section.params));
125125
});
126+
127+
it('processes recursive lists', () => {
128+
const section = { type: 'event' };
129+
const nodes = [
130+
{
131+
type: 'list',
132+
children: [
133+
{
134+
children: [
135+
{
136+
type: 'paragraph',
137+
children: [
138+
{ type: 'text', value: 'param1 {string} first parameter' },
139+
],
140+
},
141+
// This is a nested typed list
142+
{
143+
type: 'list',
144+
children: [
145+
{
146+
children: [
147+
{
148+
type: 'paragraph',
149+
children: [
150+
{ type: 'inlineCode', value: 'option' }, // inline code
151+
{ type: 'text', value: ' ' }, // space
152+
{
153+
type: 'link',
154+
children: [{ type: 'text', value: '<boolean>' }], // link with < value
155+
},
156+
{ type: 'text', value: ' option description' },
157+
],
158+
},
159+
],
160+
},
161+
],
162+
},
163+
],
164+
},
165+
],
166+
},
167+
];
168+
169+
parseList(section, nodes);
170+
assert.equal(section.params[0].options.length, 1);
171+
});
126172
});

src/generators/legacy-json/utils/parseList.mjs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,15 @@ export const extractPattern = (text, pattern, key, current) => {
4242
* @param {import('@types/mdast').List} list
4343
*/
4444
export const isTypedList = list => {
45+
if (list.type !== 'list') {
46+
// Exit early if not a list
47+
return false;
48+
}
49+
4550
const children = list?.children?.[0]?.children?.[0]?.children;
4651

47-
// The first element must be a code block
4852
return (
53+
// The first element must be a code block
4954
children?.[0]?.type === 'inlineCode' &&
5055
// Followed by a space
5156
children?.[1]?.value.trim() === '' &&
@@ -65,9 +70,11 @@ export const isTypedList = list => {
6570
export function parseListItem(child) {
6671
const current = {};
6772

73+
const subList = child.children.find(isTypedList);
74+
6875
// Extract and clean raw text from the node, excluding nested lists
6976
current.textRaw = transformTypeReferences(
70-
transformNodesToString(child.children.filter(node => node.type !== 'list'))
77+
transformNodesToString(child.children.filter(node => node !== subList))
7178
.replace(/\s+/g, ' ')
7279
.replace(/<!--.*?-->/gs, '')
7380
);
@@ -89,9 +96,8 @@ export function parseListItem(child) {
8996
current.desc = text.replace(LEADING_HYPHEN, '').trim() || undefined;
9097

9198
// Parse nested lists (options) recursively if present
92-
const optionsNode = child.children.find(node => node.type === 'list');
93-
if (isTypedList(optionsNode)) {
94-
current.options = optionsNode.children.map(parseListItem);
99+
if (subList) {
100+
current.options = subList.children.map(parseListItem);
95101
}
96102

97103
return current;

0 commit comments

Comments
 (0)