diff --git a/README.md b/README.md index c3c3fb29..1e0b2838 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,19 @@ # Markdown Transform -A framework to transform markdown to/from a CommonMark AST defined using Concerto: -https://models.accordproject.org/commonmark/markdown.html +A markdown transformation framework ## Documentation -CommonmarkParser converts markdown text to an instance of the CommonMark AST. - -CommonmarkToString converts an AST to a markdown string. +CommonMark converts markdown text to/from an instance of the CommonMark AST. +CiceroMark converts markdown ASTs to/from Cicero-specific Markdown AST. +SlateMark converts markdown ASTs to Slate DOM. See the unit test for example usage. ## Installation ``` -npm install @accordproject/markdown-transform --save +npm install -g @accordproject/markdown-cli --save ``` ## License diff --git a/packages/markdown-slate/src/Slate.test.js b/packages/markdown-slate/src/Slate.test.js index 34c24b36..8d44dc40 100644 --- a/packages/markdown-slate/src/Slate.test.js +++ b/packages/markdown-slate/src/Slate.test.js @@ -20,14 +20,15 @@ const fs = require('fs'); const path = require('path'); const Value = require('slate').Value; const CommonMark = require('@accordproject/markdown-common').CommonMark; -const slateToCommonMarkAst = require('./slateToCommonMarkAst'); -const commonMarkAstToSlate = require('./commonMarkAstToSlate'); +const SlateMark = require('./SlateMark'); let commonMark = null; +let slateMark = null; // @ts-ignore beforeAll(() => { commonMark = new CommonMark(); + slateMark = new SlateMark(); }); /** @@ -53,7 +54,7 @@ describe('slate', () => { it(`converts ${file} to concerto`, () => { const slateDom = JSON.parse(jsonText); const value = Value.fromJSON(slateDom); - const concertoObject = slateToCommonMarkAst(value.document); + const concertoObject = slateMark.toCommonMark(value.document); const json = commonMark.getSerializer().toJSON(concertoObject); console.log('From slate', JSON.stringify(json, null, 4)); @@ -74,7 +75,7 @@ describe('slate', () => { expect(json).toEqual(expectedJson); // now convert the expected ast back to slate and compare - const expectedSlate = commonMarkAstToSlate(expectedConcertoObject); + const expectedSlate = slateMark.fromCommonMark(expectedConcertoObject); console.log('Expected Slate', JSON.stringify(expectedSlate, null, 4)); // check roundtrip diff --git a/packages/markdown-slate/src/SlateMark.js b/packages/markdown-slate/src/SlateMark.js new file mode 100644 index 00000000..819dc95f --- /dev/null +++ b/packages/markdown-slate/src/SlateMark.js @@ -0,0 +1,50 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const ToSlateVisitor = require('./ToSlateVisitor'); +const slateToCommonMarkAst = require('./slateToCommonMarkAst'); + +/** + * Parses markdown using the commonmark parser into the + * intermediate representation: a JSON object that adheres to + * the 'org.accordproject.commonmark' Concerto model. + */ +class SlateMark { + /** + * Converts a commonmark ast to a Slate DOM + * @param {*} concertoObject concerto commonmark object + * @returns {*} the slate dom + */ + fromCommonMark(concertoObject) { + const parameters = {}; + parameters.result = {}; + parameters.marks = []; + const visitor = new ToSlateVisitor(); + concertoObject.accept( visitor, parameters ); + return parameters.result; + } + + /** + * Converts a Slate document node to CommonMark AST + * @param {*} document the Slate document node + * @returns {*} the common mark AST + */ + toCommonMark(document) { + return slateToCommonMarkAst(document); + } +} + +module.exports = SlateMark; \ No newline at end of file diff --git a/packages/markdown-slate/src/commonMarkAstToSlate.js b/packages/markdown-slate/src/commonMarkAstToSlate.js deleted file mode 100644 index 6e5b78b2..00000000 --- a/packages/markdown-slate/src/commonMarkAstToSlate.js +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -const ToSlateVisitor = require('./ToSlateVisitor'); - -/** - * Converts a commonmark ast to a Slate DOM - * @param {*} concertoObject concerto commonmark object - * @returns {*} the slate dom - */ -function commonMarkAstToSlate(concertoObject) { - const parameters = {}; - parameters.result = {}; - parameters.marks = []; - const visitor = new ToSlateVisitor(); - concertoObject.accept( visitor, parameters ); - return parameters.result; -} - -module.exports = commonMarkAstToSlate; \ No newline at end of file