code.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import { testDataProcessor } from '../_utils/utils';
  6. describe( 'GFMDataProcessor', () => {
  7. describe( 'code', () => {
  8. it( 'should process inline code', () => {
  9. testDataProcessor(
  10. 'regular text and `inline code`',
  11. '<p>regular text and <code>inline code</code></p>'
  12. );
  13. } );
  14. it( 'should properly process multiple code', () => {
  15. testDataProcessor(
  16. '`this is code` and this is `too`',
  17. '<p><code>this is code</code> and this is <code>too</code></p>'
  18. );
  19. } );
  20. it( 'should process spaces inside inline code', () => {
  21. testDataProcessor(
  22. 'regular text and` inline code`',
  23. '<p>regular text and<code>inline code</code></p>',
  24. // When converting back it will be normalized and spaces
  25. // at the beginning of inline code will be removed.
  26. 'regular text and`inline code`'
  27. );
  28. } );
  29. it( 'should properly process backticks inside code spans #1', () => {
  30. testDataProcessor(
  31. '`` `backticks` ``',
  32. '<p><code>`backticks`</code></p>'
  33. );
  34. } );
  35. it( 'should properly process backticks inside code spans #2', () => {
  36. testDataProcessor(
  37. '`` some `backticks` inside ``',
  38. '<p><code>some `backticks` inside</code></p>'
  39. );
  40. } );
  41. it( 'should process code blocks indented with tabs', () => {
  42. testDataProcessor(
  43. ' code block',
  44. // GitHub is rendering as:
  45. // <pre><code>code block
  46. // </code></pre>
  47. '<pre><code>code block</code></pre>',
  48. // When converting back tabs are normalized to ```.
  49. '```\n' +
  50. 'code block\n' +
  51. '```'
  52. );
  53. } );
  54. it( 'should process code blocks indented with spaces', () => {
  55. testDataProcessor(
  56. ' code block',
  57. // GitHub is rendering as:
  58. // <pre><code>code block
  59. // </code></pre>
  60. '<pre><code>code block</code></pre>',
  61. // When converting back tabs are normalized to ```.
  62. '```\n' +
  63. 'code block\n' +
  64. '```'
  65. );
  66. } );
  67. it( 'should process multi line code blocks indented with tabs', () => {
  68. testDataProcessor(
  69. ' first line\n' +
  70. ' second line',
  71. // GitHub is rendering as:
  72. // <pre><code>first line
  73. // second line
  74. // </code></pre>
  75. '<pre><code>first line\n' +
  76. 'second line</code></pre>',
  77. // When converting back tabs are normalized to ```.
  78. '```\n' +
  79. 'first line\n' +
  80. 'second line\n' +
  81. '```'
  82. );
  83. } );
  84. it( 'should process multi line code blocks indented with spaces', () => {
  85. testDataProcessor(
  86. ' first line\n' +
  87. ' second line',
  88. // GitHub is rendering as:
  89. // <pre><code>first line
  90. // second line
  91. // </code></pre>
  92. '<pre><code>first line\n' +
  93. 'second line</code></pre>',
  94. // When converting back spaces are normalized to ```.
  95. '```\n' +
  96. 'first line\n' +
  97. 'second line\n' +
  98. '```'
  99. );
  100. } );
  101. it( 'should process multi line code blocks with trailing spaces', () => {
  102. testDataProcessor(
  103. ' the lines in this block \n' +
  104. ' all contain trailing spaces ',
  105. // GitHub is rendering as:
  106. // <pre><code>the lines in this block
  107. // all contain trailing spaces
  108. // </code></pre>
  109. '<pre><code>the lines in this block \n' +
  110. 'all contain trailing spaces </code></pre>',
  111. // When converting back tabs are normalized to ```.
  112. '```\n' +
  113. 'the lines in this block \n' +
  114. 'all contain trailing spaces \n' +
  115. '```'
  116. );
  117. } );
  118. it( 'should process code block with language name', () => {
  119. testDataProcessor(
  120. '``` js\n' +
  121. 'var a = \'hello\';\n' +
  122. 'console.log(a + \' world\');\n' +
  123. '```',
  124. // GitHub is rendering as special html with syntax highlighting.
  125. // We will need to handle this separately by some feature.
  126. '<pre><code class="lang-js">var a = \'hello\';\n' +
  127. 'console.log(a + \' world\');</code></pre>'
  128. );
  129. } );
  130. it( 'should process code block with language name and using ~~~ as delimiter', () => {
  131. testDataProcessor(
  132. '~~~ bash\n' +
  133. '#!/bin/bash\n' +
  134. '~~~',
  135. // GitHub is rendering as special html with syntax highlighting.
  136. // We will need to handle this separately by some feature.
  137. '<pre><code class="lang-bash">#!/bin/bash</code></pre>',
  138. // When converting back ~~~ are normalized to ```.
  139. '``` bash\n' +
  140. '#!/bin/bash\n' +
  141. '```'
  142. );
  143. } );
  144. it( 'should process code block with language name and using ``````` as delimiter', () => {
  145. testDataProcessor(
  146. '``````` js\n' +
  147. 'var a = \'hello\';\n' +
  148. 'console.log(a + \' world\');\n' +
  149. '```````',
  150. // GitHub is rendering as special html with syntax highlighting.
  151. // We will need to handle this separately by some feature.
  152. '<pre><code class="lang-js">var a = \'hello\';\n' +
  153. 'console.log(a + \' world\');</code></pre>',
  154. // When converting back ``````` are normalized to ```.
  155. '``` js\n' +
  156. 'var a = \'hello\';\n' +
  157. 'console.log(a + \' world\');\n' +
  158. '```'
  159. );
  160. } );
  161. it( 'should process code block with language name and using ~~~~~~~~~~ as delimiter', () => {
  162. testDataProcessor(
  163. '~~~~~~~~~~ js\n' +
  164. 'var a = \'hello\';\n' +
  165. 'console.log(a + \' world\');\n' +
  166. '~~~~~~~~~~',
  167. // GitHub is rendering as special html with syntax highlighting.
  168. // We will need to handle this separately by some feature.
  169. '<pre><code class="lang-js">var a = \'hello\';\n' +
  170. 'console.log(a + \' world\');</code></pre>',
  171. // When converting back ~~~~~~~~~~ are normalized to ```.
  172. '``` js\n' +
  173. 'var a = \'hello\';\n' +
  174. 'console.log(a + \' world\');\n' +
  175. '```'
  176. );
  177. } );
  178. it( 'should process empty code block', () => {
  179. testDataProcessor(
  180. '``` js\n' +
  181. '```',
  182. // GitHub is rendering as special html with syntax highlighting.
  183. // We will need to handle this separately by some feature.
  184. '<pre><code class="lang-js"></code></pre>',
  185. // When converting back, empty code blocks will be removed.
  186. // This might be an issue when switching from source to editor
  187. // but changing this cannot be done in to-markdown converters.
  188. ''
  189. );
  190. } );
  191. it( 'should process code block with empty line', () => {
  192. testDataProcessor(
  193. '``` js\n' +
  194. '\n' +
  195. '```',
  196. // GitHub is rendering as special html with syntax highlighting.
  197. // We will need to handle this separately by some feature.
  198. '<pre><code class="lang-js"></code></pre>',
  199. // When converting back, empty code blocks will be removed.
  200. // This might be an issue when switching from source to editor
  201. // but changing this cannot be done in to-markdown converters.
  202. ''
  203. );
  204. } );
  205. it( 'should process nested code', () => {
  206. testDataProcessor(
  207. '````` code `` code ``` `````',
  208. // GitHub is rendering as:
  209. // <p><code>code `` code ```</code></p>
  210. '<p><code>code `` code ```</code></p>',
  211. // When converting back ````` will be normalized to ``.
  212. '`` code `` code ``` ``'
  213. );
  214. } );
  215. } );
  216. } );