Skip to content

Commit

Permalink
sync-api-docs: Support methods
Browse files Browse the repository at this point in the history
  • Loading branch information
motiz88 committed Feb 12, 2020
1 parent 97d0dc7 commit 0111187
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 17 deletions.
54 changes: 49 additions & 5 deletions website/scripts/sync-api-docs/generateMarkdown.js
Expand Up @@ -20,6 +20,9 @@ function generateTable(rows) {
);
}
}
if (!colWidths.size) {
return '';
}
let header = '|',
divider = '|';
for (const [col, width] of colWidths) {
Expand Down Expand Up @@ -72,6 +75,27 @@ function generateProp(propName, prop) {
);
}

// Formats information about a prop
function generateMethod(method) {
const infoTable = generateTable([
{
...(method.rnTags && method.rnTags.platform
? {Platform: formatPlatformName(method.rnTags.platform)}
: {}),
},
]);

return (
'### `' +
method.name +
'`' +
'\n' +
'\n' +
(method.description ? method.description + '\n\n' : '') +
infoTable
).trim();
}

function maybeLinkifyType(flowType) {
let url, text;
if (Object.hasOwnProperty.call(magic.linkableTypeAliases, flowType.name)) {
Expand Down Expand Up @@ -102,11 +126,12 @@ function maybeLinkifyTypeName(name) {

// Formats information about props
function generateProps({props, composes}) {
const title = 'Props';
if (!props || !Object.keys(props).length) {
return '';
}

return (
'## ' +
title +
'## Props' +
'\n' +
'\n' +
(composes && composes.length
Expand All @@ -119,7 +144,25 @@ function generateProps({props, composes}) {
.map(function(propName) {
return generateProp(propName, props[propName]);
})
.join('\n---\n\n')
.join('\n\n---\n\n')
);
}

function generateMethods({methods}) {
if (!methods || !methods.length) {
return '';
}

return (
'## Methods' +
'\n' +
'\n' +
[...methods]
.sort()
.map(function(method) {
return generateMethod(method);
})
.join('\n\n---\n\n')
);
}

Expand All @@ -138,7 +181,8 @@ function generateMarkdown({id, title}, reactAPI) {
'\n\n' +
'---\n\n' +
'# Reference\n\n' +
generateProps(reactAPI);
generateProps(reactAPI) +
generateMethods(reactAPI);

return markdownString;
}
Expand Down
31 changes: 19 additions & 12 deletions website/scripts/sync-api-docs/preprocessGeneratedApiDocs.js
Expand Up @@ -28,22 +28,29 @@ function joinDescriptionAndExamples(tokenized) {
return sections.join('\n\n');
}

function preprocessTagsInDescription(obj) {
if (obj && obj.description) {
const descriptionTokenized = tokenizeComment(obj.description);
obj.description = joinDescriptionAndExamples(descriptionTokenized);
obj.rnTags = {};
const platformTag = descriptionTokenized.tags.find(
({key}) => key === 'platform'
);
if (platformTag) {
obj.rnTags.platform = platformTag.value;
}
}
}

// NOTE: This function mutates `docs`.
function preprocessGeneratedApiDocs(docs) {
for (const doc of docs) {
if (doc.props) {
for (const [key, prop] of Object.entries(doc.props)) {
if (prop.description) {
const descriptionTokenized = tokenizeComment(prop.description);
prop.description = joinDescriptionAndExamples(descriptionTokenized);
prop.rnTags = {};
const platformTag = descriptionTokenized.tags.find(
({key}) => key === 'platform'
);
if (platformTag) {
prop.rnTags.platform = platformTag.value;
}
}
for (const prop of Object.values(doc.props)) {
preprocessTagsInDescription(prop);
}
for (const prop of doc.methods) {
preprocessTagsInDescription(prop);
}
}
}
Expand Down

0 comments on commit 0111187

Please sign in to comment.