pukiwiki-like-linker.test.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { HastNode, selectAll } from 'hast-util-select';
  2. import parse from 'remark-parse';
  3. import rehype from 'remark-rehype';
  4. import { unified } from 'unified';
  5. import { visit } from 'unist-util-visit';
  6. import { relativeLinksByPukiwikiLikeLinker } from '../../../../src/services/renderer/rehype-plugins/relative-links-by-pukiwiki-like-linker';
  7. import { pukiwikiLikeLinker } from '../../../../src/services/renderer/remark-plugins/pukiwiki-like-linker';
  8. describe('pukiwikiLikeLinker', () => {
  9. /* eslint-disable indent */
  10. describe.each`
  11. input | expectedHref | expectedValue
  12. ${'[[/page]]'} | ${'/page'} | ${'/page'}
  13. ${'[[./page]]'} | ${'./page'} | ${'./page'}
  14. ${'[[Title>./page]]'} | ${'./page'} | ${'Title'}
  15. ${'[[Title>https://example.com]]'} | ${'https://example.com'} | ${'Title'}
  16. `('should parse correctly', ({ input, expectedHref, expectedValue }) => {
  17. /* eslint-enable indent */
  18. test(`when the input is '${input}'`, () => {
  19. // setup:
  20. const processor = unified()
  21. .use(parse)
  22. .use(pukiwikiLikeLinker);
  23. // when:
  24. const ast = processor.parse(input);
  25. expect(ast).not.toBeNull();
  26. visit(ast, 'wikiLink', (node: any) => {
  27. expect(node.data.alias).toEqual(expectedValue);
  28. expect(node.data.permalink).toEqual(expectedHref);
  29. expect(node.data.hName).toEqual('a');
  30. expect(node.data.hProperties.className.startsWith('pukiwiki-like-linker')).toBeTruthy();
  31. expect(node.data.hProperties.href).toEqual(expectedHref);
  32. expect(node.data.hChildren[0].value).toEqual(expectedValue);
  33. });
  34. });
  35. });
  36. });
  37. describe('relativeLinksByPukiwikiLikeLinker', () => {
  38. /* eslint-disable indent */
  39. describe.each`
  40. input | expectedHref | expectedValue
  41. ${'[[/page]]'} | ${'/page'} | ${'/page'}
  42. ${'[[./page]]'} | ${'/user/admin/page'} | ${'./page'}
  43. ${'[[Title>./page]]'} | ${'/user/admin/page'} | ${'Title'}
  44. ${'[[Title>https://example.com]]'} | ${'https://example.com'} | ${'Title'}
  45. `('should convert relative links correctly', ({ input, expectedHref, expectedValue }) => {
  46. /* eslint-enable indent */
  47. test(`when the input is '${input}'`, () => {
  48. // setup:
  49. const processor = unified()
  50. .use(parse)
  51. .use(pukiwikiLikeLinker)
  52. .use(rehype)
  53. .use(relativeLinksByPukiwikiLikeLinker, { pagePath: '/user/admin' });
  54. // when:
  55. const mdast = processor.parse(input);
  56. const hast = processor.runSync(mdast);
  57. expect(hast).not.toBeNull();
  58. expect((hast as any).children[0].type).toEqual('element');
  59. const anchors = selectAll('a', hast as HastNode);
  60. expect(anchors.length).toEqual(1);
  61. const anchor = anchors[0];
  62. expect(anchor.tagName).toEqual('a');
  63. expect((anchor.properties as any).className.startsWith('pukiwiki-like-linker')).toBeTruthy();
  64. expect(anchor.properties?.href).toEqual(expectedHref);
  65. expect(anchor.children[0]).not.toBeNull();
  66. expect(anchor.children[0].type).toEqual('text');
  67. expect(anchor.children[0].value).toEqual(expectedValue);
  68. });
  69. });
  70. });