diff --git a/package.json b/package.json index e88391a..406f3e6 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,9 @@ "version": "0.0.0", "author": "Jamie Mason (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": [], diff --git a/src/bin-interactive.ts b/src/bin-interactive.ts old mode 100644 new mode 100755 index e37d5b4..fd60b45 --- a/src/bin-interactive.ts +++ b/src/bin-interactive.ts @@ -1,3 +1,5 @@ +#!/usr/bin/env node + import * as program from 'commander'; import { isString } from 'lodash'; import { run } from './interactive'; diff --git a/src/bin-markdown.ts b/src/bin-markdown.ts new file mode 100755 index 0000000..aa9e160 --- /dev/null +++ b/src/bin-markdown.ts @@ -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 to self-help document') + .parse(process.argv); + +if (program.source && isString(program.source)) { + run({ sourcePath: program.source }); +} else { + program.outputHelp(); + process.exit(1); +} diff --git a/src/bin.ts b/src/bin.ts old mode 100644 new mode 100755 index 6e7b442..ef3f8fc --- a/src/bin.ts +++ b/src/bin.ts @@ -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); diff --git a/src/index.ts b/src/index.ts index 1bebc62..3da316a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; @@ -58,7 +58,7 @@ export const selfHelp = async (interpreter: TreeInterpreter): Promise => { const renderValue = async (leaf: Leaf) => { console.log(''); - console.log(renderMarkdown(leaf.value)); + console.log(renderToCli(leaf.value)); }; interpreter.onTransition(async ({ context: { currentNode }, event, matches }) => { diff --git a/src/lib/markdown.ts b/src/lib/markdown.ts new file mode 100644 index 0000000..74cfcef --- /dev/null +++ b/src/lib/markdown.ts @@ -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 => + Promise.all(children.map(toMarkdownFile)).then((x) => x.join('')); + +const ul = (html: string) => `
    ${html}
`; +const li = (html: string) => `
  • ${html}
  • `; +const summary = (html: string) => `${html}`; +const details = (title: string, html: string) => `
    ${summary(title)}${ul(html)}
    `; + +export const toMarkdownFile = async (node: Node): Promise => { + 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(); diff --git a/src/markdown.ts b/src/markdown.ts index 701650a..7777e99 100644 --- a/src/markdown.ts +++ b/src/markdown.ts @@ -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();