github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/webui/src/lib/remark-plugins/imageUriReplacer.spec.ts (about)

     1  import { remark } from "remark";
     2  import { describe, test, expect } from "vitest";
     3  import { getImageUrl } from "./imageUriReplacer";
     4  
     5  import imageUriReplacer from "./imageUriReplacer";
     6  
     7  const TEST_REPO = "test-repo";
     8  const TEST_REF = "test-ref";
     9  const TEST_FILE_NAME = "image.png";
    10  const ADDITIONAL_PATH = "additional/path";
    11  
    12  describe("imageUriReplacer", async () => {
    13    test("Basic replacement", async () => {
    14      const markdown = `# README
    15  
    16  Text and whatever and hey look at this image:
    17  ![lakefs://image.png](lakefs://${TEST_REPO}/${TEST_REF}/${TEST_FILE_NAME})
    18  `;
    19  
    20      const markdownWithReplacedImage = `# README
    21  
    22  Text and whatever and hey look at this image:
    23  ![lakefs://image.png](${await getImageUrl(TEST_REPO, TEST_REF, TEST_FILE_NAME, false)})
    24  `;
    25  
    26      const result = await remark()
    27        .use(imageUriReplacer, {
    28          repo: TEST_REPO,
    29          ref: TEST_REF,
    30          path: "",
    31          presign: false,
    32        })
    33        .process(markdown);
    34      expect(result.toString()).toEqual(markdownWithReplacedImage);
    35    });
    36  
    37    test("Replacement with additional path", async () => {
    38      const markdown = `# README
    39  
    40  Text and whatever and hey look at this image:
    41  ![lakefs://image.png](lakefs://${TEST_REPO}/${TEST_REF}/${ADDITIONAL_PATH}/${TEST_FILE_NAME})
    42  `;
    43  
    44      const markdownWithReplacedImage = `# README
    45  
    46  Text and whatever and hey look at this image:
    47  ![lakefs://image.png](${await getImageUrl(
    48        TEST_REPO,
    49        TEST_REF,
    50        `${ADDITIONAL_PATH}/${TEST_FILE_NAME}`,
    51        false,
    52      )})
    53  `;
    54  
    55      const result = await remark()
    56        .use(imageUriReplacer,
    57          {
    58            repo: TEST_REPO,
    59            ref: TEST_REF,
    60            path: "",
    61            presign: false,
    62          })
    63        .process(markdown);
    64      expect(result.toString()).toEqual(markdownWithReplacedImage);
    65    });
    66  
    67    test("Supports relative paths w/o leading slash", async () => {
    68      const markdown = `# README
    69  
    70  Text and whatever and hey look at this image:
    71  ![lakefs://image.png](${TEST_FILE_NAME})
    72  `;
    73  
    74      const markdownWithReplacedImage = `# README
    75  
    76  Text and whatever and hey look at this image:
    77  ![lakefs://image.png](${await getImageUrl(TEST_REPO, TEST_REF, TEST_FILE_NAME, false)})
    78  `;
    79  
    80      const result = await remark()
    81        .use(imageUriReplacer, {
    82          repo: TEST_REPO,
    83          ref: TEST_REF,
    84          path: "",
    85          presign: false,
    86        })
    87        .process(markdown);
    88      expect(result.toString()).toEqual(markdownWithReplacedImage);
    89    });
    90  
    91    test("Supports relative paths w/ leading slash", async () => {
    92      const markdown = `# README
    93  
    94  Text and whatever and hey look at this image:
    95  ![lakefs://image.png](/${TEST_FILE_NAME})
    96  `;
    97  
    98      const markdownWithReplacedImage = `# README
    99  
   100  Text and whatever and hey look at this image:
   101  ![lakefs://image.png](${await getImageUrl(TEST_REPO, TEST_REF, TEST_FILE_NAME, false)})
   102  `;
   103  
   104      const result = await remark()
   105        .use(imageUriReplacer, {
   106          repo: TEST_REPO,
   107          ref: TEST_REF,
   108          path: "",
   109          presign: false,
   110        })
   111        .process(markdown);
   112      expect(result.toString()).toEqual(markdownWithReplacedImage);
   113    });
   114  
   115    test("Supports relative paths ./", async () => {
   116      const markdownFilePath = "test";
   117      const markdown = `# README
   118  
   119  Text and whatever and hey look at this image:
   120  ![lakefs://image.png](./${TEST_FILE_NAME})
   121  `;
   122  
   123      const markdownWithReplacedImage = `# README
   124  
   125  Text and whatever and hey look at this image:
   126  ![lakefs://image.png](${await getImageUrl(
   127        TEST_REPO,
   128        TEST_REF,
   129        `${markdownFilePath}/${TEST_FILE_NAME}`,
   130        false,
   131      )})
   132  `;
   133  
   134      const result = await remark()
   135        .use(imageUriReplacer, {
   136          repo: TEST_REPO,
   137          ref: TEST_REF,
   138          path: `${markdownFilePath}/test.md`,
   139          presign: false,
   140        })
   141        .process(markdown);
   142      expect(result.toString()).toEqual(markdownWithReplacedImage);
   143    });
   144  });