@sillsdev/docu-notion 0.13.3 → 0.13.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,3 @@
1
1
  import { ImageSet } from "./images";
2
2
  export declare function makeImagePersistencePlan(imageSet: ImageSet, imageOutputRootPath: string, imagePrefix: string): void;
3
+ export declare function hashOfString(s: string): number;
@@ -23,21 +23,21 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.makeImagePersistencePlan = void 0;
26
+ exports.hashOfString = exports.makeImagePersistencePlan = void 0;
27
27
  const Path = __importStar(require("path"));
28
28
  const log_1 = require("./log");
29
29
  const process_1 = require("process");
30
30
  function makeImagePersistencePlan(imageSet, imageOutputRootPath, imagePrefix) {
31
- var _a;
31
+ var _a, _b;
32
32
  if ((_a = imageSet.fileType) === null || _a === void 0 ? void 0 : _a.ext) {
33
33
  // Since most images come from pasting screenshots, there isn't normally a filename. That's fine, we just make a hash of the url
34
34
  // Images that are stored by notion come to us with a complex url that changes over time, so we pick out the UUID that doesn't change. Example:
35
35
  // https://s3.us-west-2.amazonaws.com/secure.notion-static.com/d1058f46-4d2f-4292-8388-4ad393383439/Untitled.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220516%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220516T233630Z&X-Amz-Expires=3600&X-Amz-Signature=f215704094fcc884d37073b0b108cf6d1c9da9b7d57a898da38bc30c30b4c4b5&X-Amz-SignedHeaders=host&x-id=GetObject
36
- let thingToHash = imageSet.primaryUrl;
37
- const m = /.*secure\.notion-static\.com\/(.*)\//gm.exec(imageSet.primaryUrl);
38
- if (m && m.length > 1) {
39
- thingToHash = m[1];
40
- }
36
+ // But around Sept 2023, they changed the url to be something like:
37
+ // https://prod-files-secure.s3.us-west-2.amazonaws.com/d9a2b712-cf69-4bd6-9d65-87a4ceeacca2/d1bcdc8c-b065-4e40-9a11-392aabeb220e/Untitled.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20230915%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20230915T161258Z&X-Amz-Expires=3600&X-Amz-Signature=28fca48e65fba86d539c3c4b7676fce1fa0857aa194f7b33dd4a468ecca6ab24&X-Amz-SignedHeaders=host&x-id=GetObject
38
+ // The thing we want is the last UUID before the ?
39
+ const urlBeforeQuery = imageSet.primaryUrl.split("?")[0];
40
+ const thingToHash = (_b = findLastUuid(urlBeforeQuery)) !== null && _b !== void 0 ? _b : urlBeforeQuery;
41
41
  const hash = hashOfString(thingToHash);
42
42
  imageSet.outputFileName = `${hash}.${imageSet.fileType.ext}`;
43
43
  imageSet.primaryFileOutputPath = Path.posix.join((imageOutputRootPath === null || imageOutputRootPath === void 0 ? void 0 : imageOutputRootPath.length) > 0
@@ -58,9 +58,18 @@ function makeImagePersistencePlan(imageSet, imageOutputRootPath, imagePrefix) {
58
58
  }
59
59
  }
60
60
  exports.makeImagePersistencePlan = makeImagePersistencePlan;
61
+ function findLastUuid(url) {
62
+ // Regex for a UUID surrounded by slashes
63
+ const uuidPattern = /(?<=\/)[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}(?=\/)/gi;
64
+ // Find all UUIDs
65
+ const uuids = url.match(uuidPattern);
66
+ // Return the last UUID if any exist, else return null
67
+ return uuids ? uuids[uuids.length - 1].trim() : null;
68
+ }
61
69
  function hashOfString(s) {
62
70
  let hash = 0;
63
71
  for (let i = 0; i < s.length; ++i)
64
72
  hash = Math.imul(31, hash) + s.charCodeAt(i);
65
73
  return Math.abs(hash);
66
74
  }
75
+ exports.hashOfString = hashOfString;
@@ -9,9 +9,10 @@ test("primary file with explicit file output path and prefix", () => {
9
9
  fileType: { ext: "png", mime: "image/png" },
10
10
  };
11
11
  (0, MakeImagePersistencePlan_1.makeImagePersistencePlan)(imageSet, "./static/notion_imgs", "/notion_imgs");
12
- expect(imageSet.outputFileName).toBe("463556435.png");
13
- expect(imageSet.primaryFileOutputPath).toBe("static/notion_imgs/463556435.png");
14
- expect(imageSet.filePathToUseInMarkdown).toBe("/notion_imgs/463556435.png");
12
+ const expectedHash = (0, MakeImagePersistencePlan_1.hashOfString)("https://s3.us-west-2.amazonaws.com/primaryImage");
13
+ expect(imageSet.outputFileName).toBe(`${expectedHash}.png`);
14
+ expect(imageSet.primaryFileOutputPath).toBe(`static/notion_imgs/${expectedHash}.png`);
15
+ expect(imageSet.filePathToUseInMarkdown).toBe(`/notion_imgs/${expectedHash}.png`);
15
16
  });
16
17
  test("primary file with defaults for image output path and prefix", () => {
17
18
  const imageSet = {
@@ -21,10 +22,31 @@ test("primary file with defaults for image output path and prefix", () => {
21
22
  fileType: { ext: "png", mime: "image/png" },
22
23
  };
23
24
  (0, MakeImagePersistencePlan_1.makeImagePersistencePlan)(imageSet, "", "");
24
- expect(imageSet.outputFileName).toBe("463556435.png");
25
+ const expectedHash = (0, MakeImagePersistencePlan_1.hashOfString)("https://s3.us-west-2.amazonaws.com/primaryImage");
26
+ expect(imageSet.outputFileName).toBe(`${expectedHash}.png`);
25
27
  // the default behavior is to put the image next to the markdown file
26
- expect(imageSet.primaryFileOutputPath).toBe("/pathToParentSomewhere/463556435.png");
27
- expect(imageSet.filePathToUseInMarkdown).toBe("./463556435.png");
28
+ expect(imageSet.primaryFileOutputPath).toBe(`/pathToParentSomewhere/${expectedHash}.png`);
29
+ expect(imageSet.filePathToUseInMarkdown).toBe(`./${expectedHash}.png`);
30
+ });
31
+ test("properly extract UUID from old-style notion image url", () => {
32
+ const imageSet = {
33
+ primaryUrl: "https://s3.us-west-2.amazonaws.com/secure.notion-static.com/e1058f46-4d2f-4292-8388-4ad393383439/Untitled.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220516%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220516T233630Z&X-Amz-Expires=3600&X-Amz-Signature=f215704094fcc884d37073b0b108cf6d1c9da9b7d57a898da38bc30c30b4c4b5&X-Amz-SignedHeaders=host&x-id=GetObject",
34
+ localizedUrls: [],
35
+ fileType: { ext: "png", mime: "image/png" },
36
+ };
37
+ (0, MakeImagePersistencePlan_1.makeImagePersistencePlan)(imageSet, "./static/notion_imgs", "/notion_imgs");
38
+ const expectedHash = (0, MakeImagePersistencePlan_1.hashOfString)("e1058f46-4d2f-4292-8388-4ad393383439");
39
+ expect(imageSet.outputFileName).toBe(`${expectedHash}.png`);
40
+ });
41
+ test("properly extract UUID from new-style (Sept 2023) notion image url", () => {
42
+ const imageSet = {
43
+ primaryUrl: "https://prod-files-secure.s3.us-west-2.amazonaws.com/d9a2b712-cf69-4bd6-9d65-87a4ceeacca2/d1bcdc8c-b065-4e40-9a11-392aabeb220e/Untitled.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20230915%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20230915T161258Z&X-Amz-Expires=3600&X-Amz-Signature=28fca48e65fba86d539c3c4b7676fce1fa0857aa194f7b33dd4a468ecca6ab24&X-Amz-SignedHeaders=host&x-id=GetObject",
44
+ localizedUrls: [],
45
+ fileType: { ext: "png", mime: "image/png" },
46
+ };
47
+ (0, MakeImagePersistencePlan_1.makeImagePersistencePlan)(imageSet, "./static/notion_imgs", "/notion_imgs");
48
+ const expectedHash = (0, MakeImagePersistencePlan_1.hashOfString)("d1bcdc8c-b065-4e40-9a11-392aabeb220e");
49
+ expect(imageSet.outputFileName).toBe(`${expectedHash}.png`);
28
50
  });
29
51
  // In order to make image fallback work with other languages, we have to have
30
52
  // a file for each image, in each Docusaurus language directory. This is true
package/package.json CHANGED
@@ -90,5 +90,5 @@
90
90
  "volta": {
91
91
  "node": "18.16.0"
92
92
  },
93
- "version": "0.13.3"
93
+ "version": "0.13.4"
94
94
  }