Skip to content

Commit

Permalink
feature(list) add option to not index lists when generating markdown …
Browse files Browse the repository at this point in the history
…text

Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Sep 18, 2019
1 parent 30cb5dc commit e115c7c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
6 changes: 6 additions & 0 deletions packages/markdown-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand All @@ -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);}
Expand Down
14 changes: 10 additions & 4 deletions packages/markdown-cli/lib/Commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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));
Expand Down
6 changes: 3 additions & 3 deletions packages/markdown-common/src/CommonMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
Expand Down
49 changes: 29 additions & 20 deletions packages/markdown-common/src/ToMarkdownStringVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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':
Expand All @@ -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':
Expand All @@ -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':
Expand All @@ -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++;
});
Expand All @@ -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()}`);
Expand All @@ -211,4 +220,4 @@ class ToStringVisitor {
}
}

module.exports = ToStringVisitor;
module.exports = ToMarkdownStringVisitor;

0 comments on commit e115c7c

Please sign in to comment.