zefyr

markdown_test.dart 6.5KB

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