rehype-og-card 2.0.2 → 3.0.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/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
- <p><div class="og-card-container">
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></p>
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
  ```
@@ -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
@@ -19,6 +19,7 @@ const DEFAULT_OPTIONS = {
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 index = parent.children.indexOf(node);
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
- parent.children.splice(index, 1, OGCard);
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)`).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rehype-og-card",
3
- "version": "2.0.2",
3
+ "version": "3.0.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",