Skip to content

Commit

Permalink
fix(docs): add support for @param doc
Browse files Browse the repository at this point in the history
- optional params
- required params
- default values
- nested structures like arrays and objects
  • Loading branch information
Hyperkid123 committed Mar 2, 2021
1 parent 8c3b4dd commit 436ae51
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions packages/docs/generate-functions-md.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,38 @@ function getDescription(item) {
return item.description;
}

function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;')
.replace(/\|/g, '&#124;')
.replace(/\*/g, '&#42;');
}

function getParamName(param) {
return escapeHtml(`${param.name}${param.optional ? '' : '*'}`);
}

function getParamType(param) {
return `<code>${escapeHtml(param.type.names.join('|'))}</code>`;
}

function getParamDefaultValue(param) {
return `<code>${escapeHtml(param.defaultvalue ? param.defaultvalue : '')}</code>`;
}

function getParamDescription(param) {
return escapeHtml(param.description || '');
}

function createParams(item) {
let content = '';
if (item.params) {
item.params.forEach(param => {
content = `${content}\n|${param.name}|<code>${param.type.names.join('&#124')}</code>|${param.description}`;
content = `${content}\n|${getParamName(param)}|${getParamType(param)}|${getParamDefaultValue(param)}|${getParamDescription(param)}`;
});
}

Expand All @@ -32,7 +59,7 @@ async function createItemMd(pathname, item) {
}

if (params) {
content = `${content}\n## Params\n|Name|Type|Description|\n|---|---|---|${params}\n`;
content = `${content}\n## Params\n|Name|Type|Default|Description|\n|---|---|---|---|${params}\n`;
}

return fse.writeFile(pathname, content);
Expand Down

0 comments on commit 436ae51

Please sign in to comment.