Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions sync-api-docs/extractDocsFromRN.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,20 @@ async function extractDocsFromRN(rnRoot) {

const result = reactDocs.parse(
contents,
reactDocs.resolver.findExportedComponentDefinition,
reactDocs.resolver.findAllComponentDefinitions,
reactDocs.defaultHandlers.filter(
handler => handler !== reactDocs.handlers.propTypeCompositionHandler
),
{filename: file}
);

const filteredResult = result.filter(item => {
if (item.description) return item;
});

docs.push({
file,
component: cleanComponentResult(result),
component: cleanComponentResult(...filteredResult),
});
}

Expand Down
131 changes: 97 additions & 34 deletions sync-api-docs/generateMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@
* LICENSE file in the root directory of this source tree.
*/
const tokenizeComment = require('tokenize-comment');
const {formatTypeColumn, formatDefaultColumn} = require('./propFormatter');
const {
formatMethodType,
formatMethodName,
formatMethodDescription,
} = require('./methodFormatter');

const {
formatMultiplePlatform,
stringToInlineCodeForTable,
maybeLinkifyType,
maybeLinkifyTypeName,
formatTypeColumn,
formatDefaultColumn,
} = require('./propFormatter');
formatType,
} = require('./utils');

// Formats an array of rows as a Markdown table
function generateTable(rows) {
Expand Down Expand Up @@ -72,24 +79,66 @@ function generateProp(propName, prop) {

// Formats information about a prop
function generateMethod(method, component) {
const infoTable = generateTable([
{
...(method.rnTags && method.rnTags.platform
? {Platform: formatPlatformName(method.rnTags.platform)}
: {}),
},
]);
let descriptionTokenized = '';
let header = 'Valid `params` keys are:';
let mdPoints = '';
if (method?.params[0]?.type?.raw) {
let desc = method?.params[0]?.type?.raw;
let len = method?.params[0]?.type?.signature?.properties?.length;
descriptionTokenized = tokenizeComment(desc);

if (
descriptionTokenized?.examples &&
descriptionTokenized?.examples.length === len
) {
let obj = [];
for (let i = 0; i < len; i++) {
let newObj = method?.params[0]?.type?.signature?.properties[i];
newObj['description'] = descriptionTokenized?.examples[i]?.value;
obj.push(newObj);
}

obj.map(item => {
if (item.description.trim() !== 'missing')
mdPoints += `- '${item.key}' (${item.value.name}) - ${
item.description
}`;
else mdPoints += `- '${item.key}' (${item.value.name})`;
});
}
}

if (method?.docblock) {
let dblock = method.docblock
.split('\n')
.map(line => {
return line.replace(/ /, '');
})
.join('\n');
const docblockTokenized = tokenizeComment(dblock);
dblock = dblock.replace(/@platform .*/g, '');
method.rnTags = {};
const platformTag = docblockTokenized.tags.find(
({key}) => key === 'platform'
);

if (platformTag) {
method.rnTags.platform = platformTag.value.split(',');
}
}

return (
'### `' +
method.name +
'()`' +
(method.rnTags && method.rnTags.platform
? formatMultiplePlatform(method.rnTags.platform)
: '') +
'\n' +
'\n' +
generateMethodSignatureBlock(method, component) +
(method.description ? method.description + '\n\n' : '') +
generateMethodSignatureTable(method, component) +
infoTable
(mdPoints && header + '\n' + mdPoints)
).trim();
}

Expand Down Expand Up @@ -118,15 +167,20 @@ function generateMethodSignatureTable(method, component) {
if (!method.params.length) {
return '';
}

return (
'**Parameters:**\n\n' +
generateTable(
method.params.map(param => ({
Name: param.name,
Type: param.type ? maybeLinkifyType(param.type) : '',
Required: param.optional ? 'No' : 'Yes',
Description: param.description,
}))
method.params.map(param => {
return {
Name: formatMethodName(param),
Type: formatMethodType(param),
Required: param.optional ? 'No' : 'Yes',
...(param.description && {
Description: formatMethodDescription(param),
}),
};
})
)
);
}
Expand Down Expand Up @@ -229,26 +283,35 @@ function preprocessDescription(desc) {
});

if (tabs === 2) {
const wrapper = `${playgroundTab}\n\n${functionalBlock}\n\n${
descriptionTokenized.examples[0].raw
}\n\n${classBlock}\n\n${
descriptionTokenized.examples[1].raw
}\n\n${endBlock}`;
return (
descriptionTokenized.description +
`\n## Example\n` +
wrapper +
'\n' +
descriptionTokenized?.footer
const firstExample = desc.substr(desc.search('```SnackPlayer') + 1);
const secondExample = firstExample.substr(
firstExample.search('```SnackPlayer') + 1
);
} else {

return (
desc.substr(0, desc.search('```SnackPlayer')) +
'\n' +
'\n## Example\n' +
'\n' +
desc.substr(desc.search('```SnackPlayer'))
`\n## Example\n` +
`${playgroundTab}\n\n${functionalBlock}\n\n${'`' +
firstExample.substr(
0,
firstExample.search('```') + 3
)}\n\n${classBlock}\n\n${'`' +
secondExample.substr(
0,
secondExample.search('```') + 3
)}\n\n${endBlock}` +
secondExample.substr(secondExample.search('```') + 3)
);
} else {
if (desc.search('```SnackPlayer') !== -1) {
return (
desc.substr(0, desc.search('```SnackPlayer')) +
'\n' +
'\n## Example\n' +
'\n' +
desc.substr(desc.search('```SnackPlayer'))
);
} else return desc;
}
}

