callout.spec.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import type { ContainerDirective } from 'mdast-util-directive';
  2. import remarkDirective from 'remark-directive';
  3. import remarkParse from 'remark-parse';
  4. import { unified } from 'unified';
  5. import { visit } from 'unist-util-visit';
  6. import { describe, expect, it } from 'vitest';
  7. import * as callout from './callout';
  8. describe('remarkPlugin', () => {
  9. it('should transform containerDirective to callout', () => {
  10. const processor = unified()
  11. .use(remarkParse)
  12. .use(remarkDirective)
  13. .use(callout.remarkPlugin);
  14. const markdown = `
  15. :::info
  16. This is an info callout.
  17. :::
  18. `;
  19. const tree = processor.parse(markdown);
  20. processor.runSync(tree);
  21. let calloutNode: ContainerDirective | undefined;
  22. visit(tree, 'containerDirective', (node) => {
  23. calloutNode = node;
  24. });
  25. expect(calloutNode).toBeDefined();
  26. assert(calloutNode?.data?.hName != null);
  27. assert(calloutNode?.data?.hProperties != null);
  28. expect(calloutNode.data.hName).toBe('callout');
  29. expect(calloutNode.data.hProperties.type).toBe('info');
  30. expect(calloutNode.data.hProperties.label).not.toBeDefined();
  31. assert('children' in calloutNode.children[0]);
  32. assert('value' in calloutNode.children[0].children[0]);
  33. expect(calloutNode.children.length).toBe(1);
  34. expect(calloutNode.children[0].children[0].value).toBe(
  35. 'This is an info callout.',
  36. );
  37. });
  38. it('should transform containerDirective to callout with custom label', () => {
  39. const processor = unified()
  40. .use(remarkParse)
  41. .use(remarkDirective)
  42. .use(callout.remarkPlugin);
  43. const markdown = `
  44. :::info[CUSTOM LABEL]
  45. This is an info callout.
  46. :::
  47. `;
  48. const tree = processor.parse(markdown);
  49. processor.runSync(tree);
  50. let calloutNode: ContainerDirective | undefined;
  51. visit(tree, 'containerDirective', (node) => {
  52. calloutNode = node;
  53. });
  54. expect(calloutNode).toBeDefined();
  55. assert(calloutNode?.data?.hName != null);
  56. assert(calloutNode?.data?.hProperties != null);
  57. expect(calloutNode.data.hName).toBe('callout');
  58. expect(calloutNode.data.hProperties.type).toBe('info');
  59. expect(calloutNode.data.hProperties.label).toBe('CUSTOM LABEL');
  60. assert('children' in calloutNode.children[0]);
  61. assert('value' in calloutNode.children[0].children[0]);
  62. expect(calloutNode.children.length).toBe(1);
  63. expect(calloutNode.children[0].children[0].value).toBe(
  64. 'This is an info callout.',
  65. );
  66. });
  67. it('should transform containerDirective to callout with empty label', () => {
  68. const processor = unified()
  69. .use(remarkParse)
  70. .use(remarkDirective)
  71. .use(callout.remarkPlugin);
  72. const markdown = `
  73. :::info[]
  74. This is an info callout.
  75. :::
  76. `;
  77. const tree = processor.parse(markdown);
  78. processor.runSync(tree);
  79. let calloutNode: ContainerDirective | undefined;
  80. visit(tree, 'containerDirective', (node) => {
  81. calloutNode = node;
  82. });
  83. expect(calloutNode).toBeDefined();
  84. assert(calloutNode?.data?.hName != null);
  85. assert(calloutNode?.data?.hProperties != null);
  86. expect(calloutNode.data.hName).toBe('callout');
  87. expect(calloutNode.data.hProperties.type).toBe('info');
  88. expect(calloutNode.data.hProperties.label).not.toBeDefined();
  89. assert('children' in calloutNode.children[0]);
  90. assert('value' in calloutNode.children[0].children[0]);
  91. expect(calloutNode.children.length).toBe(1);
  92. expect(calloutNode.children[0].children[0].value).toBe(
  93. 'This is an info callout.',
  94. );
  95. });
  96. });