https://github.com/orta/md-type-tables
https://github.com/orta/md-type-tables
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/orta/md-type-tables
- Owner: orta
- Created: 2021-03-25T21:02:43.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-03-25T21:11:48.000Z (about 5 years ago)
- Last Synced: 2025-05-06T22:57:32.750Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 15.6 KB
- Stars: 14
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
We take this type:
```ts
type SomeType = {
/**
* Client ID of your GitHub/OAuth App. Find it on your app's settings page.
* @required
*/
clientId: string;
/**
* Client Secret for your GitHub/OAuth App. Create one on your app's settings page.
* @required
*/
clientSecret: string;
/**
* Either "oauth-app" or "github-app". Defaults to "oauth-app".
*/
clientType: string;
};
```
And use it to generate this table:
name
type
description
clientId
string
**Required**
Client ID of your GitHub/OAuth App. Find it on your app's settings page.
clientSecret
string
**Required**
Client Secret for your GitHub/OAuth App. Create one on your app's settings page.
clientType
string
Either "oauth-app" or "github-app". Defaults to "oauth-app".
Using this code:
```js
const ts = require("typescript");
module.exports = {
transforms: {
/* Match */
TYPES(_content, options) {
const mds = [];
let program = ts.createProgram([options.src], {});
const sourceFile = program.getSourceFile(options.src);
/** @type {import("typescript").TypeAliasDeclaration} */
let typeNode = undefined;
const findSymbol = (node) => {
if (
node &&
node.name &&
node.name.escapedText &&
node.name.escapedText === options.symbol
) {
typeNode = node;
}
if (!typeNode) ts.forEachChild(node, findSymbol);
};
findSymbol(sourceFile);
if (!typeNode)
throw new Error(`Could not find ${options.symbol} in ${options.src}`);
mds.push(prefix);
typeNode.type.members.forEach((member) => {
const name = member.name.escapedText;
const type = sourceFile.text.slice(
member.type.pos + 1,
member.type.end
);
const required =
member.jsDoc &&
!!member.jsDoc.find(
(d) => d.tags && d.tags.find((t) => t.tagName.escapedText)
);
const info =
member.jsDoc && member.jsDoc.map((jd) => jd.comment).join("\n\n");
mds.push(`
${name}
${type}
${required ? "**Required**" : ""}
${info}
`);
});
mds.push(suffix);
return mds.join("\n\n");
},
},
};
const prefix = `
name
type
description
`;
const suffix = `
`;
```
You can run it locally:
```sh
git clone https://github.com/orta/md-type-tables
cd md-type-tables
yarn
yarn readme
```