diff --git a/packages/markdown-cli/index.js b/packages/markdown-cli/index.js index 506cdb14..678a4bdb 100755 --- a/packages/markdown-cli/index.js +++ b/packages/markdown-cli/index.js @@ -48,6 +48,11 @@ require('yargs') type: 'boolean', default: false }); + yargs.option('noIndex', { + describe: 'do not index ordered lists', + type: 'boolean', + default: false + }); }, (argv) => { if (argv.verbose) { Logger.info(`parse sample ${argv.sample} markdown`); @@ -60,6 +65,7 @@ require('yargs') options.cicero = argv.cicero; options.slate = argv.slate; options.noWrap = argv.noWrap; + options.noIndex = argv.noIndex; return Commands.parse(argv.sample, argv.out, options) .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 9236012b..5714dce1 100644 --- a/packages/markdown-cli/lib/Commands.js +++ b/packages/markdown-cli/lib/Commands.js @@ -90,11 +90,16 @@ class Commands { * @param {boolean} [options.cicero] whether to further transform for Cicero * @param {boolean} [options.slate] whether to further transform for Slate * @param {boolean} [options.noWrap] whether to avoid wrapping Cicero variables in XML tags + * @param {boolean} [options.noIndex] do not index ordered list (i.e., use 1. everywhere) * @returns {object} Promise to the result of parsing */ static parse(samplePath, outPath, options) { - const { roundtrip, cicero, slate, noWrap } = options; - const commonMark = new CommonMark({ tagInfo: true }); + const { roundtrip, cicero, slate, noWrap, noIndex } = options; + const commonOptions = {}; + commonOptions.tagInfo = true; + commonOptions.noIndex = noIndex ? true : false; + + const commonMark = new CommonMark(commonOptions); const ciceroMark = new CiceroMark(); const slateMark = new SlateMark(); @@ -108,8 +113,9 @@ class Commands { } if (roundtrip) { if (cicero) { - const options = noWrap ? { wrapVariables: false } : null; - result = ciceroMark.toCommonMark(result, options); + const ciceroOptions = {}; + ciceroOptions.wrapVariables = noWrap ? false : true; + result = ciceroMark.toCommonMark(result, ciceroOptions); } else if (slate) { result = slateMark.toCommonMark(result); //console.log('AFTER COMMONMARK ' + JSON.stringify(result)); diff --git a/packages/markdown-common/src/CommonMark.js b/packages/markdown-common/src/CommonMark.js index 9244609a..af20cfa3 100644 --- a/packages/markdown-common/src/CommonMark.js +++ b/packages/markdown-common/src/CommonMark.js @@ -38,6 +38,7 @@ class CommonMark { * @param {boolean} [options.trimText] trims all text nodes * @param {boolean} [options.disableValidation] returns unvalidated JSON, rather than a Concerto model * @param {boolean} [options.enableSourceLocation] if true then location information is returned + * @param {boolean} [options.noIndex] do not index ordered list (i.e., use 1. everywhere) * @param {boolean} [options.tagInfo] Construct tags for HTML elements */ constructor(options) { @@ -258,15 +259,14 @@ class CommonMark { /** * Converts a commonmark document to a markdown string * @param {*} concertoObject - concerto commonmark object - * @param {*} options - options (e.g., wrapVariables) * @returns {string} the markdown string */ - toMarkdownStringConcerto(concertoObject, options) { + toMarkdownStringConcerto(concertoObject) { const parameters = {}; parameters.result = ''; parameters.first = true; parameters.indent = 0; - const visitor = new ToMarkdownStringVisitor(options); + const visitor = new ToMarkdownStringVisitor(this.options); concertoObject.accept(visitor, parameters); return parameters.result.trim(); } diff --git a/packages/markdown-common/src/ToMarkdownStringVisitor.js b/packages/markdown-common/src/ToMarkdownStringVisitor.js index c10a55b0..4b7af946 100644 --- a/packages/markdown-common/src/ToMarkdownStringVisitor.js +++ b/packages/markdown-common/src/ToMarkdownStringVisitor.js @@ -21,7 +21,16 @@ * so this transformation is not guaranteed to equivalent if you roundtrip * markdown content. The resulting AST *should* be equivalent however. */ -class ToStringVisitor { +class ToMarkdownStringVisitor { + /** + * Construct the visitor. + * @param {object} [options] configuration options + * @param {boolean} [options.noIndex] do not index ordered list (i.e., use 1. everywhere) + */ + constructor(options) { + this.options = options; + } + /** * Visits a sub-tree and return the markdown * @param {*} visitor the visitor to use @@ -122,7 +131,7 @@ class ToStringVisitor { switch(thing.getType()) { case 'CodeBlock': - ToStringVisitor.newParagraph(parameters); + ToMarkdownStringVisitor.newParagraph(parameters); parameters.result += `\`\`\`${thing.info ? ' ' + thing.info : ''}\n${thing.text}\`\`\`\n\n`; break; case 'Code': @@ -132,28 +141,28 @@ class ToStringVisitor { parameters.result += thing.text; break; case 'Emph': - parameters.result += `*${ToStringVisitor.visitChildren(this, thing)}*`; + parameters.result += `*${ToMarkdownStringVisitor.visitChildren(this, thing)}*`; break; case 'Strong': - parameters.result += `**${ToStringVisitor.visitChildren(this, thing)}**`; + parameters.result += `**${ToMarkdownStringVisitor.visitChildren(this, thing)}**`; break; case 'BlockQuote': { - const parametersIn = ToStringVisitor.mkParametersIn(parameters); - ToStringVisitor.newParagraph(parameters); - parameters.result += `> ${ToStringVisitor.visitChildren(this, thing, parametersIn)}`; + const parametersIn = ToMarkdownStringVisitor.mkParametersIn(parameters); + ToMarkdownStringVisitor.newParagraph(parameters); + parameters.result += `> ${ToMarkdownStringVisitor.visitChildren(this, thing, parametersIn)}`; } break; case 'Heading': { const level = parseInt(thing.level); if (level < 3) { - parameters.result += `${ToStringVisitor.visitChildren(this, thing)}\n${ToStringVisitor.mkSetextHeading(level)}`; + parameters.result += `${ToMarkdownStringVisitor.visitChildren(this, thing)}\n${ToMarkdownStringVisitor.mkSetextHeading(level)}`; } else { - parameters.result += `${ToStringVisitor.mkATXHeading(level)} ${ToStringVisitor.visitChildren(this, thing)}`; + parameters.result += `${ToMarkdownStringVisitor.mkATXHeading(level)} ${ToMarkdownStringVisitor.visitChildren(this, thing)}`; } } break; case 'ThematicBreak': - ToStringVisitor.newParagraph(parameters); + ToMarkdownStringVisitor.newParagraph(parameters); parameters.result += '---\n'; break; case 'Linebreak': @@ -163,17 +172,17 @@ class ToStringVisitor { parameters.result += '\n'; break; case 'Link': - parameters.result += `[${ToStringVisitor.visitChildren(this, thing)}](${thing.destination})`; + parameters.result += `[${ToMarkdownStringVisitor.visitChildren(this, thing)}](${thing.destination})`; break; case 'Image': - parameters.result += `![${ToStringVisitor.visitChildren(this, thing)}](${thing.destination})`; + parameters.result += `![${ToMarkdownStringVisitor.visitChildren(this, thing)}](${thing.destination})`; break; case 'Paragraph': - ToStringVisitor.newParagraph(parameters); - parameters.result += `${ToStringVisitor.visitChildren(this, thing)}`; + ToMarkdownStringVisitor.newParagraph(parameters); + parameters.result += `${ToMarkdownStringVisitor.visitChildren(this, thing)}`; break; case 'HtmlBlock': - ToStringVisitor.newParagraph(parameters); + ToMarkdownStringVisitor.newParagraph(parameters); parameters.result += `${thing.text}`; break; case 'Text': @@ -185,15 +194,15 @@ class ToStringVisitor { // Always start with a new line parameters.result += '\n'; thing.nodes.forEach(item => { - const parametersIn = ToStringVisitor.mkParametersInList(parameters); + const parametersIn = ToMarkdownStringVisitor.mkParametersInList(parameters); if(thing.tight === 'false' && index !== first) { parameters.result += '\n'; } if(thing.type === 'ordered') { - parameters.result += `\n${ToStringVisitor.mkIndent(parameters)}${index}. ${ToStringVisitor.visitChildren(this, item, parametersIn)}`; + parameters.result += `\n${ToMarkdownStringVisitor.mkIndent(parameters)}${ this.options.noIndex ? 1 : index}. ${ToMarkdownStringVisitor.visitChildren(this, item, parametersIn)}`; } else { - parameters.result += `\n${ToStringVisitor.mkIndent(parameters)}- ${ToStringVisitor.visitChildren(this, item, parametersIn)}`; + parameters.result += `\n${ToMarkdownStringVisitor.mkIndent(parameters)}- ${ToMarkdownStringVisitor.visitChildren(this, item, parametersIn)}`; } index++; }); @@ -202,7 +211,7 @@ class ToStringVisitor { case 'Item': throw new Error('Item node should not occur outside of List nodes'); case 'Document': - parameters.result += ToStringVisitor.visitChildren(this, thing); + parameters.result += ToMarkdownStringVisitor.visitChildren(this, thing); break; default: throw new Error(`Unhandled type ${thing.getType()}`); @@ -211,4 +220,4 @@ class ToStringVisitor { } } -module.exports = ToStringVisitor; \ No newline at end of file +module.exports = ToMarkdownStringVisitor; \ No newline at end of file