Skip to content

Commit

Permalink
Merge pull request #4 from dephraiim/github
Browse files Browse the repository at this point in the history
feature: get readme from github repo
  • Loading branch information
ephraimduncan authored Dec 7, 2020
2 parents 68b0dd2 + ada8532 commit 7f59847
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "termd",
"version": "1.2.0",
"version": "1.3.0",
"description": "Render markdown files in the terminal",
"repository": "dephraiim/termd",
"main": "index.js",
Expand Down
27 changes: 23 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#!/usr/bin/env ts-node
import meow = require('meow');
import renderMarkdown, { renderString } from './src/termd';
import { getMarkdownFromUrl, npmPackageUrl } from './src/utils';
import { getMarkdownFromUrl, githubReadmeUrl, npmPackageUrl } from './src/utils';

const cli = meow(`
Usage
$ termd <filename>
Options
--string, -s Use a string with markdown syntax
--url, -u Render markdown from url in the terminal
--npm, -n Render npm package readme in the terminal
--string, -s Use a string with markdown syntax
--url, -u Render markdown from url in the terminal
--npm, -n Render npm package readme in the terminal
--github, -g Render github repository readme in the terminal
Examples
$ termd readme.md // # Heading 1
Expand All @@ -21,6 +22,9 @@ const cli = meow(`
${renderString('# Heading 1')}
$ termd -n termd
...
$ termd --github="dephraiim/termd"
...
`);

try {
Expand Down Expand Up @@ -52,6 +56,21 @@ try {
throw new Error(e);
});
}

if (cli.flags.g || cli.flags.github) {
let repoName = (cli.flags.github as string) || (cli.flags.g as string);
let repoUrl = githubReadmeUrl(repoName);
getMarkdownFromUrl(repoUrl)
.then((data) => data.content)
.then((data64) => {
let buffer = Buffer.from(data64, 'base64');
return buffer.toString('utf-8');
})
.then((readme) => console.log(renderString(readme)))
.catch((e) => {
throw new Error(e);
});
}
} catch (error) {
console.log(error.message);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "termd",
"version": "1.2.0",
"version": "1.3.0",
"description": "Render markdown files in the terminal",
"repository": "dephraiim/termd",
"main": "index.js",
Expand Down
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,8 @@ export const npmPackageUrl = (packageName: string): string => {

return NPM_REGISTRY + packageName;
};

export const githubReadmeUrl = (repoName: string): string => {
const GITHUB_URL = `https://api.github.com/repos/${repoName}/readme`;
return GITHUB_URL;
};

0 comments on commit 7f59847

Please sign in to comment.