Skip to content

Commit 5ceb7f2

Browse files
committed
Start on unit tests for image handling
1 parent c263872 commit 5ceb7f2

File tree

5 files changed

+824
-837
lines changed

5 files changed

+824
-837
lines changed

jest.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
preset: "ts-jest",
3+
testEnvironment: "node",
4+
transform: {
5+
"^.+\\.ts?$": "ts-jest",
6+
},
7+
transformIgnorePatterns: ["<rootDir>/node_modules/"],
8+
};

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
},
5757
"devDependencies": {
5858
"@types/fs-extra": "^9.0.13",
59-
"@types/jest": "^26.0.24",
59+
"@types/jest": "^28.1.6",
6060
"@types/node": "^12.20.11",
6161
"@typescript-eslint/eslint-plugin": "^4.22.0",
6262
"@typescript-eslint/parser": "^4.22.0",
@@ -66,11 +66,11 @@
6666
"eslint-config-prettier": "^8.3.0",
6767
"eslint-plugin-node": "^11.1.0",
6868
"eslint-plugin-prettier": "^3.4.0",
69-
"jest": "^27.2.0",
69+
"jest": "^28.1.3",
7070
"lint-staged": "^10.5.4",
7171
"prettier": "^2.2.1",
7272
"semantic-release": "^19.0.2",
73-
"ts-jest": "^27.0.5",
73+
"ts-jest": "^28.0.7",
7474
"ts-node": "^10.2.1",
7575
"typescript": "^4.6.4"
7676
},

src/NotionImage.spec.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { parseImageBlock } from "./NotionImage";
2+
3+
/* eslint-disable @typescript-eslint/require-await */
4+
describe("image caption", () => {
5+
it("should find a caption", async () => {
6+
const img = parseImageBlock(kImageBlock);
7+
expect(img.url).toBe("https://someimage.png");
8+
});
9+
});
10+
11+
const kImageBlock = {
12+
object: "block",
13+
id: "20b821b4-7c5b-41dc-8e30-92c23c125580",
14+
parent: {
15+
type: "page_id",
16+
page_id: "9dd05134-0401-47f6-b159-1e6b76b9aad3",
17+
},
18+
created_time: "2022-07-25T23:05:00.000Z",
19+
last_edited_time: "2022-07-25T23:07:00.000Z",
20+
created_by: {
21+
object: "user",
22+
id: "11fb7f16-0560-4aee-ab88-ed75a850cfc4",
23+
},
24+
last_edited_by: {
25+
object: "user",
26+
id: "11fb7f16-0560-4aee-ab88-ed75a850cfc4",
27+
},
28+
has_children: false,
29+
archived: false,
30+
type: "image",
31+
image: {
32+
caption: [
33+
{
34+
type: "text",
35+
text: { content: "fr-", link: null },
36+
annotations: {
37+
bold: false,
38+
italic: false,
39+
strikethrough: false,
40+
underline: false,
41+
code: false,
42+
color: "default",
43+
},
44+
plain_text: "fr-",
45+
href: null,
46+
},
47+
{
48+
type: "text",
49+
text: {
50+
content: "https://i.imgur.com/pYmE7OJ.png",
51+
link: { url: "https://i.imgur.com/pYmE7OJ.png" },
52+
},
53+
annotations: {
54+
bold: false,
55+
italic: false,
56+
strikethrough: false,
57+
underline: false,
58+
code: false,
59+
color: "default",
60+
},
61+
plain_text: "https://i.imgur.com/pYmE7OJ.png",
62+
href: "https://i.imgur.com/pYmE7OJ.png",
63+
},
64+
{
65+
type: "text",
66+
text: { content: " es-", link: null },
67+
annotations: {
68+
bold: false,
69+
italic: false,
70+
strikethrough: false,
71+
underline: false,
72+
code: false,
73+
color: "default",
74+
},
75+
plain_text: " es-",
76+
href: null,
77+
},
78+
{
79+
type: "text",
80+
text: {
81+
content: "https://i.imgur.com/8paSZ0i.png",
82+
link: { url: "https://i.imgur.com/8paSZ0i.png" },
83+
},
84+
annotations: {
85+
bold: false,
86+
italic: false,
87+
strikethrough: false,
88+
underline: false,
89+
code: false,
90+
color: "default",
91+
},
92+
plain_text: "https://i.imgur.com/8paSZ0i.png",
93+
href: "https://i.imgur.com/8paSZ0i.png",
94+
},
95+
],
96+
type: "file",
97+
file: {
98+
url: "https://someimage.png",
99+
expiry_time: "2022-07-26T00:19:09.096Z",
100+
},
101+
},
102+
};

src/NotionImage.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,33 @@ function hashOfString(s: string) {
6666
return Math.abs(hash);
6767
}
6868

69-
// Download the image if we don't have it, give it a good name, and
70-
// change the src to point to our copy of the image.
71-
export async function processImageBlock(b: any): Promise<void> {
72-
let url = "";
69+
type LocalizableImageWithCaption = {
70+
url: string;
71+
caption?: string;
72+
localizedUrls: Array<{ iso632Code: string; url: string }>;
73+
};
74+
export function parseImageBlock(b: any): LocalizableImageWithCaption {
75+
const img: LocalizableImageWithCaption = {
76+
url: "",
77+
localizedUrls: [],
78+
};
79+
7380
if ("file" in b.image) {
74-
url = b.image.file.url; // image saved on notion (actually AWS)
81+
img.url = b.image.file.url; // image saved on notion (actually AWS)
7582
} else {
76-
url = b.image.external.url; // image still pointing somewhere else. I've see this happen when copying a Google Doc into Notion. Notion kep pointing at the google doc.
83+
img.url = b.image.external.url; // image still pointing somewhere else. I've see this happen when copying a Google Doc into Notion. Notion kep pointing at the google doc.
7784
}
7885

79-
const newPath = imagePrefix + "/" + (await saveImage(url, imageOutputPath));
86+
return img;
87+
}
88+
89+
// Download the image if we don't have it, give it a good name, and
90+
// change the src to point to our copy of the image.
91+
export async function processImageBlock(b: any): Promise<void> {
92+
const img = parseImageBlock(b);
93+
94+
const newPath =
95+
imagePrefix + "/" + (await saveImage(img.url, imageOutputPath));
8096

8197
// change the src to point to our copy of the image
8298
if ("file" in b.image) {

0 commit comments

Comments
 (0)