diff --git a/src/components/Widgets/MarkitupReactRenderer.js b/src/components/Widgets/MarkitupReactRenderer.js index a923fb65..f6272f43 100644 --- a/src/components/Widgets/MarkitupReactRenderer.js +++ b/src/components/Widgets/MarkitupReactRenderer.js @@ -1,5 +1,5 @@ import React, { PropTypes } from 'react'; -import MarkupIt, { Syntax, JSONUtils, BLOCKS, STYLES, ENTITIES } from 'markup-it'; +import MarkupIt, { Syntax, BLOCKS, STYLES, ENTITIES } from 'markup-it'; const defaultRenderers = { [BLOCKS.DOCUMENT]: 'article', @@ -36,14 +36,17 @@ const defaultRenderers = { }; function renderToken(token, index = 0, key = '0') { - const { type, data, text, tokens } = token; + const type = token.get('type'); + const data = token.get('data'); + const text = token.get('text'); + const tokens = token.get('tokens'); const nodeType = defaultRenderers[type]; key = `${key}.${index}`; // Only render if type is registered as renderer if (typeof nodeType !== 'undefined') { let children = null; - if (Array.isArray(tokens) && tokens.length) { + if (tokens.size) { children = tokens.map((token, idx) => renderToken(token, idx, key)); } else if (type === 'text') { children = text; @@ -52,9 +55,9 @@ function renderToken(token, index = 0, key = '0') { // If this is a react element return React.createElement( nodeType, - { key, ...data }, // Add key as a prop - Array.isArray(children) && children.length === 1 - ? children[0] : children); // Pass single child if possible + { key, ...data.toJS() }, // Add key as a prop + children + ); } else { // If this is a text node return children; @@ -80,10 +83,7 @@ export default class MarkitupReactRenderer extends React.Component { render() { const { value } = this.props; const content = this.parser.toContent(value); - const json = JSONUtils.encode(content); - // console.log(JSON.stringify(json, null, 2)); - - return renderToken(json.token); + return renderToken(content.get('token')); } } diff --git a/src/components/Widgets/__tests__/MarkitupReactRenderer.spec.js b/src/components/Widgets/__tests__/MarkitupReactRenderer.spec.js index d2094e06..85c31a98 100644 --- a/src/components/Widgets/__tests__/MarkitupReactRenderer.spec.js +++ b/src/components/Widgets/__tests__/MarkitupReactRenderer.spec.js @@ -81,6 +81,30 @@ Text with **bold** & _em_ elements }); }); + describe('Lists', () => { + it('should render lists', () => { + const value = ` +1. ol item 1 +1. ol item 2 + * Sublist 1 + * Sublist 2 + * Sublist 3 + 1. Sub-Sublist 1 + 1. Sub-Sublist 2 + 1. Sub-Sublist 3 +1. ol item 3 +`; + const component = shallow( + + ); + const tree = component.html(); + expect(tree).toMatchSnapshot(); + }); + }); + describe('Links', () => { it('should render links', () => { const value = ` diff --git a/src/components/Widgets/__tests__/__snapshots__/MarkitupReactRenderer.spec.js.snap b/src/components/Widgets/__tests__/__snapshots__/MarkitupReactRenderer.spec.js.snap index e3854e2c..df9033e3 100644 --- a/src/components/Widgets/__tests__/__snapshots__/MarkitupReactRenderer.spec.js.snap +++ b/src/components/Widgets/__tests__/__snapshots__/MarkitupReactRenderer.spec.js.snap @@ -1,7 +1,9 @@ -exports[`MarkitupReactRenderer should render HTML 1`] = `"

Paragraph with inline element

"`; +exports[`MarkitupReactRenderer HTML rendering should render HTML 1`] = `"

Paragraph with inline element

"`; -exports[`MarkitupReactRenderer should render HTML as is using Markdown 1`] = `"

Title

"`; +exports[`MarkitupReactRenderer Markdown rendering General should render markdown 1`] = `"

H1

Text with bold & em elements

H2

H3

  1. ol item 1
  2. ol item 2
  3. ol item 3

H4

link title

H5

\"alt

H6
"`; -exports[`MarkitupReactRenderer should render markdown 1`] = `"

H1

Text with bold & em elements

H2

H3

  1. ol item 1
  2. ol item 2
  3. ol item 3

H4

link title

H5

\"alt

H6
"`; +exports[`MarkitupReactRenderer Markdown rendering HTML should render HTML as is using Markdown 1`] = `"

Title

"`; -exports[`MarkitupReactRenderer should support custom syntax 1`] = `"
"`; +exports[`MarkitupReactRenderer Markdown rendering Lists should render lists 1`] = `"
  1. ol item 1
  2. ol item 2
    • Sublist 1
    • Sublist 2
    • Sublist 3
      1. Sub-Sublist 1
      2. Sub-Sublist 2
      3. Sub-Sublist 3
  3. ol item 3
"`; + +exports[`MarkitupReactRenderer custom elements should support custom syntax 1`] = `"
"`;