Skip to content

Commit

Permalink
feature(cli) add --withSlate option to CLI
Browse files Browse the repository at this point in the history
Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Sep 12, 2019
1 parent e58b94c commit 19f45af
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
9 changes: 7 additions & 2 deletions packages/markdown-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand All @@ -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);}
})
Expand Down
21 changes: 14 additions & 7 deletions packages/markdown-cli/lib/Commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions packages/markdown-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions packages/markdown-cli/test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ Acceptance Criteria. The "Acceptance Criteria" are the specifications the <varia

describe('#parse', () => {
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);
});
});
Expand Down
3 changes: 1 addition & 2 deletions packages/markdown-slate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@
* @module markdown-transform
*/

module.exports.commonMarkAstToSlate = require('./lib/commonMarkAstToSlate');
module.exports.slateToCommonMarkAst = require('./lib/slateToCommonMarkAst');
module.exports.SlateMark = require('./lib/SlateMark');

0 comments on commit 19f45af

Please sign in to comment.