Expand Down
28 changes: 28 additions & 0 deletions sync-api-docs/magic.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,33 @@ module.exports = {
text: 'RefreshControl.SIZE',
url: 'refreshcontrol.md#refreshlayoutconstssize',
},
StatusBarAnimation: {
text: 'StatusBarAnimation',
url: 'statusbar#statusbaranimation',
},
StatusBarStyle: {
text: 'StatusBarStyle',
url: 'statusbar#statusbarstyle',
},
ReactNode: {
text: 'React.Node',
url: 'react-node.md',
},
TextStyleProps: {
text: 'Text Style Props',
url: 'text-style-props',
},
SectionT: {
text: 'Section',
url: 'sectionlist#section',
},
ViewStyleProps: {
text: 'View Style Props',
url: 'view-style-props',
},
Text: {
text: 'Text',
url: 'text#style',
},
},
};
63 changes: 63 additions & 0 deletions sync-api-docs/methodFormatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

const {typeOf} = require('tokenize-comment/lib/utils');
const magic = require('./magic');
const {formatMultiplePlatform} = require('./utils');

function formatMethodType(param) {
let text, url;
if (param?.type?.name === 'union') {
if (param?.type?.alias) {
const {alias} = param.type;
if (Object.hasOwnProperty.call(magic.linkableTypeAliases, alias)) {
({url, text} = magic.linkableTypeAliases[alias]);
}
if (url) return `[${text}](${url})`;
else return param.type.alias;
}
return param.type.name;
} else {
if (param?.type?.type) return param.type.type;
else return param.type.name;
}
}

function formatMethodName(param) {
let tag = param.description;
if (tag) {
const isMatch = tag.match(/{@platform [a-z ,]*}/);
if (isMatch) {
const platform = isMatch[0].match(/ [a-z ,]*/);
tag = tag.replace(/{@platform [a-z ,]*}/g, '');
tag = formatMultiplePlatform(platform[0].split(','));
return param.name + tag;
}
}
return param.name;
}

function formatMethodDescription(param) {
let tag = param.description;
const isMatch = tag.match(/{@platform [a-z ,]*}/);
if (isMatch) {
const platform = isMatch[0].match(/ [a-z ,]*/);

// Replaces @platform strings with empty string
// and appends type with formatted platform
tag = tag.replace(/{@platform [a-z ,]*}/g, '');
}
return tag;
}

module.exports = {
formatMethodType,
formatMethodName,
formatMethodDescription,
};
Loading