Skip to content

Commit 2c7fe53

Browse files
committed
feat: splitted generateDocs and changed the output
Splitted the `generateDocs` function in `extract` and `organise` functions. The `extract` function extracts comments from the code and it cleans them up. The `organise` function returns all the sections and the names of the root sections. Every returned section has a `subsections` property which lists the names of the subsections. BREAKING CHANGE
1 parent 6f221b8 commit 2c7fe53

File tree

4 files changed

+143
-161
lines changed

4 files changed

+143
-161
lines changed

README.md

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $ yarn add css-docs-generator
1818
## Usage
1919

2020
```javascript
21-
import {generateDocs} from 'css-docs-generator';
21+
import {extract, organise} from 'css-docs-generator';
2222

2323
const code = `
2424
/**
@@ -50,46 +50,47 @@ const code = `
5050
*/
5151
`;
5252

53-
const docs = generateDocs(code);
53+
const docs = organise(extract(code));
5454
console.log(docs);
5555

5656
// Will output:
57-
// [
58-
// {
59-
// name: 'Root section.',
60-
// description: null,
61-
// classes: [],
62-
// markup: null,
63-
// subsections: [
64-
// {
65-
// name: 'Foo',
66-
// description: null,
67-
// classes: [],
68-
// markup: null,
69-
// subsections: [
70-
// {
71-
// name: 'Bar',
72-
// description: 'Bar component.',
73-
// classes: [
74-
// {
75-
// name: '.bar',
76-
// description: 'Bar.',
77-
// markup: '<div class="bar">Bar</div>'
78-
// }
79-
// ],
80-
// markup: '<div class="{{modifier}}">Bar</div>',
81-
// subsections: []
82-
// }
83-
// ]
84-
// }
85-
// ]
86-
// },
87-
// {
88-
// name: 'Another root section.',
89-
// description: null,
90-
// classes: [],
91-
// markup: null,
92-
// subsections: []
93-
// }
94-
// ];
57+
// {
58+
// roots: ['Root section.', 'Another root section.'],
59+
// sections: [
60+
// {
61+
// name: 'Root section.',
62+
// description: null,
63+
// classes: [],
64+
// markup: null,
65+
// subsections: ['Foo']
66+
// },
67+
// {
68+
// name: 'Foo',
69+
// description: null,
70+
// classes: [],
71+
// markup: null,
72+
// subsections: ['Bar']
73+
// },
74+
// {
75+
// name: 'Bar',
76+
// description: 'Bar component.',
77+
// classes: [
78+
// {
79+
// name: '.bar',
80+
// description: 'Bar.',
81+
// markup: '<div class="bar">Bar</div>'
82+
// }
83+
// ],
84+
// markup: '<div class="{{modifier}}">Bar</div>',
85+
// subsections: []
86+
// },
87+
// {
88+
// name: 'Another root section.',
89+
// description: null,
90+
// classes: [],
91+
// markup: null,
92+
// subsections: []
93+
// }
94+
// ]
95+
// }
9596
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "css-docs-generator",
3-
"version": "1.0.2",
3+
"version": "2.0.0",
44
"description": "Generate docs from CSS comments.",
55
"main": "lib/index.min.js",
66
"scripts": {

src/__tests__/index-test.js

Lines changed: 82 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { generateDocs } from '../index';
1+
import { extract, organise } from '../index';
22

33
describe('index', () => {
44
const noTags = `
@@ -17,7 +17,7 @@ describe('index', () => {
1717
}
1818
`;
1919

20-
describe('single comment', () => {
20+
describe('extract()', () => {
2121
it('returns empty array if the section name is not defined', () => {
2222
const code = `
2323
/**
@@ -33,10 +33,10 @@ describe('index', () => {
3333
background-color: blue;
3434
}
3535
`;
36-
expect(generateDocs(code)).toEqual([]);
36+
expect(extract(code)).toEqual([]);
3737
});
3838

39-
it('returns the section data', () => {
39+
it('returns the extracted comment', () => {
4040
const code = `
4141
/**
4242
* @name Button
@@ -69,14 +69,14 @@ describe('index', () => {
6969
}
7070
],
7171
markup: '<div class="{{modifier}}">Button</div>',
72-
subsections: []
72+
section: null
7373
}
7474
];
7575

76-
expect(generateDocs(code)).toEqual(expected);
76+
expect(extract(code)).toEqual(expected);
7777
});
7878

79-
it('returns the section data without description', () => {
79+
it('returns the comment without description', () => {
8080
const code = `
8181
/**
8282
* @name Button
@@ -108,14 +108,14 @@ describe('index', () => {
108108
}
109109
],
110110
markup: '<div class="{{modifier}}">Button</div>',
111-
subsections: []
111+
section: null
112112
}
113113
];
114114

115-
expect(generateDocs(code)).toEqual(expected);
115+
expect(extract(code)).toEqual(expected);
116116
});
117117

118-
it('returns the section data without class descriptions', () => {
118+
it('returns the comment without class descriptions', () => {
119119
const code = `
120120
/**
121121
* @name Button
@@ -148,14 +148,14 @@ describe('index', () => {
148148
}
149149
],
150150
markup: '<div class="{{modifier}}">Button</div>',
151-
subsections: []
151+
section: null
152152
}
153153
];
154154

155-
expect(generateDocs(code)).toEqual(expected);
155+
expect(extract(code)).toEqual(expected);
156156
});
157157

158-
it('returns the section data without classes', () => {
158+
it('returns the comment without classes', () => {
159159
const code = `
160160
/**
161161
* @name Button
@@ -174,14 +174,14 @@ describe('index', () => {
174174
description: 'Button component.',
175175
classes: [],
176176
markup: '<div class="{{modifier}}">Button</div>',
177-
subsections: []
177+
section: null
178178
}
179179
];
180180

181-
expect(generateDocs(code)).toEqual(expected);
181+
expect(extract(code)).toEqual(expected);
182182
});
183183

184-
it('returns the section data without markup', () => {
184+
it('returns the comment without markup', () => {
185185
const code = `
186186
/**
187187
* @name Button
@@ -211,34 +211,16 @@ describe('index', () => {
211211
}
212212
],
213213
markup: null,
214-
subsections: []
214+
section: null
215215
}
216216
];
217217

218-
expect(generateDocs(code)).toEqual(expected);
219-
});
220-
221-
it('throws when the section does not exist', () => {
222-
const code = `
223-
/**
224-
* @name Button
225-
*
226-
* @class .btn Button class.
227-
* @class .btn--primary Primary button class.
228-
*
229-
* @markup
230-
* <div class="{{modifier}}">Button</div>
231-
*
232-
* @section Not a valid section.
233-
*/
234-
`;
235-
236-
expect(() => generateDocs(code)).toThrowError('Section "Not a valid section." does not exist');
218+
expect(extract(code)).toEqual(expected);
237219
});
238220
});
239221

240-
describe('multiple comments', () => {
241-
it('builds the section hierarchy tree', () => {
222+
describe('organise()', () => {
223+
it('builds the subsections', () => {
242224
const code = `
243225
/**
244226
* @name Root section.
@@ -265,79 +247,71 @@ describe('index', () => {
265247
* @name Another root section.
266248
*/
267249
`;
268-
const expected = [
269-
{
270-
name: 'Root section.',
271-
description: null,
272-
classes: [],
273-
markup: null,
274-
subsections: [
275-
{
276-
name: 'Foo',
277-
description: null,
278-
classes: [],
279-
markup: null,
280-
subsections: [
281-
{
282-
name: 'Bar',
283-
description: 'Bar component.',
284-
classes: [
285-
{
286-
name: '.bar',
287-
description: 'Bar.',
288-
markup: '<div class="bar">Bar</div>'
289-
}
290-
],
291-
markup: '<div class="{{modifier}}">Bar</div>',
292-
subsections: []
293-
}
294-
]
295-
}
296-
]
297-
},
298-
{
299-
name: 'Another root section.',
300-
description: null,
301-
classes: [],
302-
markup: null,
303-
subsections: []
304-
}
305-
];
306-
expect(generateDocs(code)).toEqual(expected);
250+
const expected = {
251+
roots: ['Root section.', 'Another root section.'],
252+
sections: [
253+
{
254+
name: 'Root section.',
255+
description: null,
256+
classes: [],
257+
markup: null,
258+
subsections: ['Foo']
259+
},
260+
{
261+
name: 'Foo',
262+
description: null,
263+
classes: [],
264+
markup: null,
265+
subsections: ['Bar']
266+
},
267+
{
268+
name: 'Bar',
269+
description: 'Bar component.',
270+
classes: [
271+
{
272+
name: '.bar',
273+
description: 'Bar.',
274+
markup: '<div class="bar">Bar</div>'
275+
}
276+
],
277+
markup: '<div class="{{modifier}}">Bar</div>',
278+
subsections: []
279+
},
280+
{
281+
name: 'Another root section.',
282+
description: null,
283+
classes: [],
284+
markup: null,
285+
subsections: []
286+
}
287+
]
288+
};
289+
expect(organise(extract(code))).toEqual(expected);
307290
});
308-
});
309291

310-
it('returns an empty array when no docs', () => {
311-
expect(generateDocs(noTags)).toEqual([]);
312-
});
313-
314-
it('returns the root sections', () => {
315-
const code = `
316-
/**
317-
* @name Some section.
318-
*/
292+
it('returns an empty array when no docs', () => {
293+
expect(organise(extract(noTags))).toEqual({
294+
roots: [],
295+
sections: []
296+
});
297+
});
319298

320-
/**
321-
* @name Other section.
322-
*/
323-
`;
324-
const expected = [
325-
{
326-
name: 'Some section.',
327-
description: null,
328-
classes: [],
329-
markup: null,
330-
subsections: []
331-
},
332-
{
333-
name: 'Other section.',
334-
description: null,
335-
classes: [],
336-
markup: null,
337-
subsections: []
338-
}
339-
];
299+
it('throws when the section does not exist', () => {
300+
const code = `
301+
/**
302+
* @name Button
303+
*
304+
* @class .btn Button class.
305+
* @class .btn--primary Primary button class.
306+
*
307+
* @markup
308+
* <div class="{{modifier}}">Button</div>
309+
*
310+
* @section Not a valid section.
311+
*/
312+
`;
340313

341-
expect(generateDocs(code)).toEqual(expected);
314+
expect(() => organise(extract(code))).toThrowError('Section "Not a valid section." does not exist');
315+
});
342316
});
343317
});

0 commit comments

Comments
 (0)