Welcome to mirror list, hosted at ThFree Co, Russian Federation.

markdown.spec.js « tests « src - github.com/nextcloud/text.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 674f5c41698df2b707da47c04003932a0b62bcd6 (plain)
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
import { createEditor, createMarkdownSerializer } from './../EditorFactory';
import spec from "./fixtures/spec"
import markdownit from './../markdownit'

const markdownThroughEditor = (markdown) => {
  const tiptap = createEditor({
    content: markdownit.render(markdown),
    enableRichEditing: true
  })
  const serializer = createMarkdownSerializer(tiptap.nodes, tiptap.marks)
  return serializer.serialize(tiptap.state.doc)
}

const markdownThroughEditorHtml = (html) => {
  const tiptap = createEditor({
    content: html,
    enableRichEditing: true
  })
  const serializer = createMarkdownSerializer(tiptap.nodes, tiptap.marks)
  return serializer.serialize(tiptap.state.doc)
}

describe('Commonmark', () => {
  beforeAll(() => {
    // Make sure html tests pass
    // entry.section === 'HTML blocks' || entry.section === 'Raw HTML'
    markdownit.set({ html: true})
  })
  afterAll(() => {
    markdownit.set({ html: false})
  })

  // failures because of some additional newline in markdownit
  const skippedMarkdownTests = [
    187, 209, 210
  ];

  spec.forEach((entry) => {
    if (skippedMarkdownTests.indexOf(entry.example) !== -1) {
      return
    }
    test('commonmark ' + entry.example, () => {
      expect(markdownit.render(entry.markdown)).toBe(entry.html, entry)
    })
  })
})

describe('Markdown though editor', () => {
  test('headlines', () => {
    expect(markdownThroughEditor('# Test')).toBe('# Test')
    expect(markdownThroughEditor('## Test')).toBe('## Test')
    expect(markdownThroughEditor('### Test')).toBe('### Test')
    expect(markdownThroughEditor('#### Test')).toBe('#### Test')
    expect(markdownThroughEditor('##### Test')).toBe('##### Test')
  })
  test('inline format', () => {
    expect(markdownThroughEditor('**Test**')).toBe('**Test**')
    expect(markdownThroughEditor('__Test__')).toBe('**Test**')
    expect(markdownThroughEditor('_Test_')).toBe('*Test*')
    expect(markdownThroughEditor('~~Test~~')).toBe('~~Test~~')
  })
  test('ul', () => {
    expect(markdownThroughEditor('- foo\n- bar')).toBe('* foo\n* bar')
    expect(markdownThroughEditor('- foo\n\n- bar')).toBe('* foo\n* bar')
    expect(markdownThroughEditor('- foo\n\n\n- bar')).toBe('* foo\n* bar')
  })
  test('ol', () => {
    expect(markdownThroughEditor('1. foo\n2. bar')).toBe('1. foo\n2. bar')
  })
  test('paragraph', () => {
    expect(markdownThroughEditor('foo\nbar\n\nfoobar\n\tfoobar')).toBe('foo bar\n\nfoobar foobar')
  })
  test('links', () => {
    expect(markdownThroughEditor('[test](foo)')).toBe('[test](foo)')
  })
  test('images', () => {
    expect(markdownThroughEditor('![test](foo)')).toBe('![test](foo)')
  })
  test('special characters', () => {
    expect(markdownThroughEditor('"\';&.-#><')).toBe('"\';&.-#><')
  })
  test('checkboxes', () => {
    expect(markdownThroughEditor('- [ ] [asd](sdf)')).toBe('* [ ] [asd](sdf)')
    expect(markdownThroughEditor('- [x] [asd](sdf)')).toBe('* [x] [asd](sdf)')
    expect(markdownThroughEditor('- [ [asd](sdf)')).toBe('* [ [asd](sdf)')
	expect(markdownThroughEditor('- [ ] asd')).toBe('* [ ] asd')
	expect(markdownThroughEditor('- [ ] foo\n- [x] bar')).toBe('* [ ] foo\n* [x] bar')
	expect(markdownThroughEditor('- [x] foo\n' +
		  '  - [ ] bar\n' +
		  '  - [x] baz\n' +
		  '- [ ] bim')).toBe('* [x] foo\n' +
		  '  * [ ] bar\n' +
		  '  * [x] baz\n' +
		  '* [ ] bim')
	expect(markdownThroughEditor('- [X] asd')).toBe('* [x] asd')
	expect(markdownThroughEditor('- [\t] asd')).toBe('* [ ] asd')
	expect(markdownThroughEditor('- [  ] asd')).toBe('* [ ] asd')
	expect(markdownThroughEditor('-   [X] asd')).toBe('* [x] asd')
	expect(markdownThroughEditor('- [F] asd')).toBe('* [F] asd')
  })

  test('escaping', () => {
    const test = '(Asdf [asdf asdf](asdf asdf) asdf asdf asdf asdf asdf asdf asdf asdf asdf)\n' +
        '\n' +
        '* [asdf asdf asdf/asdf](Asdf Asdf)\n' +
        '* asdf asdf asdf [a--f asdf asdf](a--f Asdf Asdf)\n' +
        '* [Asdf asdf asdf asdf asdf asdf](Asdf asdf)'
    expect(markdownThroughEditor(test)).toBe(test)
    expect(markdownThroughEditor('This is a [test] for escaping')).toBe('This is a [test] for escaping')
    expect(markdownThroughEditor('This is a [test for escaping')).toBe('This is a [test for escaping')
  })
})

describe('Markdown serializer from html', () => {
  test('paragraph', () => {
    expect(markdownThroughEditorHtml('<p>hello</p><p>world</p>')).toBe('hello\n\nworld')
  })
  test('links', () => {
    expect(markdownThroughEditorHtml('<a href="foo">test</a>')).toBe('[test](foo)')
  })
  test('images', () => {
    expect(markdownThroughEditorHtml('<img src="image" alt="description" />')).toBe('![description](image)')
  })
	test('checkboxes', () => {
		expect(markdownThroughEditorHtml('<ul><li><input type="checkbox" checked /><label>foo</label></li></ul>')).toBe('* [x] foo')
		expect(markdownThroughEditorHtml('<ul><li><input type="checkbox" /><label>test</label></li></ul>')).toBe('* [ ] test')
		expect(markdownThroughEditorHtml('<ul><li><input type="checkbox" checked /><div><h2>Test</h2><p><strong>content</strong></p></div></li></ul>')).toBe('* [x] Test\n\n  **content**')
		expect(markdownThroughEditorHtml('<ul><li><input type="checkbox" checked /><p>Test</p><h1>Block level headline</h1></li></ul>')).toBe('* [x] Test\n\n  # Block level headline')
	})
})