micromark-extension-growi-directive.test.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /**
  2. * @typedef {import('../src/micromark-extension-growi-directive/index.js').HtmlOptions} HtmlOptions
  3. * @typedef {import('../src/micromark-extension-growi-directive/index.js').Handle} Handle
  4. */
  5. import { htmlVoidElements } from 'html-void-elements';
  6. import { micromark } from 'micromark';
  7. import { describe, expect, it } from 'vitest';
  8. import { DirectiveType } from '../src/mdast-util-growi-directive/lib/index.js';
  9. import {
  10. directiveHtml as html,
  11. directive as syntax,
  12. } from '../src/micromark-extension-growi-directive/index.js';
  13. const own = {}.hasOwnProperty;
  14. describe('micromark-extension-directive (syntax)', () => {
  15. describe('text', () => {
  16. it('should support an escaped colon which would otherwise be a directive', () => {
  17. expect(micromark('\\$a', options())).toBe('<p>$a</p>');
  18. });
  19. it('should support a directive after an escaped colon', () => {
  20. expect(micromark('\\$$a', options())).toBe('<p>$</p>');
  21. });
  22. // it('should not support a directive after a colon', () => {
  23. // expect(micromark('a :$b', options())).toBe('<p>a :$b</p>');
  24. // });
  25. it('should not support a colon not followed by an alpha', () => {
  26. expect(micromark('$', options())).toBe('<p>$</p>');
  27. });
  28. it('should support a colon followed by an alpha', () => {
  29. expect(micromark('a $a', options())).toBe('<p>a </p>');
  30. });
  31. it('should not support a colon followed by a digit', () => {
  32. expect(micromark('$9', options())).toBe('<p>$9</p>');
  33. });
  34. it('should not support a colon followed by a dash', () => {
  35. expect(micromark('$-', options())).toBe('<p>$-</p>');
  36. });
  37. it('should not support a colon followed by an underscore', () => {
  38. expect(micromark('$_', options())).toBe('<p>$_</p>');
  39. });
  40. it('should support a digit in a name', () => {
  41. expect(micromark('a $a9', options())).toBe('<p>a </p>');
  42. });
  43. it('should support a dash in a name', () => {
  44. expect(micromark('a $a-b', options())).toBe('<p>a </p>');
  45. });
  46. it('should *not* support a dash at the end of a name', () => {
  47. expect(micromark('$a-', options())).toBe('<p>$a-</p>');
  48. });
  49. it('should support an underscore in a name', () => {
  50. expect(micromark('a $a_b', options())).toBe('<p>a </p>');
  51. });
  52. it('should *not* support an underscore at the end of a name', () => {
  53. expect(micromark('$a_', options())).toBe('<p>$a_</p>');
  54. });
  55. it('should *not* support a colon right after a name', () => {
  56. expect(micromark('$a$', options())).toBe('<p>$a$</p>');
  57. });
  58. it('should not interfere w/ emphasis (`_`)', () => {
  59. expect(micromark('_$directive_', options())).toBe(
  60. '<p><em>$directive</em></p>',
  61. );
  62. });
  63. it('should support a name followed by an unclosed `[`', () => {
  64. expect(micromark('$a[', options())).toBe('<p>[</p>');
  65. });
  66. it('should support a name followed by an unclosed `(`', () => {
  67. expect(micromark('$a(', options())).toBe('<p>(</p>');
  68. });
  69. it('should support a name followed by an unclosed `[` w/ content', () => {
  70. expect(micromark('$a[b', options())).toBe('<p>[b</p>');
  71. });
  72. it('should support a name followed by an unclosed `(` w/ content', () => {
  73. expect(micromark('$a(b', options())).toBe('<p>(b</p>');
  74. });
  75. it('should support an empty label', () => {
  76. expect(micromark('a $a[]', options())).toBe('<p>a </p>');
  77. });
  78. it('should support a whitespace only label', () => {
  79. expect(micromark('a $a[ \t]', options())).toBe('<p>a </p>');
  80. });
  81. it('should support an eol in an label', () => {
  82. expect(micromark('$a[\n]', options())).toBe('<p></p>');
  83. });
  84. it('should support content in an label', () => {
  85. expect(micromark('$a[a b c]asd', options())).toBe('<p>asd</p>');
  86. });
  87. it('should support markdown in an label', () => {
  88. expect(micromark('$a[a *b* c]asd', options())).toBe('<p>asd</p>');
  89. });
  90. // == Resolved as text directive
  91. // t.equal(
  92. // micromark('$a[]asd', options()),
  93. // '<p>$a[]asd</p>',
  94. // 'should not support content after a label',
  95. // );
  96. it('should support a directive in an label', () => {
  97. expect(micromark('a $b[c :d[e] f] g', options())).toBe('<p>a g</p>');
  98. });
  99. it('should support content after a label', () => {
  100. expect(micromark('$a[]asd', options())).toBe('<p>asd</p>');
  101. });
  102. it('should support empty attributes', () => {
  103. expect(micromark('a $a()', options())).toBe('<p>a </p>');
  104. });
  105. it('should support whitespace only attributes', () => {
  106. expect(micromark('a $a( \t)', options())).toBe('<p>a </p>');
  107. });
  108. it('should support an eol in attributes', () => {
  109. expect(micromark('$a(\n)', options())).toBe('<p></p>');
  110. });
  111. it('should support attributes w/o values', () => {
  112. expect(micromark('a $a(a b c)', options())).toBe('<p>a </p>');
  113. });
  114. it('should support attributes w/ unquoted values', () => {
  115. expect(micromark('a $a(a=b c=d)', options())).toBe('<p>a </p>');
  116. });
  117. it('should support attributes w/ class shortcut', () => {
  118. expect(micromark('a $a(.a .b)', options())).toBe('<p>a </p>');
  119. });
  120. it('should support attributes w/ class shortcut w/o whitespace between', () => {
  121. expect(micromark('a $a(.a.b)', options())).toBe('<p>a </p>');
  122. });
  123. it('should support attributes w/ id shortcut', () => {
  124. expect(micromark('a $a(#a #b)', options())).toBe('<p>a </p>');
  125. });
  126. it('should support attributes w/ id shortcut w/o whitespace between', () => {
  127. expect(micromark('a $a(#a#b)', options())).toBe('<p>a </p>');
  128. });
  129. it('should support attributes w/ shortcuts combined w/ other attributes', () => {
  130. expect(micromark('a $a(#a.b.c#d e f=g #h.i.j)', options())).toBe(
  131. '<p>a </p>',
  132. );
  133. });
  134. it('should support attrs which starts w/ continuous dots', () => {
  135. expect(micromark('a $a(..b)', options())).toBe('<p>a </p>');
  136. });
  137. it('should support attrs which start w/ `#`', () => {
  138. expect(micromark('a $a(.#b)', options())).toBe('<p>a </p>');
  139. });
  140. it('should support attrs w/ (`.`)', () => {
  141. expect(micromark('a $a(.)', options())).toBe('<p>a </p>');
  142. });
  143. it('should support with the attr `(.a=b)`', () => {
  144. expect(micromark('a $a(.a=b)', options())).toBe('<p>a </p>');
  145. });
  146. it('should support with the attr `(.a"b)`', () => {
  147. expect(micromark('a $a(.a"b)', options())).toBe('<p>a </p>');
  148. });
  149. it('should support with the attr `(.a<b)`', () => {
  150. expect(micromark('a $a(.a<b)', options())).toBe('<p>a </p>');
  151. });
  152. it('should support most characters in shortcuts', () => {
  153. expect(micromark('a $a(.a💚b)', options())).toBe('<p>a </p>');
  154. });
  155. it('should support an underscore in attribute names', () => {
  156. expect(micromark('a $a(_)', options())).toBe('<p>a </p>');
  157. });
  158. it('should support a colon in attribute names', () => {
  159. expect(micromark('a $a(xml:lang)', options())).toBe('<p>a </p>');
  160. });
  161. it('should support double quoted attributes', () => {
  162. expect(micromark('a $a(a="b" c="d e f")', options())).toBe('<p>a </p>');
  163. });
  164. it('should support single quoted attributes', () => {
  165. expect(micromark("a $a(a='b' c='d e f')", options())).toBe('<p>a </p>');
  166. });
  167. it('should support whitespace around initializers', () => {
  168. expect(micromark('a $a(a = b c\t=\t\'d\' f =\r"g")', options())).toBe(
  169. '<p>a </p>',
  170. );
  171. });
  172. it('should not support `=` to start an unquoted attribute value', () => {
  173. expect(micromark('$a(b==)', options())).toBe('<p>(b==)</p>');
  174. });
  175. it('should not support a missing attribute value after `=`', () => {
  176. expect(micromark('$a(b=)', options())).toBe('<p>(b=)</p>');
  177. });
  178. it('should not support an apostrophe in an unquoted attribute value', () => {
  179. expect(micromark("$a(b=c')", options())).toBe("<p>(b=c')</p>");
  180. });
  181. it('should not support a grave accent in an unquoted attribute value', () => {
  182. expect(micromark('$a(b=c`)', options())).toBe('<p>(b=c`)</p>');
  183. });
  184. it('should support most other characters in attribute keys', () => {
  185. expect(micromark('a $a(b💚=a💚b)', options())).toBe('<p>a </p>');
  186. });
  187. it('should support most other characters in unquoted attribute values', () => {
  188. expect(micromark('a $a(b=a💚b)', options())).toBe('<p>a </p>');
  189. });
  190. it('should not support an EOF in a quoted attribute value', () => {
  191. expect(micromark('$a(b="c', options())).toBe('<p>(b=&quot;c</p>');
  192. });
  193. it('should support most other characters in quoted attribute values', () => {
  194. expect(micromark('a $a(b="a💚b")', options())).toBe('<p>a </p>');
  195. });
  196. it('should support EOLs in quoted attribute values', () => {
  197. expect(micromark('$a(b="\nc\r d")', options())).toBe('<p></p>');
  198. });
  199. it('should not support an EOF after a quoted attribute value', () => {
  200. expect(micromark('$a(b="c"', options())).toBe('<p>(b=&quot;c&quot;</p>');
  201. });
  202. });
  203. describe('leaf', () => {
  204. it('should support a directive', () => {
  205. expect(micromark('$b', options())).toBe('');
  206. });
  207. it('should not support one colon', () => {
  208. expect(micromark(':', options())).toBe('<p>:</p>');
  209. });
  210. it('should not support two colons not followed by an alpha', () => {
  211. expect(micromark('::', options())).toBe('<p>::</p>');
  212. });
  213. it('should support two colons followed by an alpha', () => {
  214. expect(micromark('$a', options())).toBe('');
  215. });
  216. it('should not support two colons followed by a digit', () => {
  217. expect(micromark('$9', options())).toBe('<p>$9</p>');
  218. });
  219. it('should not support two colons followed by a dash', () => {
  220. expect(micromark('$-', options())).toBe('<p>$-</p>');
  221. });
  222. it('should support a digit in a name', () => {
  223. expect(micromark('$a9', options())).toBe('');
  224. });
  225. it('should support a dash in a name', () => {
  226. expect(micromark('$a-b', options())).toBe('');
  227. });
  228. // == Resolved as text directive
  229. // it('should not support a name followed by an unclosed `[`', () => {
  230. // expect(micromark('$a[', options())).toBe('<p>$a[</p>');
  231. // });
  232. // == Resolved as text directive
  233. // it('should not support a name followed by an unclosed `{`', () => {
  234. // expect(micromark('$a{', options())).toBe('<p>$a{</p>');
  235. // });
  236. // == Resolved as text directive
  237. // it('should not support a name followed by an unclosed `[` w/ content', () => {
  238. // expect(micromark('$a[b', options())).toBe('<p>$a[b</p>');
  239. // });
  240. // == Resolved as text directive
  241. // it('should not support a name followed by an unclosed `{` w/ content', () => {
  242. // expect(micromark('$a{b', options())).toBe('<p>$a{b</p>');
  243. // });
  244. it('should support an empty label', () => {
  245. expect(micromark('$a[]', options())).toBe('');
  246. });
  247. it('should support a whitespace only label', () => {
  248. expect(micromark('$a[ \t]', options())).toBe('');
  249. });
  250. // == Resolved as text directive
  251. // it('should not support an eol in an label', () => {
  252. // expect(micromark('$a[\n]', options())).toBe('<p>$a[\n]</p>');
  253. // });
  254. it('should support content in an label', () => {
  255. expect(micromark('$a[a b c]', options())).toBe('');
  256. });
  257. it('should support markdown in an label', () => {
  258. expect(micromark('$a[a *b* c]', options())).toBe('');
  259. });
  260. // == Resolved as text directive
  261. // it('should not support content after a label', () => {
  262. // expect(micromark('$a[]asd', options())).toBe('<p>$a[]asd</p>');
  263. // });
  264. it('should support empty attributes', () => {
  265. expect(micromark('$a()', options())).toBe('');
  266. });
  267. it('should support whitespace only attributes', () => {
  268. expect(micromark('$a( \t)', options())).toBe('');
  269. });
  270. // == Resolved as text directive
  271. // it('should not support an eol in attributes', () => {
  272. // expect(micromark('$a(\n)', options())).toBe('<p>$a(\n)</p>');
  273. // });
  274. it('should support attributes w/o values', () => {
  275. expect(micromark('$a(a b c)', options())).toBe('');
  276. });
  277. it('should support attributes w/ unquoted values', () => {
  278. expect(micromark('$a(a=b c=d)', options())).toBe('');
  279. });
  280. it('should support attributes w/ class shortcut', () => {
  281. expect(micromark('$a(.a .b)', options())).toBe('');
  282. });
  283. it('should support attributes w/ id shortcut', () => {
  284. expect(micromark('$a(#a #b)', options())).toBe('');
  285. });
  286. it('should support most characters in shortcuts', () => {
  287. expect(micromark('$a(.a💚b)', options())).toBe('');
  288. });
  289. it('should support double quoted attributes', () => {
  290. expect(micromark('$a(a="b" c="d e f")', options())).toBe('');
  291. });
  292. it('should support single quoted attributes', () => {
  293. expect(micromark("$a(a='b' c='d e f')", options())).toBe('');
  294. });
  295. it('should support whitespace around initializers', () => {
  296. expect(micromark("$a(a = b c\t=\t'd')", options())).toBe('');
  297. });
  298. // == Resolved as text directive
  299. // it('should not support EOLs around initializers', () => {
  300. // expect(micromark('$a(f =\rg)', options())).toBe('<p>$a(f =\rg)</p>');
  301. // });
  302. // == Resolved as text directive
  303. // it('should not support `=` to start an unquoted attribute value', () => {
  304. // expect(micromark('$a(b==)', options())).toBe('<p>$a(b==)</p>');
  305. // });
  306. it('should support most other characters in attribute keys', () => {
  307. expect(micromark('$a(b💚=a💚b)', options())).toBe('');
  308. });
  309. it('should support most other characters in unquoted attribute values', () => {
  310. expect(micromark('$a(b=a💚b)', options())).toBe('');
  311. });
  312. it('should not support an EOF in a quoted attribute value', () => {
  313. expect(micromark('$a(b="c', options())).toBe('<p>(b=&quot;c</p>');
  314. });
  315. it('should support most other characters in quoted attribute values', () => {
  316. expect(micromark('$a(b="a💚b")', options())).toBe('');
  317. });
  318. it('should not support EOLs in quoted attribute values', () => {
  319. expect(micromark('$a(b="\nc\r d")', options())).toBe('<p></p>');
  320. });
  321. it('should not support an EOF after a quoted attribute value', () => {
  322. expect(micromark('$a(b="c"', options())).toBe('<p>(b=&quot;c&quot;</p>');
  323. });
  324. it('should support whitespace after directives', () => {
  325. expect(micromark('$a(b=c) \t ', options())).toBe('');
  326. });
  327. it('should support a block quote after a leaf', () => {
  328. expect(micromark('$a(b=c)\n>a', options())).toBe(
  329. '<blockquote>\n<p>a</p>\n</blockquote>',
  330. );
  331. });
  332. it('should support code (fenced) after a leaf', () => {
  333. expect(micromark('$a(b=c)\n```js\na', options())).toBe(
  334. '<pre><code class="language-js">a\n</code></pre>\n',
  335. );
  336. });
  337. it('should support code (indented) after a leaf', () => {
  338. expect(micromark('$a(b=c)\n a', options())).toBe(
  339. '<pre><code>a\n</code></pre>',
  340. );
  341. });
  342. it('should support a definition after a leaf', () => {
  343. expect(micromark('$a(b=c)\n[a]: b', options())).toBe('');
  344. });
  345. it('should support a heading (atx) after a leaf', () => {
  346. expect(micromark('$a(b=c)\n# a', options())).toBe('<h1>a</h1>');
  347. });
  348. it('should support a heading (setext) after a leaf', () => {
  349. expect(micromark('$a(b=c)\na\n=', options())).toBe('<h1>a</h1>');
  350. });
  351. it('should support html after a leaf', () => {
  352. expect(micromark('$a(b=c)\n<!-->', options())).toBe('<!-->');
  353. });
  354. it('should support a list after a leaf', () => {
  355. expect(micromark('$a(b=c)\n* a', options())).toBe(
  356. '<ul>\n<li>a</li>\n</ul>',
  357. );
  358. });
  359. it('should support a paragraph after a leaf', () => {
  360. expect(micromark('$a(b=c)\na', options())).toBe('<p>a</p>');
  361. });
  362. it('should support a thematic break after a leaf', () => {
  363. expect(micromark('$a(b=c)\n***', options())).toBe('<hr />');
  364. });
  365. it('should support a block quote before a leaf', () => {
  366. expect(micromark('>a\n$a(b=c)', options())).toBe(
  367. '<blockquote>\n<p>a</p>\n</blockquote>\n',
  368. );
  369. });
  370. it('should support code (fenced) before a leaf', () => {
  371. expect(micromark('```js\na\n```\n$a(b=c)', options())).toBe(
  372. '<pre><code class="language-js">a\n</code></pre>\n',
  373. );
  374. });
  375. it('should support code (indented) before a leaf', () => {
  376. expect(micromark(' a\n$a(b=c)', options())).toBe(
  377. '<pre><code>a\n</code></pre>\n',
  378. );
  379. });
  380. it('should support a definition before a leaf', () => {
  381. expect(micromark('[a]: b\n$a(b=c)', options())).toBe('');
  382. });
  383. it('should support a heading (atx) before a leaf', () => {
  384. expect(micromark('# a\n$a(b=c)', options())).toBe('<h1>a</h1>\n');
  385. });
  386. it('should support a heading (setext) before a leaf', () => {
  387. expect(micromark('a\n=\n$a(b=c)', options())).toBe('<h1>a</h1>\n');
  388. });
  389. it('should support html before a leaf', () => {
  390. expect(micromark('<!-->\n$a(b=c)', options())).toBe('<!-->\n');
  391. });
  392. it('should support a list before a leaf', () => {
  393. expect(micromark('* a\n$a(b=c)', options())).toBe(
  394. '<ul>\n<li>a</li>\n</ul>\n',
  395. );
  396. });
  397. it('should support a paragraph before a leaf', () => {
  398. expect(micromark('a\n$a(b=c)', options())).toBe('<p>a</p>\n');
  399. });
  400. it('should support a thematic break before a leaf', () => {
  401. expect(micromark('***\n$a(b=c)', options())).toBe('<hr />\n');
  402. });
  403. it('should not support lazyness (1)', () => {
  404. expect(micromark('> $a\nb', options({ '*': h }))).toBe(
  405. '<blockquote><a></a>\n</blockquote>\n<p>b</p>',
  406. );
  407. });
  408. it('should not support lazyness (2)', () => {
  409. expect(micromark('> a\n$b', options({ '*': h }))).toBe(
  410. '<blockquote>\n<p>a</p>\n</blockquote>\n<b></b>',
  411. );
  412. });
  413. });
  414. });
  415. describe('micromark-extension-directive (compile)', () => {
  416. it('should support a directives (abbr)', () => {
  417. expect(
  418. micromark(
  419. [
  420. 'a $abbr',
  421. 'a $abbr[HTML]',
  422. 'a $abbr(title="HyperText Markup Language")',
  423. 'a $abbr[HTML](title="HyperText Markup Language")',
  424. ].join('\n\n'),
  425. options({ abbr }),
  426. ),
  427. ).toBe(
  428. [
  429. '<p>a <abbr></abbr></p>',
  430. '<p>a <abbr>HTML</abbr></p>',
  431. '<p>a <abbr title="HyperText Markup Language"></abbr></p>',
  432. '<p>a <abbr title="HyperText Markup Language">HTML</abbr></p>',
  433. ].join('\n'),
  434. );
  435. });
  436. it('should support directives (youtube)', () => {
  437. expect(
  438. micromark(
  439. [
  440. 'Text:',
  441. 'a $youtube',
  442. 'a $youtube[Cat in a box a]',
  443. 'a $youtube(v=1)',
  444. 'a $youtube[Cat in a box b](v=2)',
  445. 'Leaf:',
  446. '$youtube',
  447. '$youtube[Cat in a box c]',
  448. '$youtube(v=3)',
  449. '$youtube[Cat in a box d](v=4)',
  450. ].join('\n\n'),
  451. options({ youtube }),
  452. ),
  453. ).toBe(
  454. [
  455. '<p>Text:</p>',
  456. '<p>a </p>',
  457. '<p>a </p>',
  458. '<p>a <iframe src="https://www.youtube.com/embed/1" allowfullscreen></iframe></p>',
  459. '<p>a <iframe src="https://www.youtube.com/embed/2" allowfullscreen title="Cat in a box b"></iframe></p>',
  460. '<p>Leaf:</p>',
  461. '<iframe src="https://www.youtube.com/embed/3" allowfullscreen></iframe>',
  462. '<iframe src="https://www.youtube.com/embed/4" allowfullscreen title="Cat in a box d"></iframe>',
  463. ].join('\n'),
  464. );
  465. });
  466. it('should support directives (lsx)', () => {
  467. expect(
  468. micromark(
  469. [
  470. 'Text:',
  471. 'a $lsx',
  472. 'a $lsx()',
  473. 'a $lsx(num=1)',
  474. 'a $lsx(/)',
  475. 'a $lsx(/,num=5,depth=1)',
  476. 'a $lsx(/, num=5, depth=1)',
  477. 'a $lsx(💚)',
  478. 'Leaf:',
  479. '$lsx',
  480. '$lsx()',
  481. '$lsx(num=1)',
  482. '$lsx(/)',
  483. '$lsx(/,num=5,depth=1)',
  484. '$lsx(/, num=5, depth=1)',
  485. '$lsx(💚)',
  486. ].join('\n\n'),
  487. options({ lsx }),
  488. ),
  489. ).toBe(
  490. [
  491. '<p>Text:</p>',
  492. '<p>a <lsx ></lsx></p>',
  493. '<p>a <lsx ></lsx></p>',
  494. '<p>a <lsx num="1"></lsx></p>',
  495. '<p>a <lsx prefix="/"></lsx></p>',
  496. '<p>a <lsx prefix="/" num="5" depth="1"></lsx></p>',
  497. '<p>a <lsx prefix="/" num="5" depth="1"></lsx></p>',
  498. '<p>a <lsx prefix="💚"></lsx></p>',
  499. '<p>Leaf:</p>',
  500. '<lsx ></lsx>',
  501. '<lsx ></lsx>',
  502. '<lsx num="1"></lsx>',
  503. '<lsx prefix="/"></lsx>',
  504. '<lsx prefix="/" num="5" depth="1"></lsx>',
  505. '<lsx prefix="/" num="5" depth="1"></lsx>',
  506. '<lsx prefix="💚"></lsx>',
  507. ].join('\n'),
  508. );
  509. });
  510. it('should support fall through directives (`*`)', () => {
  511. expect(
  512. micromark(
  513. 'a $youtube[Cat in a box]\n$br a',
  514. options({ youtube, '*': h }),
  515. ),
  516. ).toBe('<p>a <youtube>Cat in a box</youtube>\n<br> a</p>');
  517. });
  518. it('should support fall through directives (`*`)', () => {
  519. expect(
  520. micromark('a $a[$img(src="x" alt=y)](href="z")', options({ '*': h })),
  521. ).toBe('<p>a <a href="z"><img src="x" alt="y"></a></p>');
  522. });
  523. });
  524. describe('content', () => {
  525. it('should support character escapes and character references in label', () => {
  526. expect(micromark('a $abbr[x\\&y&amp;z]', options({ abbr }))).toBe(
  527. '<p>a <abbr>x&amp;y&amp;z</abbr></p>',
  528. );
  529. });
  530. it('should support escaped brackets in a label', () => {
  531. expect(micromark('a $abbr[x\\[y\\]z]', options({ abbr }))).toBe(
  532. '<p>a <abbr>x[y]z</abbr></p>',
  533. );
  534. });
  535. it('should support balanced brackets in a label', () => {
  536. expect(micromark('a $abbr[x[y]z]', options({ abbr }))).toBe(
  537. '<p>a <abbr>x[y]z</abbr></p>',
  538. );
  539. });
  540. it('should support balanced brackets in a label, 32 levels deep', () => {
  541. expect(
  542. micromark(
  543. 'a $abbr[1[2[3[4[5[6[7[8[9[10[11[12[13[14[15[16[17[18[19[20[21[22[23[24[25[26[27[28[29[30[31[32[x]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]',
  544. options({ abbr }),
  545. ),
  546. ).toBe(
  547. '<p>a <abbr>1[2[3[4[5[6[7[8[9[10[11[12[13[14[15[16[17[18[19[20[21[22[23[24[25[26[27[28[29[30[31[32[x]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]</abbr></p>',
  548. );
  549. });
  550. it('should *not* support balanced brackets in a label, 33 levels deep', () => {
  551. expect(
  552. micromark(
  553. '$abbr[1[2[3[4[5[6[7[8[9[10[11[12[13[14[15[16[17[18[19[20[21[22[23[24[25[26[27[28[29[30[31[32[33[x]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]',
  554. options({ abbr }),
  555. ),
  556. ).toBe(
  557. '<p><abbr></abbr>[1[2[3[4[5[6[7[8[9[10[11[12[13[14[15[16[17[18[19[20[21[22[23[24[25[26[27[28[29[30[31[32[33[x]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]</p>',
  558. );
  559. });
  560. it('should support EOLs in a label', () => {
  561. expect(micromark('$abbr[a\nb\rc]', options({ abbr }))).toBe(
  562. '<p><abbr>a\nb\rc</abbr></p>',
  563. );
  564. });
  565. it('should support EOLs at the edges of a label (1)', () => {
  566. expect(micromark('$abbr[\na\r]', options({ abbr }))).toBe(
  567. '<p><abbr>\na\r</abbr></p>',
  568. );
  569. });
  570. it('should support EOLs at the edges of a label (2)', () => {
  571. expect(micromark('$abbr[\n]', options({ abbr }))).toBe(
  572. '<p><abbr>\n</abbr></p>',
  573. );
  574. });
  575. // == does not work but I don't know why.. -- 2022.08.12 Yuki Takei
  576. // it('should support EOLs around nested directives', () => {
  577. // expect(micromark('$abbr[a\n$abbr[b]\nc]', options({ abbr })))
  578. // .toBe('<p><abbr>a\n<abbr>b</abbr>\nc</abbr></p>');
  579. // });
  580. it('should support EOLs inside nested directives (1)', () => {
  581. expect(micromark('$abbr[$abbr[\n]]', options({ abbr }))).toBe(
  582. '<p><abbr><abbr>\n</abbr></abbr></p>',
  583. );
  584. });
  585. it('should support EOLs inside nested directives (2)', () => {
  586. expect(micromark('$abbr[$abbr[a\nb]]', options({ abbr }))).toBe(
  587. '<p><abbr><abbr>a\nb</abbr></abbr></p>',
  588. );
  589. });
  590. it('should support EOLs inside nested directives (3)', () => {
  591. expect(micromark('$abbr[$abbr[\nb\n]]', options({ abbr }))).toBe(
  592. '<p><abbr><abbr>\nb\n</abbr></abbr></p>',
  593. );
  594. });
  595. it('should support EOLs inside nested directives (4)', () => {
  596. expect(micromark('$abbr[$abbr[\\\n]]', options({ abbr }))).toBe(
  597. '<p><abbr><abbr><br />\n</abbr></abbr></p>',
  598. );
  599. });
  600. it('should support markdown in a label', () => {
  601. expect(micromark('a $abbr[a *b* **c** d]', options({ abbr }))).toBe(
  602. '<p>a <abbr>a <em>b</em> <strong>c</strong> d</abbr></p>',
  603. );
  604. });
  605. it('should support character references in unquoted attribute values', () => {
  606. expect(micromark('a $abbr(title=a&apos;b)', options({ abbr }))).toBe(
  607. '<p>a <abbr title="a\'b"></abbr></p>',
  608. );
  609. });
  610. it('should support character references in double attribute values', () => {
  611. expect(micromark('a $abbr(title="a&apos;b")', options({ abbr }))).toBe(
  612. '<p>a <abbr title="a\'b"></abbr></p>',
  613. );
  614. });
  615. it('should support character references in single attribute values', () => {
  616. expect(micromark("a $abbr(title='a&apos;b')", options({ abbr }))).toBe(
  617. '<p>a <abbr title="a\'b"></abbr></p>',
  618. );
  619. });
  620. it('should support unknown character references in attribute values', () => {
  621. expect(
  622. micromark('a $abbr(title="a&somethingelse;b")', options({ abbr })),
  623. ).toBe('<p>a <abbr title="a&amp;somethingelse;b"></abbr></p>');
  624. });
  625. it('should support EOLs between attributes', () => {
  626. expect(micromark('$span(a\nb)', options({ '*': h }))).toBe(
  627. '<p><span a="" b=""></span></p>',
  628. );
  629. });
  630. it('should support EOLs at the edges of attributes', () => {
  631. expect(micromark('$span(\na\n)', options({ '*': h }))).toBe(
  632. '<p><span a=""></span></p>',
  633. );
  634. });
  635. it('should support EOLs before initializer', () => {
  636. expect(micromark('$span(a\r= b)', options({ '*': h }))).toBe(
  637. '<p><span a="b"></span></p>',
  638. );
  639. });
  640. it('should support EOLs after initializer', () => {
  641. expect(micromark('$span(a=\r\nb)', options({ '*': h }))).toBe(
  642. '<p><span a="b"></span></p>',
  643. );
  644. });
  645. it('should support EOLs between an unquoted attribute value and a next attribute name', () => {
  646. expect(micromark('$span(a=b\nc)', options({ '*': h }))).toBe(
  647. '<p><span a="b" c=""></span></p>',
  648. );
  649. });
  650. it('should support EOLs in a double quoted attribute value', () => {
  651. expect(micromark('$span(a="b\nc")', options({ '*': h }))).toBe(
  652. '<p><span a="b\nc"></span></p>',
  653. );
  654. });
  655. it('should support EOLs in a single quoted attribute value', () => {
  656. expect(micromark("$span(a='b\nc')", options({ '*': h }))).toBe(
  657. '<p><span a="b\nc"></span></p>',
  658. );
  659. });
  660. it('should support attrs which contains `#` (1)', () => {
  661. expect(micromark('a $span(#a#b)', options({ '*': h }))).toBe(
  662. '<p>a <span #a#b=""></span></p>',
  663. );
  664. });
  665. it('should support attrs which contains `#` (2)', () => {
  666. expect(micromark('a $span(id=a id="b" #c#d)', options({ '*': h }))).toBe(
  667. '<p>a <span id="b" #c#d=""></span></p>',
  668. );
  669. });
  670. it('should support attrs with dot notation', () => {
  671. expect(micromark('a $span(.a.b)', options({ '*': h }))).toBe(
  672. '<p>a <span .a.b=""></span></p>',
  673. );
  674. });
  675. describe('spec for growi plugin', () => {
  676. it('should support name with slash', () => {
  677. expect(micromark('a $lsx(/Sandbox)', options())).toBe('<p>a </p>');
  678. });
  679. it('should support name=value and an attribute w/o value', () => {
  680. expect(micromark('a $lsx(key=value, reverse)', options())).toBe(
  681. '<p>a </p>',
  682. );
  683. });
  684. it('should support consecutive attributes w/o value', () => {
  685. expect(micromark('a $lsx(key=value, reverse, reverse2)', options())).toBe(
  686. '<p>a </p>',
  687. );
  688. });
  689. it('should support name=value after an empty value attribute', () => {
  690. expect(micromark('a $lsx(/Sandbox, key=value, reverse)', options())).toBe(
  691. '<p>a </p>',
  692. );
  693. });
  694. });
  695. });
  696. /** @type {Handle} */
  697. function abbr(d) {
  698. if (d.type !== DirectiveType.Text) return false;
  699. this.tag('<abbr');
  700. if (d.attributes && 'title' in d.attributes) {
  701. this.tag(` title="${this.encode(d.attributes.title)}"`);
  702. }
  703. this.tag('>');
  704. this.raw(d.label || '');
  705. this.tag('</abbr>');
  706. }
  707. /** @type {Handle} */
  708. function youtube(d) {
  709. const attrs = d.attributes || {};
  710. const v = attrs.v;
  711. /** @type {string} */
  712. let prop;
  713. if (!v) return false;
  714. const list = [
  715. `src="https://www.youtube.com/embed/${this.encode(v)}"`,
  716. 'allowfullscreen',
  717. ];
  718. if (d.label) {
  719. list.push(`title="${this.encode(d.label)}"`);
  720. }
  721. for (prop in attrs) {
  722. if (prop !== 'v') {
  723. list.push(`${this.encode(prop)}="${this.encode(attrs[prop])}"`);
  724. }
  725. }
  726. this.tag(`<iframe ${list.join(' ')}>`);
  727. if (d.content) {
  728. this.lineEndingIfNeeded();
  729. this.raw(d.content);
  730. this.lineEndingIfNeeded();
  731. }
  732. this.tag('</iframe>');
  733. }
  734. /** @type {Handle} */
  735. function lsx(d) {
  736. const attrs = d.attributes || {};
  737. const props = [];
  738. for (const key in attrs) {
  739. if (attrs[key].length === 0) {
  740. props.push(`prefix="${key}"`);
  741. } else {
  742. props.push(`${key}="${attrs[key]}"`);
  743. }
  744. }
  745. this.tag(`<lsx ${props.join(' ')}>`);
  746. this.tag('</lsx>');
  747. }
  748. /** @type {Handle} */
  749. function h(d) {
  750. const content = d.content || d.label;
  751. const attrs = d.attributes || {};
  752. /** @type {Array.<string>} */
  753. const list = [];
  754. /** @type {string} */
  755. let prop;
  756. for (prop in attrs) {
  757. if (own.call(attrs, prop)) {
  758. list.push(`${this.encode(prop)}="${this.encode(attrs[prop])}"`);
  759. }
  760. }
  761. this.tag(`<${d.name}`);
  762. if (list.length > 0) this.tag(` ${list.join(' ')}`);
  763. this.tag('>');
  764. if (content) {
  765. if (d.type === 'containerGrowiPluginDirective') this.lineEndingIfNeeded();
  766. this.raw(content);
  767. if (d.type === 'containerGrowiPluginDirective') this.lineEndingIfNeeded();
  768. }
  769. if (!htmlVoidElements.includes(d.name)) this.tag(`</${d.name}>`);
  770. }
  771. /**
  772. * @param {HtmlOptions} [options]
  773. */
  774. function options(options) {
  775. return {
  776. allowDangerousHtml: true,
  777. extensions: [syntax()],
  778. htmlExtensions: [html(options)],
  779. };
  780. }