@chrisburnell/eleventy-cache-webmentions 2.1.7 → 2.1.8

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 +1 @@
1
- [{"68a403136c61abd104ade94e69ba62":"1"},{"cachedAt":1723704454029,"type":"2"},"json"]
1
+ [{"68a403136c61abd104ade94e69ba62":"1"},{"cachedAt":1723708494710,"type":"2"},"json"]
@@ -4,6 +4,23 @@ const uniqBy = require("lodash.uniqby");
4
4
  const { AssetCache } = require("@11ty/eleventy-fetch");
5
5
  const chalk = require("chalk");
6
6
 
7
+ const defaults = {
8
+ refresh: false,
9
+ duration: "1d",
10
+ uniqueKey: "webmentions",
11
+ allowedHTML: {
12
+ allowedTags: ["b", "i", "em", "strong", "a"],
13
+ allowedAttributes: {
14
+ a: ["href"],
15
+ },
16
+ },
17
+ allowlist: [],
18
+ blocklist: [],
19
+ urlReplacements: {},
20
+ maximumHtmlLength: 1000,
21
+ maximumHtmlText: "mentioned this in",
22
+ };
23
+
7
24
  const absoluteURL = (url, domain) => {
8
25
  try {
9
26
  return new URL(url, domain).toString();
@@ -118,21 +135,42 @@ const getByTypes = (webmentions, allowedTypes) => {
118
135
  });
119
136
  };
120
137
 
121
- const defaults = {
122
- refresh: false,
123
- duration: "1d",
124
- uniqueKey: "webmentions",
125
- allowedHTML: {
126
- allowedTags: ["b", "i", "em", "strong", "a"],
127
- allowedAttributes: {
128
- a: ["href"],
129
- },
130
- },
131
- allowlist: [],
132
- blocklist: [],
133
- urlReplacements: {},
134
- maximumHtmlLength: 1000,
135
- maximumHtmlText: "mentioned this in",
138
+ const removeDuplicates = (webmentions) => {
139
+ return uniqBy(webmentions, (webmention) => {
140
+ return getSource(webmention);
141
+ });
142
+ };
143
+
144
+ const processBlocklist = (webmentions, blocklist) => {
145
+ return webmentions.filter((webmention) => {
146
+ let url = getSource(webmention);
147
+ let source = getSource(webmention);
148
+ for (let blocklistURL of blocklist) {
149
+ if (
150
+ url.includes(blocklistURL.replace(/\/?$/, "/")) ||
151
+ source.includes(blocklistURL.replace(/\/?$/, "/"))
152
+ ) {
153
+ return false;
154
+ }
155
+ }
156
+ return true;
157
+ });
158
+ };
159
+
160
+ const processAllowlist = (webmentions, allowlist) => {
161
+ return webmentions.filter((webmention) => {
162
+ let url = getSource(webmention);
163
+ let source = getSource(webmention);
164
+ for (let allowlistURL of allowlist) {
165
+ if (
166
+ url.includes(allowlistURL.replace(/\/?$/, "/")) ||
167
+ source.includes(allowlistURL.replace(/\/?$/, "/"))
168
+ ) {
169
+ return true;
170
+ }
171
+ }
172
+ return false;
173
+ });
136
174
  };
137
175
 
138
176
  const performFetch = async (options, webmentions, url) => {
@@ -158,43 +196,14 @@ const performFetch = async (options, webmentions, url) => {
158
196
  // Combine newly-fetched Webmentions with cached Webmentions
159
197
  webmentions = feed[options.key].concat(webmentions);
160
198
  // Remove duplicates by source URL
161
- webmentions = uniqBy(
162
- [...feed[options.key], ...webmentions],
163
- (webmention) => {
164
- return getSource(webmention);
165
- },
166
- );
199
+ webmentions = removeDuplicates(webmentions);
167
200
  // Process the blocklist, if it has any entries
168
201
  if (options.blocklist.length) {
169
- webmentions = webmentions.filter((webmention) => {
170
- let url = getSource(webmention);
171
- let source = getSource(webmention);
172
- for (let blocklistURL of options.blocklist) {
173
- if (
174
- url.includes(blocklistURL.replace(/\/?$/, "/")) ||
175
- source.includes(blocklistURL.replace(/\/?$/, "/"))
176
- ) {
177
- return false;
178
- }
179
- }
180
- return true;
181
- });
202
+ webmentions = processBlocklist(webmentions, options.blocklist);
182
203
  }
183
204
  // Process the allowlist, if it has any entries
184
205
  if (options.allowlist.length) {
185
- webmentions = webmentions.filter((webmention) => {
186
- let url = getSource(webmention);
187
- let source = getSource(webmention);
188
- for (let allowlistURL of options.allowlist) {
189
- if (
190
- url.includes(allowlistURL.replace(/\/?$/, "/")) ||
191
- source.includes(allowlistURL.replace(/\/?$/, "/"))
192
- ) {
193
- return true;
194
- }
195
- }
196
- return false;
197
- });
206
+ webmentions = processAllowlist(webmentions, options.allowlist);
198
207
  }
199
208
  // Sort webmentions by received date for getting most recent Webmention on subsequent requests
200
209
  webmentions = webmentions.sort((a, b) => {
@@ -314,6 +323,16 @@ const fetchWebmentions = async (options) => {
314
323
  webmentions = fetched.filtered;
315
324
  }
316
325
 
326
+ // Process the blocklist, if it has any entries
327
+ if (options.blocklist.length) {
328
+ webmentions = processBlocklist(webmentions, options.blocklist);
329
+ }
330
+
331
+ // Process the allowlist, if it has any entries
332
+ if (options.allowlist.length) {
333
+ webmentions = processAllowlist(webmentions, options.allowlist);
334
+ }
335
+
317
336
  await asset.save(webmentions, "json");
318
337
 
319
338
  const performance = process.hrtime(performanceStart);
@@ -324,7 +343,8 @@ const fetchWebmentions = async (options) => {
324
343
  `${chalk.grey(`[${hostname(options.domain)}]`)} ${chalk.bold(
325
344
  webmentions.length - webmentionsCachedLength,
326
345
  )} new Webmentions fetched into cache in ${chalk.bold(
327
- (performance[0] + performance[1] / 1e9).toFixed(3) + "s",
346
+ (performance[0] + performance[1] / 1e9).toFixed(3) +
347
+ " seconds",
328
348
  )}.`,
329
349
  );
330
350
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chrisburnell/eleventy-cache-webmentions",
3
- "version": "2.1.7",
3
+ "version": "2.1.8",
4
4
  "description": "Cache webmentions using eleventy-fetch and make them available to use in collections, layouts, pages, etc. in Eleventy.",
5
5
  "license": "MIT",
6
6
  "repository": {