File size: 3,886 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { HtmlToSlate } from '../htmlToSlate';
import defaultPlugins from '../plugins';
import makeSlatePluginsFromDef from '../utils/makeSlatePluginsFromDef';

const htmlToSlate = HtmlToSlate({
  plugins: makeSlatePluginsFromDef(defaultPlugins),
});
describe('HtmlToSlate', () => {
  it('parses h1 tags', async () => {
    expect(await htmlToSlate('<h1>Projects</h1>')).toEqual({
      slate: [
        {
          type: 'HEADINGS/HEADING-ONE',
          children: [{ text: 'Projects' }],
        },
      ],
    });
  });
  it('parses p tags', async () => {
    expect(await htmlToSlate('<p>some projects</p>')).toEqual({
      slate: [
        {
          type: 'PARAGRAPH/PARAGRAPH',
          children: [{ text: 'some projects' }],
        },
      ],
    });
  });
  it('parses a list of tags to an array', async () => {
    expect(
      await htmlToSlate(`
      <h1>Hello World</h1>
    <p>Lorem ipsum</p>`)
    ).toEqual({
      slate: [
        {
          type: 'HEADINGS/HEADING-ONE',
          children: [{ text: 'Hello World' }],
        },
        {
          type: 'PARAGRAPH/PARAGRAPH',
          children: [{ text: 'Lorem ipsum' }],
        },
      ],
    });
  });
  it('parses nested tags with headin', async () => {
    expect(
      await htmlToSlate('<p>some <em>projects</em>-<strong>foo</strong></p>')
    ).toEqual({
      slate: [
        {
          children: [
            {
              text: 'some ',
            },
            {
              'EMPHASIZE/EM': true,
              text: 'projects',
            },
            { text: '-' },
            {
              'EMPHASIZE/STRONG': true,
              text: 'foo',
            },
          ],
          type: 'PARAGRAPH/PARAGRAPH',
        },
      ],
    });
  });
  it('parses code blocks', async () => {
    expect(
      await htmlToSlate(
        '<p>a<code style="white-space:pre-wrap">bc</code>de</p>'
      )
    ).toEqual({
      slate: [
        {
          type: 'PARAGRAPH/PARAGRAPH',
          children: [
            {
              text: 'a',
            },
            {
              text: 'bc',

              'CODE/CODE': true,
            },
            {
              text: 'de',
            },
          ],
        },
      ],
    });
  });
  it('can parse links', async () => {
    expect(await htmlToSlate('<h4>a<a href="foo">asdf</a>b</h4>')).toEqual({
      slate: [
        {
          children: [
            { text: 'a' },
            {
              data: { href: 'foo', openInNewWindow: false },
              children: [{ text: 'asdf' }],
              type: 'LINK/LINK',
            },
            { text: 'b' },
          ],
          type: 'HEADINGS/HEADING-FOUR',
        },
      ],
    });
  });
  it('parses text align on headings', async () => {
    expect(
      await htmlToSlate('<h3 style="text-align:center">asdfgh</h3>')
    ).toEqual({
      slate: [
        {
          type: 'HEADINGS/HEADING-THREE',
          children: [
            {
              text: 'asdfgh',
            },
          ],

          data: {
            align: 'center',
          },
        },
      ],
    });
  });
  it('parses text align on paragraphs', async () => {
    expect(await htmlToSlate('<p style="text-align:center">ab</p>')).toEqual({
      slate: [
        {
          type: 'PARAGRAPH/PARAGRAPH',
          children: [{ text: 'ab' }],
          data: {
            align: 'center',
          },
        },
      ],
    });
  });
  it('parses blockquote', async () => {
    expect(
      await htmlToSlate(
        '<blockquote style="text-align:center">asdfgh</blockquote>'
      )
    ).toEqual({
      slate: [
        {
          type: 'BLOCKQUOTE/BLOCKQUOTE',
          children: [
            {
              text: 'asdfgh',
            },
          ],

          data: {
            align: 'center',
          },
        },
      ],
    });
  });
});