Created
December 31, 2016 11:27
-
-
Save ryasmi/a249d54057d4b6c6b5dbc9a0dc9ec987 to your computer and use it in GitHub Desktop.
Uses typedoc's json output to produce documentation in markdown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const docTokens = require('./ts.json'); | |
const renderToken = token => { | |
const functionKind = 64; | |
switch (token.kind) { | |
case functionKind: { | |
const source = token.sources && renderSources(token.sources); | |
const sig = renderSignatures(token.signatures); | |
const kind = renderKind(token.kindString); | |
return `${kind}[${token.name}](${source}) = ${sig}`; | |
} default: { | |
const source = token.sources && renderSources(token.sources); | |
const sig = renderType(token.type); | |
const kind = renderKind(token.kindString); | |
return `${kind}[${token.name}](${source}) = ${sig}`; | |
} | |
} | |
}; | |
const renderKind = kindString => { | |
if (kindString === 'Type alias') return `${kindString} `; | |
return ''; | |
}; | |
const renderParameter = param => { | |
const sig = renderType(param.type); | |
return `${param.name}: ${sig}`; | |
}; | |
const renderSources = sources => { | |
const source = sources[0]; | |
return `../${source.fileName}#L${source.line}`; | |
}; | |
const renderType = type => { | |
switch (type.type) { | |
case 'instrinct': | |
return `**${type.name}${type.isArray ? '[]' : ''}**`; | |
case 'reflection': | |
return renderDeclaration(type.declaration); | |
case 'reference': | |
if (type.name === '(Anonymous function)') return `**function**`; | |
return `**${type.name}**`; | |
} | |
}; | |
const renderSignatures = signatures => { | |
const signature = signatures[0]; | |
const callKind = 4096; | |
const indexSignature = 8192; | |
switch (signature.kind) { | |
case callKind: | |
const params = signature.parameters.map(renderParameter); | |
const returnType = renderType(signature.type); | |
return `(${params.join(', ')}) => ${returnType}`; | |
case indexSignature: | |
const keyToken = renderParameter(signature.parameters[0]); | |
const valueType = renderType(signature.type); | |
return `{[${keyToken}]: ${valueType}}`; | |
default: | |
return signature; | |
} | |
}; | |
const renderDeclaration = declaration => { | |
const literalKind = 65536; | |
switch (declaration.kind) { | |
case literalKind: | |
if (declaration.signatures !== undefined) { | |
return renderSignatures(declaration.signatures); | |
} else { | |
return renderSignatures(declaration.indexSignature); | |
} | |
default: | |
return declaration; | |
} | |
}; | |
const renderTokens = tokens => { | |
return tokens.map(t => | |
`- ${renderToken(t)}` | |
).join('\r\n'); | |
}; | |
const renderedTokens = renderTokens(docTokens.children[0].children); | |
const readme = `# API\r\n${renderedTokens}\r\n`; | |
fs.writeFile('docs/readme.md', readme, function(err) { | |
if (err) return console.error(err); | |
console.log('The file was saved!'); | |
}); |
Author
ryasmi
commented
Dec 31, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment