Yuki Takei 2 лет назад
Родитель
Сommit
505e0d0048
1 измененных файлов с 62 добавлено и 0 удалено
  1. 62 0
      apps/app/src/services/renderer/rehype-plugins/relative-links.spec.ts

+ 62 - 0
apps/app/src/services/renderer/rehype-plugins/relative-links.spec.ts

@@ -0,0 +1,62 @@
+import { describe, test, expect } from 'vitest';
+
+import { select, type HastNode } from 'hast-util-select';
+
+import { unified } from 'unified';
+import parse from 'remark-parse';
+import remarkRehype from 'remark-rehype';
+
+import { relativeLinks } from './relative-links';
+
+describe('relativeLinks', () => {
+
+  test.concurrent.each`
+    originalHref
+      ${'http://example.com/Sandbox'}
+      ${'#header'}
+    `('leaves the original href \'$originalHref\' as-is', ({ originalHref }) => {
+
+      // setup
+      const processor = unified()
+        .use(parse)
+        .use(remarkRehype)
+        .use(relativeLinks, {});
+
+      // when
+      const mdastTree = processor.parse(`[link](${originalHref})`);
+      const hastTree = processor.runSync(mdastTree) as HastNode;
+
+      // then
+      const anchorElement = select('a', hastTree);
+      expect(anchorElement?.properties?.href).toBe(originalHref);
+    });
+
+  test.concurrent.each`
+    originalHref                        | expectedHref
+      ${'/Sandbox'}                     | ${'/Sandbox'}
+      ${'/Sandbox?q=foo'}               | ${'/Sandbox?q=foo'}
+      ${'/Sandbox#header'}              | ${'/Sandbox#header'}
+      ${'/Sandbox?q=foo#header'}        | ${'/Sandbox?q=foo#header'}
+      ${'./Sandbox'}                    | ${'/foo/bar/Sandbox'}
+      ${'./Sandbox?q=foo'}              | ${'/foo/bar/Sandbox?q=foo'}
+      ${'./Sandbox#header'}             | ${'/foo/bar/Sandbox#header'}
+      ${'./Sandbox?q=foo#header'}       | ${'/foo/bar/Sandbox?q=foo#header'}
+    `('rewrites the original href \'$originalHref\' to \'$expectedHref\'', ({ originalHref, expectedHref }) => {
+
+      // setup
+      const pagePath = '/foo/bar/baz';
+      const processor = unified()
+        .use(parse)
+        .use(remarkRehype)
+        .use(relativeLinks, { pagePath });
+
+      // when
+      const mdastTree = processor.parse(`[link](${originalHref})`);
+      const hastTree = processor.runSync(mdastTree) as HastNode;
+
+      // then
+      const anchorElement = select('a', hastTree);
+      expect(anchorElement?.properties?.href).toBe(expectedHref);
+    });
+
+})