diff --git a/src/components/Widgets/MarkitupReactRenderer.js b/src/components/Widgets/MarkitupReactRenderer.js index f6272f43..2f2e2948 100644 --- a/src/components/Widgets/MarkitupReactRenderer.js +++ b/src/components/Widgets/MarkitupReactRenderer.js @@ -1,5 +1,6 @@ import React, { PropTypes } from 'react'; import MarkupIt, { Syntax, BLOCKS, STYLES, ENTITIES } from 'markup-it'; +import htmlSyntax from 'markup-it/syntaxes/html'; const defaultRenderers = { [BLOCKS.DOCUMENT]: 'article', @@ -8,7 +9,12 @@ const defaultRenderers = { [BLOCKS.BLOCKQUOTE]: 'blockquote', [BLOCKS.PARAGRAPH]: 'p', [BLOCKS.FOOTNOTE]: 'footnote', - [BLOCKS.HTML]: (props) => null, + [BLOCKS.HTML]: (token) => { + return ; + }, [BLOCKS.HR]: 'hr', [BLOCKS.HEADING_1]: 'h1', [BLOCKS.HEADING_2]: 'h2', @@ -39,6 +45,7 @@ function renderToken(token, index = 0, key = '0') { const type = token.get('type'); const data = token.get('data'); const text = token.get('text'); + const raw = token.get('raw'); const tokens = token.get('tokens'); const nodeType = defaultRenderers[type]; key = `${key}.${index}`; @@ -52,7 +59,12 @@ function renderToken(token, index = 0, key = '0') { children = text; } if (nodeType !== null) { + + if (typeof nodeType === 'function') { + return nodeType(token); + } // If this is a react element + console.log(data.toJS()); return React.createElement( nodeType, { key, ...data.toJS() }, // Add key as a prop diff --git a/src/components/Widgets/__tests__/MarkitupReactRenderer.spec.js b/src/components/Widgets/__tests__/MarkitupReactRenderer.spec.js index 85c31a98..8705e420 100644 --- a/src/components/Widgets/__tests__/MarkitupReactRenderer.spec.js +++ b/src/components/Widgets/__tests__/MarkitupReactRenderer.spec.js @@ -81,6 +81,22 @@ Text with **bold** & _em_ elements }); }); + describe('Headings', () => { + for (const heading of [...Array(6).keys()]) { + it(`should render Heading ${heading + 1}`, () => { + const value = padStart(' Title', heading + 7, '#') + const component = shallow( + + ); + const tree = component.html(); + expect(tree).toMatchSnapshot() + }) + } + }) + describe('Lists', () => { it('should render lists', () => { const value = ` @@ -121,8 +137,7 @@ I get 10 times more traffic from [Google] [1] than from [Yahoo] [2] or [MSN] [3] /> ); const tree = component.html(); - const expected = ''; - expect(tree).toEqual(expected); + expect(tree).toMatchSnapshot(); }); }); @@ -136,8 +151,7 @@ I get 10 times more traffic from [Google] [1] than from [Yahoo] [2] or [MSN] [3] /> ); const tree = component.html(); - const expected = '

Use the printf() function.

'; - expect(tree).toEqual(expected); + expect(tree).toMatchSnapshot(); }); it('should render code 2', () => { @@ -149,8 +163,7 @@ I get 10 times more traffic from [Google] [1] than from [Yahoo] [2] or [MSN] [3] /> ); const tree = component.html(); - const expected = '

There is a literal backtick (`) here.

'; - expect(tree).toEqual(expected); + expect(tree).toMatchSnapshot(); }); }); @@ -159,10 +172,10 @@ I get 10 times more traffic from [Google] [1] than from [Yahoo] [2] or [MSN] [3] const value = ` # Title -
-
Test HTML content
-
Testing HTML in Markdown
-
+ `; const component = shallow( { it('should render HTML', () => { - const value = '

Paragraph with inline element

'; + const value = '

Paragraph with inline element

'; const component = shallow(

Paragraph with inline element

"`; +exports[`MarkitupReactRenderer Markdown rendering Code should render code 1`] = `"

Use the printf() function.

"`; + +exports[`MarkitupReactRenderer Markdown rendering Code should render code 2 1`] = `"

There is a literal backtick (\`) here.

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

H1

Text with bold & em elements

H2

  • ul item 1
  • ul item 2

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 Markdown rendering HTML should render HTML as is using Markdown 1`] = `"

Title

  • Test HTML content
  • Testing HTML in Markdown
"`; + +exports[`MarkitupReactRenderer Markdown rendering Headings should render Heading 1 1`] = `"

Title

"`; + +exports[`MarkitupReactRenderer Markdown rendering Headings should render Heading 2 1`] = `"

Title

"`; + +exports[`MarkitupReactRenderer Markdown rendering Headings should render Heading 3 1`] = `"

Title

"`; + +exports[`MarkitupReactRenderer Markdown rendering Headings should render Heading 4 1`] = `"

Title

"`; + +exports[`MarkitupReactRenderer Markdown rendering Headings should render Heading 5 1`] = `"
Title
"`; + +exports[`MarkitupReactRenderer Markdown rendering Headings should render Heading 6 1`] = `"
Title
"`; + +exports[`MarkitupReactRenderer Markdown rendering Links should render links 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 extend default renderers with custom ones 1`] = `"

"`; + exports[`MarkitupReactRenderer custom elements should support custom syntax 1`] = `"
"`; + +exports[`MarkitupReactRenderer custom elements should support custom syntaxes 1`] = `"

"`;