From 7fe1a6f8b68b6512bad900786c91bb54aa68db04 Mon Sep 17 00:00:00 2001 From: Andrey Okonetchnikov Date: Thu, 22 Sep 2016 17:44:25 +0200 Subject: [PATCH] Extracted renderToken function from class. Do not render additional `
`. --- .../Widgets/MarkitupReactRenderer.js | 46 +++-- .../__tests__/MarkitupReactRenderer.spec.js | 3 +- .../MarkitupReactRenderer.spec.js.snap | 168 +++++++++--------- 3 files changed, 106 insertions(+), 111 deletions(-) diff --git a/src/components/Widgets/MarkitupReactRenderer.js b/src/components/Widgets/MarkitupReactRenderer.js index 26137f7d..4f291cf2 100644 --- a/src/components/Widgets/MarkitupReactRenderer.js +++ b/src/components/Widgets/MarkitupReactRenderer.js @@ -21,30 +21,30 @@ const defaultRenderers = { 'unstyled': null, }; -export default class MarkitupReactRenderer extends React.Component { +function renderToken(token) { + const { type, data, text, tokens } = token; + const element = defaultRenderers[type]; - renderToken = (token) => { - const { type, data, text, tokens } = token; - const element = defaultRenderers[type]; - - // Only render if type is registered as renderer - if (typeof element !== 'undefined') { - let children = null; - if (Array.isArray(tokens) && tokens.length) { - children = tokens.map(this.renderToken); - } else if (type === 'text') { - children = text; - } - if (element !== null) { - // If this is a react element - return React.createElement(element, data, children); - } else { - // If this is a text node - return children; - } + // Only render if type is registered as renderer + if (typeof element !== 'undefined') { + let children = null; + if (Array.isArray(tokens) && tokens.length) { + children = tokens.map(renderToken); + } else if (type === 'text') { + children = text; + } + if (element !== null) { + // If this is a react element + return React.createElement(element, data, children); + } else { + // If this is a text node + return children; } - return null; } + return null; +} + +export default class MarkitupReactRenderer extends React.Component { render() { const { value, syntax } = this.props; @@ -57,9 +57,7 @@ export default class MarkitupReactRenderer extends React.Component { const json = JSONUtils.encode(content); // console.log(JSON.stringify(json, null, 2)); - return ( -
{this.renderToken(json.token)}
- ); + return renderToken(json.token); } } diff --git a/src/components/Widgets/__tests__/MarkitupReactRenderer.spec.js b/src/components/Widgets/__tests__/MarkitupReactRenderer.spec.js index 65ede560..c8b0e396 100644 --- a/src/components/Widgets/__tests__/MarkitupReactRenderer.spec.js +++ b/src/components/Widgets/__tests__/MarkitupReactRenderer.spec.js @@ -44,8 +44,7 @@ Text with **bold** & _em_ elements }); it('should support custom syntax', () => { - const value = ` -`; + const value = ''; const component = renderer.create( -
-

- Paragraph with - - inline - - element -

-
-
+
+

+ Paragraph with + + inline + + element +

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

- H1 -

-

- Text with - - bold - - & - - em - - elements -

-

- H2 -

-
    -
  • - ul item 1 -
  • -
  • - ul item 2 -
  • -
-

- H3 -

-
    -
  1. - ol item 1 -
  2. -
  3. - ol item 2 -
  4. -
  5. - ol item 3 -
  6. -
-

- H4 -

-

- - link title - -

-
- H5 -
-

- alt text -

-
- H6 -
-
-
+
+

+ H1 +

+

+ Text with + + bold + + & + + em + + elements +

+

+ H2 +

+ +

+ H3 +

+
    +
  1. + ol item 1 +
  2. +
  3. + ol item 2 +
  4. +
  5. + ol item 3 +
  6. +
+

+ H4 +

+

+ + link title + +

+
+ H5 +
+

+ alt text +

+
+ H6 +
+
`; + +exports[`MarkitupReactRenderer should support custom syntax 1`] = `
`;