rehype-og-card 0.3.0 → 0.3.1

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.
package/dist/index.js CHANGED
@@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import { convertTextToAnchorElement, downloadImage, isAnchorElement, isTextNode, isValidURL } from "./util.js";
11
11
  import { createOGCard, getOGData } from "./openGraphUtil.js";
12
+ import { restoreBuildCache, saveBuildCache } from "./util/cache.js";
12
13
  import { isElement } from "hast-util-is-element";
13
14
  import path from "path";
14
15
  import { visitParents } from "unist-util-visit-parents";
@@ -45,6 +46,9 @@ const rehypeOGCard = (options) => {
45
46
  */
46
47
  // eslint-disable-next-line max-lines-per-function
47
48
  const transform = (tree) => __awaiter(void 0, void 0, void 0, function* () {
49
+ if (mergedOptions.buildCache) {
50
+ yield restoreBuildCache(mergedOptions.buildCachePath, mergedOptions.serverCachePath);
51
+ }
48
52
  const linkCardPromises = [];
49
53
  // eslint-disable-next-line max-statements, max-lines-per-function
50
54
  visitParents(tree, ["element", "text"], (node, ancestors) => {
@@ -88,10 +92,8 @@ const rehypeOGCard = (options) => {
88
92
  return;
89
93
  if (OGData.OGImageURL && mergedOptions.serverCache) {
90
94
  const filename = yield downloadImage({
91
- buildCacheDirectory: mergedOptions.buildCachePath,
92
95
  directly: mergedOptions.serverCachePath,
93
96
  url: OGData.OGImageURL,
94
- useBuildCache: mergedOptions.buildCache,
95
97
  userAgent: mergedOptions.crawlerUserAgent
96
98
  });
97
99
  if (filename && mergedOptions.serverCache) {
@@ -100,10 +102,8 @@ const rehypeOGCard = (options) => {
100
102
  }
101
103
  if (OGData.faviconURL && mergedOptions.serverCache) {
102
104
  const filename = yield downloadImage({
103
- buildCacheDirectory: mergedOptions.buildCachePath,
104
105
  directly: mergedOptions.serverCachePath,
105
106
  url: OGData.faviconURL,
106
- useBuildCache: mergedOptions.buildCache,
107
107
  userAgent: mergedOptions.crawlerUserAgent
108
108
  });
109
109
  if (filename && mergedOptions.serverCache) {
@@ -126,6 +126,9 @@ const rehypeOGCard = (options) => {
126
126
  linkCardPromises.push(linkCardPromise());
127
127
  });
128
128
  yield Promise.all(linkCardPromises);
129
+ if (mergedOptions.buildCache) {
130
+ yield saveBuildCache(mergedOptions.serverCachePath, mergedOptions.buildCachePath);
131
+ }
129
132
  });
130
133
  return transform;
131
134
  };
package/dist/types.d.ts CHANGED
@@ -31,16 +31,20 @@ interface RehypeOGCardOptions {
31
31
  serverCache?: boolean;
32
32
  /**
33
33
  * Path to save the server cache.
34
+ * Specify the directory whose contents will be copied to the root.
35
+ * For example, for an Astro project, specify the `public` directory.
34
36
  * @default "./public"
35
37
  */
36
38
  serverCachePath?: string;
37
39
  /**
38
40
  * Whether to cache the OG metadata and images for the next build.
41
+ * `buildCache` option requires `serverCache` option to be enabled.
39
42
  * @default false
40
43
  */
41
44
  buildCache?: boolean;
42
45
  /**
43
46
  * Path to save the build cache.
47
+ * For Astro projects, specify the `./node_modules/.astro` directory.
44
48
  * @default "./node_modules/.cache"
45
49
  */
46
50
  buildCachePath?: string;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Save build cache if server cache exists.
3
+ * @param serverCachePath Server cache path.
4
+ * @param buildCachePath Build cache path.
5
+ */
6
+ declare const saveBuildCache: (serverCachePath: string, buildCachePath: string) => Promise<void>;
7
+ /**
8
+ * Restore build cache if it exists.
9
+ * @param buildCachePath Build cache path.
10
+ * @param serverCachePath Server cache path.
11
+ */
12
+ declare const restoreBuildCache: (buildCachePath: string, serverCachePath: string) => Promise<void>;
13
+ export { saveBuildCache, restoreBuildCache };
@@ -0,0 +1,52 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { checkFileExists } from "../util.js";
11
+ import fs from "fs/promises";
12
+ import path from "path";
13
+ /**
14
+ * Copy directory from source to destination.
15
+ * @param source Source directory.
16
+ * @param destination Destination directory.
17
+ */
18
+ // eslint-disable-next-line max-statements
19
+ const copyDirectory = (source, destination) => __awaiter(void 0, void 0, void 0, function* () {
20
+ const sourceExists = yield checkFileExists(source);
21
+ if (!sourceExists)
22
+ return;
23
+ const destinationExists = yield checkFileExists(destination);
24
+ if (!destinationExists) {
25
+ yield fs.mkdir(destination, { recursive: true });
26
+ }
27
+ const files = yield fs.readdir(source);
28
+ const promises = [];
29
+ for (const file of files) {
30
+ const sourceFile = path.join(source, file);
31
+ const destFile = path.join(destination, file);
32
+ promises.push(fs.copyFile(sourceFile, destFile));
33
+ }
34
+ yield Promise.all(promises);
35
+ });
36
+ /**
37
+ * Save build cache if server cache exists.
38
+ * @param serverCachePath Server cache path.
39
+ * @param buildCachePath Build cache path.
40
+ */
41
+ const saveBuildCache = (serverCachePath, buildCachePath) => __awaiter(void 0, void 0, void 0, function* () {
42
+ yield copyDirectory(serverCachePath, buildCachePath);
43
+ });
44
+ /**
45
+ * Restore build cache if it exists.
46
+ * @param buildCachePath Build cache path.
47
+ * @param serverCachePath Server cache path.
48
+ */
49
+ const restoreBuildCache = (buildCachePath, serverCachePath) => __awaiter(void 0, void 0, void 0, function* () {
50
+ yield copyDirectory(buildCachePath, serverCachePath);
51
+ });
52
+ export { saveBuildCache, restoreBuildCache };
package/dist/util.d.ts CHANGED
@@ -29,6 +29,12 @@ declare const isValidURL: (url: string) => boolean;
29
29
  * @returns Anchor element.
30
30
  */
31
31
  declare const convertTextToAnchorElement: (text: Text) => AnchorElement;
32
+ /**
33
+ * Check if the file exists.
34
+ * @param filePath Path to check.
35
+ * @returns `true` if the file exists, `false` otherwise.
36
+ */
37
+ declare const checkFileExists: (filePath: string) => Promise<boolean>;
32
38
  interface DownloadImageOptions {
33
39
  /**
34
40
  * URL to download image.
@@ -38,14 +44,6 @@ interface DownloadImageOptions {
38
44
  * Directory to save the downloaded image.
39
45
  */
40
46
  directly: string;
41
- /**
42
- * Whether to save and restore the image cache.
43
- */
44
- useBuildCache: boolean;
45
- /**
46
- * Directory to save the image cache.
47
- */
48
- buildCacheDirectory: string;
49
47
  /**
50
48
  * User agent to use for fetching the image.
51
49
  */
@@ -57,4 +55,4 @@ interface DownloadImageOptions {
57
55
  * @returns Path to the downloaded image.
58
56
  */
59
57
  declare const downloadImage: (options: DownloadImageOptions) => Promise<string | null>;
60
- export { AnchorElement, isTextNode, isAnchorElement, isValidURL, convertTextToAnchorElement, downloadImage };
58
+ export { AnchorElement, isTextNode, isAnchorElement, isValidURL, convertTextToAnchorElement, checkFileExists, downloadImage };
package/dist/util.js CHANGED
@@ -81,18 +81,11 @@ const downloadImage = (options) => __awaiter(void 0, void 0, void 0, function* (
81
81
  const hash = createHash("sha256").update(options.url).digest("hex");
82
82
  const filename = hash + path.extname(new URL(options.url).pathname);
83
83
  const savePath = path.posix.join(options.directly, filename);
84
- const buildCachePath = path.posix.join(options.buildCacheDirectory, filename);
85
84
  // If the file already exists, return the filename.
86
85
  const fileExists = yield checkFileExists(savePath);
87
86
  if (fileExists) {
88
87
  return filename;
89
88
  }
90
- const buildCacheExists = yield checkFileExists(buildCachePath);
91
- if (options.useBuildCache && buildCacheExists) {
92
- yield fs.mkdir(options.directly, { recursive: true });
93
- yield fs.copyFile(buildCachePath, savePath);
94
- return filename;
95
- }
96
89
  const response = yield fetch(options.url, {
97
90
  headers: {
98
91
  "user-agent": options.userAgent
@@ -102,10 +95,6 @@ const downloadImage = (options) => __awaiter(void 0, void 0, void 0, function* (
102
95
  const buffer = Buffer.from(arrayBuffer);
103
96
  yield fs.mkdir(options.directly, { recursive: true });
104
97
  yield fs.writeFile(savePath, buffer);
105
- if (options.useBuildCache) {
106
- yield fs.mkdir(options.buildCacheDirectory, { recursive: true });
107
- yield fs.writeFile(buildCachePath, buffer);
108
- }
109
98
  return filename;
110
99
  }
111
100
  catch (error) {
@@ -114,4 +103,4 @@ const downloadImage = (options) => __awaiter(void 0, void 0, void 0, function* (
114
103
  return null;
115
104
  }
116
105
  });
117
- export { isTextNode, isAnchorElement, isValidURL, convertTextToAnchorElement, downloadImage };
106
+ export { isTextNode, isAnchorElement, isValidURL, convertTextToAnchorElement, checkFileExists, downloadImage };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rehype-og-card",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Rehype plugin to convert text links to link cards",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",