Skip to content

Commit

Permalink
fix: ignore data urls in parseImagePaths (#4053)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored Jul 12, 2024
1 parent a03fd5f commit 9224240
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,15 @@ describe("parseImagePaths", () => {
);
});

it("should ignore data urls", () => {
const page = "This is a test page with an image ![image](data:image/png;base64,abc)";
const result = parseImagePaths(page, PATHS);
expect(result.filepaths).toEqual([]);
expect(result.markdown.trim()).toMatchInlineSnapshot(
'"This is a test page with an image ![image](data:image/png;base64,abc)"'
);
});

it("should ignore external urls in html tags", () => {
const page = "This is a test page with an image <img src='https://external.com/image.png' />";
const result = parseImagePaths(page, PATHS);
Expand All @@ -282,6 +291,15 @@ describe("parseImagePaths", () => {
);
});

it("should ignore data urls in html tags", () => {
const page = "This is a test page with an image <img src='data:image/png;base64,abc' />";
const result = parseImagePaths(page, PATHS);
expect(result.filepaths).toEqual([]);
expect(result.markdown.trim()).toMatchInlineSnapshot(
"\"This is a test page with an image <img src='data:image/png;base64,abc' />\""
);
});

it("should ignore external urls in mdx img tags", () => {
const page = "This is a test page with an image <img src={'https://external.com/image.png'} />";
const result = parseImagePaths(page, PATHS);
Expand Down
8 changes: 6 additions & 2 deletions packages/cli/docs-markdown-utils/src/parseImagePaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function resolvePath(
pathToImage: string | undefined,
{ absolutePathToFernFolder, absolutePathToMdx }: AbsolutePathMetadata
): AbsoluteFilePath | undefined {
if (pathToImage == null || isExternalUrl(pathToImage)) {
if (pathToImage == null || isExternalUrl(pathToImage) || isDataUrl(pathToImage)) {
return undefined;
}

Expand All @@ -154,6 +154,10 @@ function isExternalUrl(url: string): boolean {
return /^(https?:)?\/\//.test(url);
}

function isDataUrl(url: string): boolean {
return /^data:/.test(url);

Check warning on line 158 in packages/cli/docs-markdown-utils/src/parseImagePaths.ts

View workflow job for this annotation

GitHub Actions / eslint

Use 'String#startsWith' method instead
}

/**
* This step should run after the images have been uploaded. It replaces the image paths in the markdown with the fileIDs.
* In the frontend, the fileIDs are then used to securely fetch the images.
Expand Down Expand Up @@ -184,7 +188,7 @@ export function replaceImagePathsAndUrls(
let replaced = original;

function replaceSrc(src: string | undefined) {
if (src != null && !isExternalUrl(src)) {
if (src != null && !isExternalUrl(src) && !isDataUrl(src)) {
try {
const fileId = fileIdsMap.get(AbsoluteFilePath.of(src));
if (fileId != null) {
Expand Down

0 comments on commit 9224240

Please sign in to comment.