rehype-og-card 2.0.1 → 3.0.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/README.md +10 -3
- package/dist/index.js +11 -4
- package/dist/types.d.ts +6 -1
- package/dist/util/network.js +2 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -63,7 +63,7 @@ Output (formatted for readability):
|
|
|
63
63
|
```html
|
|
64
64
|
<p>this is a link: https://blog.google/products/android/world-emoji-day-2024/</p>
|
|
65
65
|
<p><code>https://blog.google/products/android/world-emoji-day-2024/</code></p>
|
|
66
|
-
<
|
|
66
|
+
<div class="og-card-container">
|
|
67
67
|
<a href="https://blog.google/products/android/world-emoji-day-2024/">
|
|
68
68
|
<div class="og-card-info">
|
|
69
69
|
<div class="og-card-title">10 fun facts about emoji for World Emoji Day</div>
|
|
@@ -77,7 +77,7 @@ Output (formatted for readability):
|
|
|
77
77
|
<img class="og-card-image" alt="https://storage.googleapis.com/gweb-uniblog-publish-prod/images/world_emoji_day_v2_1.width-1300.png" decoding="async" loading="lazy" src="https://storage.googleapis.com/gweb-uniblog-publish-prod/images/world_emoji_day_v2_1.width-1300.png">
|
|
78
78
|
</div>
|
|
79
79
|
</a>
|
|
80
|
-
</div
|
|
80
|
+
</div>
|
|
81
81
|
<p><a href="https://blog.google/products/android/world-emoji-day-2024/">https://blog.google/products/android/world-emoji-day-2024/</a></p>
|
|
82
82
|
<p><a href="https://blog.google/products/android/world-emoji-day-2024/">Google blog</a></p>
|
|
83
83
|
```
|
|
@@ -128,7 +128,7 @@ Expiration is calculated using the cached timestamp stored in the cache itself.
|
|
|
128
128
|
User agent to use for fetching the OG metadata and images.
|
|
129
129
|
|
|
130
130
|
- Type: `string`
|
|
131
|
-
- Default: `"Mozilla/5.0 (
|
|
131
|
+
- Default: `"Mozilla/5.0 (compatible; rehype-og-card/2.0; +https://github.com/Robot-Inventor/rehype-og-card/; purpose=link-preview; twitterbot-compatible)"`
|
|
132
132
|
|
|
133
133
|
### `decoding`
|
|
134
134
|
|
|
@@ -165,6 +165,13 @@ Whether to open the link in a new tab.
|
|
|
165
165
|
- Type: `boolean`
|
|
166
166
|
- Default: `false`
|
|
167
167
|
|
|
168
|
+
### `removeParentPTag`
|
|
169
|
+
|
|
170
|
+
Whether to remove the parent `<p>` tag that contains only the bare link.
|
|
171
|
+
|
|
172
|
+
- Type: `boolean`
|
|
173
|
+
- Default: `true`
|
|
174
|
+
|
|
168
175
|
### `serverCache`
|
|
169
176
|
|
|
170
177
|
Whether to cache the OG data on the server.
|
package/dist/index.js
CHANGED
|
@@ -13,12 +13,13 @@ const DEFAULT_OPTIONS = {
|
|
|
13
13
|
buildCache: false,
|
|
14
14
|
buildCacheMaxAge: DEFAULT_CACHE_MAX_AGE_MS,
|
|
15
15
|
buildCachePath: "./node_modules/.cache",
|
|
16
|
-
crawlerUserAgent: "Mozilla/5.0 (
|
|
16
|
+
crawlerUserAgent: "Mozilla/5.0 (compatible; rehype-og-card/2.0; +https://github.com/Robot-Inventor/rehype-og-card/; purpose=link-preview; twitterbot-compatible)",
|
|
17
17
|
decoding: "async",
|
|
18
18
|
enableSameTextURLConversion: false,
|
|
19
19
|
excludeDomains: [],
|
|
20
20
|
loading: "lazy",
|
|
21
21
|
openInNewTab: false,
|
|
22
|
+
removeParentPTag: true,
|
|
22
23
|
serverCache: true,
|
|
23
24
|
serverCacheMaxAge: DEFAULT_CACHE_MAX_AGE_MS,
|
|
24
25
|
serverCachePath: "./public",
|
|
@@ -94,7 +95,7 @@ const rehypeOGCard = (options) => {
|
|
|
94
95
|
const targetURL = new URL(anchorNode.properties.href);
|
|
95
96
|
if (mergedOptions.excludeDomains.includes(targetURL.hostname))
|
|
96
97
|
return;
|
|
97
|
-
// eslint-disable-next-line jsdoc/require-jsdoc, max-statements, max-lines-per-function
|
|
98
|
+
// eslint-disable-next-line jsdoc/require-jsdoc, max-statements, max-lines-per-function, complexity
|
|
98
99
|
const linkCardPromise = async () => {
|
|
99
100
|
let OGData = mergedOptions.buildCache
|
|
100
101
|
? await restoreOGDataBuildCache(anchorNode.properties.href, mergedOptions.buildCachePath, mergedOptions.buildCacheMaxAge)
|
|
@@ -144,12 +145,18 @@ const rehypeOGCard = (options) => {
|
|
|
144
145
|
OGData.displayURL = shortenedURL.hostname;
|
|
145
146
|
}
|
|
146
147
|
const OGCard = createOGCard(OGData, mergedOptions);
|
|
147
|
-
const
|
|
148
|
+
const { removeParentPTag: shouldRemoveParentPTag } = mergedOptions;
|
|
149
|
+
// eslint-disable-next-line no-magic-numbers
|
|
150
|
+
const replacementContainer = shouldRemoveParentPTag ? ancestors[ancestors.length - 2] : parent;
|
|
151
|
+
const replacementTarget = shouldRemoveParentPTag ? parent : node;
|
|
152
|
+
if (!replacementContainer || !("children" in replacementContainer))
|
|
153
|
+
return;
|
|
154
|
+
const index = replacementContainer.children.indexOf(replacementTarget);
|
|
148
155
|
// eslint-disable-next-line no-magic-numbers
|
|
149
156
|
if (index === -1)
|
|
150
157
|
return;
|
|
151
158
|
// eslint-disable-next-line no-magic-numbers
|
|
152
|
-
|
|
159
|
+
replacementContainer.children.splice(index, 1, OGCard);
|
|
153
160
|
};
|
|
154
161
|
linkCardPromises.push(linkCardPromise());
|
|
155
162
|
});
|
package/dist/types.d.ts
CHANGED
|
@@ -23,6 +23,11 @@ interface RehypeOGCardOptions {
|
|
|
23
23
|
* @default false
|
|
24
24
|
*/
|
|
25
25
|
openInNewTab?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Whether to remove the parent `<p>` tag that contains only the bare link.
|
|
28
|
+
* @default true
|
|
29
|
+
*/
|
|
30
|
+
removeParentPTag?: boolean;
|
|
26
31
|
/**
|
|
27
32
|
* Whether to enable converting the links
|
|
28
33
|
* that have the same URL and text (e.g., `[https://example.com](https://example.com)`).
|
|
@@ -69,7 +74,7 @@ interface RehypeOGCardOptions {
|
|
|
69
74
|
buildCacheMaxAge?: number | false;
|
|
70
75
|
/**
|
|
71
76
|
* User agent to use for fetching the OG metadata and images.
|
|
72
|
-
* @default "Mozilla/5.0 (
|
|
77
|
+
* @default "Mozilla/5.0 (compatible; rehype-og-card/2.0; +https://github.com/Robot-Inventor/rehype-og-card/; purpose=link-preview; twitterbot-compatible)"
|
|
73
78
|
*/
|
|
74
79
|
crawlerUserAgent?: string;
|
|
75
80
|
}
|
package/dist/util/network.js
CHANGED
|
@@ -46,10 +46,8 @@ const getOGData = async (url, userAgent) => {
|
|
|
46
46
|
});
|
|
47
47
|
const OGImage = result.ogImage ? result.ogImage[0] : null;
|
|
48
48
|
const OGImageURL = resolveAbsoluteURL(OGImage?.url, url);
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
: // eslint-disable-next-line no-undefined
|
|
52
|
-
undefined;
|
|
49
|
+
const resolvedFaviconURL = result.favicon ? resolveAbsoluteURL(result.favicon, url) : null;
|
|
50
|
+
const faviconURL = resolvedFaviconURL ?? `https://www.google.com/s2/favicons?domain=${new URL(url).hostname}`;
|
|
53
51
|
return {
|
|
54
52
|
OGImageAlt: OGImage?.alt,
|
|
55
53
|
OGImageHeight: OGImage?.height,
|