Skip to content

Commit

Permalink
feat(cli): add command to generate markdown from help documents
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMason committed Sep 12, 2019
1 parent 0ec109d commit 4639b08
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 23 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"version": "0.0.0",
"author": "Jamie Mason <[email protected]> (https://github.com/JamieMason)",
"bin": {
"self-help-interactive": "./bin-interactive.js",
"self-help": "./bin.js"
"self-help-interactive": "./dist/bin-interactive.js",
"self-help-markdown": "./dist/bin-markdown.js",
"self-help": "./dist/bin.js"
},
"bugs": "https://github.com/JamieMason/self-help/issues",
"contributors": [],
Expand Down
2 changes: 2 additions & 0 deletions src/bin-interactive.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env node

import * as program from 'commander';
import { isString } from 'lodash';
import { run } from './interactive';
Expand Down
17 changes: 17 additions & 0 deletions src/bin-markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env node

import * as program from 'commander';
import { isString } from 'lodash';
import { run } from './markdown';

program
.description('generate markdown from a self-help document')
.option('-s, --source <path>', 'path to self-help document')
.parse(process.argv);

if (program.source && isString(program.source)) {
run({ sourcePath: program.source });
} else {
program.outputHelp();
process.exit(1);
}
1 change: 1 addition & 0 deletions src/bin.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import * as program from 'commander';
program
.version(require('../package.json').version)
.command('interactive', 'navigate a self-help document from the command line')
.command('markdown', 'generate markdown from a self-help document')
.parse(process.argv);
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import chalk from 'chalk';
import { prompt } from 'inquirer';
import { WriteStream } from 'tty';
import { renderToCli } from './lib/markdown';
import { TreeInterpreter } from './machine/tree';
import { Branch, isBranch, isLeaf, Leaf, Node } from './machine/tree/nodes';
import { renderMarkdown } from './markdown';

interface Answer {
name: string;
Expand Down Expand Up @@ -58,7 +58,7 @@ export const selfHelp = async (interpreter: TreeInterpreter): Promise<void> => {

const renderValue = async (leaf: Leaf) => {
console.log('');
console.log(renderMarkdown(leaf.value));
console.log(renderToCli(leaf.value));
};

interpreter.onTransition(async ({ context: { currentNode }, event, matches }) => {
Expand Down
45 changes: 45 additions & 0 deletions src/lib/markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as marked from 'marked';
import * as TerminalRenderer from 'marked-terminal';
import * as prettier from 'prettier';
import { isBranch, isLeaf, isUnresolvedBranch, Node } from '../machine/tree/nodes';

marked.setOptions({
renderer: new TerminalRenderer({
reflowText: false,
tab: 2,
}),
});

const prettierrc: prettier.Options = {
parser: 'markdown',
printWidth: 120,
proseWrap: 'always',
tabWidth: 2,
useTabs: false,
};

const getNestedMarkdown = (children: Node[]): Promise<string> =>
Promise.all(children.map(toMarkdownFile)).then((x) => x.join(''));

const ul = (html: string) => `<ul>${html}</ul>`;
const li = (html: string) => `<li>${html}</li>`;
const summary = (html: string) => `<summary>${html}</summary>`;
const details = (title: string, html: string) => `<details>${summary(title)}${ul(html)}</details>`;

export const toMarkdownFile = async (node: Node): Promise<string> => {
if (isUnresolvedBranch(node)) {
const children = await node.children();
const contents = await getNestedMarkdown(children);
return li(details(node.label, ul(contents)));
} else if (isBranch(node)) {
const children = node.children;
const contents = await getNestedMarkdown(children);
return li(details(node.label, ul(contents)));
} else if (isLeaf(node)) {
return li(details(node.label, ul(`\n\n${node.value}\n\n`)));
}
return '';
};

export const renderToCli = (markdown: string) =>
marked(prettier.format(markdown, prettierrc)).trim();
27 changes: 8 additions & 19 deletions src/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
import * as marked from 'marked';
import * as TerminalRenderer from 'marked-terminal';
import * as prettier from 'prettier';
import { resolve } from 'path';
import { toMarkdownFile } from './lib/markdown';

marked.setOptions({
renderer: new TerminalRenderer({
reflowText: false,
tab: 2,
}),
});

const prettierrc: prettier.Options = {
parser: 'markdown',
printWidth: 120,
proseWrap: 'always',
tabWidth: 2,
useTabs: false,
export const run = async ({ sourcePath }: { sourcePath: string }) => {
const dataPath = resolve(process.cwd(), sourcePath);
const source = require(dataPath);
const tree = await source.getData();
const markdown = await toMarkdownFile(tree);
console.log(markdown);
};

export const renderMarkdown = (markdown: string) =>
marked(prettier.format(markdown, prettierrc)).trim();

0 comments on commit 4639b08

Please sign in to comment.