zefyr

markdown_test.dart 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright (c) 2018, the Zefyr project authors. Please see the AUTHORS file
  2. // for details. All rights reserved. Use of this source code is governed by a
  3. // BSD-style license that can be found in the LICENSE file.
  4. import 'dart:convert';
  5. import 'package:test/test.dart';
  6. import 'package:quill_delta/quill_delta.dart';
  7. import 'package:notus/notus.dart';
  8. import 'package:notus/convert.dart';
  9. void main() {
  10. group('$NotusMarkdownCodec.encode', () {
  11. test('unimplemented', () {
  12. expect(() {
  13. notusMarkdown.decode('test');
  14. }, throwsUnimplementedError);
  15. });
  16. });
  17. group('$NotusMarkdownCodec.encode', () {
  18. test('split adjacent paragraphs', () {
  19. final delta = new Delta()..insert('First line\nSecond line\n');
  20. final result = notusMarkdown.encode(delta);
  21. expect(result, 'First line\n\nSecond line\n\n');
  22. });
  23. test('bold italic', () {
  24. runFor(NotusAttribute<bool> attribute, String expected) {
  25. final delta = new Delta()
  26. ..insert('This ')
  27. ..insert('house', attribute.toJson())
  28. ..insert(' is a ')
  29. ..insert('circus', attribute.toJson())
  30. ..insert('\n');
  31. final result = notusMarkdown.encode(delta);
  32. expect(result, expected);
  33. }
  34. runFor(NotusAttribute.bold, 'This **house** is a **circus**\n\n');
  35. runFor(NotusAttribute.italic, 'This _house_ is a _circus_\n\n');
  36. });
  37. test('intersecting inline styles', () {
  38. final b = NotusAttribute.bold.toJson();
  39. final i = NotusAttribute.italic.toJson();
  40. final bi = new Map<String, dynamic>.from(b);
  41. bi.addAll(i);
  42. final delta = new Delta()
  43. ..insert('This ')
  44. ..insert('house', b)
  45. ..insert(' is a ', bi)
  46. ..insert('circus', b)
  47. ..insert('\n');
  48. final result = notusMarkdown.encode(delta);
  49. expect(result, 'This **house _is a_ circus**\n\n');
  50. });
  51. test('normalize inline styles', () {
  52. final b = NotusAttribute.bold.toJson();
  53. final i = NotusAttribute.italic.toJson();
  54. final delta = new Delta()
  55. ..insert('This')
  56. ..insert(' house ', b)
  57. ..insert('is a')
  58. ..insert(' circus ', i)
  59. ..insert('\n');
  60. final result = notusMarkdown.encode(delta);
  61. expect(result, 'This **house** is a _circus_ \n\n');
  62. });
  63. test('links', () {
  64. final b = NotusAttribute.bold.toJson();
  65. final link = NotusAttribute.link.fromString('https://github.com');
  66. final delta = new Delta()
  67. ..insert('This')
  68. ..insert(' house ', b)
  69. ..insert('is a')
  70. ..insert(' circus ', link.toJson())
  71. ..insert('\n');
  72. final result = notusMarkdown.encode(delta);
  73. expect(result, 'This **house** is a [circus](https://github.com) \n\n');
  74. });
  75. test('heading styles', () {
  76. runFor(NotusAttribute<int> attribute, String source, String expected) {
  77. final delta = new Delta()
  78. ..insert(source)
  79. ..insert('\n', attribute.toJson());
  80. final result = notusMarkdown.encode(delta);
  81. expect(result, expected);
  82. }
  83. runFor(NotusAttribute.h1, 'Title', '# Title\n\n');
  84. runFor(NotusAttribute.h2, 'Title', '## Title\n\n');
  85. runFor(NotusAttribute.h3, 'Title', '### Title\n\n');
  86. });
  87. test('block styles', () {
  88. runFor(NotusAttribute<String> attribute, String source, String expected) {
  89. final delta = new Delta()
  90. ..insert(source)
  91. ..insert('\n', attribute.toJson());
  92. final result = notusMarkdown.encode(delta);
  93. expect(result, expected);
  94. }
  95. runFor(NotusAttribute.ul, 'List item', '* List item\n\n');
  96. runFor(NotusAttribute.ol, 'List item', '1. List item\n\n');
  97. runFor(NotusAttribute.bq, 'List item', '> List item\n\n');
  98. runFor(NotusAttribute.code, 'List item', '```\nList item\n```\n\n');
  99. });
  100. test('multiline blocks', () {
  101. runFor(NotusAttribute<String> attribute, String source, String expected) {
  102. final delta = new Delta()
  103. ..insert(source)
  104. ..insert('\n', attribute.toJson())
  105. ..insert(source)
  106. ..insert('\n', attribute.toJson());
  107. final result = notusMarkdown.encode(delta);
  108. expect(result, expected);
  109. }
  110. runFor(NotusAttribute.ul, 'text', '* text\n* text\n\n');
  111. runFor(NotusAttribute.ol, 'text', '1. text\n1. text\n\n');
  112. runFor(NotusAttribute.bq, 'text', '> text\n> text\n\n');
  113. runFor(NotusAttribute.code, 'text', '```\ntext\ntext\n```\n\n');
  114. });
  115. test('multiple styles', () {
  116. final result = notusMarkdown.encode(delta);
  117. expect(result, expectedMarkdown);
  118. });
  119. });
  120. }
  121. final doc =
  122. r'[{"insert":"Zefyr"},{"insert":"\n","attributes":{"heading":1}},{"insert":"Soft and gentle rich text editing for Flutter applications.","attributes":{"i":true}},{"insert":"\nZefyr is an "},{"insert":"early preview","attributes":{"b":true}},{"insert":" open source library.\nDocumentation"},{"insert":"\n","attributes":{"heading":3}},{"insert":"Quick Start"},{"insert":"\n","attributes":{"block":"ul"}},{"insert":"Data format and Document Model"},{"insert":"\n","attributes":{"block":"ul"}},{"insert":"Style attributes"},{"insert":"\n","attributes":{"block":"ul"}},{"insert":"Heuristic rules"},{"insert":"\n","attributes":{"block":"ul"}},{"insert":"Clean and modern look"},{"insert":"\n","attributes":{"heading":2}},{"insert":"Zefyr’s rich text editor is built with simplicity and flexibility in mind. It provides clean interface for distraction-free editing. Think Medium.com-like experience.\nimport ‘package:flutter/material.dart’;"},{"insert":"\n","attributes":{"block":"code"}},{"insert":"import ‘package:notus/notus.dart’;"},{"insert":"\n\n","attributes":{"block":"code"}},{"insert":"void main() {"},{"insert":"\n","attributes":{"block":"code"}},{"insert":" print(“Hello world!”);"},{"insert":"\n","attributes":{"block":"code"}},{"insert":"}"},{"insert":"\n","attributes":{"block":"code"}}]';
  123. final delta = Delta.fromJson(json.decode(doc));
  124. final expectedMarkdown = '''
  125. # Zefyr
  126. _Soft and gentle rich text editing for Flutter applications._
  127. Zefyr is an **early preview** open source library.
  128. ### Documentation
  129. * Quick Start
  130. * Data format and Document Model
  131. * Style attributes
  132. * Heuristic rules
  133. ## Clean and modern look
  134. Zefyr’s rich text editor is built with simplicity and flexibility in mind. It provides clean interface for distraction-free editing. Think Medium.com-like experience.
  135. ```
  136. import ‘package:flutter/material.dart’;
  137. import ‘package:notus/notus.dart’;
  138. void main() {
  139. print(“Hello world!”);
  140. }
  141. ```
  142. ''';