Skip to content

Commit d8e6e84

Browse files
committed
fix(slate) Add toMarkdownCicero convenience function to Slate transformer
Signed-off-by: Jerome Simeon <[email protected]>
1 parent a6d88fe commit d8e6e84

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

packages/markdown-slate/lib/SlateTransformer.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ class SlateTransformer {
108108
return this.ciceroMarkTransformer.toMarkdown(ciceroMark, options);
109109
}
110110

111+
/**
112+
* Converts a Slate JSON to a markdown cicero string
113+
* @param {*} value - Slate json
114+
* @param {object} [options] - configuration options
115+
* @returns {*} markdown cicero string
116+
*/
117+
toMarkdownCicero(value, options) {
118+
const ciceroMark = this.toCiceroMark(value);
119+
const ciceroMarkUnwrapped = this.ciceroMarkTransformer.toCiceroMarkUnwrapped(ciceroMark, options);
120+
return this.ciceroMarkTransformer.toMarkdownCicero(ciceroMarkUnwrapped, options);
121+
}
122+
111123
/**
112124
* Converts a markdown string to a Slate JSON
113125
* @param {string} markdown - a markdown string

packages/markdown-slate/lib/SlateTransformer.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ let slateTransformer = null;
2323
/* eslint-disable no-undef */
2424
// @ts-nocheck
2525

26+
/**
27+
* Prepare the text for parsing (normalizes new lines, etc)
28+
* @param {string} input - the text for the clause
29+
* @return {string} - the normalized text for the clause
30+
*/
31+
function normalizeNLs(input) {
32+
// we replace all \r and \n with \n
33+
let text = input.replace(/\r/gm,'');
34+
return text;
35+
}
36+
2637
// @ts-ignore
2738
// eslint-disable-next-line no-undef
2839
beforeAll(() => {
@@ -129,4 +140,20 @@ describe('ciceromark <-> slate', () => {
129140
expect(expectedSlateValue).toEqual(value);
130141
});
131142
});
143+
});
144+
145+
describe('slate -> markdown_cicero', () => {
146+
it('converts acceptance from slate to markdown cicero', () => {
147+
// load slate DOM
148+
const slateDom = JSON.parse(fs.readFileSync(__dirname + '/../test/data/ciceromark/acceptance_slate.json', 'utf8'));
149+
// load expected markdown cicero
150+
const expectedMarkdownCicero = normalizeNLs(fs.readFileSync(__dirname + '/../test/data/ciceromark/acceptance.md', 'utf8'));
151+
152+
// convert the slate to markdown cicero and compare
153+
const actualMarkdownCicero = slateTransformer.toMarkdownCicero(slateDom);
154+
expect(actualMarkdownCicero).toMatchSnapshot(); // (3)
155+
156+
// check roundtrip
157+
expect(actualMarkdownCicero).toEqual(expectedMarkdownCicero);
158+
});
132159
});

packages/markdown-slate/lib/__snapshots__/SlateTransformer.test.js.snap

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6084,3 +6084,38 @@ Object {
60846084
"xmlns": "http://commonmark.org/xml/1.0",
60856085
}
60866086
`;
6087+
6088+
exports[`slate -> markdown_cicero converts acceptance from slate to markdown cicero 1`] = `
6089+
"Heading
6090+
====
6091+
6092+
And below is a **clause**.
6093+
6094+
{{#clause deliveryClause}}
6095+
Acceptance of Delivery.
6096+
----
6097+
6098+
\\"Party A\\" will be deemed to have completed its delivery obligations
6099+
if in \\"Party B\\"'s opinion, the \\"Widgets\\" satisfies the
6100+
Acceptance Criteria, and \\"Party B\\" notifies \\"Party A\\" in writing
6101+
that it is accepting the \\"Widgets\\".
6102+
6103+
Inspection and Notice.
6104+
----
6105+
6106+
\\"Party B\\" will have 10 Business Days to inspect and
6107+
evaluate the \\"Widgets\\" on the delivery date before notifying
6108+
\\"Party A\\" that it is either accepting or rejecting the
6109+
\\"Widgets\\".
6110+
6111+
Acceptance Criteria.
6112+
----
6113+
6114+
The \\"Acceptance Criteria\\" are the specifications the \\"Widgets\\"
6115+
must meet for the \\"Party A\\" to comply with its requirements and
6116+
obligations under this agreement, detailed in \\"Attachment X\\", attached
6117+
to this agreement.
6118+
{{/clause}}
6119+
6120+
More text"
6121+
`;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Heading
2+
====
3+
4+
And below is a **clause**.
5+
6+
{{#clause deliveryClause}}
7+
Acceptance of Delivery.
8+
----
9+
10+
"Party A" will be deemed to have completed its delivery obligations
11+
if in "Party B"'s opinion, the "Widgets" satisfies the
12+
Acceptance Criteria, and "Party B" notifies "Party A" in writing
13+
that it is accepting the "Widgets".
14+
15+
Inspection and Notice.
16+
----
17+
18+
"Party B" will have 10 Business Days to inspect and
19+
evaluate the "Widgets" on the delivery date before notifying
20+
"Party A" that it is either accepting or rejecting the
21+
"Widgets".
22+
23+
Acceptance Criteria.
24+
----
25+
26+
The "Acceptance Criteria" are the specifications the "Widgets"
27+
must meet for the "Party A" to comply with its requirements and
28+
obligations under this agreement, detailed in "Attachment X", attached
29+
to this agreement.
30+
{{/clause}}
31+
32+
More text

0 commit comments

Comments
 (0)