rehype-og-card 3.0.0 → 3.1.0

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
@@ -95,7 +95,7 @@ const rehypeOGCard = (options) => {
95
95
  const targetURL = new URL(anchorNode.properties.href);
96
96
  if (mergedOptions.excludeDomains.includes(targetURL.hostname))
97
97
  return;
98
- // eslint-disable-next-line jsdoc/require-jsdoc, max-statements, max-lines-per-function, complexity
98
+ // eslint-disable-next-line jsdoc/require-jsdoc, max-statements, max-lines-per-function
99
99
  const linkCardPromise = async () => {
100
100
  let OGData = mergedOptions.buildCache
101
101
  ? await restoreOGDataBuildCache(anchorNode.properties.href, mergedOptions.buildCachePath, mergedOptions.buildCacheMaxAge)
@@ -117,11 +117,14 @@ const rehypeOGCard = (options) => {
117
117
  });
118
118
  if (filename) {
119
119
  OGData.OGImageURL = path.posix.join("/rehype-og-card", filename);
120
+ if (mergedOptions.buildCache) {
121
+ const downloadedFilePath = path.join(mergedOptions.serverCachePath, filename);
122
+ const buildCachePath = path.join(mergedOptions.buildCachePath, filename);
123
+ void saveBuildCacheFile(downloadedFilePath, buildCachePath);
124
+ }
120
125
  }
121
- if (filename && mergedOptions.buildCache) {
122
- const downloadedFilePath = path.join(mergedOptions.serverCachePath, filename);
123
- const buildCachePath = path.join(mergedOptions.buildCachePath, filename);
124
- void saveBuildCacheFile(downloadedFilePath, buildCachePath);
126
+ else {
127
+ delete OGData.OGImageURL;
125
128
  }
126
129
  }
127
130
  if (OGData.faviconURL && mergedOptions.serverCache) {
@@ -133,11 +136,14 @@ const rehypeOGCard = (options) => {
133
136
  });
134
137
  if (filename) {
135
138
  OGData.faviconURL = path.posix.join("/rehype-og-card", filename);
139
+ if (mergedOptions.buildCache) {
140
+ const downloadedFilePath = path.join(mergedOptions.serverCachePath, filename);
141
+ const buildCachePath = path.join(mergedOptions.buildCachePath, filename);
142
+ void saveBuildCacheFile(downloadedFilePath, buildCachePath);
143
+ }
136
144
  }
137
- if (filename && mergedOptions.buildCache) {
138
- const downloadedFilePath = path.join(mergedOptions.serverCachePath, filename);
139
- const buildCachePath = path.join(mergedOptions.buildCachePath, filename);
140
- void saveBuildCacheFile(downloadedFilePath, buildCachePath);
145
+ else {
146
+ delete OGData.faviconURL;
141
147
  }
142
148
  }
143
149
  if (mergedOptions.shortenURL) {
package/dist/util/hast.js CHANGED
@@ -42,14 +42,16 @@ const createOGCard = (data, options) => {
42
42
  h("div.og-card-title", data.title),
43
43
  data.description ? h("div.og-card-description", data.description) : null,
44
44
  h("div.og-card-url-container", [
45
- h("img.og-card-favicon", {
46
- alt: "favicon",
47
- decoding: options.decoding,
48
- height: 16,
49
- loading: options.loading,
50
- src: data.faviconURL,
51
- width: 16
52
- }),
45
+ data.faviconURL
46
+ ? h("img.og-card-favicon", {
47
+ alt: "favicon",
48
+ decoding: options.decoding,
49
+ height: 16,
50
+ loading: options.loading,
51
+ src: data.faviconURL,
52
+ width: 16
53
+ })
54
+ : null,
53
55
  h("span.og-card-url", data.displayURL)
54
56
  ])
55
57
  ]),
@@ -107,6 +107,15 @@ const downloadImage = async (options) => {
107
107
  // eslint-disable-next-line no-magic-numbers
108
108
  signal: AbortSignal.timeout(10 * 1000)
109
109
  });
110
+ if (!response.ok)
111
+ return null;
112
+ const contentType = response.headers.get("content-type");
113
+ const normalizedContentType = contentType?.split(";")[0]?.trim().toLowerCase();
114
+ if (!normalizedContentType?.startsWith("image/")) {
115
+ // eslint-disable-next-line no-console
116
+ console.error("[rehype-og-card] Invalid image content type:", contentType ?? "unknown", "for", options.url);
117
+ return null;
118
+ }
110
119
  const arrayBuffer = await response.arrayBuffer();
111
120
  const buffer = Buffer.from(arrayBuffer);
112
121
  const saveDirectoryExists = await checkFileExists(options.directory);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rehype-og-card",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
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",