Skip to content

Commit b4828e7

Browse files
authored
(feat) add option to remove formatting when converting from ciceromark to commonmark (#195)
Signed-off-by: Dan Selman <[email protected]>
1 parent ad0644f commit b4828e7

File tree

7 files changed

+502
-4
lines changed

7 files changed

+502
-4
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,4 @@
131131
}
132132
},
133133
"dependencies": {}
134-
}
134+
}

packages/markdown-cicero/src/CiceroMarkTransformer.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,41 @@ class CiceroMarkTransformer {
137137
* @param {string} [format] result format, defaults to 'concerto'. Pass
138138
* 'json' to return the JSON data.
139139
* @param {object} [options] configuration options
140+
* @param {boolean} [options.removeFormatting] if true the formatting nodes are removed
140141
* @returns {*} json commonmark object
141142
*/
142143
toCommonMark(input, format='concerto', options) {
144+
145+
// remove formatting
146+
if(options && options.removeFormatting) {
147+
// convert to JSON
148+
if(input.getType) {
149+
input = this.serializer.toJSON(input);
150+
}
151+
152+
const cmt = new CommonMarkTransformer(options);
153+
input = cmt.removeFormatting(input);
154+
155+
// now we need to also remove the formatting for clause nodes
156+
const clauses = input.nodes.filter(element => element.$class === 'org.accordproject.ciceromark.Clause');
157+
clauses.forEach(clause => {
158+
const clauseContent = {
159+
$class : input.$class,
160+
xmlns : input.xmlns,
161+
nodes : clause.nodes
162+
};
163+
164+
const unformatted = cmt.removeFormatting(clauseContent);
165+
clause.nodes = unformatted.nodes;
166+
});
167+
}
168+
169+
// convert to concerto
143170
if(!input.getType) {
144171
input = this.serializer.fromJSON(input);
145172
}
146173

147-
// Add Cicero nodes
174+
// convert to common mark
148175
const parameters = {
149176
commonMark: this.commonMark,
150177
modelManager : this.modelManager,

packages/markdown-cicero/src/CiceroMarkTransformer.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ describe('acceptance', () => {
164164
expect(clauseText).toMatchSnapshot();
165165
expect(ciceroMarkTransformer.getClauseText.bind(json.nodes[1], { wrapVariables: false})).toThrow('Cannot apply getClauseText to non-clause node');
166166
});
167+
168+
169+
it('converts acceptance clause content to CommonMark string (removeFormatting)', () => {
170+
const markdownText = fs.readFileSync(__dirname + '/../test/data/acceptance-formatted.md', 'utf8');
171+
const json = ciceroMarkTransformer.fromMarkdown(markdownText, 'json');
172+
// console.log(JSON.stringify(json, null, 4));
173+
expect(json).toMatchSnapshot();
174+
const newMarkdown = ciceroMarkTransformer.toCommonMark(json, 'json', { removeFormatting: true });
175+
expect(newMarkdown).toMatchSnapshot();
176+
});
167177
});
168178

169179
describe('markdown-spec', () => {

0 commit comments

Comments
 (0)