zefyr

markdown_test.dart 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 = 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 = 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 = Map<String, dynamic>.from(b);
  41. bi.addAll(i);
  42. final delta = 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 = 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 = 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 = Delta()..insert(source)..insert('\n', attribute.toJson());
  78. final result = notusMarkdown.encode(delta);
  79. expect(result, expected);
  80. }
  81. runFor(NotusAttribute.h1, 'Title', '# Title\n\n');
  82. runFor(NotusAttribute.h2, 'Title', '## Title\n\n');
  83. runFor(NotusAttribute.h3, 'Title', '### Title\n\n');
  84. });
  85. test('block styles', () {
  86. runFor(NotusAttribute<String> attribute, String source, String expected) {
  87. final delta = Delta()..insert(source)..insert('\n', attribute.toJson());
  88. final result = notusMarkdown.encode(delta);
  89. expect(result, expected);
  90. }
  91. runFor(NotusAttribute.ul, 'List item', '* List item\n\n');
  92. runFor(NotusAttribute.ol, 'List item', '1. List item\n\n');
  93. runFor(NotusAttribute.bq, 'List item', '> List item\n\n');
  94. runFor(NotusAttribute.code, 'List item', '```\nList item\n```\n\n');
  95. });
  96. test('multiline blocks', () {
  97. runFor(NotusAttribute<String> attribute, String source, String expected) {
  98. final delta = Delta()
  99. ..insert(source)
  100. ..insert('\n', attribute.toJson())
  101. ..insert(source)
  102. ..insert('\n', attribute.toJson());
  103. final result = notusMarkdown.encode(delta);
  104. expect(result, expected);
  105. }
  106. runFor(NotusAttribute.ul, 'text', '* text\n* text\n\n');
  107. runFor(NotusAttribute.ol, 'text', '1. text\n1. text\n\n');
  108. runFor(NotusAttribute.bq, 'text', '> text\n> text\n\n');
  109. runFor(NotusAttribute.code, 'text', '```\ntext\ntext\n```\n\n');
  110. });
  111. test('multiple styles', () {
  112. final result = notusMarkdown.encode(delta);
  113. expect(result, expectedMarkdown);
  114. });
  115. });
  116. }
  117. final doc =
  118. 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"}}]';
  119. final delta = Delta.fromJson(json.decode(doc) as List);
  120. final expectedMarkdown = '''
  121. # Zefyr
  122. _Soft and gentle rich text editing for Flutter applications._
  123. Zefyr is an **early preview** open source library.
  124. ### Documentation
  125. * Quick Start
  126. * Data format and Document Model
  127. * Style attributes
  128. * Heuristic rules
  129. ## Clean and modern look
  130. 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.
  131. ```
  132. import ‘package:flutter/material.dart’;
  133. import ‘package:notus/notus.dart’;
  134. void main() {
  135. print(“Hello world!”);
  136. }
  137. ```
  138. ''';