Skip to content

Commit ed01017

Browse files
committed
[FIX] markdown script
1 parent 131c67c commit ed01017

25 files changed

Lines changed: 189 additions & 39 deletions

apps/react-tools-demo/scripts/generateMarkdown.js

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,80 @@ function getIndexClosedBracketTypeParam(line) {
3030
return end;
3131
}
3232

33-
function getTypeParam(line) {
33+
/**
34+
*
35+
* @param {string} string
36+
* @returns {string[]}
37+
*/
38+
function splitType(string){
39+
string = string.split(',');
40+
let newCode = [];
41+
let last = "";
42+
for (let part of string){
43+
if (last.split("(").length == last.split(")").length && last.split("[").length == last.split("]").length && last.split("{").length == last.split("}").length){
44+
last = part;
45+
newCode.push(part);
46+
}
47+
else{
48+
last += "," + part;
49+
newCode[newCode.length - 1] = last;
50+
}
51+
}
52+
return newCode;
53+
}
54+
55+
/**
56+
* @param {string} string
57+
*/
58+
function parseType(string, tab=0) {
59+
const typeArray= [];
60+
let currTab = tab;
61+
if(string.startsWith("[")) {
62+
typeArray.push(Array(currTab*4).fill(true).map(() => " ").join("")+ "- __Array__: ");
63+
currTab++;
64+
string = string.substring(1, string.length-1);
65+
const stringSplitted = splitType(string).map(el => el.trim());
66+
for(let val of stringSplitted) {
67+
if(val.startsWith("[") || val.startsWith("{")) {
68+
typeArray.push(...parseType(val, currTab));
69+
}
70+
else {
71+
typeArray.push(Array(currTab*4).fill(true).map(() => " ").join("")+ `- _${val}_ `)
72+
}
73+
}
74+
} else if(string.startsWith("{")) {
75+
typeArray.push(Array(currTab*4).fill(true).map(() => " ").join("")+ "- __Object__: ");
76+
currTab++;
77+
string = string.substring(1, string.length-1);
78+
const stringSplitted = splitType(string).map(el => el.trim());
79+
for(let val of stringSplitted) {
80+
const semicolonIndex = val.indexOf(":");
81+
const [key, value] = [val.substring(0, semicolonIndex).trim(), val.substring(semicolonIndex+1, val.length).trim()];
82+
if(value.startsWith("[") || value.startsWith("{")) {
83+
typeArray.push(...parseType(val, currTab));
84+
} else {
85+
typeArray.push(Array(currTab*4).fill(true).map(() => " ").join("")+`- ___${key}__ : _${value}_ `);
86+
}
87+
}
88+
} else {
89+
const stringSplitted = splitType(string).map(el => el.trim());
90+
for(let val of stringSplitted) {
91+
if(val.startsWith("[") || val.startsWith("{")) {
92+
typeArray.push(...parseType(val, currTab));
93+
} else {
94+
typeArray.push(Array(currTab*4).fill(true).map(() => " ").join("")+`- _${val}_ `);
95+
}
96+
}
97+
}
98+
return typeArray;
99+
}
100+
101+
function getTypeString(line, isReturn) {
34102
const end = getIndexClosedBracketTypeParam(line);
35-
return line.substring(1,end);
103+
let type = line.substring(1,end);
104+
return isReturn
105+
? parseType(type)
106+
: type;
36107
}
37108

38109
function getNameDescriptionParam(line) {
@@ -95,7 +166,7 @@ function getJsDoc(fileSplitted) {
95166

96167
/**
97168
*
98-
* @param {{title: string, description: string, usage?: string, params: [{name: string, description: string, type: string}], returns: string, type: string}} jsDoc
169+
* @param {{title: string, description: string, usage?: string, params: [{name: string, description: string, type: string}], returns: {name: string, description: string, type: string[]}, type: string}} jsDoc
99170
*/
100171
function jsDoc2Markdown(jsDoc) {
101172
const markdown =
@@ -115,7 +186,8 @@ ${jsDoc.params.map(el => `> - __${el.name}__: _${el.type}_${el.description ? '
115186
116187
> ### Returns
117188
>
118-
> - ${jsDoc.returns.name ? `__${jsDoc.returns.name}__: ` : ""}_${jsDoc.returns.type}_${jsDoc.returns.description ? ' \n'+jsDoc.returns.description : ""}
189+
> ${jsDoc.returns.name ? `__${jsDoc.returns.name}__` : ""}${jsDoc.returns.description ? ': '+jsDoc.returns.description : ""}
190+
${jsDoc.returns.type.map(el => `> `+el).join("\n")}
119191
>`;
120192

121193
return markdown;
@@ -124,7 +196,7 @@ ${jsDoc.params.map(el => `> - __${el.name}__: _${el.type}_${el.description ? '
124196
/**
125197
*
126198
* @param {string} file
127-
* @returns {{title: string, description: string, params: [{name: string, description: string, type: string}], returns: string, type: string}}
199+
* @returns {{title: string, description: string, params: [{name: string, description: string, type: string}], returns: {name: string, description: string, type: string}, type: string}}
128200
*/
129201
function buildHooksUtilsMarkdownObject(file) {
130202
const fileSplitted = file.split("\n");
@@ -148,15 +220,15 @@ function buildHooksUtilsMarkdownObject(file) {
148220
if(depuratedLine.startsWith("@param")) {
149221
const typeNameDescriptionParam = depuratedLine.split("@param ")[1];
150222
const param = {
151-
type: getTypeParam(typeNameDescriptionParam),
223+
type: getTypeString(typeNameDescriptionParam, false),
152224
...getNameDescriptionParam(typeNameDescriptionParam)
153225
};
154226
obj.params.push(param);
155227
}
156228
if(depuratedLine.startsWith("@returns")) {
157229
const returnSplitted = depuratedLine.split("@returns ")[1];
158230
const returns = {
159-
type: getTypeParam(returnSplitted),
231+
type: getTypeString(returnSplitted, true),
160232
...getNameDescriptionParam(returnSplitted)
161233
};
162234
obj.returns = returns;

apps/react-tools-demo/src/markdown/isDeepEqual.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ isDeepEqual (objA: unknown, objB: unknown, map = new WeakMap())
1616
1717
> ### Returns
1818
>
19-
> - __result__: _boolean_
19+
> __result__
20+
> - _boolean_
2021
>

apps/react-tools-demo/src/markdown/isMouseEvent.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ isMouseEvent (event: SyntheticEvent)
1414
1515
> ### Returns
1616
>
17-
> - __result__: _boolean_
17+
> __result__
18+
> - _boolean_
1819
>

apps/react-tools-demo/src/markdown/isShallowEqual.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ isShallowEqual (objA: unknown, objB: unknown)
1515
1616
> ### Returns
1717
>
18-
> - __result__: _boolean_
18+
> __result__
19+
> - _boolean_
1920
>

apps/react-tools-demo/src/markdown/isTouchEvent.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ isTouchEvent (event: SyntheticEvent)
1414
1515
> ### Returns
1616
>
17-
> - __result__: _boolean_
17+
> __result__
18+
> - _boolean_
1819
>

apps/react-tools-demo/src/markdown/useCallbackCompare.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ optional function that executes comparing between old and new `deps`: it returns
6060
6161
> ### Returns
6262
>
63-
> - __cb__: _T_
64-
memoized callback
63+
> __cb__: memoized callback
64+
> - _T_
6565
>

apps/react-tools-demo/src/markdown/useCallbackDeepCompare.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ DependencyList.
6060
6161
> ### Returns
6262
>
63-
> - __cb__: _T_
64-
memoized callback
63+
> __cb__: memoized callback
64+
> - _T_
6565
>

apps/react-tools-demo/src/markdown/useEffectCompare.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,6 @@ optional function that executes comparing between old and new `deps`: it returns
7171

7272
> ### Returns
7373
>
74-
> - _void_
74+
>
75+
> - _void_
7576
>

apps/react-tools-demo/src/markdown/useEffectDeepCompare.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,6 @@ dependency list.
5656
5757
> ### Returns
5858
>
59-
> - _void_
59+
>
60+
> - _void_
6061
>

apps/react-tools-demo/src/markdown/useLayoutEffectCompare.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ optional function that executes comparing between old and new `deps`: it returns
2525

2626
> ### Returns
2727
>
28-
> - _void_
28+
>
29+
> - _void_
2930
>

0 commit comments

Comments
 (0)