Skip to content

Commit

Permalink
fix(slate) Add toMarkdownCicero convenience function to Slate transfo…
Browse files Browse the repository at this point in the history
…rmer

Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Aug 6, 2020
1 parent a6d88fe commit d8e6e84
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/markdown-slate/lib/SlateTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ class SlateTransformer {
return this.ciceroMarkTransformer.toMarkdown(ciceroMark, options);
}

/**
* Converts a Slate JSON to a markdown cicero string
* @param {*} value - Slate json
* @param {object} [options] - configuration options
* @returns {*} markdown cicero string
*/
toMarkdownCicero(value, options) {
const ciceroMark = this.toCiceroMark(value);
const ciceroMarkUnwrapped = this.ciceroMarkTransformer.toCiceroMarkUnwrapped(ciceroMark, options);
return this.ciceroMarkTransformer.toMarkdownCicero(ciceroMarkUnwrapped, options);
}

/**
* Converts a markdown string to a Slate JSON
* @param {string} markdown - a markdown string
Expand Down
27 changes: 27 additions & 0 deletions packages/markdown-slate/lib/SlateTransformer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ let slateTransformer = null;
/* eslint-disable no-undef */
// @ts-nocheck

/**
* Prepare the text for parsing (normalizes new lines, etc)
* @param {string} input - the text for the clause
* @return {string} - the normalized text for the clause
*/
function normalizeNLs(input) {
// we replace all \r and \n with \n
let text = input.replace(/\r/gm,'');
return text;
}

// @ts-ignore
// eslint-disable-next-line no-undef
beforeAll(() => {
Expand Down Expand Up @@ -129,4 +140,20 @@ describe('ciceromark <-> slate', () => {
expect(expectedSlateValue).toEqual(value);
});
});
});

describe('slate -> markdown_cicero', () => {
it('converts acceptance from slate to markdown cicero', () => {
// load slate DOM
const slateDom = JSON.parse(fs.readFileSync(__dirname + '/../test/data/ciceromark/acceptance_slate.json', 'utf8'));
// load expected markdown cicero
const expectedMarkdownCicero = normalizeNLs(fs.readFileSync(__dirname + '/../test/data/ciceromark/acceptance.md', 'utf8'));

// convert the slate to markdown cicero and compare
const actualMarkdownCicero = slateTransformer.toMarkdownCicero(slateDom);
expect(actualMarkdownCicero).toMatchSnapshot(); // (3)

// check roundtrip
expect(actualMarkdownCicero).toEqual(expectedMarkdownCicero);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6084,3 +6084,38 @@ Object {
"xmlns": "http://commonmark.org/xml/1.0",
}
`;

exports[`slate -> markdown_cicero converts acceptance from slate to markdown cicero 1`] = `
"Heading
====
And below is a **clause**.
{{#clause deliveryClause}}
Acceptance of Delivery.
----
\\"Party A\\" will be deemed to have completed its delivery obligations
if in \\"Party B\\"'s opinion, the \\"Widgets\\" satisfies the
Acceptance Criteria, and \\"Party B\\" notifies \\"Party A\\" in writing
that it is accepting the \\"Widgets\\".
Inspection and Notice.
----
\\"Party B\\" will have 10 Business Days to inspect and
evaluate the \\"Widgets\\" on the delivery date before notifying
\\"Party A\\" that it is either accepting or rejecting the
\\"Widgets\\".
Acceptance Criteria.
----
The \\"Acceptance Criteria\\" are the specifications the \\"Widgets\\"
must meet for the \\"Party A\\" to comply with its requirements and
obligations under this agreement, detailed in \\"Attachment X\\", attached
to this agreement.
{{/clause}}
More text"
`;
32 changes: 32 additions & 0 deletions packages/markdown-slate/test/data/ciceromark/acceptance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Heading
====

And below is a **clause**.

{{#clause deliveryClause}}
Acceptance of Delivery.
----

"Party A" will be deemed to have completed its delivery obligations
if in "Party B"'s opinion, the "Widgets" satisfies the
Acceptance Criteria, and "Party B" notifies "Party A" in writing
that it is accepting the "Widgets".

Inspection and Notice.
----

"Party B" will have 10 Business Days to inspect and
evaluate the "Widgets" on the delivery date before notifying
"Party A" that it is either accepting or rejecting the
"Widgets".

Acceptance Criteria.
----

The "Acceptance Criteria" are the specifications the "Widgets"
must meet for the "Party A" to comply with its requirements and
obligations under this agreement, detailed in "Attachment X", attached
to this agreement.
{{/clause}}

More text

0 comments on commit d8e6e84

Please sign in to comment.