-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add command to generate markdown from help documents
- Loading branch information
1 parent
0ec109d
commit 4639b08
Showing
7 changed files
with
78 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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": [], | ||
|
This file contains 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
This file contains 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
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); | ||
} |
This file contains 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
This file contains 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
This file contains 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
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(); |
This file contains 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
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(); |