linker.spec.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { describe, expect, it } from 'vitest';
  2. import { Linker } from './linker';
  3. describe('Linker.fromMarkdownString', () => {
  4. describe('pukiwiki link with label', () => {
  5. it('should parse [[label>link]] format', () => {
  6. const linker = Linker.fromMarkdownString('[[my label>my link]]');
  7. expect(linker.type).toBe(Linker.types.pukiwikiLink);
  8. expect(linker.label).toBe('my label');
  9. expect(linker.link).toBe('my link');
  10. });
  11. });
  12. describe('pukiwiki link without label', () => {
  13. it('should parse [[link]] format and use link as label', () => {
  14. const linker = Linker.fromMarkdownString('[[my link]]');
  15. expect(linker.type).toBe(Linker.types.pukiwikiLink);
  16. expect(linker.label).toBe('my link');
  17. expect(linker.link).toBe('my link');
  18. });
  19. });
  20. describe('markdown link', () => {
  21. it('should parse [label](link) format', () => {
  22. const linker = Linker.fromMarkdownString(
  23. '[my label](https://example.com)',
  24. );
  25. expect(linker.type).toBe(Linker.types.markdownLink);
  26. expect(linker.label).toBe('my label');
  27. expect(linker.link).toBe('https://example.com');
  28. });
  29. it('should parse [label](link) with empty label and fill label with link', () => {
  30. const linker = Linker.fromMarkdownString('[](https://example.com)');
  31. expect(linker.type).toBe(Linker.types.markdownLink);
  32. // label is filled with link when empty (see initWhenMarkdownLink)
  33. expect(linker.label).toBe('https://example.com');
  34. expect(linker.link).toBe('https://example.com');
  35. });
  36. it('should parse [label](link) with path', () => {
  37. const linker = Linker.fromMarkdownString('[page](/path/to/page)');
  38. expect(linker.type).toBe(Linker.types.markdownLink);
  39. expect(linker.label).toBe('page');
  40. expect(linker.link).toBe('/path/to/page');
  41. });
  42. });
  43. describe('non-matching string', () => {
  44. it('should create markdownLink with string as label when no pattern matches', () => {
  45. const linker = Linker.fromMarkdownString('plain text');
  46. expect(linker.type).toBe(Linker.types.markdownLink);
  47. expect(linker.label).toBe('plain text');
  48. expect(linker.link).toBe('');
  49. });
  50. it('should handle empty string', () => {
  51. const linker = Linker.fromMarkdownString('');
  52. expect(linker.type).toBe(Linker.types.markdownLink);
  53. expect(linker.label).toBe('');
  54. expect(linker.link).toBe('');
  55. });
  56. });
  57. });