From 19f45af643b93b0c707bdeada2f5ce9f76fc3d32 Mon Sep 17 00:00:00 2001 From: Jerome Simeon Date: Thu, 12 Sep 2019 07:42:01 -0400 Subject: [PATCH] feature(cli) add --withSlate option to CLI Signed-off-by: Jerome Simeon --- packages/markdown-cli/index.js | 9 +++++++-- packages/markdown-cli/lib/Commands.js | 21 ++++++++++++++------- packages/markdown-cli/package.json | 1 + packages/markdown-cli/test/cli.js | 4 ++-- packages/markdown-slate/index.js | 3 +-- 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/packages/markdown-cli/index.js b/packages/markdown-cli/index.js index 24856e30..0b15b593 100755 --- a/packages/markdown-cli/index.js +++ b/packages/markdown-cli/index.js @@ -34,7 +34,12 @@ require('yargs') default: false }); yargs.option('--withCicero', { - describe: 'further transform for Cicero', + describe: 'further transform to Cicero Markdown', + type: 'boolean', + default: false + }); + yargs.option('--withSlate', { + describe: 'further transform to Slate DOM', type: 'boolean', default: false }); @@ -50,7 +55,7 @@ require('yargs') try { argv = Commands.validateParseArgs(argv); - return Commands.parse(argv.sample, argv.out, argv.generateMarkdown, argv.withCicero, argv.noWrap) + return Commands.parse(argv.sample, argv.out, argv.generateMarkdown, argv.withCicero, argv.withSlate, argv.noWrap) .then((result) => { if(result) {Logger.info('\n'+result);} }) diff --git a/packages/markdown-cli/lib/Commands.js b/packages/markdown-cli/lib/Commands.js index 0178cb9e..8fe541f5 100644 --- a/packages/markdown-cli/lib/Commands.js +++ b/packages/markdown-cli/lib/Commands.js @@ -19,6 +19,7 @@ const Logger = require('@accordproject/markdown-common').Logger; const CommonMark = require('@accordproject/markdown-common').CommonMark; const CiceroMark = require('@accordproject/markdown-cicero').CiceroMark; +const SlateMark = require('@accordproject/markdown-slate').SlateMark; /** * Utility class that implements the commands exposed by the CLI. @@ -86,26 +87,32 @@ class Commands { * @param {string} outPath to an output file * @param {boolean} generateMarkdown whether to transform back to markdown * @param {boolean} withCicero whether to further transform for Cicero + * @param {boolean} withSlate whether to further transform for Slate * @param {boolean} noWrap whether to avoid wrapping Cicero variables in XML tags * @returns {object} Promise to the result of parsing */ - static parse(samplePath, outPath, generateMarkdown, withCicero, noWrap) { + static parse(samplePath, outPath, generateMarkdown, withCicero, withSlate, noWrap) { const commonMark = new CommonMark({ tagInfo: true }); const ciceroMark = new CiceroMark(); + const slateMark = new SlateMark(); + const markdownText = Fs.readFileSync(samplePath, 'utf8'); - let concertoObject = commonMark.fromString(markdownText); + let result = commonMark.fromString(markdownText); if (withCicero) { - concertoObject = ciceroMark.fromCommonMark(concertoObject); + result = ciceroMark.fromCommonMark(result); + } else if (withSlate) { + result = slateMark.fromCommonMark(result); } - let result; if (generateMarkdown) { if (withCicero) { const options = noWrap ? { wrapVariables: false } : null; - concertoObject = ciceroMark.toCommonMark(concertoObject, options); + result = ciceroMark.toCommonMark(result, options); + } else if (withSlate) { + result = slateMark.toCommonMark(result); } - result = commonMark.toString(concertoObject); + result = commonMark.toString(result); } else { - const json = ciceroMark.getSerializer().toJSON(concertoObject); + const json = withSlate ? result : ciceroMark.getSerializer().toJSON(result); result = JSON.stringify(json); } if (outPath) { diff --git a/packages/markdown-cli/package.json b/packages/markdown-cli/package.json index 11e1ee93..2d57d703 100644 --- a/packages/markdown-cli/package.json +++ b/packages/markdown-cli/package.json @@ -55,6 +55,7 @@ "dependencies": { "@accordproject/markdown-common": "0.1.15", "@accordproject/markdown-cicero": "0.1.15", + "@accordproject/markdown-slate": "0.1.15", "winston": "3.2.1" }, "license-check-config": { diff --git a/packages/markdown-cli/test/cli.js b/packages/markdown-cli/test/cli.js index 781e8265..014a64f2 100644 --- a/packages/markdown-cli/test/cli.js +++ b/packages/markdown-cli/test/cli.js @@ -42,14 +42,14 @@ Acceptance Criteria. The "Acceptance Criteria" are the specifications the { it('should parse a markdown file', async () => { - const result = await Commands.parse(sample, null, false, false, true); + const result = await Commands.parse(sample, null, false, false, false, true); result.should.eql(sampleExpectedJson); }); }); describe('#generateMarkdown', () => { it('should generate the text for a markdown file', async () => { - const result = await Commands.parse(sample, null, true, false, true); + const result = await Commands.parse(sample, null, true, false, false, true); result.should.eql(sampleExpectedText); }); }); diff --git a/packages/markdown-slate/index.js b/packages/markdown-slate/index.js index 76dec925..f20502c1 100644 --- a/packages/markdown-slate/index.js +++ b/packages/markdown-slate/index.js @@ -19,5 +19,4 @@ * @module markdown-transform */ -module.exports.commonMarkAstToSlate = require('./lib/commonMarkAstToSlate'); -module.exports.slateToCommonMarkAst = require('./lib/slateToCommonMarkAst'); \ No newline at end of file +module.exports.SlateMark = require('./lib/SlateMark');