Skip to content

Commit

Permalink
refactor(slate) Add simple class to serve as toplevel Slate transform…
Browse files Browse the repository at this point in the history
… API

Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Sep 12, 2019
1 parent 8f5a9d4 commit e58b94c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 43 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <a name="license"></a>
Expand Down
9 changes: 5 additions & 4 deletions packages/markdown-slate/src/Slate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

/**
Expand All @@ -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));

Expand All @@ -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
Expand Down
50 changes: 50 additions & 0 deletions packages/markdown-slate/src/SlateMark.js
Original file line number Diff line number Diff line change
@@ -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;
33 changes: 0 additions & 33 deletions packages/markdown-slate/src/commonMarkAstToSlate.js

This file was deleted.

0 comments on commit e58b94c

Please sign in to comment.