luce_spec.cr 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. require "./spec_helper"
  2. test_directory "original"
  3. # Block syntax extensions.
  4. test_file "extensions/fenced_blockquotes.unit",
  5. block_syntaxes: [Luce::FencedBlockquoteSyntax.new]
  6. test_file "extensions/fenced_code_blocks.unit",
  7. block_syntaxes: [Luce::FencedCodeBlockSyntax.new]
  8. test_file "extensions/headers_with_ids.unit",
  9. block_syntaxes: [Luce::HeaderWithIdSyntax.new]
  10. test_file "extensions/ordered_list_with_checkboxes.unit",
  11. block_syntaxes: [Luce::OrderedListWithCheckboxSyntax.new]
  12. test_file "extensions/setext_headers_with_ids.unit",
  13. block_syntaxes: [Luce::SetextHeaderWithIdSyntax.new]
  14. test_file "extensions/tables.unit",
  15. block_syntaxes: [Luce::TableSyntax.new]
  16. test_file "extensions/unordered_list_with_checkboxes.unit",
  17. block_syntaxes: [Luce::UnorderedListWithCheckboxSyntax.new]
  18. test_file "extensions/footnote_block.unit",
  19. block_syntaxes: [Luce::FootnoteDefSyntax.new]
  20. test_file "extensions/alert_extension.unit",
  21. block_syntaxes: [Luce::AlertBlockSyntax.new]
  22. # Inline syntax extensions
  23. test_file "extensions/autolink_extension.unit",
  24. inline_syntaxes: [Luce::AutolinkExtensionSyntax.new]
  25. test_file "extensions/emojis.unit",
  26. inline_syntaxes: [Luce::EmojiSyntax.new]
  27. test_file "extensions/inline_html.unit",
  28. inline_syntaxes: [Luce::InlineHTMLSyntax.new]
  29. test_file "extensions/strikethrough.unit",
  30. inline_syntaxes: [Luce::StrikethroughSyntax.new]
  31. # Luce custom
  32. test_file "luce/autolink_extension.unit",
  33. inline_syntaxes: [Luce::AutolinkExtensionSyntax.new]
  34. test_directory "common_mark"
  35. test_directory "gfm"
  36. describe "corner cases" do
  37. validate_core("Incorrect Links",
  38. "5 Ethernet ([Music](",
  39. "<p>5 Ethernet ([Music](</p>\n")
  40. validate_core("Escaping code block language",
  41. %q{```"/><a/href="url">arbitrary_html</a>},
  42. "<pre><code class=\"language-&quot;/&gt;&lt;a/href=&quot;url&quot;&gt;arbitrary_html&lt;/a&gt;\"></code></pre>\n")
  43. validate_core("Unicode ellipsis as punctuation",
  44. %{"Connecting dot **A** to **B.**…"},
  45. "<p>&quot;Connecting dot <strong>A</strong> to <strong>B.</strong>…&quot;</p>\n")
  46. describe Luce::Resolver do
  47. nyan_resolver = Luce::Resolver.new do |text, _|
  48. text.empty? ? nil : Luce::Text.new("~=[,,_#{text}_,,]:3").as Luce::Node
  49. end
  50. validate_core("simple link resolver",
  51. "resolve [this] thing",
  52. "<p>resolve ~=[,,_this_,,]:3 thing</p>\n",
  53. link_resolver: nyan_resolver)
  54. validate_core("simple image resolver",
  55. "resolve ![this] thing",
  56. "<p>resolve ~=[,,_this_,,]:3 thing</p>\n",
  57. image_link_resolver: nyan_resolver)
  58. validate_core("can resolve link containing inline tags",
  59. "resolve [*star* _underline_] thing",
  60. "<p>resolve ~=[,,_*star* _underline__,,]:3 thing</p>\n",
  61. link_resolver: nyan_resolver)
  62. validate_core("link resolver uses un-normalized link label",
  63. "resolve [TH IS] thing",
  64. "<p>resolve ~=[,,_TH IS_,,]:3 thing</p>\n",
  65. link_resolver: nyan_resolver)
  66. validate_core("can resolve escaped brackets",
  67. %q{resolve [\[\]] thing},
  68. "<p>resolve ~=[,,_[]_,,]:3 thing</p>\n",
  69. link_resolver: nyan_resolver)
  70. validate_core("can choose to _not_ resolve something, like an empty link",
  71. "resolve [[]] thing",
  72. "<p>resolve ~=[,,_[]_,,]:3 thing</p>\n",
  73. link_resolver: nyan_resolver)
  74. end
  75. describe "Custom inline syntax" do
  76. nyan_syntax = [] of Luce::InlineSyntax
  77. nyan_syntax << Luce::TextSyntax.new("nyan", sub: "~=[,,_,,]:3")
  78. link_resolver = Luce::Resolver.new do |text, _|
  79. Luce::Element.text("a", text.gsub("<", "&lt;"))
  80. end
  81. validate_core("simple inline syntax",
  82. "nyan",
  83. "<p>~=[,,_,,]:3</p>\n",
  84. inline_syntaxes: nyan_syntax)
  85. validate_core("dart custom links",
  86. "links [are<foo>] awesome",
  87. "<p>links <a>are&lt;foo></a> awesome</p>\n",
  88. link_resolver: link_resolver)
  89. # TODO(amouravski): need more tests here for custom syntaxes, as some
  90. # things are not quite working properly. The regexps are sometime a little
  91. # too greedy, I think.
  92. end
  93. describe "Inline only" do
  94. validate_core("simple line",
  95. "This would normally create a paragraph",
  96. "This would normally create a paragraph",
  97. inline_only: true)
  98. validate_core("strong and em",
  99. "This would _normally_ create a **paragraph**",
  100. "This would <em>normally</em> create a <strong>paragraph</strong>",
  101. inline_only: true)
  102. validate_core("link",
  103. "This [link](http://example.com/) will work normally",
  104. "This <a href=\"http://example.com/\">link</a> will work normally",
  105. inline_only: true)
  106. validate_core("references do not work",
  107. "[This][] shouldn't work, though.",
  108. "[This][] shouldn't work, though.",
  109. inline_only: true)
  110. validate_core("less than and ampersand are escaped",
  111. "< &",
  112. "&lt; &amp;",
  113. inline_only: true)
  114. validate_core("keep newlines",
  115. "This paragraph
  116. continues after a newline",
  117. "This paragraph
  118. continues after a newline",
  119. inline_only: true)
  120. validate_core("ignores block-level markdown syntax",
  121. "1. This will not be an <ol>.",
  122. "1. This will not be an <ol>.",
  123. inline_only: true)
  124. end
  125. describe "ExtensionSet" do
  126. it "3 asterisks separated with spaces horizontal rule while it is GITHUB_FLAVOURED" do
  127. Luce.to_html("* * *", extension_set: Luce::ExtensionSet::GITHUB_FLAVOURED).should eq "<hr />\n"
  128. end
  129. end
  130. end